yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
dungeon_room_store.h
Go to the documentation of this file.
1#ifndef YAZE_APP_EDITOR_DUNGEON_DUNGEON_ROOM_STORE_H
2#define YAZE_APP_EDITOR_DUNGEON_DUNGEON_ROOM_STORE_H
3
4#include <array>
5#include <cstddef>
6#include <memory>
7
8#include "rom/rom.h"
10#include "zelda3/game_data.h"
11
12namespace yaze::editor {
13
15 public:
16 static constexpr size_t kRoomCount = zelda3::kNumberOfRooms;
17
18 DungeonRoomStore() = default;
21
22 size_t size() const { return kRoomCount; }
23
24 zelda3::Room& operator[](size_t index) { return EnsureRoom(index); }
25 const zelda3::Room& operator[](size_t index) const {
26 return EnsureRoom(index);
27 }
28
30 return const_cast<zelda3::Room*>(
31 const_cast<const DungeonRoomStore*>(this)->TryEnsureRoom(room_id));
32 }
33
34 const zelda3::Room* TryEnsureRoom(int room_id) const {
35 if (room_id < 0 || room_id >= static_cast<int>(kRoomCount)) {
36 return nullptr;
37 }
38 return &EnsureRoom(static_cast<size_t>(room_id));
39 }
40
42 return const_cast<zelda3::Room*>(
43 const_cast<const DungeonRoomStore*>(this)->GetIfMaterialized(room_id));
44 }
45
46 const zelda3::Room* GetIfMaterialized(int room_id) const {
47 if (room_id < 0 || room_id >= static_cast<int>(kRoomCount)) {
48 return nullptr;
49 }
50 return rooms_[room_id].get();
51 }
52
53 zelda3::Room* GetIfLoaded(int room_id) {
54 auto* room = GetIfMaterialized(room_id);
55 return room != nullptr && room->IsLoaded() ? room : nullptr;
56 }
57
58 const zelda3::Room* GetIfLoaded(int room_id) const {
59 auto* room = GetIfMaterialized(room_id);
60 return room != nullptr && room->IsLoaded() ? room : nullptr;
61 }
62
63 void SetRom(Rom* rom) {
64 rom_ = rom;
65 ForEachMaterialized([rom](int, zelda3::Room& room) { room.SetRom(rom); });
66 }
67
73
74 Rom* rom() const { return rom_; }
76
77 void Clear() {
78 for (auto& room : rooms_) {
79 room.reset();
80 }
81 }
82
83 template <typename Fn>
84 void ForEachMaterialized(Fn&& fn) {
85 for (size_t i = 0; i < rooms_.size(); ++i) {
86 if (rooms_[i]) {
87 fn(static_cast<int>(i), *rooms_[i]);
88 }
89 }
90 }
91
92 template <typename Fn>
93 void ForEachMaterialized(Fn&& fn) const {
94 for (size_t i = 0; i < rooms_.size(); ++i) {
95 if (rooms_[i]) {
96 fn(static_cast<int>(i), *rooms_[i]);
97 }
98 }
99 }
100
101 template <typename Fn>
102 void ForEachLoaded(Fn&& fn) {
103 ForEachMaterialized([&fn](int room_id, zelda3::Room& room) {
104 if (room.IsLoaded()) {
105 fn(room_id, room);
106 }
107 });
108 }
109
110 template <typename Fn>
111 void ForEachLoaded(Fn&& fn) const {
112 ForEachMaterialized([&fn](int room_id, const zelda3::Room& room) {
113 if (room.IsLoaded()) {
114 fn(room_id, room);
115 }
116 });
117 }
118
119 int LoadedCount() const {
120 int count = 0;
121 ForEachLoaded([&count](int, const zelda3::Room&) { ++count; });
122 return count;
123 }
124
125 private:
126 zelda3::Room& EnsureRoom(size_t index) const {
127 if (!rooms_[index]) {
128 auto room = std::make_unique<zelda3::Room>();
129 room->SetRom(rom_);
130 room->SetGameData(game_data_);
131 rooms_[index] = std::move(room);
132 }
133 return *rooms_[index];
134 }
135
136 Rom* rom_ = nullptr;
138 mutable std::array<std::unique_ptr<zelda3::Room>, kRoomCount> rooms_;
139};
140
141} // namespace yaze::editor
142
143#endif // YAZE_APP_EDITOR_DUNGEON_DUNGEON_ROOM_STORE_H
The Rom class is used to load, save, and modify Rom data. This is a generic SNES ROM container and do...
Definition rom.h:28
const zelda3::Room * TryEnsureRoom(int room_id) const
const zelda3::Room * GetIfLoaded(int room_id) const
zelda3::Room * GetIfMaterialized(int room_id)
static constexpr size_t kRoomCount
void ForEachMaterialized(Fn &&fn) const
const zelda3::Room & operator[](size_t index) const
zelda3::Room * GetIfLoaded(int room_id)
std::array< std::unique_ptr< zelda3::Room >, kRoomCount > rooms_
void SetGameData(zelda3::GameData *game_data)
const zelda3::Room * GetIfMaterialized(int room_id) const
zelda3::GameData * game_data() const
zelda3::Room * TryEnsureRoom(int room_id)
zelda3::Room & EnsureRoom(size_t index) const
zelda3::Room & operator[](size_t index)
DungeonRoomStore(Rom *rom, zelda3::GameData *game_data=nullptr)
bool IsLoaded() const
Definition room.h:871
void SetRom(Rom *rom)
Definition room.h:961
void SetGameData(GameData *data)
Definition room.h:963
Editors are the view controllers for the application.
constexpr int kNumberOfRooms