diff options
author | max-001 <maxsmits21@kpnmail.nl> | 2024-11-22 11:14:13 +0100 |
---|---|---|
committer | max-001 <maxsmits21@kpnmail.nl> | 2024-11-22 11:14:13 +0100 |
commit | 08fee6eff251337dd4dca2b514a9c06d57ede0af (patch) | |
tree | ead93ad5594e1254a66a6429a600f080880f4d90 /src/crepe/ComponentManager.cpp | |
parent | 07bca38a27f794ed6a2f74536f7a909530ce7df0 (diff) |
Implemented persistent objects
Diffstat (limited to 'src/crepe/ComponentManager.cpp')
-rw-r--r-- | src/crepe/ComponentManager.cpp | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/src/crepe/ComponentManager.cpp b/src/crepe/ComponentManager.cpp index e310577..97df1f3 100644 --- a/src/crepe/ComponentManager.cpp +++ b/src/crepe/ComponentManager.cpp @@ -10,6 +10,11 @@ ComponentManager::ComponentManager() { dbg_trace(); } ComponentManager::~ComponentManager() { dbg_trace(); } void ComponentManager::delete_all_components_of_id(game_object_id_t id) { + // Do not delete persistent objects + if (this->persistent[id]) { + return; + } + // Loop through all the types (in the unordered_map<>) for (auto & [type, componentArray] : this->components) { // Make sure that the id (that we are looking for) is within the boundaries of the vector<> @@ -21,14 +26,33 @@ void ComponentManager::delete_all_components_of_id(game_object_id_t id) { } void ComponentManager::delete_all_components() { - this->components.clear(); + // Loop through all the ids and delete all components of each id + for (game_object_id_t id = 0; id < next_id; id++) { + delete_all_components_of_id(id); + } this->next_id = 0; } GameObject ComponentManager::new_object(const string & name, const string & tag, const Vector2 & position, double rotation, double scale) { + // Find the first available id (taking persistent objects into account) + while (this->persistent[this->next_id]) { + this->next_id++; + // Make sure that the persistent vector is large enough + if (persistent.size() <= next_id) { + this->persistent.resize(next_id + 1, false); + } + } + GameObject object{*this, this->next_id, name, tag, position, rotation, scale}; this->next_id++; + + // Make sure that the persistent vector is large enough + if (persistent.size() <= next_id) { + this->persistent.resize(next_id + 1, false); + } return object; } + +void ComponentManager::set_persistent(game_object_id_t id) { this->persistent[id] = true; } |