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 :
35 std::filesystem::directory_iterator(directory_path)) {
36 if (entry.path().extension() == ".json") {
37 try {
38 std::ifstream i(entry.path());
39 json j;
40 i >> j;
41 ObjectTemplate tmpl = j.get<ObjectTemplate>();
42 templates_.push_back(tmpl);
43 } catch (const std::exception& e) {
44 std::cerr << "Failed to load template " << entry.path() << ": "
45 << e.what() << std::endl;
46 // Continue loading other templates
47 }
48 }
49 }
50
51 return absl::OkStatus();
52}
53
55 const ObjectTemplate& tmpl, const std::string& directory_path) {
56 if (!std::filesystem::exists(directory_path)) {
57 std::filesystem::create_directories(directory_path);
58 }
59
60 // Sanitize filename
61 std::string filename = tmpl.name;
62 std::replace(filename.begin(), filename.end(), ' ', '_');
63 std::string path =
64 absl::StrFormat("%s/%s.json", directory_path.c_str(), filename.c_str());
65
66 try {
67 json j = tmpl;
68 std::ofstream o(path);
69 o << std::setw(4) << j << std::endl;
70
71 // Add to in-memory list if not present
72 bool exists = false;
73 for (auto& t : templates_) {
74 if (t.name == tmpl.name) {
75 t = tmpl; // Update existing
76 exists = true;
77 break;
78 }
79 }
80 if (!exists) {
81 templates_.push_back(tmpl);
82 }
83
84 return absl::OkStatus();
85 } catch (const std::exception& e) {
86 return absl::InternalError(
87 absl::StrFormat("Failed to save template: %s", e.what()));
88 }
89}
90
92 const std::string& name, const std::string& description,
93 const std::vector<RoomObject>& objects, int origin_x, int origin_y) {
94 ObjectTemplate tmpl;
95 tmpl.name = name;
97 tmpl.category = "Custom"; // Default category
98
99 for (const auto& obj : objects) {
100 TemplateObject t_obj;
101 t_obj.id = obj.id_;
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_);
106 tmpl.objects.push_back(t_obj);
107 }
108
109 return tmpl;
110}
111
113 const ObjectTemplate& tmpl, int x, int y, Rom* rom) {
114 std::vector<RoomObject> new_objects;
115
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;
119
120 // Basic bounds check
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);
123 if (rom) {
124 obj.SetRom(rom);
125 obj.EnsureTilesLoaded();
126 }
127 new_objects.push_back(obj);
128 }
129 }
130
131 return new_objects;
132}
133
134} // namespace zelda3
135} // 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