queue object destruction

This commit is contained in:
2026-04-22 08:13:20 +02:00
parent b211fbf2cd
commit a7a7f9f4e9
6 changed files with 45 additions and 1 deletions

View File

@@ -56,6 +56,7 @@ namespace Game {
static T* getEntityByName(const std::string& name);
template<typename T>
static void destroyEntity(T* entity);
static void processPendingEntityRemovals();
private:
int mTargetUpdatesPerSecond = TARGET_UPDATE_RATE;
@@ -140,7 +141,8 @@ namespace Game {
void GameManager::destroyEntity(T* entity) {
static_assert(std::is_base_of_v<Object::Entity, T>, "T must derive from Object::Entity");
if (entity) {
State::GameState::getInstance().removeEntity(entity->getName());
entity->setActive(false);
State::GameState::getInstance().queueEntityRemoval(entity->getName());
}
}
}

View File

@@ -7,6 +7,7 @@
#include <object/entity.hpp>
#include <mutex>
#include <unordered_map>
#include <vector>
namespace Game::State {
class GameState {
@@ -23,6 +24,8 @@ namespace Game::State {
void wipe();
Object::Entity* getEntityByName(const std::string& name); // Get an entity by name, returns nullptr if no entity with the name exists
std::vector<Object::Entity*> getEntitiesSnapshot(bool sortByZIndex = false); // Get a stable snapshot of entity pointers for iteration outside the lock
void queueEntityRemoval(const std::string& name);
void processPendingRemovals();
// Add an entity to the gamestate; returns a pointer to the stored entity.
Object::Entity* addEntity(std::unique_ptr<Object::Entity> entity);
@@ -31,5 +34,6 @@ namespace Game::State {
private:
mutable std::mutex mMutex; // Shared mutex for thread safety
std::unordered_map<std::string, std::unique_ptr<Object::Entity>> mEntityMap; // Own entities while allowing O(1) lookup by name
std::vector<std::string> mPendingRemovals;
};
}