Move entities to hashmap
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include <utils.hpp>
|
||||
#include <object/entity.hpp>
|
||||
#include <mutex>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace Game::State {
|
||||
class GameState {
|
||||
@@ -16,22 +17,19 @@ namespace Game::State {
|
||||
template<typename Fn>
|
||||
void withEntitiesLocked(Fn&& fn) {
|
||||
std::scoped_lock lock(mMutex);
|
||||
fn(mEntities);
|
||||
fn(mEntityMap);
|
||||
}
|
||||
|
||||
void sort(); // Sort entities by zIndex for correct rendering order
|
||||
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
|
||||
|
||||
// Update entity at index, by REFERENCE.
|
||||
Object::Entity* getAtIndex(size_t at);
|
||||
// Add an entity to the gamestate; Returns a temporary pointer to the entity added. Note that the validity of this pointer cannot be guaranteed after the first call.
|
||||
// Add an entity to the gamestate; returns a pointer to the stored entity.
|
||||
Object::Entity* addEntity(std::unique_ptr<Object::Entity> entity);
|
||||
bool removeEntity(const std::string& name);
|
||||
|
||||
private:
|
||||
mutable std::mutex mMutex; // Shared mutex for thread safety
|
||||
std::vector<std::unique_ptr<Object::Entity>> mEntities;
|
||||
std::unordered_map<std::string, std::unique_ptr<Object::Entity>> mEntityMap; // Own entities while allowing O(1) lookup by name
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user