Skip to content

Commit 70fc2d6

Browse files
fix NPCEntity
1 parent f24165b commit 70fc2d6

7 files changed

Lines changed: 214 additions & 198 deletions

File tree

include/game/LogicEntity.hpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
#include <memory>
66
#include <mutex>
77

8+
#include "config/ConfigManager.hpp"
9+
#include "logging/Logger.hpp"
10+
#include "network/BinaryProtocol.hpp"
11+
#include "network/ConnectionManager.hpp"
12+
813
#include "game/NPCSystem.hpp"
914
#include "game/MobSystem.hpp"
1015
#include "game/EntityManager.hpp"
1116
#include "game/CollisionSystem.hpp"
12-
#include "game/LootItem.hpp"
17+
//#include "game/LootItem.hpp"
18+
#include "game/LootTableManager.hpp"
1319

1420
class LogicEntity {
1521
public:

include/game/LootTableManager.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919

2020
class LootTableManager {
2121
public:
22+
LootTableManager();
23+
~LootTableManager();
24+
2225
// Singleton pattern
2326
static LootTableManager& GetInstance();
2427

@@ -78,8 +81,6 @@ class LootTableManager {
7881
);
7982

8083
private:
81-
LootTableManager();
82-
~LootTableManager() = default;
8384

8485
// Helper methods for loot generation
8586
std::shared_ptr<LootItem> CreateItemFromEntry(

include/game/NPCEntity.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ struct NPCAIProfile {
171171
float patrol_speed = 2.0f;
172172
float chase_speed_multiplier = 1.5f;
173173
float flee_health_threshold = 0.3f;
174+
float sight_range = 25.0f;
175+
float chase_range = 40.0f;
174176

175177
bool can_summon_allies = false;
176178
int max_allies = 0;
@@ -245,6 +247,7 @@ class NPCEntity : public GameEntity {
245247
// AI and behavior
246248
const NPCAIProfile& GetAIProfile() const { return ai_profile_; }
247249
void SetAIProfile(const NPCAIProfile& profile);
250+
void SetDefaultAIProfile();
248251

249252
void UpdateAI(float delta_time);
250253
void SetTarget(uint64_t target_id);
@@ -329,10 +332,12 @@ class NPCEntity : public GameEntity {
329332
std::string GetRarityString() const;
330333
std::string GetFactionString() const;
331334
std::string GetAIStateString() const;
335+
std::string AIStateToString(NPCAIState state) const;
332336

333337
NPCStats GetStats() const { return npc_stats_; };
334338
uint64_t GetOwnerId() const { return target_id_; };
335339
void SetBehaviorState(NPCAIState st) { ai_state_ = st; };
340+
void MoveTo(const glm::vec3& destination, float speed_multiplier = 1.0f);
336341

337342
private:
338343
NPCType npc_type_;

src/game/EntityManager.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ EntityManager& EntityManager::GetInstance() {
1010
EntityManager::EntityManager() : nextEntityId_(1) {
1111
Logger::Info("EntityManager initialized");
1212
}
13-
1413
// =============== Entity Lifecycle ===============
1514
uint64_t EntityManager::CreateEntity(EntityType type, const glm::vec3& position) {
1615
std::lock_guard<std::mutex> lock(mutex_);
@@ -26,7 +25,7 @@ uint64_t EntityManager::CreateEntity(EntityType type, const glm::vec3& position)
2625
break;
2726
}
2827
case EntityType::NPC: {
29-
auto npcEntity = std::make_unique<NPCEntity>(position);
28+
auto npcEntity = std::make_unique<NPCEntity>(NPCType::VILLAGER, position);
3029
npcEntities_[entityId] = npcEntity.get();
3130
entity = std::move(npcEntity);
3231
break;
@@ -410,4 +409,4 @@ void EntityManager::DeactivateEntity(uint64_t entityId) {
410409
inactiveEntities_[entityId] = std::move(it->second);
411410
entities_.erase(it);
412411
Logger::Debug("Deactivated entity {} to pool", entityId);
413-
}
412+
}

src/game/GameLogic.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,13 @@ void GameLogic::HandleFamiliarCommand(uint64_t sessionId, const nlohmann::json&
443443
}
444444

445445
if (command == "follow") {
446-
familiar->SetBehaviorState(NPCBehaviorState::FOLLOW);
446+
familiar->SetBehaviorState(NPCAIState::FOLLOW);
447447
familiar->SetTarget(playerId);
448448
} else if (command == "attack") {
449-
familiar->SetBehaviorState(NPCBehaviorState::CHASE);
449+
familiar->SetBehaviorState(NPCAIState::CHASE);
450450
familiar->SetTarget(targetId);
451451
} else if (command == "stay") {
452-
familiar->SetBehaviorState(NPCBehaviorState::IDLE);
452+
familiar->SetBehaviorState(NPCAIState::IDLE);
453453
familiar->SetTarget(0);
454454
}
455455

@@ -556,7 +556,7 @@ void GameLogic::GameLoop() {
556556

557557
auto lastUpdate = std::chrono::steady_clock::now();
558558

559-
while (instanceMutex_) {
559+
while (!instanceMutex_.try_lock()) {
560560
try {
561561
auto startTime = std::chrono::steady_clock::now();
562562

@@ -589,7 +589,9 @@ void GameLogic::GameLoop() {
589589
Logger::Error("Error in game loop: {}", e.what());
590590
}
591591
}
592-
592+
593+
instanceMutex_.unlock();
594+
593595
Logger::Info("Game loop stopped");
594596
}
595597

src/game/LogicEntity.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
#include "config/ConfigManager.hpp"
2-
#include "logging/Logger.hpp"
3-
#include "network/BinaryProtocol.hpp"
4-
#include "network/ConnectionManager.hpp"
51
#include "game/LogicEntity.hpp"
62

73
LogicEntity::LogicEntity()
@@ -21,8 +17,9 @@ void LogicEntity::Initialize() {
2117
auto& config = ConfigManager::GetInstance();
2218
// Initialize loot systems
2319
//inventorySystem_ = std::make_unique<InventorySystem>();
24-
lootTableManager_ = std::make_unique<LootTableManager>();
25-
lootTableManager_->LoadLootTables("config/loot_tables.json");
20+
//lootTableManager_ = std::make_unique<LootTableManager>();
21+
//lootTableManager_->LoadLootTables("config/loot_tables.json");
22+
LootTableManager::GetInstance().LoadLootTables("config/loot_tables.json");
2623
Logger::Info("LogicEntity initialized");
2724
}
2825

0 commit comments

Comments
 (0)