Skip to content

Commit 5965a1e

Browse files
refactor Player
1 parent 4d18f9f commit 5965a1e

24 files changed

Lines changed: 3160 additions & 3619 deletions

ARCHITECTURE_DIAGRAM.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ EntityManager::CreateEntity()
146146
147147
├─→ Create Entity Object
148148
│ │
149-
│ ├─→ PlayerEntity
149+
│ ├─→ Player
150150
│ ├─→ NPCEntity
151151
│ └─→ GameEntity
152152

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ set(LOGIC_CORE_SOURCES
9898
src/game/LogicEntity.cpp
9999
src/game/GameLogic.cpp
100100
src/game/EntityManager.cpp
101-
src/game/PlayerEntity.cpp
101+
src/game/Player.cpp
102102
src/game/NPCEntity.cpp
103103
)
104104

clients/wx-cpp/src/core/GameWorld.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,11 @@ bool GameWorld::AddEntity(uint64_t entityId, const nlohmann::json& data) {
140140
std::shared_ptr<GameEntity> entity;
141141

142142
if (entityType == "player") {
143-
entity = std::make_shared<PlayerEntity>();
143+
entity = std::make_shared<Player>();
144144
entity->SetPosition(position);
145145
// Store reference to player
146146
if (data.value("is_local", false)) {
147-
player_ = std::static_pointer_cast<PlayerEntity>(entity);
147+
player_ = std::static_pointer_cast<Player>(entity);
148148
}
149149
}
150150
else if (entityType == "npc") {
@@ -583,4 +583,4 @@ void GameWorld::RequestChunk(int chunkX, int chunkZ) {
583583

584584
chunk->GenerateGeometry();
585585
loadedChunks_[GetChunkKey(chunkX, chunkZ)] = chunk;
586-
}
586+
}

include/game/EntityManager.hpp

Lines changed: 84 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -11,99 +11,97 @@
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

1920
class EntityManager {
2021
public:
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

Comments
 (0)