-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameRoom.h
157 lines (129 loc) · 4.85 KB
/
GameRoom.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// Created by floweryclover on 2024-01-30.
//
#ifndef BATTLEGAME_SERVER_GAMEROOM_H
#define BATTLEGAME_SERVER_GAMEROOM_H
#include <set>
#include <map>
#include <memory>
#include <chrono>
#include "UnrealTypes.h"
#define INTERFACE class
#define IMPLEMENTS
using SerializedEndpoint = unsigned long long;
using ClientId = SerializedEndpoint;
using GameRoomId = unsigned int;
using EntityId = int;
static constexpr GameRoomId ROOM_MAINMENU = 0;
class BaseRoom
{
public:
BaseRoom(const BaseRoom&) noexcept = delete;
BaseRoom(BaseRoom&&) noexcept = delete;
BaseRoom& operator=(const BaseRoom&) noexcept = delete;
BaseRoom& operator=(BaseRoom&&) noexcept = delete;
explicit BaseRoom(GameRoomId roomId) noexcept : mRoomId(roomId) {}
inline GameRoomId GetId() const noexcept { return mRoomId; }
virtual ~BaseRoom() = default;
virtual void OnPlayerJoined(ClientId clientId) noexcept = 0;
virtual void OnPlayerLeft(ClientId clientId) noexcept = 0;
virtual void OnPlayerPrepared(ClientId clientId) noexcept = 0;
private:
const GameRoomId mRoomId;
};
INTERFACE IPlayerCountable
{
public:
virtual bool IsEmpty() const noexcept = 0;
virtual bool IsFull() const noexcept = 0;
virtual bool IsPlayerJoined(ClientId clientId) const noexcept = 0;
virtual std::vector<ClientId> GetAllPlayers() const noexcept = 0;
};
INTERFACE IHasEntity
{
public:
virtual void OnOwningCharacterDestroyed(ClientId clientId) noexcept = 0;
virtual void OnEntityMove(EntityId entityId, const Vector3D& location, double direction) noexcept = 0;
virtual void OnPlayerMove(ClientId clientId, const Vector3D& location, double direction) noexcept = 0;
};
INTERFACE ITickable
{
public:
virtual void Tick() noexcept = 0;
};
INTERFACE ITakesCommand
{
public:
virtual void OnPlayerEnterCommand(ClientId clientId, int commandId) noexcept = 0;
};
class MainMenuRoom : public BaseRoom
{
public:
explicit MainMenuRoom(GameRoomId roomId) noexcept : BaseRoom(roomId) {}
~MainMenuRoom() override = default;
void OnPlayerJoined(ClientId clientId) noexcept override;
void OnPlayerLeft(ClientId clientId) noexcept override;
void OnPlayerPrepared(ClientId clientId) noexcept override;
};
class OneVsOneGameRoom
:public BaseRoom,
IMPLEMENTS
public ITickable,
public IPlayerCountable,
public IHasEntity,
public ITakesCommand
{
public:
explicit OneVsOneGameRoom(GameRoomId roomId, ClientId blueClientId, ClientId redClientId) noexcept;
~OneVsOneGameRoom() override = default;
void OnPlayerJoined(ClientId clientId) noexcept override;
void OnPlayerLeft(ClientId clientId) noexcept override;
void OnPlayerPrepared(ClientId clientId) noexcept override;
void Tick() noexcept override;
bool IsEmpty() const noexcept override;
bool IsFull() const noexcept override;
bool IsPlayerJoined(ClientId clientId) const noexcept override;
std::vector<ClientId> GetAllPlayers() const noexcept override;
void OnEntityMove(EntityId entityId, const Vector3D& location, double direction) noexcept override;
void OnPlayerMove(ClientId clientId, const Vector3D& location, double direction) noexcept override;
void OnOwningCharacterDestroyed(ClientId clientId) noexcept override;
void OnPlayerEnterCommand(ClientId clientId, int commandId) noexcept override;
private:
static constexpr int STATE_PREPARE_GAME = 1;
static constexpr int STATE_PLAY_GAME = 2;
static constexpr int STATE_GAME_END = 3;
static constexpr int STATE_PENDING_DESTROY = 4;
static constexpr int TIME_PREPARE_GAME = 10;
static constexpr int TIME_GAME_ONE_ROUND = 20;
static constexpr int TIME_GAME_END = 10;
static constexpr EntityId ENTITY_ID_PLAYER_BLUE = 1;
static constexpr EntityId ENTITY_ID_PLAYER_RED = 2;
static constexpr int TEAM_ID_BLUE = 1;
static constexpr int TEAM_ID_RED = 2;
static constexpr int WIN_SCORE = 5;
static const Vector3D CENTER_LOCATION;
int mBlueScore;
int mRedScore;
void SetRemainTimeTo(ClientId clientId, const std::string& text) const noexcept;
void IncrementScore(int teamId) noexcept;
void RespawnPlayer(int teamId) noexcept;
void HandleCommand(bool isBlue, int commandId);
std::chrono::time_point<std::chrono::steady_clock> mTimePoint;
std::chrono::seconds mTimeSet;
int mGameState;
const ClientId mBluePlayer;
const ClientId mRedPlayer;
bool mIsBlueValid;
bool mIsRedValid;
Vector3D mBlueLocation;
Vector3D mRedLocation;
double mBlueAngle;
double mRedAngle;
std::chrono::time_point<std::chrono::steady_clock> mBlueLastCommandTime;
std::chrono::time_point<std::chrono::steady_clock> mRedLastCommandTime;
std::chrono::milliseconds mBlueLastDelay;
std::chrono::milliseconds mRedLastDelay;
int mBlueLastCommand;
int mRedLastCommand;
};
#endif //BATTLEGAME_SERVER_GAMEROOM_H