Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
Player.cpp
Go to the documentation of this file.
1
8#include "Game/Player.hpp"
9#include <algorithm>
10
11namespace rtp::server
12{
14 // Public API
16
17 Player::Player(uint32_t sessionId)
18 : _sessionId(sessionId), _state(PlayerState::NotLogged), _entityId(0)
19 {
20 _isReady = false;
21 _roomId = 0;
22 }
23
25 {
26 }
27
28 void Player::setUsername(const std::string &username)
29 {
30 std::lock_guard lock(_mutex);
31 _username = username;
32 }
33
34 std::string Player::getUsername() const
35 {
36 std::lock_guard lock(_mutex);
37 return _username;
38 }
39
40 void Player::setRoomId(uint32_t roomId)
41 {
42 std::lock_guard lock(_mutex);
43 _roomId = roomId;
44 }
45
46 uint32_t Player::getRoomId() const
47 {
48 std::lock_guard lock(_mutex);
49 return _roomId;
50 }
51
52 bool Player::isInRoom() const
53 {
54 std::lock_guard lock(_mutex);
55 return _roomId != 0;
56 }
57
59 {
60 std::lock_guard lock(_mutex);
61 _state = state;
62 }
63
65 {
66 std::lock_guard lock(_mutex);
67 return _state;
68 }
69
70 void Player::setReady(bool ready)
71 {
72 std::lock_guard lock(_mutex);
73 _isReady = ready;
74 }
75
76 bool Player::isReady() const
77 {
78 std::lock_guard lock(_mutex);
79 return _isReady;
80 }
81
82 void Player::setEntityId(uint32_t entityId)
83 {
84 std::lock_guard lock(_mutex);
85 _entityId = entityId;
86 }
87
88 uint32_t Player::getId() const
89 {
90 std::lock_guard lock(_mutex);
91 return _sessionId;
92 }
93
94 uint32_t Player::getEntityId() const
95 {
96 std::lock_guard lock(_mutex);
97 return _entityId;
98 }
99
100 void Player::setMuted(bool muted)
101 {
102 std::lock_guard lock(_mutex);
103 _isMuted = muted;
104 }
105
106 bool Player::isMuted() const
107 {
108 std::lock_guard lock(_mutex);
109 return _isMuted;
110 }
111
113 {
114 std::lock_guard lock(_mutex);
115 _weaponKind = weapon;
116 }
117
119 {
120 std::lock_guard lock(_mutex);
121 return _weaponKind;
122 }
123
124 void Player::addScore(int delta)
125 {
126 std::lock_guard lock(_mutex);
127 _score = std::max(0, _score + delta);
128 }
129
130 void Player::setScore(int score)
131 {
132 std::lock_guard lock(_mutex);
133 _score = std::max(0, score);
134 }
135
137 {
138 std::lock_guard lock(_mutex);
139 return _score;
140 }
141} // namespace rtp::server
Player(uint32_t sessionId)
Constructor for Player.
Definition Player.cpp:17
uint32_t _sessionId
Unique player identifier.
Definition Player.hpp:161
uint32_t getId() const
Get the unique identifier of the Session.
Definition Player.cpp:88
void setRoomId(uint32_t roomId)
Set the room ID the player is in.
Definition Player.cpp:40
std::string getUsername() const
Get the username of the player.
Definition Player.cpp:34
void setScore(int score)
Set player's score.
Definition Player.cpp:130
void setWeaponKind(rtp::ecs::components::WeaponKind weapon)
Set the selected weapon kind for this player.
Definition Player.cpp:112
uint32_t _entityId
Associated entity ID in the ECS.
Definition Player.hpp:167
void setState(PlayerState state)
Set the state of the player.
Definition Player.cpp:58
void setReady(bool ready)
Set the readiness status of the player.
Definition Player.cpp:70
uint32_t getRoomId() const
Get the room ID the player is in.
Definition Player.cpp:46
bool isReady() const
Get the readiness status of the player.
Definition Player.cpp:76
void setEntityId(uint32_t entityId)
Set the entity ID associated with the player.
Definition Player.cpp:82
int _score
Player score.
Definition Player.hpp:171
std::string _username
Player username.
Definition Player.hpp:162
uint32_t getEntityId() const
Get the associated entity ID.
Definition Player.cpp:94
bool _isReady
Readiness status of the player.
Definition Player.hpp:165
uint32_t _roomId
ID of the room the player is in ; by default 0 (no room)
Definition Player.hpp:163
void addScore(int delta)
Add to player's score (can be negative)
Definition Player.cpp:124
void setMuted(bool muted)
Definition Player.cpp:100
rtp::ecs::components::WeaponKind getWeaponKind() const
Get the selected weapon kind for this player.
Definition Player.cpp:118
int getScore() const
Get player's score.
Definition Player.cpp:136
void setUsername(const std::string &username)
Set the username of the player.
Definition Player.cpp:28
~Player()
Destructor for Player.
Definition Player.cpp:24
PlayerState _state
Current state of the player.
Definition Player.hpp:164
bool _isMuted
Mute status of the player.
Definition Player.hpp:169
bool isInRoom() const
Check if the player is in a room.
Definition Player.cpp:52
PlayerState getState() const
Get the state of the player.
Definition Player.cpp:64
std::mutex _mutex
Mutex to protect access to player state.
Definition Player.hpp:168
rtp::ecs::components::WeaponKind _weaponKind
Selected weapon.
Definition Player.hpp:170
bool isMuted() const
Definition Player.cpp:106
WeaponKind
Types of player weapons.
File : GameManager.hpp License: MIT Author : Elias Josué HAJJAR LLAUQUEN elias-josue....
PlayerState
Enum representing the state of a player in the session.
Definition Player.hpp:28