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 :
35 std::filesystem::directory_iterator(directory_path)) {
36 if (entry.path().extension() ==
".json") {
38 std::ifstream i(entry.path());
43 }
catch (
const std::exception& e) {
44 std::cerr <<
"Failed to load template " << entry.path() <<
": "
45 << e.what() << std::endl;
51 return absl::OkStatus();
56 if (!std::filesystem::exists(directory_path)) {
57 std::filesystem::create_directories(directory_path);
61 std::string filename = tmpl.
name;
62 std::replace(filename.begin(), filename.end(),
' ',
'_');
64 absl::StrFormat(
"%s/%s.json", directory_path.c_str(), filename.c_str());
68 std::ofstream o(path);
69 o << std::setw(4) << j << std::endl;
74 if (t.name == tmpl.
name) {
84 return absl::OkStatus();
85 }
catch (
const std::exception& e) {
86 return absl::InternalError(
87 absl::StrFormat(
"Failed to save template: %s", e.what()));
93 const std::vector<RoomObject>& objects,
int origin_x,
int origin_y) {
99 for (
const auto& obj : objects) {
102 t_obj.
rel_x = obj.x_ - origin_x;
103 t_obj.
rel_y = obj.y_ - origin_y;
104 t_obj.
size = obj.size_;
105 t_obj.
layer =
static_cast<int>(obj.layer_);
114 std::vector<RoomObject> new_objects;
116 for (
const auto& t_obj : tmpl.
objects) {
117 int obj_x = x + t_obj.rel_x;
118 int obj_y = y + t_obj.rel_y;
121 if (obj_x >= 0 && obj_x < 64 && obj_y >= 0 && obj_y < 64) {
122 RoomObject obj(t_obj.id, obj_x, obj_y, t_obj.size, t_obj.layer);
127 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