1111#include < glm/glm.hpp>
1212#include < nlohmann/json.hpp>
1313
14- #include " game/GameEntity.hpp"
15- #include " game/NPCEntity.hpp"
16- #include " game/PlayerEntity.hpp"
1714#include " logging/Logger.hpp"
15+ #include " game/GameEntity.hpp"
16+
17+ // Python integration
18+ #include < Python.h>
1819
1920class EntityManager {
2021public:
22+ virtual ~EntityManager ();
23+
24+ // Singleton access (optional, can be removed if you prefer dependency injection)
2125 static EntityManager& GetInstance ();
2226
23- // Entity lifecycle
24- uint64_t CreateEntity (EntityType type, const glm::vec3& position);
25- void DestroyEntity (uint64_t entityId);
26-
27- // Entity access
28- GameEntity* GetEntity (uint64_t entityId);
29- PlayerEntity* GetPlayerEntity (uint64_t playerId);
30- NPCEntity* GetNPCEntity (uint64_t npcId);
31-
32- // Spatial queries
33- std::vector<uint64_t > GetEntitiesInRadius (const glm::vec3& position,
34- float radius,
35- EntityType filter = EntityType::ANY);
36- std::vector<uint64_t > GetEntitiesInChunk (int chunkX, int chunkZ);
37-
38- // Updates
39- void Update (float deltaTime);
40- void UpdateEntityPosition (uint64_t entityId, const glm::vec3& newPosition);
41-
42- // Serialization
43- nlohmann::json SerializeEntity (uint64_t entityId) const ;
44- nlohmann::json SerializeEntitiesInRadius (const glm::vec3& position,
45- float radius) const ;
46-
47- // Ownership
48- void SetEntityOwner (uint64_t entityId, uint64_t ownerId);
49- std::vector<uint64_t > GetOwnedEntities (uint64_t ownerId);
50-
51- // Statistics
52- size_t GetTotalEntities () const ;
53- size_t GetPlayerCount () const ;
54- size_t GetNPCCount () const ;
55- size_t GetPendingDestructionCount () const ;
56-
57- // Debugging
58- void DumpEntityStats () const ;
59- const char * EntityTypeToString (EntityType type) const ;
60-
61- // Advanced queries
62- std::vector<uint64_t > FindEntitiesByCriteria (
63- const std::function<bool (const GameEntity&)>& predicate) const ;
64- std::vector<uint64_t > FindEntitiesInBox (const glm::vec3& minBounds,
65- const glm::vec3& maxBounds,
66- EntityType filter = EntityType::ANY) const ;
67-
68- // Entity pooling
69- void PreallocateEntityPool (EntityType type, size_t count);
70- uint64_t ActivatePooledEntity (EntityType type, const glm::vec3& position);
71- void DeactivateEntity (uint64_t entityId);
72-
73- private:
74- // Constructor is now defined in the .cpp file (no '= default')
75- EntityManager ();
76- ~EntityManager () = default ;
27+ // ----- Entity lifecycle -----
28+ virtual uint64_t CreateEntity (EntityType type, const glm::vec3& position) = 0;
29+ virtual void DestroyEntity (uint64_t entityId) = 0;
30+
31+ // ----- Entity access -----
32+ virtual GameEntity* GetEntity (uint64_t entityId) = 0;
33+ virtual const GameEntity* GetEntity (uint64_t entityId) const = 0;
34+
35+ // ----- Spatial queries -----
36+ virtual std::vector<uint64_t > GetEntitiesInRadius (const glm::vec3& position,
37+ float radius,
38+ EntityType filter = EntityType::ANY) const = 0;
39+ virtual std::vector<uint64_t > GetEntitiesInChunk (int chunkX, int chunkZ) const = 0;
40+
41+ // ----- Updates -----
42+ virtual void Update (float deltaTime) = 0;
43+ virtual void UpdateEntityPosition (uint64_t entityId, const glm::vec3& newPosition) = 0;
44+
45+ // ----- Serialization -----
46+ virtual nlohmann::json SerializeEntity (uint64_t entityId) const = 0;
47+ virtual nlohmann::json SerializeEntitiesInRadius (const glm::vec3& position,
48+ float radius) const = 0;
49+
50+ // ----- Ownership (generic) -----
51+ virtual void SetEntityOwner (uint64_t entityId, uint64_t ownerId) = 0;
52+ virtual std::vector<uint64_t > GetOwnedEntities (uint64_t ownerId) const = 0;
53+
54+ // ----- Statistics -----
55+ virtual size_t GetTotalEntities () const = 0;
56+ virtual size_t GetPendingDestructionCount () const = 0;
57+
58+ // ----- Debugging -----
59+ virtual void DumpEntityStats () const = 0;
60+ virtual const char * EntityTypeToString (EntityType type) const = 0;
61+
62+ // ----- Advanced queries -----
63+ virtual std::vector<uint64_t > FindEntitiesByCriteria (
64+ const std::function<bool (const GameEntity&)>& predicate) const = 0;
65+ virtual std::vector<uint64_t > FindEntitiesInBox (const glm::vec3& minBounds,
66+ const glm::vec3& maxBounds,
67+ EntityType filter = EntityType::ANY) const = 0;
68+
69+ // ----- Entity pooling -----
70+ virtual void PreallocateEntityPool (EntityType type, size_t count) = 0;
71+ virtual uint64_t ActivatePooledEntity (EntityType type, const glm::vec3& position) = 0;
72+ virtual void DeactivateEntity (uint64_t entityId) = 0;
73+
74+ // ----- Python scripting -----
75+ // Initializes the Python interpreter (call once at startup)
76+ virtual bool InitializePython () = 0;
77+ // Shuts down the Python interpreter
78+ virtual void ShutdownPython () = 0;
79+
80+ // Attach a Python script to an entity. The script should define a class
81+ // with the same name as the file (without .py) and implement:
82+ // - __init__(self, entity_id)
83+ // - on_create(self)
84+ // - on_update(self, dt)
85+ // - on_destroy(self)
86+ // - on_collision(self, other_id)
87+ // - etc.
88+ virtual bool AttachScript (uint64_t entityId, const std::string& scriptPath) = 0;
89+
90+ // Call a specific method on the entity's script (if attached).
91+ // Returns true if the method exists and was called.
92+ virtual bool CallScriptMethod (uint64_t entityId, const std::string& methodName,
93+ PyObject* args = nullptr , PyObject* kwargs = nullptr ) = 0;
94+
95+ // Detach and release the script object for an entity.
96+ virtual void DetachScript (uint64_t entityId) = 0;
97+
98+ // Check if an entity has a script attached.
99+ virtual bool HasScript (uint64_t entityId) const = 0;
100+
101+ protected:
102+ EntityManager () = default ;
77103
78104 // Non-copyable
79105 EntityManager (const EntityManager&) = delete ;
80106 EntityManager& operator =(const EntityManager&) = delete ;
81-
82- // Helper struct for delayed destruction
83- struct PendingDestruction {
84- uint64_t entityId;
85- EntityType type;
86- std::chrono::steady_clock::time_point destructionTime;
87- };
88-
89- // Core containers
90- std::unordered_map<uint64_t , std::unique_ptr<GameEntity>> entities_;
91- std::unordered_map<uint64_t , PlayerEntity*> playerEntities_;
92- std::unordered_map<uint64_t , NPCEntity*> npcEntities_;
93-
94- // Ownership mapping
95- std::unordered_map<uint64_t , std::vector<uint64_t >> ownership_;
96-
97- // Object pool
98- std::unordered_map<uint64_t , std::unique_ptr<GameEntity>> inactiveEntities_;
99-
100- // Destruction queue
101- std::vector<PendingDestruction> pendingDestruction_;
102-
103- uint64_t nextEntityId_;
104- mutable std::mutex mutex_;
105-
106- // Cleanup helpers
107- void CleanupDestroyedEntities ();
108- void CleanupStaleOwnershipReferences ();
109- };
107+ };
0 commit comments