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)->GetIfMaterialized(room_id));
32 }
33
34 const zelda3::Room* GetIfMaterialized(int room_id) const {
35 if (room_id < 0 || room_id >= static_cast<int>(kRoomCount)) {
36 return nullptr;
37 }
38 return rooms_[room_id].get();
39 }
40
41 zelda3::Room* GetIfLoaded(int room_id) {
42 auto* room = GetIfMaterialized(room_id);
43 return room != nullptr && room->IsLoaded() ? room : nullptr;
44 }
45
46 const zelda3::Room* GetIfLoaded(int room_id) const {
47 auto* room = GetIfMaterialized(room_id);
48 return room != nullptr && room->IsLoaded() ? room : nullptr;
49 }
50
51 void SetRom(Rom* rom) {
52 rom_ = rom;
53 ForEachMaterialized([rom](int, zelda3::Room& room) { room.SetRom(rom); });
54 }
55
61
62 Rom* rom() const { return rom_; }
64
65 void Clear() {
66 for (auto& room : rooms_) {
67 room.reset();
68 }
69 }
70
71 template <typename Fn>
72 void ForEachMaterialized(Fn&& fn) {
73 for (size_t i = 0; i < rooms_.size(); ++i) {
74 if (rooms_[i]) {
75 fn(static_cast<int>(i), *rooms_[i]);
76 }
77 }
78 }
79
80 template <typename Fn>
81 void ForEachMaterialized(Fn&& fn) const {
82 for (size_t i = 0; i < rooms_.size(); ++i) {
83 if (rooms_[i]) {
84 fn(static_cast<int>(i), *rooms_[i]);
85 }
86 }
87 }
88
89 template <typename Fn>
90 void ForEachLoaded(Fn&& fn) {
91 ForEachMaterialized([&fn](int room_id, zelda3::Room& room) {
92 if (room.IsLoaded()) {
93 fn(room_id, room);
94 }
95 });
96 }
97
98 template <typename Fn>
99 void ForEachLoaded(Fn&& fn) const {
100 ForEachMaterialized([&fn](int room_id, const zelda3::Room& room) {
101 if (room.IsLoaded()) {
102 fn(room_id, room);
103 }
104 });
105 }
106
107 int LoadedCount() const {
108 int count = 0;
109 ForEachLoaded([&count](int, const zelda3::Room&) { ++count; });
110 return count;
111 }
112
113 private:
114 zelda3::Room& EnsureRoom(size_t index) const {
115 if (!rooms_[index]) {
116 auto room = std::make_unique<zelda3::Room>();
117 room->SetRom(rom_);
118 room->SetGameData(game_data_);
119 rooms_[index] = std::move(room);
120 }
121 return *rooms_[index];
122 }
123
124 Rom* rom_ = nullptr;
126 mutable std::array<std::unique_ptr<zelda3::Room>, kRoomCount> rooms_;
127};
128
129} // namespace yaze::editor
130
131#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 * 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 & 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:579
void SetRom(Rom *rom)
Definition room.h:655
void SetGameData(GameData *data)
Definition room.h:657
Editors are the view controllers for the application.
constexpr int kNumberOfRooms