7#include "absl/strings/str_format.h"
8#include "nlohmann/json.hpp"
13using json = nlohmann::json;
26 const std::string& directory_path) {
29 if (!std::filesystem::exists(directory_path)) {
31 return absl::OkStatus();
34 for (
const auto& entry : std::filesystem::directory_iterator(directory_path)) {
35 if (entry.path().extension() ==
".json") {
37 std::ifstream i(entry.path());
42 }
catch (
const std::exception& e) {
43 std::cerr <<
"Failed to load template " << entry.path() <<
": "
44 << e.what() << std::endl;
50 return absl::OkStatus();
55 if (!std::filesystem::exists(directory_path)) {
56 std::filesystem::create_directories(directory_path);
60 std::string filename = tmpl.
name;
61 std::replace(filename.begin(), filename.end(),
' ',
'_');
63 absl::StrFormat(
"%s/%s.json", directory_path.c_str(), filename.c_str());
67 std::ofstream o(path);
68 o << std::setw(4) << j << std::endl;
73 if (t.name == tmpl.
name) {
83 return absl::OkStatus();
84 }
catch (
const std::exception& e) {
85 return absl::InternalError(absl::StrFormat(
"Failed to save template: %s", e.what()));
91 const std::vector<RoomObject>& objects,
int origin_x,
int origin_y) {
97 for (
const auto& obj : objects) {
100 t_obj.
rel_x = obj.x_ - origin_x;
101 t_obj.
rel_y = obj.y_ - origin_y;
102 t_obj.
size = obj.size_;
103 t_obj.
layer =
static_cast<int>(obj.layer_);
112 std::vector<RoomObject> new_objects;
114 for (
const auto& t_obj : tmpl.
objects) {
115 int obj_x = x + t_obj.rel_x;
116 int obj_y = y + t_obj.rel_y;
119 if (obj_x >= 0 && obj_x < 64 && obj_y >= 0 && obj_y < 64) {
120 RoomObject obj(t_obj.id, obj_x, obj_y, t_obj.size, t_obj.layer);
125 new_objects.push_back(obj);
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
static ObjectTemplate CreateFromObjects(const std::string &name, const std::string &description, const std::vector< RoomObject > &objects, int origin_x, int origin_y)
std::vector< ObjectTemplate > templates_
std::vector< RoomObject > InstantiateTemplate(const ObjectTemplate &tmpl, int x, int y, Rom *rom)
absl::Status LoadTemplates(const std::string &directory_path)
absl::Status SaveTemplate(const ObjectTemplate &tmpl, const std::string &directory_path)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TemplateObject, id, rel_x, rel_y, size, layer) NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ObjectTemplate
std::vector< TemplateObject > objects