yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
object_templates.cc
Go to the documentation of this file.
1#include "object_templates.h"
2
3#include <filesystem>
4#include <fstream>
5#include <iostream>
6
7#include "absl/strings/str_format.h"
8#include "nlohmann/json.hpp"
9
10namespace yaze {
11namespace zelda3 {
12
13using json = nlohmann::json;
14
15// JSON serialization helpers
17 layer)
19 objects)
20
22 // Initialize with some default templates if needed
23}
24
26 const std::string& directory_path) {
27 templates_.clear();
28
29 if (!std::filesystem::exists(directory_path)) {
30 // It's okay if the directory doesn't exist yet, just return empty
31 return absl::OkStatus();
32 }
33
34 for (const auto& entry : std::filesystem::directory_iterator(directory_path)) {
35 if (entry.path().extension() == ".json") {
36 try {
37 std::ifstream i(entry.path());
38 json j;
39 i >> j;
40 ObjectTemplate tmpl = j.get<ObjectTemplate>();
41 templates_.push_back(tmpl);
42 } catch (const std::exception& e) {
43 std::cerr << "Failed to load template " << entry.path() << ": "
44 << e.what() << std::endl;
45 // Continue loading other templates
46 }
47 }
48 }
49
50 return absl::OkStatus();
51}
52
54 const ObjectTemplate& tmpl, const std::string& directory_path) {
55 if (!std::filesystem::exists(directory_path)) {
56 std::filesystem::create_directories(directory_path);
57 }
58
59 // Sanitize filename
60 std::string filename = tmpl.name;
61 std::replace(filename.begin(), filename.end(), ' ', '_');
62 std::string path =
63 absl::StrFormat("%s/%s.json", directory_path.c_str(), filename.c_str());
64
65 try {
66 json j = tmpl;
67 std::ofstream o(path);
68 o << std::setw(4) << j << std::endl;
69
70 // Add to in-memory list if not present
71 bool exists = false;
72 for (auto& t : templates_) {
73 if (t.name == tmpl.name) {
74 t = tmpl; // Update existing
75 exists = true;
76 break;
77 }
78 }
79 if (!exists) {
80 templates_.push_back(tmpl);
81 }
82
83 return absl::OkStatus();
84 } catch (const std::exception& e) {
85 return absl::InternalError(absl::StrFormat("Failed to save template: %s", e.what()));
86 }
87}
88
90 const std::string& name, const std::string& description,
91 const std::vector<RoomObject>& objects, int origin_x, int origin_y) {
92 ObjectTemplate tmpl;
93 tmpl.name = name;
95 tmpl.category = "Custom"; // Default category
96
97 for (const auto& obj : objects) {
98 TemplateObject t_obj;
99 t_obj.id = obj.id_;
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_);
104 tmpl.objects.push_back(t_obj);
105 }
106
107 return tmpl;
108}
109
111 const ObjectTemplate& tmpl, int x, int y, Rom* rom) {
112 std::vector<RoomObject> new_objects;
113
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;
117
118 // Basic bounds check
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);
121 if (rom) {
122 obj.SetRom(rom);
123 obj.EnsureTilesLoaded();
124 }
125 new_objects.push_back(obj);
126 }
127 }
128
129 return new_objects;
130}
131
132} // namespace zelda3
133} // namespace yaze
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:24
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)
void SetRom(Rom *rom)
Definition room_object.h:68
nlohmann::json json
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(TemplateObject, id, rel_x, rel_y, size, layer) NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ObjectTemplate
std::vector< TemplateObject > objects