Skip to content

Commit deb622f

Browse files
fix Player
1 parent 8643c29 commit deb622f

9 files changed

Lines changed: 171 additions & 266 deletions

File tree

include/game/GameEntity.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#pragma once
22

33
#include <algorithm>
4+
#include <chrono>
45
#include <cmath>
56
#include <cstdint>
67
#include <iomanip>
78
#include <memory>
89
#include <random>
10+
#include <shared_mutex>
911
#include <sstream>
1012
#include <string>
1113
#include <vector>
@@ -200,7 +202,7 @@ class GameEntity : public std::enable_shared_from_this<GameEntity> {
200202
// JSON
201203
nlohmann::json ToJson() const;
202204
nlohmann::json JsonGetPosition() const;
203-
nlohmann::json JsonGetAttribute() const;
205+
nlohmann::json JsonGetAttribute(const std::string& key, const nlohmann::json& defaultValue = {}) const;
204206
void JsonSetAttribute(const std::string& key, const nlohmann::json& value);
205207
nlohmann::json JsonGetAttributes() const;
206208

@@ -251,6 +253,8 @@ class GameEntity : public std::enable_shared_from_this<GameEntity> {
251253
static std::atomic<uint64_t> next_entity_id_;
252254

253255
private:
256+
mutable std::shared_mutex mutex_;
257+
std::chrono::steady_clock::time_point last_movement_;
254258

255259
// Events
256260
//std::unordered_map<std::string, std::vector<EventCallback>> event_callbacks_;

include/game/GameLogic.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// #include "game/ChunkLOD.hpp"
1515
// #include "game/CollisionSystem.hpp"
1616
#include "game/LogicCore.hpp"
17+
#include "game/PlayerManager.hpp"
1718

1819
class GameLogic : public LogicCore
1920
{
@@ -63,7 +64,7 @@ class GameLogic : public LogicCore
6364
void DespawnNPC(uint64_t npcId);
6465
NPCEntity* GetNPCEntity(uint64_t npcId);
6566
GameEntity* GetEntity(uint64_t entityId);
66-
Player* GetPlayer(uint64_t playerId);
67+
std::shared_ptr<Player> GetPlayer(uint64_t playerId);
6768

6869
// Collision methods
6970
CollisionResult CheckCollision(const glm::vec3& position, float radius, uint64_t excludeEntityId = 0);
@@ -112,8 +113,6 @@ class GameLogic : public LogicCore
112113
static GameLogic* instance_;
113114

114115
// Component systems
115-
LogicWorld worldLogic_;
116-
LogicEntity entityLogic_;
117116
//PlayerManager& playerManager_;
118117

119118
// Database backend

include/game/LogicEntity.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818

1919
class LogicEntity {
2020
public:
21-
LogicEntity();
22-
~LogicEntity();
21+
static LogicEntity& GetInstance();
2322

2423
// Initialization
2524
void Initialize();
@@ -46,6 +45,12 @@ class LogicEntity {
4645
int GetActiveNPCCount() const { return activeNPCCount_; }
4746

4847
private:
48+
LogicEntity();
49+
~LogicEntity();
50+
51+
static std::mutex instanceMutex_;
52+
static LogicEntity* instance_;
53+
4954
std::unique_ptr<NPCManager> npcManager_;
5055
std::unordered_map<uint64_t, std::unique_ptr<NPCEntity>> npcEntities_;
5156
std::mutex npcMutex_;

include/game/Player.hpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525

2626
class InventorySystem;
2727
class SkillSystem;
28-
class QuestSystem;
2928

3029
struct PlayerAttributes {
3130
int strength = 10; // Physical power
@@ -159,6 +158,10 @@ class Player : public GameEntity {
159158
const std::string& GetUsername() const { return username_; }
160159

161160
void UpdatePosition(float x, float y, float z);
161+
void UpdateHeartbeat();
162+
bool IsHeartbeatExpired(int timeoutSeconds) const;
163+
void ApplyDamage(int damage, int64_t attackerId);
164+
void ApplyHealing(int amount, int64_t healerId);
162165

163166
// Player-specific properties
164167
void SetPlayerClass(PlayerClass player_class) { player_class_ = player_class; }
@@ -271,7 +274,7 @@ class Player : public GameEntity {
271274
// Player systems access
272275
InventorySystem& GetInventorySystem() const { return inventory_system_; }
273276
SkillSystem& GetSkillSystem() const { return skill_system_; }
274-
QuestSystem& GetQuestSystem() const { return quest_system_; }
277+
QuestManager& GetQuestManager() const { return quest_manager_; }
275278

276279
// Utility methods
277280
bool IsAlive() const { return stats_.health > 0; }
@@ -298,6 +301,10 @@ class Player : public GameEntity {
298301

299302
void SetCosmetic(const std::string& slot, const std::string& cosmetic_id);
300303
std::string GetCosmetic(const std::string& slot) const;
304+
float GetDistanceTo(const glm::vec3& other) const;
305+
void AddCurrencyGold(int amount);
306+
void AddCurrencyGems(int amount);
307+
void SetOnline(bool online);
301308

302309
// Player session
303310
void SetSessionId(uint64_t session_id) { session_id_ = session_id; }
@@ -310,13 +317,20 @@ class Player : public GameEntity {
310317
virtual nlohmann::json Serialize() const override;
311318
virtual void Deserialize(const nlohmann::json& data) override;
312319
nlohmann::json JsonGetInventory() const;
320+
nlohmann::json ToJson() const;
313321

314322
private:
315323
int64_t id_;
316324
std::string username_;
317325

318326
//struct Position {float x, y, z;} position_;
319-
//nlohmann::json attributes_;
327+
std::chrono::system_clock::time_point last_movement_;
328+
329+
bool online_ = false;
330+
std::chrono::system_clock::time_point created_at_;
331+
std::chrono::system_clock::time_point last_login_;
332+
std::chrono::system_clock::time_point last_logout_;
333+
std::chrono::system_clock::time_point last_heartbeat_;
320334

321335
mutable std::shared_mutex mutex_;
322336

@@ -341,7 +355,7 @@ class Player : public GameEntity {
341355
nlohmann::json buff_data;
342356
float duration;
343357
float time_remaining;
344-
std::chrono::steady_clock::time_point applied_time;
358+
std::chrono::system_clock::time_point applied_time;
345359
};
346360
std::unordered_map<std::string, ActiveBuff> active_buffs_;
347361

@@ -350,7 +364,7 @@ class Player : public GameEntity {
350364
std::string ability_id;
351365
float duration;
352366
float time_remaining;
353-
std::chrono::steady_clock::time_point start_time;
367+
std::chrono::system_clock::time_point start_time;
354368
};
355369
std::unordered_map<std::string, Cooldown> cooldowns_;
356370

@@ -377,7 +391,7 @@ class Player : public GameEntity {
377391
// Systems
378392
InventorySystem& inventory_system_;
379393
SkillSystem& skill_system_;
380-
QuestSystem& quest_system_;
394+
QuestManager& quest_manager_;
381395

382396
// Private methods
383397
void OnLevelUp();
@@ -404,14 +418,14 @@ class Player : public GameEntity {
404418
struct DamageSource {
405419
uint64_t attacker_id;
406420
int damage;
407-
std::chrono::steady_clock::time_point timestamp;
421+
std::chrono::system_clock::time_point timestamp;
408422
};
409423
std::deque<DamageSource> damage_sources_;
410424

411425
struct HealingSource {
412426
uint64_t healer_id;
413427
int healing;
414-
std::chrono::steady_clock::time_point timestamp;
428+
std::chrono::system_clock::time_point timestamp;
415429
};
416430
std::deque<HealingSource> healing_sources_;
417431

include/game/QuestManager.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <filesystem>
99
#include <memory>
1010
#include <mutex>
11+
#include <shared_mutex>
1112
#include <string>
1213
#include <unordered_map>
1314
#include <vector>

src/game/GameEntity.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,7 @@ uint64_t GameEntity::GenerateEntityId() {
715715

716716
nlohmann::json GameEntity::JsonGetPosition() const {
717717
std::shared_lock<std::shared_mutex> lock(mutex_);
718-
return {
718+
return nlohmann::json{
719719
{"x", position_.x},
720720
{"y", position_.y},
721721
{"z", position_.z},
@@ -726,15 +726,15 @@ nlohmann::json GameEntity::JsonGetPosition() const {
726726

727727
void GameEntity::JsonSetAttribute(const std::string& key, const nlohmann::json& value) {
728728
std::unique_lock<std::shared_mutex> lock(mutex_);
729-
attributes_[key] = value;
729+
properties_[key] = value;
730730
}
731731

732732
nlohmann::json GameEntity::JsonGetAttribute(const std::string& key, const nlohmann::json& defaultValue) const {
733733
std::shared_lock<std::shared_mutex> lock(mutex_);
734-
return attributes_.value(key, defaultValue);
734+
return properties_.value(key, defaultValue);
735735
}
736736

737737
nlohmann::json GameEntity::JsonGetAttributes() const {
738738
std::shared_lock<std::shared_mutex> lock(mutex_);
739-
return attributes_;
739+
return properties_;
740740
}

0 commit comments

Comments
 (0)