Skip to content

Commit 31b87a7

Browse files
fix LogicEntity
1 parent 6ec04dc commit 31b87a7

2 files changed

Lines changed: 13 additions & 34 deletions

File tree

include/game/LogicEntity.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,46 +15,46 @@ class LogicEntity {
1515
public:
1616
LogicEntity();
1717
~LogicEntity();
18-
18+
1919
// Initialization
2020
void Initialize();
2121
void Shutdown();
22-
22+
2323
// NPC management
2424
uint64_t SpawnNPC(NPCType type, const glm::vec3& position, uint64_t ownerId = 0);
2525
void DespawnNPC(uint64_t npcId);
2626
void UpdateNPCs(float deltaTime);
2727
NPCEntity* GetNPCEntity(uint64_t npcId);
28-
28+
2929
// Entity management
3030
GameEntity* GetEntity(uint64_t entityId);
3131
PlayerEntity* GetPlayerEntity(uint64_t playerId);
32-
32+
3333
// Collision management
3434
CollisionResult CheckCollision(const glm::vec3& position, float radius, uint64_t excludeEntityId = 0);
3535
bool Raycast(const glm::vec3& origin, const glm::vec3& direction, float maxDistance, RaycastHit& hit);
3636
void UpdateCollisions(float deltaTime);
37-
37+
3838
// Loot management
3939
void CreateLootEntity(const glm::vec3& position, std::shared_ptr<LootItem> item, int quantity);
40-
40+
4141
// Statistics
4242
int GetActiveNPCCount() const { return activeNPCCount_; }
43-
43+
4444
private:
4545
std::unique_ptr<NPCManager> npcManager_;
4646
std::unordered_map<uint64_t, std::unique_ptr<NPCEntity>> npcEntities_;
4747
std::mutex npcMutex_;
4848
std::atomic<int> activeNPCCount_{0};
49-
49+
5050
MobSystem& mobSystem_;
5151
EntityManager& entityManager_;
5252
std::unique_ptr<CollisionSystem> collisionSystem_;
53-
53+
5454
// Loot systems
55-
std::unique_ptr<InventorySystem> inventorySystem_;
5655
std::unique_ptr<LootTableManager> lootTableManager_;
57-
56+
//std::unique_ptr<InventorySystem> inventorySystem_;
57+
5858
void InitializeNPCSystem();
5959
void InitializeMobSystem();
6060
void InitializeCollisionSystem();

src/game/LogicEntity.cpp

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,11 @@ void LogicEntity::Initialize() {
1818
InitializeNPCSystem();
1919
InitializeMobSystem();
2020
InitializeCollisionSystem();
21-
2221
auto& config = ConfigManager::GetInstance();
23-
2422
// Initialize loot systems
25-
inventorySystem_ = std::make_unique<InventorySystem>();
23+
//inventorySystem_ = std::make_unique<InventorySystem>();
2624
lootTableManager_ = std::make_unique<LootTableManager>();
2725
lootTableManager_->LoadLootTables("config/loot_tables.json");
28-
2926
Logger::Info("LogicEntity initialized");
3027
}
3128

@@ -35,42 +32,35 @@ void LogicEntity::Shutdown() {
3532
npcEntities_.clear();
3633
activeNPCCount_ = 0;
3734
}
38-
3935
npcManager_.reset();
4036
collisionSystem_.reset();
41-
inventorySystem_.reset();
37+
//inventorySystem_.reset();
4238
lootTableManager_.reset();
43-
4439
Logger::Info("LogicEntity shutdown");
4540
}
4641

4742
void LogicEntity::InitializeNPCSystem() {
4843
Logger::Info("Initializing NPC system...");
4944
npcManager_ = std::make_unique<NPCManager>();
50-
5145
auto& config = ConfigManager::GetInstance();
5246
int initialNPCCount = config.GetInt("npcs.initial_count", 20);
53-
5447
std::random_device rd;
5548
std::mt19937 gen(rd());
5649
std::uniform_real_distribution<float> posDist(-200.0f, 200.0f);
5750
std::uniform_int_distribution<int> npcTypeDist(0, 3);
58-
5951
for (int i = 0; i < initialNPCCount; ++i) {
6052
float x = posDist(gen);
6153
float z = posDist(gen);
6254
float y = 20.0f; // Default height
6355
NPCType type = static_cast<NPCType>(npcTypeDist(gen));
6456
SpawnNPC(type, glm::vec3(x, y, z));
6557
}
66-
6758
Logger::Info("Spawned {} initial NPCs", initialNPCCount);
6859
}
6960

7061
void LogicEntity::InitializeMobSystem() {
7162
Logger::Info("Initializing mob system...");
7263
mobSystem_.Initialize();
73-
7464
auto& config = ConfigManager::GetInstance();
7565
if (config.HasKey("mobs")) {
7666
nlohmann::json mobConfig = config.GetJson("mobs");
@@ -85,56 +75,46 @@ void LogicEntity::InitializeCollisionSystem() {
8575

8676
uint64_t LogicEntity::SpawnNPC(NPCType type, const glm::vec3& position, uint64_t ownerId) {
8777
std::lock_guard<std::mutex> lock(npcMutex_);
88-
8978
uint64_t npcId = npcManager_->SpawnNPC(type, position, ownerId);
9079
if (npcId == 0) {
9180
Logger::Error("Failed to spawn NPC type {}", static_cast<int>(type));
9281
return 0;
9382
}
94-
9583
NPCEntity* npc = npcManager_->GetNPC(npcId);
9684
if (!npc) {
9785
Logger::Error("Failed to get spawned NPC");
9886
return 0;
9987
}
100-
10188
npcEntities_[npcId] = std::unique_ptr<NPCEntity>(npc);
10289
activeNPCCount_++;
103-
10490
// Register in collision system
10591
BoundingSphere bounds{position, 1.0f};
10692
collisionSystem_->RegisterEntity(npcId, bounds, CollisionType::ENTITY);
107-
10893
Logger::Debug("Spawned NPC {} at [{:.1f}, {:.1f}, {:.1f}]",
10994
npcId, position.x, position.y, position.z);
11095
return npcId;
11196
}
11297

11398
void LogicEntity::DespawnNPC(uint64_t npcId) {
11499
std::lock_guard<std::mutex> lock(npcMutex_);
115-
116100
auto it = npcEntities_.find(npcId);
117101
if (it == npcEntities_.end()) {
118102
return;
119103
}
120-
121104
collisionSystem_->UnregisterEntity(npcId);
122105
npcManager_->DespawnNPC(npcId);
123106
npcEntities_.erase(it);
124107
activeNPCCount_--;
125-
126108
Logger::Debug("Despawned NPC {}", npcId);
127109
}
128110

129111
void LogicEntity::UpdateNPCs(float deltaTime) {
130112
std::lock_guard<std::mutex> lock(npcMutex_);
131-
132113
for (auto& [npcId, npc] : npcEntities_) {
133114
if (!npc) continue;
134115
npc->Update(deltaTime);
135116
collisionSystem_->UpdateEntity(npcId, npc->GetPosition());
136117
}
137-
138118
mobSystem_.UpdateSpawnZones(deltaTime);
139119
mobSystem_.ProcessRespawns(deltaTime);
140120
}
@@ -177,6 +157,5 @@ void LogicEntity::CreateLootEntity(const glm::vec3& position, std::shared_ptr<Lo
177157
uint64_t entityId = entityManager_.CreateEntity(EntityType::ITEM, position);
178158
BoundingSphere bounds{position, 0.5f};
179159
collisionSystem_->RegisterEntity(entityId, bounds, CollisionType::TRIGGER);
180-
181160
Logger::Debug("Created loot entity {}: {} x{}", entityId, item->GetName(), quantity);
182161
}

0 commit comments

Comments
 (0)