Skip to content

Commit bbfc653

Browse files
fix GameLogic
1 parent 7052f94 commit bbfc653

2 files changed

Lines changed: 30 additions & 41 deletions

File tree

include/game/GameLogic.hpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,11 @@
1616
#include "game/ChunkLOD.hpp"
1717
#include "game/CollisionSystem.hpp"
1818

19-
//class LogicCore;
20-
//#include "game/LogicCore.hpp"
2119

2220
class GameLogic {
2321
public:
2422
static GameLogic& GetInstance();
2523

26-
//LogicCore& GetLogicCore() { return LogicCore::GetInstance(); }
27-
//LogicCore& GetLogicCore() { return logicCore_; }
28-
2924
// Core lifecycle
3025
void Initialize();
3126
void Shutdown();
@@ -57,7 +52,7 @@ class GameLogic {
5752
// IPC message handling
5853
void HandleIPCMessage(const nlohmann::json& message);
5954

60-
// 3D World methods
55+
// World methods
6156
std::shared_ptr<WorldChunk> GetOrCreateChunk(int chunkX, int chunkZ);
6257
void GenerateWorldAroundPlayer(uint64_t playerId, const glm::vec3& position);
6358
void PreloadWorldData(float radius);
@@ -84,17 +79,16 @@ class GameLogic {
8479
void HandleTradeRequest(uint64_t sessionId, const nlohmann::json& data);
8580
void HandleGoldTransaction(uint64_t sessionId, const nlohmann::json& data);
8681

87-
// 3D World message handlers
82+
// World message handlers
8883
void HandleWorldChunkRequest(uint64_t sessionId, const nlohmann::json& data);
8984
void HandlePlayerPositionUpdate(uint64_t sessionId, const nlohmann::json& data);
9085
void HandleNPCInteraction(uint64_t sessionId, const nlohmann::json& data);
9186
void HandleCollisionCheck(uint64_t sessionId, const nlohmann::json& data);
9287
void HandleEntitySpawnRequest(uint64_t sessionId, const nlohmann::json& data);
9388
void HandleFamiliarCommand(uint64_t sessionId, const nlohmann::json& data);
9489

95-
// Message handling (inherited from LogicCore)
90+
// Message handling
9691
void HandleMessage(uint64_t sessionId, const nlohmann::json& message) {
97-
// Delegate to base class or implement here
9892
HandleMessage(sessionId, message);
9993
}
10094

@@ -124,7 +118,6 @@ class GameLogic {
124118

125119
static std::mutex instanceMutex_;
126120
static GameLogic* instance_;
127-
//LogicCore& logicCore_;
128121

129122
// Component systems
130123
LogicWorld worldLogic_;
@@ -147,7 +140,7 @@ class GameLogic {
147140

148141
// Helper methods
149142
void RegisterWorldHandlers();
150-
void LoadGameData();
143+
bool LoadGameData();
151144
void SaveGameState();
152145
void SaveChunkData();
153146
void CleanupOldData();

src/game/GameLogic.cpp

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ GameLogic::GameLogic() {
1919
}
2020

2121
GameLogic::~GameLogic() {
22-
if (IsRunning()) {
22+
if (instance_) {
2323
Shutdown();
2424
}
2525
}
2626

2727
// =============== Initialization and Shutdown ===============
2828
void GameLogic::Initialize() {
29-
if (IsRunning()) {
29+
if (instance_) {
3030
Logger::Warn("GameLogic already initialized");
3131
return;
3232
}
3333

34-
Logger::Info("Initializing GameLogic with 3D world system...");
34+
Logger::Info("Initializing GameLogic with world system...");
3535

3636
auto& config = ConfigManager::GetInstance();
3737

@@ -58,21 +58,17 @@ void GameLogic::Initialize() {
5858
}
5959

6060
// Register handlers
61-
RegisterDefaultHandlers();
6261
RegisterWorldHandlers();
6362

64-
// Initialize base LogicCore
65-
LogicCore::Initialize();
66-
67-
Logger::Info("GameLogic 3D world system initialized successfully");
63+
Logger::Info("GameLogic world system initialized successfully");
6864
}
6965

7066
void GameLogic::Shutdown() {
71-
if (!IsRunning()) {
67+
if (!instance_) {
7268
return;
7369
}
7470

75-
Logger::Info("Shutting down GameLogic 3D world system...");
71+
Logger::Info("Shutting down GameLogic world system...");
7672

7773
// Shutdown component systems
7874
entityLogic_.Shutdown();
@@ -81,7 +77,7 @@ void GameLogic::Shutdown() {
8177
// Shutdown base LogicCore
8278
LogicCore::Shutdown();
8379

84-
Logger::Info("GameLogic 3D world system shutdown complete");
80+
Logger::Info("GameLogic world system shutdown complete");
8581
}
8682

8783
// =============== World Configuration ===============
@@ -93,7 +89,7 @@ const GameLogic::WorldConfig& GameLogic::GetWorldConfig() const {
9389
return static_cast<const WorldConfig&>(worldLogic_.GetConfig());
9490
}
9591

96-
// =============== 3D World Methods ===============
92+
// =============== World Methods ===============
9793
std::shared_ptr<WorldChunk> GameLogic::GetOrCreateChunk(int chunkX, int chunkZ) {
9894
return worldLogic_.GetOrCreateChunk(chunkX, chunkZ);
9995
}
@@ -215,7 +211,7 @@ void GameLogic::HandleGoldTransaction(uint64_t sessionId, const nlohmann::json&
215211
SendError(sessionId, "Gold transactions not implemented yet", 501);
216212
}
217213

218-
// =============== 3D World Message Handlers ===============
214+
// =============== World Message Handlers ===============
219215
void GameLogic::RegisterWorldHandlers() {
220216
RegisterHandler("world_chunk_request", [this](uint64_t sessionId, const nlohmann::json& data) {
221217
HandleWorldChunkRequest(sessionId, data);
@@ -288,7 +284,7 @@ void GameLogic::RegisterWorldHandlers() {
288284
HandleWorldChunkRequest(sessionId, jsonData);
289285
});
290286

291-
Logger::Info("Registered 3D world message handlers");
287+
Logger::Info("Registered world message handlers");
292288
}
293289

294290
void GameLogic::HandleWorldChunkRequest(uint64_t sessionId, const nlohmann::json& data) {
@@ -557,11 +553,11 @@ void GameLogic::SyncNearbyEntitiesToPlayer(uint64_t sessionId, const glm::vec3&
557553

558554
// =============== Thread Functions ===============
559555
void GameLogic::GameLoop() {
560-
Logger::Info("3D Game loop started");
556+
Logger::Info("Game loop started");
561557

562558
auto lastUpdate = std::chrono::steady_clock::now();
563559

564-
while (IsRunning()) {
560+
while (instanceMutex_) {
565561
try {
566562
auto startTime = std::chrono::steady_clock::now();
567563

@@ -570,7 +566,7 @@ void GameLogic::GameLoop() {
570566
float deltaTime = deltaTimeMillis.count() / 1000.0f;
571567
lastUpdate = now;
572568

573-
// Update 3D systems
569+
// Update systems
574570
UpdateWorld(deltaTime);
575571
entityLogic_.UpdateNPCs(deltaTime);
576572
entityLogic_.UpdateCollisions(deltaTime);
@@ -586,35 +582,35 @@ void GameLogic::GameLoop() {
586582
std::unique_lock<std::mutex> lock(gameLoopMutex_);
587583
gameLoopCV_.wait_for(lock,
588584
gameLoopInterval_ - std::chrono::milliseconds(processingTime),
589-
[this] { return !IsRunning(); });
585+
[this] { return !instance_; });
590586
} else {
591-
Logger::Warn("3D Game loop lagging: {}ms", processingTime);
587+
Logger::Warn("Game loop lagging: {}ms", processingTime);
592588
}
593589
} catch (const std::exception& e) {
594-
Logger::Error("Error in 3D game loop: {}", e.what());
590+
Logger::Error("Error in game loop: {}", e.what());
595591
}
596592
}
597593

598-
Logger::Info("3D Game loop stopped");
594+
Logger::Info("Game loop stopped");
599595
}
600596

601597
void GameLogic::SpawnerLoop() {
602-
Logger::Info("3D Spawner loop started");
598+
Logger::Info("Spawner loop started");
603599
LogicCore::SpawnerLoop();
604-
Logger::Info("3D Spawner loop stopped");
600+
Logger::Info("Spawner loop stopped");
605601
}
606602

607603
void GameLogic::SaveLoop() {
608-
Logger::Info("3D Save loop started");
604+
Logger::Info("Save loop started");
609605
LogicCore::SaveLoop();
610606
SaveChunkData();
611-
Logger::Info("3D Save loop stopped");
607+
Logger::Info("Save loop stopped");
612608
}
613609

614610
// =============== Game Tick Processing ===============
615611
void GameLogic::ProcessGameTick(float deltaTime) {
616612
LogicCore::ProcessGameTick(deltaTime);
617-
// Additional 3D game tick processing
613+
// Additional game tick processing
618614
}
619615

620616
void GameLogic::UpdateWorld(float deltaTime) {
@@ -635,7 +631,7 @@ void GameLogic::UpdateWorld(float deltaTime) {
635631
}
636632

637633
// =============== Data Management ===============
638-
void GameLogic::LoadGameData() {
634+
bool GameLogic::LoadGameData() {
639635
Logger::Debug("Loading game data");
640636
return true;
641637
}
@@ -659,9 +655,9 @@ void GameLogic::SaveGameState() {
659655
auto& dbClient = CitusClient::GetInstance();
660656
dbClient.SaveGameState("current_game", gameState);
661657

662-
Logger::Debug("3D game state saved");
658+
Logger::Debug("game state saved");
663659
} catch (const std::exception& e) {
664-
Logger::Error("Failed to save 3D game state: {}", e.what());
660+
Logger::Error("Failed to save game state: {}", e.what());
665661
}
666662
}
667663

@@ -671,7 +667,7 @@ void GameLogic::SaveChunkData() {
671667

672668
void GameLogic::CleanupOldData() {
673669
LogicCore::CleanupOldData();
674-
Logger::Debug("Cleaning up 3D game data");
670+
Logger::Debug("Cleaning up game data");
675671
}
676672

677673
// =============== World maintenance ===============

0 commit comments

Comments
 (0)