Compare commits
89 Commits
cd03175b98
...
hotkeymenu
| Author | SHA1 | Date | |
|---|---|---|---|
|
a5d7cc20ed
|
|||
|
d1d2b63be8
|
|||
|
62c5c762f9
|
|||
|
1d436aa1c3
|
|||
|
73dd2dabfa
|
|||
|
5b6254c690
|
|||
|
688b6e93ea
|
|||
|
d64c366f4b
|
|||
|
7d58b881b2
|
|||
|
20acbc0499
|
|||
|
4fe2e25029
|
|||
|
fd08833f3f
|
|||
|
165ebf7b2e
|
|||
|
a02cb2b309
|
|||
|
bd56fb0646
|
|||
|
39580c15c5
|
|||
|
ee39c1e429
|
|||
|
631e14e66e
|
|||
|
53d2e3cf6b
|
|||
|
cd33ea28dc
|
|||
|
a50898a88b
|
|||
|
833173b5e8
|
|||
|
1e4af7f298
|
|||
| e720439109 | |||
| 5f1e9a8d81 | |||
|
953b5dcc86
|
|||
|
f879c5fe8f
|
|||
|
b5ff44d793
|
|||
|
24252896c7
|
|||
|
4c0078a5f2
|
|||
|
4072e49b32
|
|||
|
e0080fa50c
|
|||
|
8bdcffcfa6
|
|||
|
6b987cf78d
|
|||
|
cba790f804
|
|||
|
ac3e949323
|
|||
|
b09c7f9efd
|
|||
|
110e6a62d2
|
|||
|
ba84864b6a
|
|||
|
c813c49707
|
|||
|
89213e9a97
|
|||
|
fb53ece340
|
|||
|
5d196c4b61
|
|||
|
0d9e5b647f
|
|||
|
079d653405
|
|||
|
990c6f078d
|
|||
|
599dfa0cee
|
|||
|
9c5ad07747
|
|||
|
df79a35eae
|
|||
|
d1d71ed086
|
|||
|
1ca88106ac
|
|||
|
7ca374ea53
|
|||
|
8641ddc525
|
|||
|
731c742346
|
|||
|
c0a560a16e
|
|||
|
50a6caf82e
|
|||
|
31bb0198fc
|
|||
|
51dc910359
|
|||
|
ced20ca991
|
|||
|
fa663d0481
|
|||
|
1a455a3d6b
|
|||
|
c8159bae6e
|
|||
|
02d872c49b
|
|||
|
2b8447766a
|
|||
|
2e556e0d45
|
|||
|
56a43d7a60
|
|||
|
4c2ac7e3f0
|
|||
|
da1586baed
|
|||
|
d0b30ba6f8
|
|||
|
4f0a81d670
|
|||
|
4128b7fbb7
|
|||
|
47b5a281fc
|
|||
|
05edb95150
|
|||
|
e146f490cf
|
|||
|
9477099cfc
|
|||
|
705671148b
|
|||
| 090ea962d3 | |||
| 6d0e56eb46 | |||
| b788caafa6 | |||
| b21439718b | |||
| 9a3356eb16 | |||
| 74848fbd78 | |||
| db4df33861 | |||
| 9667454811 | |||
| 1bee6aed9c | |||
| aaf76a3ff0 | |||
| d1690192db | |||
| be8b5dd8a7 | |||
| cc57e03bc4 |
27
.clang-tidy
Normal file
27
.clang-tidy
Normal file
@@ -0,0 +1,27 @@
|
||||
Checks: >-
|
||||
-*,
|
||||
readability-identifier-naming,
|
||||
readability-redundant-string-cstr,
|
||||
readability-redundant-string-init,
|
||||
readability-simplify-boolean-expr,
|
||||
performance-unnecessary-value-param,
|
||||
performance-unnecessary-copy-initialization,
|
||||
performance-for-range-copy,
|
||||
performance-implicit-conversion-in-loop,
|
||||
CheckOptions:
|
||||
- key: readability-identifier-naming.PrivateMemberPrefix
|
||||
value: 'm_'
|
||||
- key: readability-identifier-naming.ClassConstantCase
|
||||
value: aNy_CasE
|
||||
# an empty *Prefix needs a *Case to work
|
||||
- key: readability-identifier-naming.ClassConstantPrefix
|
||||
value: ''
|
||||
#- key: readability-identifier-naming.PrivateMemberCase
|
||||
# value: CamelCase
|
||||
#- key: readability-identifier-naming.FunctionCase
|
||||
# value: CamelCase
|
||||
#- key: readability-identifier-naming.EnumCase
|
||||
# value: camelBack
|
||||
|
||||
WarningsAsErrors: '*'
|
||||
FormatStyle: 'file'
|
||||
6
.gitignore
vendored
6
.gitignore
vendored
@@ -6,4 +6,8 @@ build/
|
||||
.DS_Store
|
||||
|
||||
|
||||
.vscode
|
||||
.vscode
|
||||
|
||||
imgui.ini
|
||||
|
||||
test/tdmap.tdmap3
|
||||
13
.vscode/c_cpp_properties.json
vendored
Normal file
13
.vscode/c_cpp_properties.json
vendored
Normal file
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "TD2",
|
||||
"cppStandard": "c++20",
|
||||
"includePath": [
|
||||
"include"
|
||||
],
|
||||
"compileCommands": ".vscode/compile_commands.json"
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
3
README.md
Normal file
3
README.md
Normal file
@@ -0,0 +1,3 @@
|
||||
# (CubeCraft) Tower Defense
|
||||
|
||||
## Currently in contruction 🏗️👷⚙️
|
||||
41
include/client/Client.h
Normal file
41
include/client/Client.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/IClientSocket.h>
|
||||
#include <client/PlayerManager.h>
|
||||
#include <td/common/StateMachine.h>
|
||||
#include <optional>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class ClientState;
|
||||
class LoggingState;
|
||||
|
||||
class Client : public StateMachine<Client, void, float> {
|
||||
private:
|
||||
std::shared_ptr<IClientSocket> m_Socket;
|
||||
PlayerManager m_Players;
|
||||
std::optional<PlayerID> m_Id;
|
||||
|
||||
public:
|
||||
Client(const std::shared_ptr<IClientSocket>& a_Socket);
|
||||
~Client();
|
||||
|
||||
void SendPacket(const protocol::PacketBase& a_Packet);
|
||||
|
||||
void Disconnect();
|
||||
|
||||
const PlayerManager& GetPlayers() const {
|
||||
return m_Players;
|
||||
}
|
||||
|
||||
const std::optional<PlayerID> GetId() const {
|
||||
return m_Id;
|
||||
}
|
||||
|
||||
friend class ClientState;
|
||||
friend class LoggingState;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
19
include/client/ClientState.h
Normal file
19
include/client/ClientState.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/Client.h>
|
||||
#include <td/misc/SlotGuard.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class ClientState : public Client::State, public protocol::PacketHandler, private utils::SlotGuard {
|
||||
public:
|
||||
ClientState(Client& a_Client);
|
||||
virtual ~ClientState() {}
|
||||
|
||||
protected:
|
||||
void SendPacket(const protocol::PacketBase& a_Packet);
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
24
include/client/IClientSocket.h
Normal file
24
include/client/IClientSocket.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/Types.h>
|
||||
#include <td/misc/Signal.h>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class IClientSocket {
|
||||
public:
|
||||
utils::Signal<> OnConnect;
|
||||
utils::Signal<> OnDisconnect;
|
||||
utils::Signal<const protocol::PacketBase&> OnReceive;
|
||||
|
||||
virtual void Send(const protocol::PacketBase& a_Packet) = 0;
|
||||
virtual void Disconnect() = 0;
|
||||
|
||||
IClientSocket() {}
|
||||
virtual ~IClientSocket() {}
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
39
include/client/PlayerManager.h
Normal file
39
include/client/PlayerManager.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
#include <td/misc/Signal.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class IClientSocket;
|
||||
|
||||
class PlayerManager : public protocol::PacketHandler {
|
||||
private:
|
||||
std::map<PlayerID, PlayerInfo> m_Players;
|
||||
std::shared_ptr<IClientSocket> m_Socket;
|
||||
|
||||
public:
|
||||
utils::Signal<const PlayerInfo&> OnPlayerJoin;
|
||||
utils::Signal<PlayerID> OnPlayerLeave;
|
||||
|
||||
PlayerManager(const std::shared_ptr<IClientSocket>& a_Socket);
|
||||
~PlayerManager();
|
||||
|
||||
PlayerInfo GetPlayer(PlayerID a_Player);
|
||||
|
||||
auto begin() const {
|
||||
return m_Players.begin();
|
||||
}
|
||||
|
||||
auto end() const {
|
||||
return m_Players.end();
|
||||
}
|
||||
|
||||
virtual void Handle(const protocol::packets::PlayerJoinPacket&) override;
|
||||
virtual void Handle(const protocol::packets::PlayerListPacket&) override;
|
||||
virtual void Handle(const protocol::packets::PlayerLeavePacket&) override;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
34
include/client/socket/FakeSocket.h
Normal file
34
include/client/socket/FakeSocket.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/IClientSocket.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
namespace server {
|
||||
class FakeSocket;
|
||||
} // namespace server
|
||||
|
||||
|
||||
namespace client {
|
||||
|
||||
class FakeSocket : public IClientSocket {
|
||||
private:
|
||||
std::shared_ptr<server::FakeSocket> m_Server;
|
||||
PeerID m_PeerId;
|
||||
|
||||
struct Private{ explicit Private() = default; };
|
||||
|
||||
public:
|
||||
FakeSocket(Private) {}
|
||||
~FakeSocket() {}
|
||||
|
||||
static std::shared_ptr<FakeSocket> Connect(const std::shared_ptr<server::FakeSocket>& a_Server);
|
||||
|
||||
void ReceiveFromFakePeer(PeerID a_Peer, const protocol::PacketBase& a_Packet);
|
||||
|
||||
virtual void Send(const protocol::PacketBase& a_Packet) override;
|
||||
virtual void Disconnect() override;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
35
include/client/state/GameState.h
Normal file
35
include/client/state/GameState.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/ClientState.h>
|
||||
#include <td/game/World.h>
|
||||
#include <td/simulation/ClientSimulation.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class GameState : public ClientState {
|
||||
private:
|
||||
game::WorldPtr m_World;
|
||||
sim::ClientSimulation m_Simulation;
|
||||
float m_CurrentLerp;
|
||||
|
||||
public:
|
||||
GameState(Client& a_Client, const game::WorldPtr& a_World, std::uint64_t a_StepTime, const std::vector<protocol::LockStep> a_FirstSteps);
|
||||
~GameState() {}
|
||||
|
||||
virtual void Update(float a_Delta) override;
|
||||
|
||||
float GetCurrentLerp() const {
|
||||
return m_CurrentLerp;
|
||||
}
|
||||
|
||||
game::WorldPtr GetWorld() const {
|
||||
return m_World;
|
||||
}
|
||||
|
||||
virtual void Handle(const protocol::packets::LockStepsPacket& a_LockStep) override;
|
||||
virtual void Handle(const protocol::packets::LockStepResponsePacket& a_LockStep) override;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
25
include/client/state/LobbyState.h
Normal file
25
include/client/state/LobbyState.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/ClientState.h>
|
||||
#include <td/game/World.h>
|
||||
#include <td/simulation/ClientSimulation.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class LobbyState : public ClientState {
|
||||
private:
|
||||
std::shared_ptr<game::World> m_World;
|
||||
public:
|
||||
LobbyState(Client& a_Client);
|
||||
~LobbyState();
|
||||
|
||||
virtual void Update(float a_Delta) override;
|
||||
|
||||
virtual void Handle(const protocol::packets::WorldHeaderPacket& a_Packet) override;
|
||||
virtual void Handle(const protocol::packets::WorldDataPacket& a_Packet) override;
|
||||
virtual void Handle(const protocol::packets::BeginGamePacket& a_Packet) override;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
22
include/client/state/LoggingState.h
Normal file
22
include/client/state/LoggingState.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/ClientState.h>
|
||||
#include <td/game/World.h>
|
||||
#include <td/simulation/ClientSimulation.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class LoggingState : public ClientState {
|
||||
public:
|
||||
LoggingState(Client& a_Client, const std::string& a_PlayerName);
|
||||
~LoggingState();
|
||||
|
||||
virtual void Update(float a_Delta) override;
|
||||
|
||||
virtual void Handle(const protocol::packets::PlayerJoinPacket& a_Packet) override;
|
||||
virtual void Handle(const protocol::packets::LoggingSuccessPacket& a_Packet) override;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
42
include/server/IServerSocket.h
Normal file
42
include/server/IServerSocket.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
#include <server/PlayerIds.h>
|
||||
#include <td/misc/Signal.h>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class IServerSocket {
|
||||
public:
|
||||
using PlayerPacketHandlerType = std::unique_ptr<protocol::PacketHandler>(PlayerID);
|
||||
using PlayerPacketHandler = std::function<PlayerPacketHandlerType>;
|
||||
|
||||
utils::Signal<PlayerID> OnPlayerConnect;
|
||||
utils::Signal<PlayerID> OnPlayerDisconnect;
|
||||
utils::Signal<PlayerID, const protocol::PacketBase&> OnReceive;
|
||||
|
||||
void Send(PlayerID a_PlayerId, const protocol::PacketBase& a_Packet);
|
||||
void Broadcast(const protocol::PacketBase& a_Packet);
|
||||
|
||||
void Disconnect(PlayerID a_PlayerId);
|
||||
|
||||
void RegisterHandler(const PlayerPacketHandler& a_Handler);
|
||||
void UnregisterHandler(const PlayerPacketHandler& a_Handler);
|
||||
|
||||
IServerSocket();
|
||||
virtual ~IServerSocket() {}
|
||||
|
||||
private:
|
||||
PlayerIds m_Ids;
|
||||
std::vector<PlayerPacketHandler> m_Handlers;
|
||||
|
||||
protected:
|
||||
void OnConnectPeer(PeerID a_PeerId);
|
||||
void OnDisconnectPeer(PeerID a_PeerId);
|
||||
void OnReceivePeer(PeerID a_PeerId, const protocol::PacketBase& a_Packet);
|
||||
virtual void SendPeer(PeerID a_PeerId, const protocol::PacketBase& a_Packet) = 0;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
49
include/server/PlayerIds.h
Normal file
49
include/server/PlayerIds.h
Normal file
@@ -0,0 +1,49 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <td/Types.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class PlayerIds {
|
||||
private:
|
||||
std::map<PeerID, PlayerID> m_PeerToPlayer;
|
||||
std::map<PlayerID, PeerID> m_PlayerToPeer;
|
||||
PlayerID m_NextId;
|
||||
|
||||
public:
|
||||
PlayerIds() : m_NextId(0) {}
|
||||
~PlayerIds() {}
|
||||
|
||||
void AddConnection(PeerID a_PeerId) {
|
||||
m_PeerToPlayer.emplace(a_PeerId, m_NextId);
|
||||
m_PlayerToPeer.emplace(m_NextId, a_PeerId);
|
||||
m_NextId++;
|
||||
}
|
||||
|
||||
PlayerID GetPlayerId(PeerID a_PeerId) const {
|
||||
return m_PeerToPlayer.at(a_PeerId);
|
||||
}
|
||||
|
||||
PeerID GetPeerId(PlayerID a_PlayerId) const {
|
||||
return m_PlayerToPeer.at(a_PlayerId);
|
||||
}
|
||||
|
||||
void RemovePeer(PeerID a_PeerId) {
|
||||
PlayerID playerId = GetPlayerId(a_PeerId);
|
||||
m_PeerToPlayer.erase(a_PeerId);
|
||||
m_PlayerToPeer.erase(playerId);
|
||||
}
|
||||
|
||||
auto begin() {
|
||||
return m_PeerToPlayer.begin();
|
||||
}
|
||||
|
||||
auto end() {
|
||||
return m_PeerToPlayer.end();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
44
include/server/PlayerManager.h
Normal file
44
include/server/PlayerManager.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/misc/Signal.h>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class IServerSocket;
|
||||
|
||||
class PlayerManager {
|
||||
private:
|
||||
std::map<PlayerID, PlayerInfo> m_Players;
|
||||
std::shared_ptr<IServerSocket> m_Socket;
|
||||
|
||||
public:
|
||||
utils::Signal<PlayerID, const PlayerInfo&> OnPlayerJoin;
|
||||
utils::Signal<PlayerID> OnPlayerLeave;
|
||||
|
||||
PlayerManager(const std::shared_ptr<IServerSocket>& a_Socket);
|
||||
~PlayerManager();
|
||||
|
||||
void RemovePlayer(PlayerID a_Player);
|
||||
PlayerInfo GetPlayer(PlayerID a_Player);
|
||||
|
||||
private:
|
||||
void Disconnect(PlayerID a_Player);
|
||||
|
||||
class ConnectionHandler : public protocol::PacketHandler {
|
||||
private:
|
||||
PlayerManager& m_PlayerManager;
|
||||
PlayerID m_Player;
|
||||
|
||||
public:
|
||||
ConnectionHandler(PlayerManager& a_PlayerManager, PlayerID a_Player);
|
||||
~ConnectionHandler() = default;
|
||||
|
||||
virtual void Handle(const protocol::packets::PlayerLoginPacket& a_Packet) override;
|
||||
virtual void Handle(const protocol::packets::DisconnectPacket& a_Packet) override;
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
31
include/server/Server.h
Normal file
31
include/server/Server.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <server/IServerSocket.h>
|
||||
#include <td/common/StateMachine.h>
|
||||
#include <server/PlayerManager.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class ServerState;
|
||||
|
||||
class Server : public StateMachine<Server, void, float> {
|
||||
private:
|
||||
std::shared_ptr<IServerSocket> m_Socket;
|
||||
PlayerManager m_Players;
|
||||
float m_LastMspt;
|
||||
|
||||
public:
|
||||
Server(const std::shared_ptr<IServerSocket>& a_Socket);
|
||||
|
||||
virtual void Update(float a_Delta);
|
||||
|
||||
const PlayerManager& GetPlayers() const {
|
||||
return m_Players;
|
||||
}
|
||||
|
||||
friend class ServerState;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
25
include/server/ServerState.h
Normal file
25
include/server/ServerState.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <server/Server.h>
|
||||
#include <td/misc/SlotGuard.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class ServerState : public Server::State, private utils::SlotGuard {
|
||||
public:
|
||||
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) = 0;
|
||||
virtual void Update(float a_Delta) = 0;
|
||||
virtual void OnPlayerJoin(PlayerID a_Id, const td::PlayerInfo& a_Info) {}
|
||||
virtual void OnPlayerLeave(PlayerID a_Id) {}
|
||||
|
||||
ServerState(Server& a_Server);
|
||||
virtual ~ServerState();
|
||||
|
||||
protected:
|
||||
void SendPacket(PlayerID a_Id, const protocol::PacketBase& a_Packet);
|
||||
void BroadcastPacket(const protocol::PacketBase& a_Packet);
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
35
include/server/socket/FakeSocket.h
Normal file
35
include/server/socket/FakeSocket.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/socket/FakeSocket.h>
|
||||
#include <optional>
|
||||
#include <server/IServerSocket.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class FakeSocket : public IServerSocket {
|
||||
private:
|
||||
std::vector<std::optional<std::weak_ptr<client::FakeSocket>>> m_Clients;
|
||||
|
||||
public:
|
||||
FakeSocket() {}
|
||||
~FakeSocket() {}
|
||||
|
||||
PeerID ConnectFakePeer(const std::shared_ptr<client::FakeSocket>& a_Socket);
|
||||
void DisconnectFakePeer(PeerID a_Peer);
|
||||
void ReceiveFromFakePeer(PeerID a_Peer, const protocol::PacketBase& a_Packet);
|
||||
|
||||
protected:
|
||||
virtual void SendPeer(PeerID a_Peer, const protocol::PacketBase& a_Packet) override;
|
||||
|
||||
private:
|
||||
/**
|
||||
* \return -1 if all previous ids are not free
|
||||
*/
|
||||
int GetNextFreeId();
|
||||
|
||||
friend class client::FakeSocket;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
17
include/server/state/EndGameState.h
Normal file
17
include/server/state/EndGameState.h
Normal file
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <server/ServerState.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class EndGameState : public ServerState {
|
||||
private:
|
||||
/* data */
|
||||
public:
|
||||
EndGameState(/* args */) {}
|
||||
~EndGameState() {}
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
31
include/server/state/GameState.h
Normal file
31
include/server/state/GameState.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <server/ServerState.h>
|
||||
#include <td/game/World.h>
|
||||
#include <td/simulation/ServerSimulation.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class GameStateHandler;
|
||||
|
||||
class GameState : public ServerState {
|
||||
private:
|
||||
std::shared_ptr<game::World> m_World;
|
||||
sim::ServerSimulation m_Simulation;
|
||||
float m_Time;
|
||||
|
||||
public:
|
||||
GameState(Server& a_Server, const std::shared_ptr<game::World>& a_World);
|
||||
~GameState() {}
|
||||
|
||||
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) override;
|
||||
virtual void Update(float a_Delta) override;
|
||||
|
||||
virtual void OnPlayerJoin(PlayerID a_Id, const td::PlayerInfo& a_Info) override;
|
||||
|
||||
friend class GameStateHandler;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
26
include/server/state/GameStateHandler.h
Normal file
26
include/server/state/GameStateHandler.h
Normal file
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class GameState;
|
||||
|
||||
class GameStateHandler : public protocol::PacketHandler {
|
||||
private:
|
||||
GameState& m_GameState;
|
||||
PlayerID m_PlayerId;
|
||||
|
||||
using protocol::PacketHandler::Handle;
|
||||
|
||||
public:
|
||||
GameStateHandler(GameState& a_GameState, PlayerID a_PlayerId);
|
||||
|
||||
virtual void Handle(const protocol::packets::SpawnTroopPacket& a_Packet) override;
|
||||
virtual void Handle(const protocol::packets::PlaceTowerPacket& a_Packet) override;
|
||||
virtual void Handle(const protocol::packets::LockStepRequestPacket& a_Packet) override;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
23
include/server/state/LobbyState.h
Normal file
23
include/server/state/LobbyState.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <server/ServerState.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
// this class is temporary useless
|
||||
class LobbyState : public ServerState {
|
||||
private:
|
||||
std::shared_ptr<game::World> m_World;
|
||||
public:
|
||||
LobbyState(Server& a_Server);
|
||||
~LobbyState() {}
|
||||
|
||||
virtual void OnPlayerJoin(PlayerID a_Id, const td::PlayerInfo& a_Info) override;
|
||||
|
||||
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) override;
|
||||
virtual void Update(float a_Delta) override;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
217
include/td/Maths.h
Normal file
217
include/td/Maths.h
Normal file
@@ -0,0 +1,217 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/common/DataBufferOperators.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
static constexpr float PI = 3.141592653f;
|
||||
|
||||
template <typename T>
|
||||
struct Vec2 {
|
||||
union {
|
||||
T x;
|
||||
T r;
|
||||
};
|
||||
|
||||
union {
|
||||
T y;
|
||||
T g;
|
||||
};
|
||||
|
||||
constexpr Vec2(T X = T(0), T Y = T(0)) : x(X), y(Y) {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline bool operator==(const Vec2<T>& vec2, const Vec2<T>& other) {
|
||||
return vec2.x == other.x && vec2.y == other.y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct Vec3 {
|
||||
union {
|
||||
T x;
|
||||
T r;
|
||||
};
|
||||
|
||||
union {
|
||||
T y;
|
||||
T g;
|
||||
};
|
||||
|
||||
union {
|
||||
T z;
|
||||
T b;
|
||||
};
|
||||
|
||||
constexpr Vec3(T X = 0, T Y = 0, T Z = 0) : x(X), y(Y), z(Z) {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline bool operator==(const Vec3<T>& vec3, const Vec3<T>& other) {
|
||||
return vec3.x == other.x && vec3.y == other.y && vec3.z == other.z;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
struct Vec4 {
|
||||
union {
|
||||
T x;
|
||||
T r;
|
||||
};
|
||||
|
||||
union {
|
||||
T y;
|
||||
T g;
|
||||
};
|
||||
|
||||
union {
|
||||
T z;
|
||||
T b;
|
||||
};
|
||||
|
||||
union {
|
||||
T w;
|
||||
T a;
|
||||
};
|
||||
|
||||
constexpr Vec4(Vec3<T> vec, T W = 1) : x(vec.x), y(vec.y), z(vec.z), w(W) {}
|
||||
constexpr Vec4(T X = 0, T Y = 0, T Z = 0, T W = 0) : x(X), y(Y), z(Z), w(W) {}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline bool operator==(const Vec4<T>& vec4, const Vec4<T>& other) {
|
||||
return vec4.x == other.x && vec4.y == other.y && vec4.z == other.z && vec4.w = other.w;
|
||||
}
|
||||
|
||||
using Vec2i = Vec2<int>;
|
||||
using Vec2u = Vec2<unsigned int>;
|
||||
using Vec2f = Vec2<float>;
|
||||
using Vec2d = Vec2<double>;
|
||||
|
||||
using Vec3i = Vec3<int>;
|
||||
using Vec3u = Vec3<unsigned int>;
|
||||
using Vec3f = Vec3<float>;
|
||||
using Vec3d = Vec3<double>;
|
||||
|
||||
using Vec4i = Vec4<int>;
|
||||
using Vec4u = Vec4<unsigned int>;
|
||||
using Vec4f = Vec4<float>;
|
||||
using Vec4d = Vec4<double>;
|
||||
|
||||
using Color = Vec3<unsigned char>;
|
||||
|
||||
template <typename T>
|
||||
struct Mat4 {
|
||||
static const std::size_t MATRIX_SIZE = 4;
|
||||
|
||||
T x0, x1, x2, x3;
|
||||
T y0, y1, y2, y3;
|
||||
T z0, z1, z2, z3;
|
||||
T w0, w1, w2, w3;
|
||||
|
||||
T operator[](std::size_t offset) const {
|
||||
return reinterpret_cast<const T*>(this)[offset];
|
||||
}
|
||||
|
||||
T& operator[](std::size_t offset) {
|
||||
return reinterpret_cast<T*>(this)[offset];
|
||||
}
|
||||
|
||||
T* data() {
|
||||
return reinterpret_cast<T*>(this);
|
||||
}
|
||||
|
||||
const T* data() const {
|
||||
return reinterpret_cast<const T*>(this);
|
||||
}
|
||||
|
||||
T at(std::size_t row, std::size_t column) const {
|
||||
return operator[](row * MATRIX_SIZE + column);
|
||||
}
|
||||
|
||||
T& at(std::size_t row, std::size_t column) {
|
||||
return operator[](row * MATRIX_SIZE + column);
|
||||
}
|
||||
};
|
||||
|
||||
typedef Mat4<float> Mat4f;
|
||||
typedef Mat4<int> Mat4i;
|
||||
typedef Mat4<double> Mat4d;
|
||||
|
||||
template <typename T>
|
||||
inline bool operator==(const Mat4<T>& mat, const Mat4<T>& other) {
|
||||
return mat.x0 == other.x0 && mat.y0 == other.y0 && mat.z0 == other.z0 && mat.w0 == other.w0 && mat.x1 == other.x1 &&
|
||||
mat.y1 == other.y1 && mat.z1 == other.z1 && mat.w1 == other.w1 && mat.x2 == other.x2 && mat.y2 == other.y2 &&
|
||||
mat.z2 == other.z2 && mat.w2 == other.w2 && mat.x3 == other.x3 && mat.y3 == other.y3 && mat.z3 == other.z3 &&
|
||||
mat.w3 == other.w3;
|
||||
}
|
||||
|
||||
namespace maths {
|
||||
|
||||
template<typename T>
|
||||
Mat4<T> Transpose(const Mat4<T>& mat) {
|
||||
Mat4<T> result;
|
||||
|
||||
result.x1 = mat.y0;
|
||||
result.x2 = mat.z0;
|
||||
result.x3 = mat.w0;
|
||||
result.y0 = mat.x1;
|
||||
result.y2 = mat.z1;
|
||||
result.y3 = mat.w1;
|
||||
result.z0 = mat.x2;
|
||||
result.z1 = mat.y2;
|
||||
result.z3 = mat.w2;
|
||||
result.w0 = mat.x3;
|
||||
result.w1 = mat.y3;
|
||||
result.w2 = mat.z3;
|
||||
result.x0 = mat.x0;
|
||||
result.y1 = mat.y1;
|
||||
result.z2 = mat.z2;
|
||||
result.w3 = mat.w3;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
Mat4f Perspective(float fovY, float aspectRatio, float zNear, float zFar);
|
||||
Mat4f Look(const Vec3f& eye, const Vec3f& center, const Vec3f& up);
|
||||
|
||||
Mat4f Inverse(const Mat4f& mat);
|
||||
|
||||
template<typename T>
|
||||
T Lerp(T v0, T v1, T t) {
|
||||
return (T(1) - t) * v0 + t * v1;
|
||||
}
|
||||
|
||||
} // namespace maths
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Vec2<T>& a_Vec) {
|
||||
return a_Buffer << a_Vec.x << a_Vec.y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, Vec2<T>& a_Vec) {
|
||||
return a_Buffer >> a_Vec.x >> a_Vec.y;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Vec3<T>& a_Vec) {
|
||||
return a_Buffer << a_Vec.x << a_Vec.y << a_Vec.z;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, Vec3<T>& a_Vec) {
|
||||
return a_Buffer >> a_Vec.x >> a_Vec.y >> a_Vec.z;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Vec4<T>& a_Vec) {
|
||||
return a_Buffer << a_Vec.x << a_Vec.y << a_Vec.z << a_Vec.w;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, Vec4<T>& a_Vec) {
|
||||
return a_Buffer >> a_Vec.x >> a_Vec.y >> a_Vec.z >> a_Vec.w;
|
||||
}
|
||||
|
||||
} // namespace td
|
||||
@@ -2,13 +2,23 @@
|
||||
|
||||
#include <cstdint>
|
||||
#include <fpm/fixed.hpp>
|
||||
#include <td/Maths.h>
|
||||
|
||||
namespace sp {
|
||||
class DataBuffer;
|
||||
} // namespace sp
|
||||
|
||||
namespace td {
|
||||
|
||||
using FpFloat = fpm::fixed_16_16;
|
||||
|
||||
enum class Team : std::uint8_t {
|
||||
Blue = 0,
|
||||
using StepTime = std::uint16_t;
|
||||
|
||||
constexpr int STEP_TIME = 50;
|
||||
|
||||
enum class TeamColor : std::int8_t {
|
||||
None = -1,
|
||||
Blue,
|
||||
Red,
|
||||
};
|
||||
|
||||
@@ -30,7 +40,7 @@ enum class EntityType : std::uint8_t {
|
||||
Zombie = 0,
|
||||
Spider,
|
||||
Pigman,
|
||||
Skeleton,
|
||||
Skelon,
|
||||
Creeper,
|
||||
Silverfish,
|
||||
Blaze,
|
||||
@@ -55,17 +65,28 @@ using TowerID = std::uint16_t;
|
||||
using PlayerID = std::uint8_t;
|
||||
using EntityID = std::uint16_t;
|
||||
|
||||
struct TowerCoords {
|
||||
std::int16_t x;
|
||||
std::int16_t y;
|
||||
};
|
||||
using TowerCoords = Vec2<std::int16_t>;
|
||||
|
||||
struct EntityCoords {
|
||||
FpFloat x;
|
||||
FpFloat y;
|
||||
};
|
||||
using EntityCoords = Vec2<FpFloat>;
|
||||
|
||||
using PeerID = std::uint16_t;
|
||||
using StepsType = std::uint16_t;
|
||||
|
||||
enum class Direction : std::uint8_t {
|
||||
PositiveX = 1 << 0,
|
||||
NegativeX = 1 << 1,
|
||||
PositiveY = 1 << 2,
|
||||
NegativeY = 1 << 3,
|
||||
};
|
||||
|
||||
struct PlayerInfo {
|
||||
PlayerID m_PlayerId;
|
||||
std::string m_PlayerName;
|
||||
};
|
||||
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const EntityCoords& a_Coords);
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const FpFloat& a_Float);
|
||||
|
||||
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, EntityCoords& a_Coords);
|
||||
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, FpFloat& a_Float);
|
||||
|
||||
} // namespace td
|
||||
|
||||
60
include/td/common/Array.h
Normal file
60
include/td/common/Array.h
Normal file
@@ -0,0 +1,60 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace td {
|
||||
|
||||
/**
|
||||
* Reflectable std::array
|
||||
*/
|
||||
template <typename T, std::size_t S>
|
||||
class Array {
|
||||
private:
|
||||
T* m_Data;
|
||||
|
||||
public:
|
||||
Array() : m_Data(new T[S]) {}
|
||||
|
||||
Array(const Array& a_Other) : Array() {
|
||||
*this = a_Other;
|
||||
}
|
||||
|
||||
Array(Array&& a_Other) {
|
||||
m_Data = a_Other.m_Data;
|
||||
a_Other.m_Data = nullptr;
|
||||
}
|
||||
|
||||
Array(const std::initializer_list<T>& args) {
|
||||
std::size_t i = 0;
|
||||
for(const T& element : args) {
|
||||
m_Data[i] = element;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
Array& operator=(const Array& a_Other) {
|
||||
for (std::size_t i = 0; i < S; i++) {
|
||||
m_Data[i] = a_Other.m_Data[i];
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
Array& operator=(Array&& a_Other) {
|
||||
std::swap(m_Data, a_Other.m_Data);
|
||||
return *this;
|
||||
}
|
||||
|
||||
T& operator[](std::size_t a_Index) {
|
||||
return m_Data[a_Index];
|
||||
}
|
||||
|
||||
const T& operator[](std::size_t a_Index) const {
|
||||
return m_Data[a_Index];
|
||||
}
|
||||
|
||||
~Array() {
|
||||
delete [] m_Data;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace td
|
||||
@@ -1,300 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file DataBuffer.h
|
||||
* \brief File containing the td::DataBuffer class
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <td/common/VarInt.h>
|
||||
#include <vector>
|
||||
|
||||
namespace td {
|
||||
|
||||
/**
|
||||
* \class DataBuffer
|
||||
* \brief Class used to manipulate memory
|
||||
*/
|
||||
class DataBuffer {
|
||||
private:
|
||||
typedef std::vector<std::uint8_t> Data;
|
||||
Data m_Buffer;
|
||||
std::size_t m_ReadOffset;
|
||||
|
||||
public:
|
||||
typedef Data::iterator iterator;
|
||||
typedef Data::const_iterator const_iterator;
|
||||
typedef Data::reference reference;
|
||||
typedef Data::const_reference const_reference;
|
||||
typedef Data::difference_type difference_type;
|
||||
|
||||
DataBuffer();
|
||||
DataBuffer(const DataBuffer& other);
|
||||
DataBuffer(const DataBuffer& other, difference_type offset);
|
||||
DataBuffer(DataBuffer&& other);
|
||||
DataBuffer(const std::string& str);
|
||||
|
||||
DataBuffer& operator=(const DataBuffer& other);
|
||||
DataBuffer& operator=(DataBuffer&& other);
|
||||
|
||||
/**
|
||||
* \brief Append data to the buffer
|
||||
*/
|
||||
template <typename T>
|
||||
void Append(const T& data) {
|
||||
std::size_t size = sizeof(data);
|
||||
std::size_t end_pos = m_Buffer.size();
|
||||
m_Buffer.resize(m_Buffer.size() + size);
|
||||
std::memcpy(&m_Buffer[end_pos], &data, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Append data to the buffer
|
||||
*/
|
||||
template <typename T>
|
||||
DataBuffer& operator<<(const T& data) {
|
||||
Append(data);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Append a string to the buffer
|
||||
* \warning Don't use it for binary data !
|
||||
* \param str The string to append
|
||||
*/
|
||||
DataBuffer& operator<<(const std::string& str);
|
||||
|
||||
/**
|
||||
* \brief Append data to the buffer from another const buffer
|
||||
* \param data The buffer to append
|
||||
*/
|
||||
DataBuffer& operator<<(const DataBuffer& data);
|
||||
|
||||
/**
|
||||
* \brief Append a vector to the buffer by first writing the size
|
||||
* \param data The vector to append
|
||||
*/
|
||||
template <typename T>
|
||||
DataBuffer& operator<<(const std::vector<T>& data) {
|
||||
*this << VarInt{data.size()};
|
||||
for (const auto& element : data) {
|
||||
*this << element;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Append an array to the buffer by first writing the size
|
||||
* \param data The buffer to append
|
||||
*/
|
||||
template <typename T, std::size_t Size>
|
||||
DataBuffer& operator<<(const std::array<T, Size>& data) {
|
||||
for (const auto& element : data) {
|
||||
*this << element;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Read some data from the buffer and assign to desired variable
|
||||
*/
|
||||
template <typename T>
|
||||
DataBuffer& operator>>(T& data) {
|
||||
assert(m_ReadOffset + sizeof(T) <= GetSize());
|
||||
data = *(reinterpret_cast<T*>(&m_Buffer[m_ReadOffset]));
|
||||
m_ReadOffset += sizeof(T);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Read some data from the buffer and assign to the new buffer
|
||||
* \param data The buffer to assign
|
||||
*/
|
||||
DataBuffer& operator>>(DataBuffer& data);
|
||||
|
||||
/**
|
||||
* \brief Read a string from the buffer
|
||||
* \param str The string to assign in the new buffer
|
||||
* \warning Don't use it for binary data !
|
||||
*/
|
||||
DataBuffer& operator>>(std::string& str);
|
||||
|
||||
/**
|
||||
* \brief Read a vector (size + data) from the buffer
|
||||
* \pre The vector is assumed to be empty
|
||||
*/
|
||||
template <typename T>
|
||||
DataBuffer& operator>>(std::vector<T>& data) {
|
||||
VarInt arraySize;
|
||||
*this >> arraySize;
|
||||
for (std::size_t i = 0; i < arraySize.GetValue(); i++) {
|
||||
T newElement;
|
||||
*this >> newElement;
|
||||
data.push_back(newElement);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Read an array from the buffer
|
||||
*/
|
||||
template <std::size_t Size, typename T>
|
||||
DataBuffer& operator>>(std::array<T, Size>& data) {
|
||||
for (std::size_t i = 0; i < Size; i++) {
|
||||
T newElement;
|
||||
*this >> newElement;
|
||||
data[i] = newElement;
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Write some data to the buffer
|
||||
* \param buffer The buffer to write
|
||||
* \param amount The amount of data to write
|
||||
*/
|
||||
void WriteSome(const char* buffer, std::size_t amount);
|
||||
|
||||
/**
|
||||
* \brief Write some data to the buffer
|
||||
* \param buffer The buffer to write
|
||||
* \param amount The amount of data to write
|
||||
*/
|
||||
void WriteSome(const std::uint8_t* buffer, std::size_t amount);
|
||||
|
||||
/**
|
||||
* \brief Read some data from the buffer
|
||||
* \param buffer The buffer to Read
|
||||
* \param amount The amount of data from the buffer
|
||||
*/
|
||||
void ReadSome(char* buffer, std::size_t amount);
|
||||
|
||||
/**
|
||||
* \brief Read some data from the buffer
|
||||
* \param buffer The buffer to Read
|
||||
* \param amount The amount of data from the buffer
|
||||
*/
|
||||
void ReadSome(std::uint8_t* buffer, std::size_t amount);
|
||||
|
||||
/**
|
||||
* \brief Read some data from the buffer
|
||||
* \param buffer The buffer to Read
|
||||
* \param amount The amount of data from the buffer
|
||||
*/
|
||||
void ReadSome(DataBuffer& buffer, std::size_t amount);
|
||||
|
||||
/**
|
||||
* \brief Resize the buffer
|
||||
* \param size The new size of the buffer
|
||||
*/
|
||||
void Resize(std::size_t size) {
|
||||
m_Buffer.resize(size);
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Reserve some space in the buffer
|
||||
* \param amount The amount of space to reserve
|
||||
*/
|
||||
void Reserve(std::size_t amount) {
|
||||
m_Buffer.reserve(amount);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* \brief Clear the buffer
|
||||
*/
|
||||
void Clear() {
|
||||
m_Buffer.clear();
|
||||
m_ReadOffset = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief When the buffer has been read entirely
|
||||
*/
|
||||
bool IsFinished() const {
|
||||
return m_ReadOffset >= m_Buffer.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Get the buffer data
|
||||
*/
|
||||
std::uint8_t* data() {
|
||||
return m_Buffer.data();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Get the buffer data
|
||||
*/
|
||||
const std::uint8_t* data() const {
|
||||
return m_Buffer.data();
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Get the read offset
|
||||
*/
|
||||
std::size_t GetReadOffset() const {
|
||||
return m_ReadOffset;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Set the read offset
|
||||
* \param pos The new read offset
|
||||
*/
|
||||
void SetReadOffset(std::size_t pos);
|
||||
|
||||
/**
|
||||
* \brief Get the size of the buffer
|
||||
*/
|
||||
std::size_t GetSize() const;
|
||||
|
||||
/**
|
||||
* \brief Get the remaining size of the buffer
|
||||
*/
|
||||
std::size_t GetRemaining() const;
|
||||
|
||||
/**
|
||||
* \brief Read a file into the buffer
|
||||
* \param fileName The name of the file to read
|
||||
*/
|
||||
bool ReadFile(const std::string& fileName);
|
||||
|
||||
/**
|
||||
* \brief Write a file into the buffer
|
||||
* \param fileName The name of the file to write to
|
||||
*/
|
||||
bool WriteFile(const std::string& fileName) const;
|
||||
|
||||
/**
|
||||
* \brief Allocate the buffer on the heap
|
||||
* \warning Don't forget to free the data !
|
||||
*/
|
||||
std::uint8_t* HeapAllocatedData() const {
|
||||
std::uint8_t* newBuffer = new std::uint8_t[GetSize()];
|
||||
std::memcpy(newBuffer, data(), GetSize());
|
||||
return newBuffer;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Operator == to compare two DataBuffer
|
||||
*/
|
||||
bool operator==(const DataBuffer& other) const {
|
||||
return m_Buffer == other.m_Buffer;
|
||||
}
|
||||
|
||||
iterator begin();
|
||||
iterator end();
|
||||
const_iterator begin() const;
|
||||
const_iterator end() const;
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief Operator << to write a DataBuffer to an ostream
|
||||
*/
|
||||
std::ostream& operator<<(std::ostream& os, const DataBuffer& buffer);
|
||||
|
||||
} // namespace td
|
||||
54
include/td/common/StateMachine.h
Normal file
54
include/td/common/StateMachine.h
Normal file
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <td/misc/Signal.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
template <typename TDerived, typename TReturn, typename... TArgs>
|
||||
class StateMachine {
|
||||
public:
|
||||
class State {
|
||||
public:
|
||||
State(TDerived& a_StateMachine) : m_StateMachine(a_StateMachine) {}
|
||||
virtual ~State() {}
|
||||
|
||||
virtual TReturn Update(TArgs... args) = 0;
|
||||
|
||||
template <typename T, typename... Args>
|
||||
void ChangeState(Args... args) {
|
||||
m_StateMachine.template ChangeState<T>(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
protected:
|
||||
TDerived& m_StateMachine;
|
||||
};
|
||||
|
||||
utils::Signal<State&> OnStateChange;
|
||||
|
||||
StateMachine() {}
|
||||
StateMachine(StateMachine&&) = default;
|
||||
virtual ~StateMachine() {}
|
||||
|
||||
virtual TReturn Update(TArgs... args) {
|
||||
assert(m_State && "You must change state at least once before updating !");
|
||||
return m_State->Update(std::forward<TArgs>(args)...);
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
void ChangeState(Args... args) {
|
||||
auto* currentState = m_State.get();
|
||||
auto newState = std::make_unique<T>(static_cast<TDerived&>(*this), std::forward<Args>(args)...);
|
||||
// This allows chaining
|
||||
if (m_State.get() == currentState) {
|
||||
m_State = std::move(newState);
|
||||
OnStateChange(*m_State);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<State> m_State;
|
||||
};
|
||||
|
||||
|
||||
} // namespace td
|
||||
69
include/td/common/StateStack.h
Normal file
69
include/td/common/StateStack.h
Normal file
@@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
namespace td {
|
||||
|
||||
template <typename TDerived, typename TReturn, typename... TArgs>
|
||||
class StateStack {
|
||||
public:
|
||||
class State {
|
||||
public:
|
||||
State(TDerived& a_StateStack) : m_StateStack(a_StateStack) {}
|
||||
virtual ~State() {}
|
||||
|
||||
virtual TReturn Update(TArgs... args) = 0;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* \brief Called when enabled (can be used to connect signals)
|
||||
*/
|
||||
virtual void OnEnable() {}
|
||||
|
||||
/**
|
||||
* \brief Called when disabled (can be used to disconnect signals)
|
||||
*/
|
||||
virtual void OnDisable() {}
|
||||
|
||||
protected:
|
||||
TDerived& m_StateStack;
|
||||
|
||||
friend class StateStack;
|
||||
};
|
||||
|
||||
StateStack() {}
|
||||
StateStack(StateStack&&) = default;
|
||||
virtual ~StateStack() {}
|
||||
|
||||
virtual TReturn Update(TArgs... args) {
|
||||
assert(!m_States.empty() && "You must push at least one state before updating !");
|
||||
return m_States.back()->Update(args...);
|
||||
}
|
||||
|
||||
void PopState() {
|
||||
assert(!m_States.empty() && "You must push at least one state before poping !");
|
||||
m_States.back()->OnDisable();
|
||||
m_States.pop_back();
|
||||
if (m_States.empty())
|
||||
return;
|
||||
m_States.back()->OnEnable();
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
T* PushState(Args&&... args) {
|
||||
if (!m_States.empty())
|
||||
m_States.back()->OnDisable();
|
||||
auto newState = std::make_unique<T>(static_cast<TDerived&>(*this), std::forward<Args>(args)...);
|
||||
newState->OnEnable();
|
||||
m_States.push_back(std::move(newState));
|
||||
return static_cast<T*>(newState.get());
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<State>> m_States;
|
||||
};
|
||||
|
||||
|
||||
} // namespace td
|
||||
@@ -1,58 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file VarInt.h
|
||||
* \brief File containing the td::VarInt class
|
||||
*/
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace td {
|
||||
|
||||
class DataBuffer;
|
||||
|
||||
/**
|
||||
* \class VarInt
|
||||
* \brief Variable-length format such that smaller numbers use fewer bytes.
|
||||
*/
|
||||
class VarInt {
|
||||
private:
|
||||
std::uint64_t m_Value;
|
||||
|
||||
public:
|
||||
VarInt() : m_Value(0) {}
|
||||
/**
|
||||
* \brief Construct a variable integer from a value
|
||||
* \param value The value of the variable integer
|
||||
*/
|
||||
VarInt(std::uint64_t value) : m_Value(value) {}
|
||||
|
||||
/**
|
||||
* \brief Get the value of the variable integer
|
||||
*/
|
||||
std::uint64_t GetValue() const {
|
||||
return m_Value;
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Get the length of the serialized variable integer
|
||||
*/
|
||||
std::size_t GetSerializedLength() const;
|
||||
|
||||
/**
|
||||
* \brief Serialize the variable integer
|
||||
* \param out The buffer to write the serialized variable integer to
|
||||
* \param var The variable integer to serialize
|
||||
*/
|
||||
friend DataBuffer& operator<<(DataBuffer& out, const VarInt& var);
|
||||
|
||||
/**
|
||||
* \brief Deserialize the variable integer
|
||||
* \param in The buffer to read the serialized variable integer from
|
||||
* \param var The variable integer to deserialize
|
||||
*/
|
||||
friend DataBuffer& operator>>(DataBuffer& in, VarInt& var);
|
||||
};
|
||||
|
||||
} // namespace td
|
||||
50
include/td/display/Display.h
Normal file
50
include/td/display/Display.h
Normal file
@@ -0,0 +1,50 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <SDL3/SDL_keycode.h>
|
||||
#include <SDL3/SDL_video.h>
|
||||
#include <td/common/StateMachine.h>
|
||||
#include <td/misc/Signal.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
class Display : public StateMachine<Display, void, float> {
|
||||
private:
|
||||
SDL_Window* m_Window;
|
||||
SDL_GLContext m_GLContext;
|
||||
|
||||
int m_LastWidth, m_LastHeight;
|
||||
float m_AspectRatio;
|
||||
|
||||
bool m_ShouldClose;
|
||||
|
||||
public:
|
||||
utils::Signal<float> OnAspectRatioChange;
|
||||
utils::Signal<SDL_Keycode> OnKeyDown;
|
||||
|
||||
Display(int a_Width, int a_Height, const std::string& a_Title);
|
||||
~Display();
|
||||
|
||||
void PollEvents();
|
||||
void Update(float a_Delta) override;
|
||||
void Close();
|
||||
|
||||
bool IsCloseRequested() {
|
||||
return m_ShouldClose;
|
||||
}
|
||||
|
||||
float GetAspectRatio() {
|
||||
return m_AspectRatio;
|
||||
}
|
||||
|
||||
int GetWindowWidth() {
|
||||
return m_LastWidth;
|
||||
}
|
||||
|
||||
int GetWindowHeight() {
|
||||
return m_LastHeight;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace td
|
||||
19
include/td/display/DisplayState.h
Normal file
19
include/td/display/DisplayState.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/display/Display.h>
|
||||
#include <td/misc/SlotGuard.h>
|
||||
#include <client/ClientState.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
class DisplayState : public Display::State, private utils::SlotGuard {
|
||||
public:
|
||||
DisplayState(Display& a_Display);
|
||||
virtual ~DisplayState() {}
|
||||
|
||||
protected:
|
||||
virtual void OnAspectRatioChange(float a_Ratio) {}
|
||||
virtual void OnKeyDown(SDL_Keycode a_Key) {}
|
||||
};
|
||||
|
||||
} // namespace td
|
||||
7
include/td/display/ImGuiTheme.h
Normal file
7
include/td/display/ImGuiTheme.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
namespace td {
|
||||
|
||||
void LoadTheme();
|
||||
|
||||
} // namespace td
|
||||
15
include/td/display/menu/CreatePartyMenu.h
Normal file
15
include/td/display/menu/CreatePartyMenu.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/display/state/MainMenuState.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
class CreatePartyMenu : public MainMenuState::Menu {
|
||||
public:
|
||||
CreatePartyMenu(MainMenuState& a_MainMenu);
|
||||
~CreatePartyMenu();
|
||||
|
||||
virtual void Update() override;
|
||||
};
|
||||
|
||||
} // namespace td
|
||||
15
include/td/display/menu/JoinPartyMenu.h
Normal file
15
include/td/display/menu/JoinPartyMenu.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/display/state/MainMenuState.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
class JoinPartyMenu : public MainMenuState::Menu {
|
||||
public:
|
||||
JoinPartyMenu(MainMenuState& a_MainMenu);
|
||||
~JoinPartyMenu();
|
||||
|
||||
virtual void Update() override;
|
||||
};
|
||||
|
||||
} // namespace td
|
||||
15
include/td/display/menu/MainMenu.h
Normal file
15
include/td/display/menu/MainMenu.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/display/state/MainMenuState.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
class MainMenu : public MainMenuState::Menu {
|
||||
public:
|
||||
MainMenu(MainMenuState& a_MainMenu);
|
||||
~MainMenu();
|
||||
|
||||
virtual void Update() override;
|
||||
};
|
||||
|
||||
} // namespace td
|
||||
15
include/td/display/menu/SettingsMenu.h
Normal file
15
include/td/display/menu/SettingsMenu.h
Normal file
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/display/state/MainMenuState.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
class SettingsMenu : public MainMenuState::Menu {
|
||||
public:
|
||||
SettingsMenu(MainMenuState& a_MainMenu);
|
||||
~SettingsMenu();
|
||||
|
||||
virtual void Update() override;
|
||||
};
|
||||
|
||||
} // namespace td
|
||||
35
include/td/display/state/DebugWorldState.h
Normal file
35
include/td/display/state/DebugWorldState.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/Client.h>
|
||||
#include <server/Server.h>
|
||||
#include <td/display/DisplayState.h>
|
||||
#include <td/render/Renderer.h>
|
||||
#include <td/simulation/ClientSimulation.h>
|
||||
#include <client/state/GameState.h>
|
||||
#include <server/socket/FakeSocket.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
class DebugWorldState : public DisplayState {
|
||||
private:
|
||||
render::RenderPipeline m_Renderer;
|
||||
render::Camera m_Camera;
|
||||
std::unique_ptr<server::Server> m_Server;
|
||||
std::unique_ptr<client::Client> m_Client;
|
||||
client::GameState* m_ClientState;
|
||||
|
||||
std::vector<std::unique_ptr<client::Client>> m_FakeClients;
|
||||
std::shared_ptr<server::FakeSocket> m_ServerSocket;
|
||||
|
||||
public:
|
||||
DebugWorldState(Display& a_Display);
|
||||
~DebugWorldState();
|
||||
|
||||
virtual void Update(float a_Delta) override;
|
||||
|
||||
protected:
|
||||
virtual void OnAspectRatioChange(float a_Ratio) override;
|
||||
virtual void OnKeyDown(SDL_Keycode a_Key) override;
|
||||
};
|
||||
|
||||
} // namespace td
|
||||
27
include/td/display/state/MainMenuState.h
Normal file
27
include/td/display/state/MainMenuState.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/common/StateStack.h>
|
||||
#include <td/display/DisplayState.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
class MainMenuState;
|
||||
|
||||
using MainMenuStateStack = StateStack<MainMenuState, void>;
|
||||
|
||||
class MainMenuState : public DisplayState, public MainMenuStateStack {
|
||||
public:
|
||||
using Menu = MainMenuStateStack::State;
|
||||
|
||||
MainMenuState(Display& a_Display);
|
||||
~MainMenuState();
|
||||
|
||||
virtual void Update(float a_Delta) override;
|
||||
|
||||
void RenderBackButton();
|
||||
|
||||
protected:
|
||||
virtual void OnKeyDown(SDL_Keycode a_Key) override;
|
||||
};
|
||||
|
||||
} // namespace td
|
||||
46
include/td/game/Game.h
Normal file
46
include/td/game/Game.h
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/game/World.h>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
enum class GameState : std::uint8_t {
|
||||
Lobby,
|
||||
Game,
|
||||
EndGame,
|
||||
Disconnected,
|
||||
Closed
|
||||
};
|
||||
|
||||
typedef std::map<std::uint8_t, Player> PlayerList;
|
||||
|
||||
|
||||
class Game {
|
||||
protected:
|
||||
World* m_World;
|
||||
|
||||
GameState m_GameState = GameState::Lobby;
|
||||
PlayerList m_Players;
|
||||
public:
|
||||
Game(World* world);
|
||||
virtual ~Game();
|
||||
|
||||
virtual void Tick(std::uint64_t delta);
|
||||
|
||||
GameState GetGameState() const { return m_GameState; }
|
||||
void SetGameState(GameState gameState) { m_GameState = gameState; };
|
||||
|
||||
const World* GetWorld() const { return m_World; }
|
||||
World* GetWorld() { return m_World; }
|
||||
|
||||
const PlayerList& GetPlayers() const { return m_Players; }
|
||||
PlayerList& GetPlayers() { return m_Players; }
|
||||
|
||||
const Player* GetPlayerById(PlayerID id) const;
|
||||
Player* GetPlayerById(PlayerID id);
|
||||
|
||||
};
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
@@ -1,31 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <optional>
|
||||
#include <td/protocol/command/Commands.h>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
class GameHistory {
|
||||
private:
|
||||
using HistorySizeType = StepsType;
|
||||
|
||||
std::vector<std::optional<protocol::LockStep>> m_History;
|
||||
|
||||
public:
|
||||
GameHistory();
|
||||
|
||||
void SetLockStep(HistorySizeType a_Index, protocol::LockStep&& a_LockStep);
|
||||
|
||||
const protocol::LockStep& GetLockStep(HistorySizeType a_Index) const;
|
||||
|
||||
bool HasLockStep(HistorySizeType a_Index) const;
|
||||
|
||||
void FromPacket(td::protocol::pdata::LockSteps&& a_Steps);
|
||||
|
||||
td::protocol::packets::LockSteps ToPacket(HistorySizeType a_StartIndex);
|
||||
};
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
132
include/td/game/Mobs.h
Normal file
132
include/td/game/Mobs.h
Normal file
@@ -0,0 +1,132 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/game/Towers.h>
|
||||
|
||||
#include <td/Maths.h>
|
||||
#include <td/Types.h>
|
||||
#include <td/game/Team.h>
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include <sp/protocol/ConcreteMessage.h>
|
||||
#include <sp/common/GenericHandler.h>
|
||||
#include <sp/protocol/MessageFactory.h>
|
||||
|
||||
namespace td {
|
||||
using Vec2fp = Vec2<FpFloat>;
|
||||
|
||||
namespace game {
|
||||
|
||||
enum class EffectType : std::uint8_t {
|
||||
Slowness = 0,
|
||||
Stun,
|
||||
Fire,
|
||||
Poison,
|
||||
Heal,
|
||||
};
|
||||
|
||||
enum class MobType : std::uint8_t {
|
||||
Zombie = 0,
|
||||
Spider,
|
||||
Skeleton,
|
||||
Pigman,
|
||||
Creeper,
|
||||
Silverfish,
|
||||
Blaze,
|
||||
Witch,
|
||||
Slime,
|
||||
Giant,
|
||||
|
||||
MOB_COUNT
|
||||
};
|
||||
|
||||
typedef std::uint32_t MobID;
|
||||
typedef std::uint8_t MobLevel;
|
||||
typedef std::vector<TowerType> TowerImmunities;
|
||||
typedef std::vector<EffectType> EffectImmunities;
|
||||
|
||||
struct MobStats {
|
||||
float m_Damage;
|
||||
float m_Speed;
|
||||
Vec2f m_Size;
|
||||
std::uint16_t m_MoneyCost;
|
||||
std::uint16_t m_ExpCost;
|
||||
std::uint16_t m_MaxLife;
|
||||
std::uint16_t m_ExpReward;
|
||||
};
|
||||
|
||||
struct EffectDuration {
|
||||
EffectType type;
|
||||
float duration; // in seconds
|
||||
Tower* tower; // the tower that gived the effect
|
||||
};
|
||||
|
||||
const MobStats* GetMobStats(MobType type, std::uint8_t level);
|
||||
const TowerImmunities& GetMobTowerImmunities(MobType type, std::uint8_t level);
|
||||
const EffectImmunities& GetMobEffectImmunities(MobType type, std::uint8_t level);
|
||||
|
||||
class MobHandler;
|
||||
|
||||
struct MobData {};
|
||||
|
||||
class Mob : public sp::MessageBase<MobType, MobHandler> {
|
||||
public:
|
||||
MobID m_ID;
|
||||
MobLevel m_Level;
|
||||
PlayerID m_Sender;
|
||||
float m_Health;
|
||||
Vec2fp m_Position;
|
||||
Direction m_Direction;
|
||||
std::vector<EffectDuration> m_Effects;
|
||||
const Tower* m_LastDamage; // the last tower that damaged the mob
|
||||
float m_HitCooldown;
|
||||
TeamCastle* m_CastleTarget;
|
||||
// utils::CooldownTimer m_AttackTimer;
|
||||
|
||||
MobPtr m_Next;
|
||||
|
||||
Mob() {}
|
||||
|
||||
Mob& operator=(const Mob& a_Other) = default;
|
||||
};
|
||||
|
||||
template <MobType ID>
|
||||
using ConcreteMob = sp::ConcreteMessage<MobData, Mob, ID>;
|
||||
|
||||
using Zombie = ConcreteMob<MobType::Zombie>;
|
||||
using Spider = ConcreteMob<MobType::Spider>;
|
||||
using Skeleton = ConcreteMob<MobType::Skeleton>;
|
||||
using PigMan = ConcreteMob<MobType::Pigman>;
|
||||
using Creeper = ConcreteMob<MobType::Creeper>;
|
||||
using Silverfish = ConcreteMob<MobType::Silverfish>;
|
||||
using Blaze = ConcreteMob<MobType::Blaze>;
|
||||
using Witch = ConcreteMob<MobType::Witch>;
|
||||
using Slime = ConcreteMob<MobType::Slime>;
|
||||
using Giant = ConcreteMob<MobType::Giant>;
|
||||
|
||||
using AllMobs = std::tuple<Zombie, Spider, Skeleton, PigMan, Creeper, Silverfish, Blaze, Witch, Slime, Giant>;
|
||||
|
||||
class MobHandler : public sp::GenericHandler<AllMobs> {};
|
||||
|
||||
using MobFactory = sp::MessageFactory<Mob, AllMobs>;
|
||||
|
||||
class MobListener {
|
||||
public:
|
||||
virtual void OnMobSpawn(Mob* mob) {
|
||||
MobHandler h;
|
||||
}
|
||||
virtual void OnMobDie(Mob* mob) {}
|
||||
|
||||
virtual void OnMobDamage(Mob* target, float damage, Tower* damager) {}
|
||||
|
||||
virtual void OnMobTouchCastle(Mob* damager, TeamCastle* enemyCastle) {}
|
||||
virtual void OnMobCastleDamage(Mob* damager, TeamCastle* enemyCastle, float damage) {}
|
||||
};
|
||||
|
||||
using MobList = std::vector<MobPtr>;
|
||||
|
||||
// typedef utils::ObjectNotifier<MobListener> MobNotifier;
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
102
include/td/game/Team.h
Normal file
102
include/td/game/Team.h
Normal file
@@ -0,0 +1,102 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/Types.h>
|
||||
|
||||
#include <td/misc/Shapes.h>
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <cmath>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
class Player;
|
||||
|
||||
class Spawn : public utils::shape::Rectangle {
|
||||
private:
|
||||
Direction m_Direction;
|
||||
public:
|
||||
Spawn() : m_Direction(Direction::PositiveX) {
|
||||
SetWidth(5);
|
||||
SetHeight(5);
|
||||
}
|
||||
|
||||
Direction GetDirection() const { return m_Direction; }
|
||||
|
||||
void SetDirection(Direction direction) { m_Direction = direction; }
|
||||
};
|
||||
|
||||
class Team;
|
||||
|
||||
class TeamCastle : public utils::shape::Rectangle {
|
||||
private:
|
||||
float m_Life;
|
||||
public:
|
||||
static constexpr int CastleMaxLife = 1000;
|
||||
|
||||
TeamCastle() : m_Life(CastleMaxLife) {
|
||||
SetWidth(5);
|
||||
SetHeight(5);
|
||||
}
|
||||
|
||||
float GetLife() const { return m_Life; }
|
||||
|
||||
void SetLife(float life) { m_Life = life; }
|
||||
void Damage(float damage) { m_Life = std::max(0.0f, m_Life - damage); }
|
||||
|
||||
void SetShape(utils::shape::Rectangle rect) {
|
||||
SetCenter(rect.GetCenter());
|
||||
SetSize(rect.GetSize());
|
||||
}
|
||||
};
|
||||
|
||||
class Team {
|
||||
private:
|
||||
std::vector<Player*> m_Players;
|
||||
TeamColor m_Color;
|
||||
Spawn m_Spawn;
|
||||
TeamCastle m_TeamCastle;
|
||||
public:
|
||||
Team(TeamColor color) : m_Color(color) {}
|
||||
|
||||
void AddPlayer(Player* newPlayer);
|
||||
void RemovePlayer(const Player* player);
|
||||
|
||||
TeamColor GetColor() const;
|
||||
|
||||
const Spawn& GetSpawn() const { return m_Spawn; }
|
||||
Spawn& GetSpawn() { return m_Spawn; }
|
||||
|
||||
const TeamCastle& GetCastle() const { return m_TeamCastle; }
|
||||
TeamCastle& GetCastle() { return m_TeamCastle; }
|
||||
|
||||
std::uint8_t GetPlayerCount() const;
|
||||
};
|
||||
|
||||
struct TeamList {
|
||||
std::array<Team, 2> m_Teams;
|
||||
|
||||
TeamList() : m_Teams{Team{TeamColor::Blue}, Team{TeamColor::Red}}{
|
||||
|
||||
}
|
||||
|
||||
Team& operator[](std::size_t a_Index) {
|
||||
return m_Teams[a_Index];
|
||||
}
|
||||
|
||||
Team& operator[](TeamColor a_Index) {
|
||||
return m_Teams[static_cast<std::size_t>(a_Index)];
|
||||
}
|
||||
|
||||
const Team& operator[](std::size_t a_Index) const {
|
||||
return m_Teams[a_Index];
|
||||
}
|
||||
|
||||
const Team& operator[](TeamColor a_Index) const {
|
||||
return m_Teams[static_cast<std::size_t>(a_Index)];
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
147
include/td/game/Towers.h
Normal file
147
include/td/game/Towers.h
Normal file
@@ -0,0 +1,147 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include <td/misc/Shapes.h>
|
||||
|
||||
#include <td/Types.h>
|
||||
|
||||
#include <sp/common/GenericHandler.h>
|
||||
#include <sp/protocol/ConcreteMessage.h>
|
||||
#include <sp/protocol/MessageFactory.h>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
class World;
|
||||
class Mob;
|
||||
|
||||
typedef std::shared_ptr<Mob> MobPtr;
|
||||
|
||||
enum class TowerSize : std::uint8_t {
|
||||
Little = 3, // 3x3
|
||||
Big = 5, // 5x5
|
||||
};
|
||||
|
||||
enum class TowerPath : std::uint8_t {
|
||||
Top = 0,
|
||||
Base, // Base Path
|
||||
Bottom
|
||||
};
|
||||
|
||||
class TowerStats {
|
||||
private:
|
||||
float m_Rate;
|
||||
float m_Damage;
|
||||
std::uint8_t m_Range;
|
||||
|
||||
public:
|
||||
TowerStats(float rate, float damage, std::uint8_t range) : m_Rate(rate), m_Damage(damage), m_Range(range) {}
|
||||
|
||||
float GetDamageRate() const {
|
||||
return m_Rate;
|
||||
}
|
||||
float GetDamage() const {
|
||||
return m_Damage;
|
||||
}
|
||||
std::uint8_t GetRange() const {
|
||||
return m_Range;
|
||||
}
|
||||
};
|
||||
|
||||
class TowerLevel {
|
||||
private:
|
||||
// 1, 2, 3, 4
|
||||
std::uint8_t m_Level : 3;
|
||||
// 0 : base path 1 : top path (if there is bottom path) 2 : bottom path (if there is top path)
|
||||
TowerPath m_Path : 2;
|
||||
|
||||
public:
|
||||
TowerLevel() : m_Level(1), m_Path(TowerPath::Base) {}
|
||||
TowerLevel(std::uint8_t level, TowerPath path) : m_Level(level), m_Path(path) {}
|
||||
|
||||
std::uint8_t GetLevel() const {
|
||||
return m_Level;
|
||||
}
|
||||
TowerPath GetPath() const {
|
||||
return m_Path;
|
||||
}
|
||||
|
||||
void SetLevel(std::uint8_t level) {
|
||||
m_Level = level;
|
||||
}
|
||||
void SetPath(TowerPath path) {
|
||||
m_Path = path;
|
||||
}
|
||||
|
||||
// operator to sort maps
|
||||
friend bool operator<(const TowerLevel& level, const TowerLevel& other) {
|
||||
return level.GetLevel() + static_cast<std::uint8_t>(level.GetPath()) * 4 <
|
||||
other.GetLevel() + static_cast<std::uint8_t>(other.GetPath()) * 4;
|
||||
}
|
||||
};
|
||||
|
||||
using TowerID = std::uint16_t;
|
||||
|
||||
class TowerHandler;
|
||||
|
||||
class Tower : public utils::shape::Circle, public sp::MessageBase<TowerType, TowerHandler> {
|
||||
public:
|
||||
TowerID m_ID;
|
||||
TowerLevel m_Level{};
|
||||
PlayerID m_Builder;
|
||||
|
||||
public:
|
||||
Tower() : m_ID(0), m_Level({}), m_Builder(0) {}
|
||||
|
||||
virtual TowerType GetType() const = 0;
|
||||
virtual TowerSize GetSize() const = 0;
|
||||
virtual void Tick(std::uint64_t delta, World* world) = 0;
|
||||
};
|
||||
|
||||
struct TowerData {};
|
||||
|
||||
template <TowerType Type, TowerSize Size>
|
||||
class ConcreteTower : public sp::ConcreteMessage<TowerData, Tower, Type, false> {
|
||||
public:
|
||||
using HandlerType = typename sp::ConcreteMessage<TowerData, Tower, Type, false>::HandlerType;
|
||||
|
||||
virtual TowerSize GetSize() const override {
|
||||
return Size;
|
||||
}
|
||||
|
||||
virtual TowerType GetType() const override {
|
||||
return Type;
|
||||
}
|
||||
|
||||
virtual void Tick(std::uint64_t delta, World* world) override {}
|
||||
|
||||
virtual void Dispatch(HandlerType& handler) const override {
|
||||
handler.Handle(*this);
|
||||
}
|
||||
};
|
||||
|
||||
using TowerPtr = std::shared_ptr<Tower>;
|
||||
|
||||
using ArcherTower = ConcreteTower<TowerType::Archer, TowerSize::Little>;
|
||||
using ArtilleryTower = ConcreteTower<TowerType::Artillery, TowerSize::Little>;
|
||||
using IceTower = ConcreteTower<TowerType::Ice, TowerSize::Little>;
|
||||
using LeachTower = ConcreteTower<TowerType::Leach, TowerSize::Big>;
|
||||
using MageTower = ConcreteTower<TowerType::Mage, TowerSize::Little>;
|
||||
using NecromancerTower = ConcreteTower<TowerType::Necromancer, TowerSize::Big>;
|
||||
using PoisonTower = ConcreteTower<TowerType::Poison, TowerSize::Little>;
|
||||
using QuakeTower = ConcreteTower<TowerType::Quake, TowerSize::Little>;
|
||||
using SorcererTower = ConcreteTower<TowerType::Sorcerer, TowerSize::Little>;
|
||||
using TurretTower = ConcreteTower<TowerType::Turret, TowerSize::Big>;
|
||||
using ZeusTower = ConcreteTower<TowerType::Zeus, TowerSize::Little>;
|
||||
|
||||
using AllTowers = std::tuple<ArcherTower, ArtilleryTower, IceTower, LeachTower, MageTower, NecromancerTower, PoisonTower, QuakeTower,
|
||||
SorcererTower, TurretTower, ZeusTower>;
|
||||
|
||||
using TowerFactory = sp::MessageFactory<Tower, AllTowers>;
|
||||
|
||||
class TowerHandler : public sp::GenericHandler<AllTowers> {};
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
@@ -1,27 +1,142 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/game/GameHistory.h>
|
||||
#include <td/game/WorldState.h>
|
||||
#include <vector>
|
||||
#include <td/game/WorldTypes.h>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
#include <td/simulation/WorldTicker.h>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
class World {
|
||||
private:
|
||||
std::vector<WorldState> m_WorldStates;
|
||||
GameHistory m_GameHistory;
|
||||
protected:
|
||||
// header
|
||||
TowerTileColorPalette m_TowerPlacePalette;
|
||||
Color m_WalkablePalette;
|
||||
std::vector<Color> m_DecorationPalette;
|
||||
Color m_Background;
|
||||
SpawnColorPalette m_SpawnColorPalette;
|
||||
TilePalette m_TilePalette;
|
||||
|
||||
float m_OffsetTime;
|
||||
//data
|
||||
ChunkList m_Chunks;
|
||||
|
||||
std::shared_ptr<sim::WorldSnapshot> m_CurrentState;
|
||||
std::shared_ptr<sim::WorldSnapshot> m_NextState;
|
||||
|
||||
private:
|
||||
sim::WorldTicker m_Ticker;
|
||||
|
||||
public:
|
||||
World();
|
||||
World(World&&) = default;
|
||||
|
||||
void Tick(float a_Delta);
|
||||
bool LoadMap(const protocol::pdata::WorldHeader& worldHeader);
|
||||
bool LoadMap(const protocol::pdata::WorldData& worldData);
|
||||
|
||||
protocol::packets::WorldHeaderPacket GetPacketHeader() const;
|
||||
protocol::packets::WorldDataPacket GetPacketData() const;
|
||||
|
||||
bool LoadMapFromFile(const std::string& fileName);
|
||||
bool SaveMap(const std::string& fileName) const;
|
||||
|
||||
void SpawnMobAt(MobID id, MobType type, std::uint8_t level, PlayerID sender, float x, float y, Direction dir);
|
||||
|
||||
TowerPtr PlaceTowerAt(TowerID id, TowerType type, std::int32_t x, std::int32_t y, PlayerID builder);
|
||||
TowerPtr RemoveTower(TowerID id);
|
||||
|
||||
TilePtr GetTile(std::int32_t x, std::int32_t y) const;
|
||||
|
||||
const TowerTileColorPalette& GetTowerTileColorPalette() const {
|
||||
return m_TowerPlacePalette;
|
||||
}
|
||||
const Color& GetWalkableTileColor() const {
|
||||
return m_WalkablePalette;
|
||||
}
|
||||
const std::vector<Color>& GetDecorationPalette() const {
|
||||
return m_DecorationPalette;
|
||||
}
|
||||
const Color& GetBackgroundColor() const {
|
||||
return m_Background;
|
||||
}
|
||||
|
||||
const TilePalette& GetTilePalette() const {
|
||||
return m_TilePalette;
|
||||
}
|
||||
|
||||
TilePtr GetTilePtr(TileIndex index) const {
|
||||
if (index == 0)
|
||||
return TilePtr(nullptr);
|
||||
return m_TilePalette.at(index - 1);
|
||||
}
|
||||
|
||||
bool CanPlaceLittleTower(const Vec2f& worldPos, PlayerID player) const;
|
||||
bool CanPlaceBigTower(const Vec2f& worldPos, PlayerID player) const;
|
||||
|
||||
TowerPtr GetTower(const Vec2f& position) const; // returns null if no tower is here
|
||||
|
||||
const ChunkList& GetChunks() const {
|
||||
return m_Chunks;
|
||||
}
|
||||
|
||||
const Color& GetSpawnColor(TeamColor color) const {
|
||||
return m_SpawnColorPalette[static_cast<std::size_t>(color)];
|
||||
}
|
||||
const SpawnColorPalette& GetSpawnColors() const {
|
||||
return m_SpawnColorPalette;
|
||||
}
|
||||
|
||||
const MobList& GetMobList() const {
|
||||
return m_CurrentState->m_Mobs;
|
||||
}
|
||||
MobList& GetMobList() {
|
||||
return m_CurrentState->m_Mobs;
|
||||
}
|
||||
|
||||
const Color* GetTileColor(const TilePtr& tile) const;
|
||||
|
||||
Team& GetRedTeam() {
|
||||
return m_CurrentState->m_Teams[TeamColor::Red];
|
||||
}
|
||||
const Team& GetRedTeam() const {
|
||||
return m_CurrentState->m_Teams[TeamColor::Red];
|
||||
}
|
||||
|
||||
Team& GetBlueTeam() {
|
||||
return m_CurrentState->m_Teams[TeamColor::Blue];
|
||||
}
|
||||
const Team& GetBlueTeam() const {
|
||||
return m_CurrentState->m_Teams[TeamColor::Blue];
|
||||
}
|
||||
|
||||
Team& GetTeam(TeamColor team) {
|
||||
return m_CurrentState->m_Teams[team];
|
||||
}
|
||||
const Team& GetTeam(TeamColor team) const {
|
||||
return m_CurrentState->m_Teams[team];
|
||||
}
|
||||
|
||||
const TeamList& GetTeams() const {
|
||||
return m_CurrentState->m_Teams;
|
||||
}
|
||||
|
||||
const TowerList& GetTowers() const {
|
||||
return m_CurrentState->m_Towers;
|
||||
}
|
||||
|
||||
TowerPtr GetTowerById(TowerID tower);
|
||||
|
||||
const Player* GetPlayerById(PlayerID id) const;
|
||||
|
||||
const std::shared_ptr<sim::WorldSnapshot>& Tick(const protocol::LockStep& a_LockStep, FpFloat a_Delta);
|
||||
|
||||
void ResetSnapshots(std::shared_ptr<sim::WorldSnapshot>& a_Current, std::shared_ptr<sim::WorldSnapshot>& a_Next);
|
||||
|
||||
private:
|
||||
std::uint16_t GetCursorPos();
|
||||
void TickMobs(std::uint64_t delta);
|
||||
void CleanDeadMobs();
|
||||
};
|
||||
|
||||
using WorldPtr = std::shared_ptr<World>;
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
} // namespace td
|
||||
@@ -1,21 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/protocol/command/Commands.h>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
class WorldState {
|
||||
private:
|
||||
// list of players, mobs, towers, castles
|
||||
public:
|
||||
WorldState() {}
|
||||
|
||||
WorldState GetNextState(const protocol::LockStep& a_Step);
|
||||
|
||||
private:
|
||||
void Tick(float a_Delta);
|
||||
};
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
117
include/td/game/WorldTypes.h
Normal file
117
include/td/game/WorldTypes.h
Normal file
@@ -0,0 +1,117 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/Maths.h>
|
||||
#include <td/game/Team.h>
|
||||
#include <td/game/Towers.h>
|
||||
|
||||
#include <sp/io/SerializableMessage.h>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
using ChunkCoord = Vec2<std::int16_t>;
|
||||
|
||||
class Game;
|
||||
|
||||
enum class TileType : std::uint8_t {
|
||||
None = 0,
|
||||
Tower,
|
||||
Walk,
|
||||
Decoration,
|
||||
/*Heal,
|
||||
Lava,
|
||||
Bedrock,
|
||||
Freeze,
|
||||
Ice,*/
|
||||
};
|
||||
|
||||
static constexpr Color BLACK{0, 0, 0};
|
||||
static constexpr Color WHITE{255, 255, 255};
|
||||
|
||||
static constexpr Color RED{255, 0, 0};
|
||||
static constexpr Color GREEN{0, 255, 0};
|
||||
static constexpr Color BLUE{0, 0, 255};
|
||||
|
||||
class TileHandler;
|
||||
|
||||
using Tile = sp::MessageBase<TileType, TileHandler>;
|
||||
|
||||
template <TileType ID, typename TileData>
|
||||
using ConcreteTile = sp::ConcreteMessage<TileData, Tile, ID>;
|
||||
|
||||
namespace data {
|
||||
struct EmptyData {};
|
||||
|
||||
struct TowerTileData {
|
||||
std::uint8_t m_ColorPaletteRef;
|
||||
TeamColor m_TeamOwner;
|
||||
};
|
||||
|
||||
struct WalkableTileData {
|
||||
Direction m_Direction;
|
||||
};
|
||||
|
||||
struct DecorationTileData {
|
||||
std::uint16_t m_ColorPaletteRef;
|
||||
};
|
||||
} // namespace data
|
||||
|
||||
using EmptyTile = ConcreteTile<TileType::None, data::EmptyData>;
|
||||
using TowerTile = ConcreteTile<TileType::Tower, data::TowerTileData>;
|
||||
using WalkableTile = ConcreteTile<TileType::Walk, data::WalkableTileData>;
|
||||
using DecorationTile = ConcreteTile<TileType::Decoration, data::DecorationTileData>;
|
||||
|
||||
using AllTiles = std::tuple<EmptyTile, TowerTile, WalkableTile, DecorationTile>;
|
||||
|
||||
using TileFactory = sp::MessageFactory<Tile, AllTiles>;
|
||||
|
||||
class TileHandler : public sp::GenericHandler<AllTiles> {};
|
||||
|
||||
using TilePtr = sp::SerializableMessage<TileFactory>;
|
||||
|
||||
// typedef std::shared_ptr<Tile> TilePtr;
|
||||
typedef std::vector<std::uint16_t> ChunkPalette;
|
||||
|
||||
typedef std::shared_ptr<WalkableTile> WalkableTilePtr;
|
||||
|
||||
typedef std::uint32_t TileIndex;
|
||||
|
||||
// 32 x 32 area
|
||||
struct Chunk {
|
||||
enum { ChunkWidth = 32, ChunkHeight = 32, ChunkSize = ChunkWidth * ChunkHeight };
|
||||
using ChunkData = std::array<std::uint16_t, ChunkSize>;
|
||||
using ChunkPackedData = std::vector<uint64_t>;
|
||||
|
||||
// stores index of tile palette
|
||||
ChunkPalette m_Palette;
|
||||
ChunkPackedData m_Data;
|
||||
|
||||
TileIndex GetTileIndex(std::uint16_t tileNumber) const;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<Chunk> ChunkPtr;
|
||||
|
||||
typedef std::array<Color, 2> TowerTileColorPalette;
|
||||
|
||||
typedef std::vector<TilePtr> TilePalette;
|
||||
|
||||
typedef std::array<Color, 2> SpawnColorPalette;
|
||||
|
||||
typedef std::vector<TowerPtr> TowerList;
|
||||
|
||||
using ChunkList = std::unordered_map<ChunkCoord, ChunkPtr>;
|
||||
|
||||
} // namespace game
|
||||
|
||||
} // namespace td
|
||||
|
||||
|
||||
|
||||
namespace std {
|
||||
template <>
|
||||
struct hash<td::game::ChunkCoord> {
|
||||
std::size_t operator()(const td::game::ChunkCoord& key) const noexcept {
|
||||
return std::hash<std::int16_t>()(key.x << 16 | key.y);
|
||||
}
|
||||
};
|
||||
} // namespace std
|
||||
96
include/td/misc/Shapes.h
Normal file
96
include/td/misc/Shapes.h
Normal file
@@ -0,0 +1,96 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
namespace td {
|
||||
namespace utils {
|
||||
namespace shape {
|
||||
|
||||
class Point {
|
||||
private:
|
||||
float m_X, m_Y;
|
||||
public:
|
||||
Point() : m_X(0), m_Y(0) {}
|
||||
Point(float x, float y) : m_X(x), m_Y(y) {}
|
||||
|
||||
float GetX() const { return m_X; }
|
||||
float GetY() const { return m_Y; }
|
||||
|
||||
void SetX(float x) { m_X = x; }
|
||||
void SetY(float y) { m_Y = y; }
|
||||
|
||||
float Distance(const Point& point) const;
|
||||
float DistanceSquared(const Point& point) const;
|
||||
};
|
||||
|
||||
class Circle;
|
||||
|
||||
class Rectangle {
|
||||
private:
|
||||
Point m_Center;
|
||||
float m_Width, m_Height;
|
||||
public:
|
||||
Rectangle() : m_Center(), m_Width(0), m_Height(0) {}
|
||||
|
||||
const Point& GetCenter() const { return m_Center; }
|
||||
float GetCenterX() const { return m_Center.GetX(); }
|
||||
float GetCenterY() const { return m_Center.GetY(); }
|
||||
|
||||
float GetWidth() const { return m_Width; }
|
||||
float GetHeight() const { return m_Height; }
|
||||
|
||||
Point GetTopLeft() const { return { m_Center.GetX() - (m_Width / 2.0f), m_Center.GetY() - (m_Height / 2.0f) }; }
|
||||
Point GetBottomRight() const { return { m_Center.GetX() + (m_Width / 2.0f), m_Center.GetY() + (m_Height / 2.0f) }; }
|
||||
|
||||
void SetCenter(const Point& center) { m_Center = center; }
|
||||
void SetCenterX(float x) { m_Center.SetX(x); }
|
||||
void SetCenterY(float y) { m_Center.SetY(y); }
|
||||
|
||||
void SetSize(float width, float height) { SetWidth(width); SetHeight(height); }
|
||||
void SetSize(Point size) { SetSize(size.GetX(), size.GetY()); }
|
||||
Point GetSize() { return { m_Width, m_Height }; }
|
||||
|
||||
void SetWidth(float width) { m_Width = width; }
|
||||
void SetHeight(float height) { m_Height = height; }
|
||||
|
||||
bool CollidesWith(const Point& point) const;
|
||||
bool CollidesWith(const Rectangle& rect) const;
|
||||
bool CollidesWith(const Circle& circle) const;
|
||||
|
||||
// distance from the closest side of the rectangle
|
||||
float Distance(const Circle& circle) const;
|
||||
float DistanceSquared(const Circle& circle) const;
|
||||
};
|
||||
|
||||
class Circle {
|
||||
private:
|
||||
Point m_Center;
|
||||
float m_Radius;
|
||||
public:
|
||||
Circle(float x, float y, float radius) : m_Center(x, y), m_Radius(radius) {}
|
||||
Circle() : m_Radius(0) {}
|
||||
|
||||
const Point& GetCenter() const { return m_Center; }
|
||||
float GetCenterX() const { return m_Center.GetX(); }
|
||||
float GetCenterY() const { return m_Center.GetY(); }
|
||||
|
||||
float GetRadius() const { return m_Radius; }
|
||||
|
||||
void SetCenter(const Point& center) { m_Center = center; }
|
||||
void SetCenterX(float x) { m_Center.SetX(x); }
|
||||
void SetCenterY(float y) { m_Center.SetY(y); }
|
||||
|
||||
void SetRadius(float radius) { m_Radius = radius; }
|
||||
|
||||
bool CollidesWith(const Point& point) const;
|
||||
bool CollidesWith(const Rectangle& rect) const;
|
||||
bool CollidesWith(const Circle& circle) const;
|
||||
|
||||
// distance from the closest side of the rectangle
|
||||
float Distance(const Rectangle& rect) const;
|
||||
float DistanceSquared(const Rectangle& rect) const;
|
||||
};
|
||||
|
||||
} // namespace shape
|
||||
} // namespace utils
|
||||
} // namespace td
|
||||
105
include/td/misc/Signal.h
Normal file
105
include/td/misc/Signal.h
Normal file
@@ -0,0 +1,105 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <td/common/NonCopyable.h>
|
||||
#include <vector>
|
||||
|
||||
namespace td {
|
||||
namespace utils {
|
||||
|
||||
/**
|
||||
* \brief Signal class
|
||||
*/
|
||||
template <typename... Args>
|
||||
class SignalRaw : private NonCopyable {
|
||||
public:
|
||||
using FnType = void(Args...);
|
||||
using CallBack = std::shared_ptr<std::function<FnType>>;
|
||||
|
||||
private:
|
||||
std::vector<CallBack> m_Callbacks;
|
||||
|
||||
public:
|
||||
void Connect(const CallBack& a_Callback) {
|
||||
m_Callbacks.push_back(a_Callback);
|
||||
}
|
||||
|
||||
void Disconnect(const CallBack& a_Callback) {
|
||||
auto it = std::find(m_Callbacks.begin(), m_Callbacks.end(), a_Callback);
|
||||
m_Callbacks.erase(it);
|
||||
}
|
||||
|
||||
void operator()(Args... args) const {
|
||||
for (const CallBack& callback : m_Callbacks) {
|
||||
callback->operator()(args...);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief Memory managed Signal class
|
||||
*/
|
||||
template <typename... Args>
|
||||
class Signal {
|
||||
public:
|
||||
using SignalBase = SignalRaw<Args...>;
|
||||
using CallBack = typename SignalBase::CallBack;
|
||||
using CallBackRaw = typename CallBack::element_type;
|
||||
using SignalPtr = std::shared_ptr<SignalBase>;
|
||||
class ConnectionGuard;
|
||||
|
||||
private:
|
||||
SignalPtr m_Signal;
|
||||
|
||||
public:
|
||||
Signal() : m_Signal(std::make_shared<SignalBase>()) {}
|
||||
Signal(const Signal&) = default;
|
||||
|
||||
/**
|
||||
* \warning The callback won't be disconnectable, use it wisely!
|
||||
*/
|
||||
void Connect(const CallBackRaw& a_Callback) {
|
||||
m_Signal->Connect(std::make_shared<CallBackRaw>(a_Callback));
|
||||
}
|
||||
|
||||
[[nodiscard]] std::unique_ptr<ConnectionGuard> ConnectSafe(const CallBack& a_Callback) {
|
||||
m_Signal->Connect(a_Callback);
|
||||
return std::make_unique<ConnectionGuard>(*this, a_Callback);
|
||||
}
|
||||
|
||||
void Disconnect(const CallBack& a_Callback) {
|
||||
m_Signal->Disconnect(a_Callback);
|
||||
}
|
||||
|
||||
void operator()(Args... args) const {
|
||||
m_Signal->operator()(args...);
|
||||
}
|
||||
};
|
||||
|
||||
class Connection {
|
||||
public:
|
||||
virtual ~Connection() {}
|
||||
};
|
||||
|
||||
template <typename... Args>
|
||||
class Signal<Args...>::ConnectionGuard : public Connection {
|
||||
private:
|
||||
using CallBack = typename Signal<Args...>::CallBack;
|
||||
|
||||
std::weak_ptr<SignalRaw<Args...>> m_Signal;
|
||||
CallBack m_Callback;
|
||||
|
||||
public:
|
||||
ConnectionGuard(const Signal<Args...>& a_Signal, const CallBack& a_Callback) :
|
||||
m_Signal(a_Signal.m_Signal), m_Callback(a_Callback) {}
|
||||
|
||||
~ConnectionGuard() {
|
||||
if (!m_Signal.expired())
|
||||
m_Signal.lock()->Disconnect(m_Callback);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace utils
|
||||
} // namespace td
|
||||
33
include/td/misc/SlotGuard.h
Normal file
33
include/td/misc/SlotGuard.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/misc/Signal.h>
|
||||
|
||||
namespace td {
|
||||
namespace utils {
|
||||
|
||||
/**
|
||||
* \brief Wrapper class to automatically disconnect from a Signal on object destruction
|
||||
* \note You should inherit this class privately
|
||||
* \sa Signal
|
||||
*/
|
||||
class SlotGuard {
|
||||
private:
|
||||
std::vector<std::unique_ptr<Connection>> m_Connections;
|
||||
|
||||
public:
|
||||
/**
|
||||
* \brief Connect a signal to a function (with the same signature)
|
||||
*/
|
||||
template <typename... Args>
|
||||
void Connect(Signal<Args...> a_Signal, const typename Signal<Args...>::CallBack::element_type& a_Callback) {
|
||||
auto ptr = std::make_shared<typename Signal<Args...>::CallBack::element_type>(a_Callback);
|
||||
m_Connections.push_back(a_Signal.ConnectSafe(ptr));
|
||||
}
|
||||
|
||||
void Disconnect() {
|
||||
m_Connections.clear();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace utils
|
||||
} // namespace td
|
||||
22
include/td/misc/Time.h
Normal file
22
include/td/misc/Time.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace td {
|
||||
|
||||
class Timer {
|
||||
private:
|
||||
std::chrono::time_point<std::chrono::system_clock> m_LastTime;
|
||||
|
||||
public:
|
||||
Timer() : m_LastTime(std::chrono::system_clock::now()) {}
|
||||
|
||||
float GetDelta() {
|
||||
auto timeElapsed = std::chrono::system_clock::now() - m_LastTime;
|
||||
float timeSeconds = std::chrono::duration<float, std::chrono::seconds::period>(timeElapsed).count();
|
||||
m_LastTime = std::chrono::system_clock::now();
|
||||
return timeSeconds;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace td
|
||||
@@ -1,61 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file PacketDispatcher.h
|
||||
* \brief File containing the td::protocol::PacketDispatcher class
|
||||
*/
|
||||
|
||||
#include <td/common/NonCopyable.h>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
|
||||
#include <map>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
/**
|
||||
* \class Dispatcher
|
||||
* \brief Class used to dispatch things
|
||||
*/
|
||||
template <typename T_Enum, typename T_Handler, typename T>
|
||||
class Dispatcher : private NonCopyable {
|
||||
private:
|
||||
std::map<T_Enum, std::vector<T_Handler*>> m_Handlers;
|
||||
|
||||
public:
|
||||
/**
|
||||
* \brief Constructor
|
||||
*/
|
||||
Dispatcher() {}
|
||||
|
||||
/**
|
||||
* \brief Dispatch a packet
|
||||
* \param packet The packet to dispatch
|
||||
*/
|
||||
void Dispatch(const T& packet);
|
||||
|
||||
/**
|
||||
* \brief Register a packet handler
|
||||
* \param type The packet type
|
||||
* \param handler The packet handler
|
||||
*/
|
||||
void RegisterHandler(T_Enum type, T_Handler& handler);
|
||||
|
||||
/**
|
||||
* \brief Unregister a packet handler
|
||||
* \param type The packet type
|
||||
* \param handler The packet handler
|
||||
*/
|
||||
void UnregisterHandler(T_Enum type, T_Handler& handler);
|
||||
|
||||
/**
|
||||
* \brief Unregister a packet handler
|
||||
* \param handler The packet handler
|
||||
*/
|
||||
void UnregisterHandler(T_Handler& handler);
|
||||
};
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
|
||||
#include "Dispatcher.inl"
|
||||
@@ -1,38 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
template <typename T_Enum, typename T_Handler, typename T>
|
||||
void Dispatcher<T_Enum, T_Handler, T>::Dispatch(const T& packet) {
|
||||
T_Enum type = packet.GetType();
|
||||
for (auto* handler : m_Handlers[type])
|
||||
handler->Check(packet);
|
||||
}
|
||||
|
||||
template <typename T_Enum, typename T_Handler, typename T>
|
||||
void Dispatcher<T_Enum, T_Handler, T>::RegisterHandler(T_Enum type, T_Handler& handler) {
|
||||
auto found = std::find(m_Handlers[type].begin(), m_Handlers[type].end(), &handler);
|
||||
if (found == m_Handlers[type].end())
|
||||
m_Handlers[type].push_back(&handler);
|
||||
}
|
||||
|
||||
template <typename T_Enum, typename T_Handler, typename T>
|
||||
void Dispatcher<T_Enum, T_Handler, T>::UnregisterHandler(T_Enum type, T_Handler& handler) {
|
||||
m_Handlers[type].erase(std::remove(m_Handlers[type].begin(), m_Handlers[type].end(), &handler), m_Handlers[type].end());
|
||||
}
|
||||
|
||||
template <typename T_Enum, typename T_Handler, typename T>
|
||||
void Dispatcher<T_Enum, T_Handler, T>::UnregisterHandler(T_Handler& handler) {
|
||||
for (auto& pair : m_Handlers) {
|
||||
if (pair.second.empty())
|
||||
continue;
|
||||
|
||||
PacketType type = pair.first;
|
||||
|
||||
m_Handlers[type].erase(std::remove(m_Handlers[type].begin(), m_Handlers[type].end(), &handler), m_Handlers[type].end());
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
@@ -3,40 +3,40 @@
|
||||
#include <array>
|
||||
#include <string>
|
||||
#include <td/Types.h>
|
||||
#include <sp/protocol/BitField.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
namespace cdata {
|
||||
|
||||
|
||||
struct PlaceTower {
|
||||
TowerType m_Type : 4;
|
||||
PlayerID m_Placer : 4;
|
||||
sp::BitField<TowerType, 4> m_Type;
|
||||
sp::BitField<PlayerID, 4> m_Placer;
|
||||
TowerCoords m_Position;
|
||||
};
|
||||
|
||||
struct UpgradeTower {
|
||||
TowerID m_Tower : 12;
|
||||
std::uint8_t m_Upgrade : 4;
|
||||
sp::BitField<TowerID, 12> m_Tower;
|
||||
sp::BitField<std::uint8_t, 4> m_Upgrade;
|
||||
};
|
||||
|
||||
struct SpawnTroop {
|
||||
EntityType m_Type : 5;
|
||||
std::uint8_t m_Level : 3;
|
||||
sp::BitField<EntityType, 5> m_Type;
|
||||
sp::BitField<std::uint8_t, 3> m_Level;
|
||||
EntityCoords m_Position;
|
||||
PlayerID m_Sender;
|
||||
};
|
||||
|
||||
struct UseItem {
|
||||
ShopItem m_Item : 4;
|
||||
PlayerID m_User : 4;
|
||||
sp::BitField<ShopItem, 4> m_Item;
|
||||
sp::BitField<PlayerID, 4> m_User;
|
||||
EntityCoords m_Position;
|
||||
};
|
||||
|
||||
struct TeamChange {
|
||||
PlayerID m_Player : 7;
|
||||
Team m_NewTeam : 1;
|
||||
sp::BitField<PlayerID, 7> m_Player;
|
||||
sp::BitField<TeamColor, 1> m_NewTeam;
|
||||
};
|
||||
|
||||
struct PlayerJoin {
|
||||
|
||||
@@ -1,22 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
/**
|
||||
* \def DeclareAllPacket
|
||||
* \brief Avoids repetitive operations on commands
|
||||
*/
|
||||
#define DeclareAllCommand() \
|
||||
DeclareCommand(End) \
|
||||
DeclareCommand(PlaceTower) \
|
||||
DeclareCommand(PlayerJoin) \
|
||||
DeclareCommand(SpawnTroop) \
|
||||
DeclareCommand(TeamChange) \
|
||||
DeclareCommand(UpgradeTower) \
|
||||
DeclareCommand(UseItem)
|
||||
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
@@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file CommandDispatcher.h
|
||||
* \brief File containing the td::protocol::CommandDispatcher class
|
||||
*/
|
||||
|
||||
#include <td/protocol/Dispatcher.h>
|
||||
#include <td/protocol/command/Commands.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
using CommandDispatcher = Dispatcher<CommandType, CommandVisitor, Command>;
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
@@ -1,19 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <td/protocol/command/Commands.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
namespace CommandFactory {
|
||||
|
||||
template <typename CommandDerived, typename = typename std::enable_if<std::is_base_of<Command, CommandDerived>::value>::type>
|
||||
std::shared_ptr<CommandDerived> CreateCommand() {
|
||||
return std::make_shared<CommandDerived>();
|
||||
}
|
||||
|
||||
const std::shared_ptr<Command>& CreateReadOnlyCommand(CommandType a_Type);
|
||||
|
||||
} // namespace CommandFactory
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
@@ -1,21 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <td/common/DataBuffer.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
class Command;
|
||||
|
||||
using CommandPtr = std::shared_ptr<Command>;
|
||||
|
||||
namespace CommandSerializer {
|
||||
|
||||
DataBuffer Serialize(const Command& a_Command);
|
||||
|
||||
std::shared_ptr<Command> Deserialize(DataBuffer& a_Data);
|
||||
|
||||
} // namespace CommandSerializer
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
@@ -1,39 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file CommandVisitor.h
|
||||
* \brief File containing the td::protocol::CommandVisitor class
|
||||
*/
|
||||
|
||||
#include <td/protocol/command/Commands.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
#define DeclareCommand(CommandName, ...) \
|
||||
/** This function is called when the packet processed by CommandVisitor::Check is a CommandName */ \
|
||||
virtual void Visit(const commands::CommandName&) {}
|
||||
|
||||
/**
|
||||
* \class CommandVisitor
|
||||
* \brief This class uses double-dispatch in order to find the real type of a packet
|
||||
*/
|
||||
class CommandVisitor : private NonCopyable {
|
||||
protected:
|
||||
CommandVisitor() {}
|
||||
virtual ~CommandVisitor() {}
|
||||
|
||||
public:
|
||||
/**
|
||||
* \brief Calls the right CommandVisitor::Visit method corresponding to the real type of the packet
|
||||
* \param packet the Command to visit
|
||||
*/
|
||||
void Check(const Command& packet);
|
||||
|
||||
DeclareAllCommand()
|
||||
};
|
||||
|
||||
#undef DeclareCommand
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
@@ -5,107 +5,63 @@
|
||||
* \brief File containing the definitions of the lockstep commands
|
||||
*/
|
||||
|
||||
#include <memory>
|
||||
#include <sp/common/GenericHandler.h>
|
||||
#include <sp/protocol/ConcreteMessage.h>
|
||||
#include <sp/protocol/MessageDispatcher.h>
|
||||
#include <sp/protocol/MessageFactory.h>
|
||||
#include <td/Types.h>
|
||||
#include <td/common/NonCopyable.h>
|
||||
#include <td/protocol/command/CommandData.h>
|
||||
#include <td/protocol/command/CommandDeclare.h>
|
||||
#include <memory>
|
||||
|
||||
#include <sp/io/SerializableMessage.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
class CommandVisitor;
|
||||
|
||||
/** A Command id is 8 bits wide */
|
||||
using CommandID = std::uint8_t;
|
||||
|
||||
#define DeclareCommand(CommandName, ...) /** CommandName */ CommandName,
|
||||
|
||||
/**
|
||||
* \enum CommandType
|
||||
* \brief Map a Command to an id
|
||||
*/
|
||||
enum class CommandType : CommandID {
|
||||
|
||||
DeclareAllCommand()
|
||||
|
||||
/** The number of Commands */
|
||||
COMMAND_COUNT
|
||||
};
|
||||
|
||||
|
||||
#undef DeclareCommand
|
||||
|
||||
|
||||
class Command : private NonCopyable {
|
||||
public:
|
||||
/**
|
||||
* \return The real type of the Command
|
||||
*/
|
||||
virtual CommandType GetType() const = 0;
|
||||
|
||||
private:
|
||||
/** Use a CommandVisitor to make double-dispatch possible */
|
||||
virtual void Accept(CommandVisitor& a_Visitor) const = 0;
|
||||
|
||||
friend class CommandVisitor;
|
||||
enum class CommandID : std::uint8_t {
|
||||
End = 0,
|
||||
PlaceTower,
|
||||
PlayerJoin,
|
||||
SpawnTroop,
|
||||
TeamChange,
|
||||
UpgradeTower,
|
||||
UseItem,
|
||||
};
|
||||
|
||||
class CommandHandler;
|
||||
|
||||
using CommandBase = sp::MessageBase<CommandID, CommandHandler>;
|
||||
|
||||
template <typename TData, CommandID ID>
|
||||
using CommandMessage = sp::ConcreteMessage<TData, CommandBase, ID>;
|
||||
|
||||
|
||||
namespace commands {
|
||||
|
||||
/**
|
||||
* \class ConcreteCommand
|
||||
* \brief A Command associated with an id and holding data
|
||||
* \tparam PT The Command type
|
||||
* \tparam Data The structure holding the data of the Command (in td::protocol::data namespace)
|
||||
*/
|
||||
template <CommandType CT, typename Data>
|
||||
class ConcreteCommand : public Command {
|
||||
public:
|
||||
/** The type of the struct holding the data */
|
||||
using CommandDataType = Data;
|
||||
|
||||
/** The structure holding the actual data */
|
||||
CommandDataType m_Data;
|
||||
|
||||
/** Construct the Command with data of type CommandDataType */
|
||||
ConcreteCommand(const CommandDataType& a_Data = {});
|
||||
|
||||
constexpr CommandType GetType() const override {
|
||||
return CT;
|
||||
};
|
||||
|
||||
private:
|
||||
void Accept(CommandVisitor& a_Visitor) const override;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// define TD_INSTANCIATE_COMMANDS
|
||||
// before including this file
|
||||
// if you want to instantiate templates
|
||||
#ifdef TD_INSTANCIATE_COMMANDS
|
||||
#define DeclareCommand(CommandName, ...) \
|
||||
using CommandName = ConcreteCommand<CommandType::CommandName, cdata::CommandName>; \
|
||||
template class ConcreteCommand<CommandType::CommandName, cdata::CommandName>;
|
||||
#else
|
||||
#define DeclareCommand(CommandName, ...) /** Defines the CommandName Command */ \
|
||||
using CommandName = ConcreteCommand<CommandType::CommandName, cdata::CommandName>;
|
||||
#endif
|
||||
|
||||
DeclareAllCommand()
|
||||
|
||||
#undef DeclareCommand
|
||||
using EndCommand = CommandMessage<cdata::End, CommandID::End>;
|
||||
using PlaceTowerCommand = CommandMessage<cdata::PlaceTower, CommandID::PlaceTower>;
|
||||
using PlayerJoinCommand = CommandMessage<cdata::PlayerJoin, CommandID::PlayerJoin>;
|
||||
using SpawnTroopCommand = CommandMessage<cdata::SpawnTroop, CommandID::SpawnTroop>;
|
||||
using TeamChangeCommand = CommandMessage<cdata::TeamChange, CommandID::TeamChange>;
|
||||
using UpgradeTowerCommand = CommandMessage<cdata::UpgradeTower, CommandID::UpgradeTower>;
|
||||
using UseItemCommand = CommandMessage<cdata::UseItem, CommandID::UseItem>;
|
||||
|
||||
} // namespace commands
|
||||
|
||||
using LockStep = std::vector<std::shared_ptr<protocol::Command>>;
|
||||
using AllCommands = std::tuple<commands::EndCommand, commands::PlaceTowerCommand, commands::PlayerJoinCommand,
|
||||
commands::SpawnTroopCommand, commands::TeamChangeCommand, commands::UpgradeTowerCommand, commands::UseItemCommand>;
|
||||
|
||||
class CommandHandler : public sp::GenericHandler<AllCommands> {};
|
||||
|
||||
using CommandDispatcher = sp::MessageDispatcher<CommandBase>;
|
||||
|
||||
using CommandFactory = sp::MessageFactory<CommandBase, AllCommands>;
|
||||
|
||||
using CommandPtr = sp::SerializableMessage<CommandFactory>;
|
||||
|
||||
using LockStep = std::vector<CommandPtr>;
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
|
||||
@@ -3,34 +3,35 @@
|
||||
#include <td/Types.h>
|
||||
#include <vector>
|
||||
#include <td/protocol/command/Commands.h>
|
||||
#include <td/common/Array.h>
|
||||
#include <td/game/WorldTypes.h>
|
||||
|
||||
// Make it dynamic ?
|
||||
#ifdef NDEBUG
|
||||
#define LOCKSTEP_BUFFER_SIZE 10
|
||||
#else
|
||||
#define LOCKSTEP_BUFFER_SIZE 1
|
||||
#endif
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
struct PlayerInfo {
|
||||
PlayerID m_PlayerId;
|
||||
std::string m_PlayerName;
|
||||
};
|
||||
|
||||
struct MapData {
|
||||
|
||||
};
|
||||
|
||||
namespace pdata {
|
||||
|
||||
/** Client attempts to login (very basic) */
|
||||
struct PlayerLogin {
|
||||
std::string m_PlayerName;
|
||||
};
|
||||
|
||||
/** Server indicates success */
|
||||
struct LoggingSuccess {
|
||||
PlayerID m_PlayerId;
|
||||
};
|
||||
|
||||
/** Client attempts to login (very basic) */
|
||||
struct PlayerLogin {
|
||||
std::string m_PlayerName;
|
||||
};
|
||||
|
||||
/** Player joins the lobby */
|
||||
struct PlayerJoin {
|
||||
PlayerInfo m_Player;
|
||||
@@ -41,6 +42,16 @@ struct PlayerLeave {
|
||||
PlayerID m_PlayerId;
|
||||
};
|
||||
|
||||
/** List of players who joined before */
|
||||
struct PlayerList {
|
||||
std::vector<PlayerInfo> m_Players;
|
||||
};
|
||||
|
||||
struct PredictCommand {
|
||||
CommandPtr m_Command;
|
||||
StepTime m_FrameNumber;
|
||||
};
|
||||
|
||||
/** Keep alive */
|
||||
struct KeepAlive {
|
||||
std::uint64_t m_KeepAliveId;
|
||||
@@ -56,10 +67,7 @@ struct ChatMessage {
|
||||
std::string m_Text;
|
||||
};
|
||||
|
||||
// TODO: handle players joining in the first second
|
||||
|
||||
struct BeginGame {
|
||||
MapData m_Map;
|
||||
std::vector<PlayerInfo> m_BlueTeam;
|
||||
std::vector<PlayerInfo> m_RedTeam;
|
||||
// optional, used for players joining mid game
|
||||
@@ -67,10 +75,47 @@ struct BeginGame {
|
||||
};
|
||||
|
||||
struct LockSteps {
|
||||
std::uint16_t m_FirstFrameNumber;
|
||||
StepTime m_FirstFrameNumber;
|
||||
std::array<LockStep, LOCKSTEP_BUFFER_SIZE> m_LockSteps;
|
||||
};
|
||||
|
||||
struct WorldHeader {
|
||||
game::TowerTileColorPalette m_TowerPlacePalette;
|
||||
Color m_WalkablePalette;
|
||||
std::vector<Color> m_DecorationPalette;
|
||||
Color m_Background;
|
||||
|
||||
game::SpawnColorPalette m_SpawnColorPalette;
|
||||
|
||||
game::TilePalette m_TilePalette;
|
||||
|
||||
game::Spawn m_RedSpawn, m_BlueSpawn;
|
||||
game::TeamCastle m_RedCastle, m_BlueCastle;
|
||||
};
|
||||
|
||||
struct WorldData {
|
||||
game::ChunkList m_Chunks;
|
||||
};
|
||||
|
||||
// TODO: spawn multiple troops at the same time
|
||||
struct SpawnTroop {
|
||||
sp::BitField<EntityType, 5> m_Type;
|
||||
sp::BitField<std::uint8_t, 3> m_Level;
|
||||
};
|
||||
|
||||
struct PlaceTower {
|
||||
TowerType m_Type;
|
||||
TowerCoords m_Position;
|
||||
};
|
||||
|
||||
struct LockStepRequest {
|
||||
std::vector<StepTime> m_Missing;
|
||||
};
|
||||
|
||||
struct LockStepResponse {
|
||||
std::map<StepTime, LockStep> m_Steps;
|
||||
};
|
||||
|
||||
} // namespace pdata
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
|
||||
@@ -1,48 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file PacketDeclare.h
|
||||
* \brief Holds the definitions of the packets (but not their content)
|
||||
*/
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
/**
|
||||
* \enum PacketSender
|
||||
* \brief Indicate who should send a packet
|
||||
*/
|
||||
enum class PacketSenderType {
|
||||
/** Sent by clients and server */
|
||||
Both = 1,
|
||||
/** Sent by clients to the server */
|
||||
Client,
|
||||
/** Sent by server to the clients */
|
||||
Server,
|
||||
};
|
||||
|
||||
enum class PacketSendType {
|
||||
Reliable = 1,
|
||||
Unreliable,
|
||||
UnreliableOrdered,
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* \def DeclareAllPacket
|
||||
* \brief Avoids repetitive operations on packets
|
||||
*/
|
||||
#define DeclareAllPacket() \
|
||||
DeclarePacket(ChatMessage, Reliable, Both) \
|
||||
DeclarePacket(BeginGame, Reliable, Server) \
|
||||
DeclarePacket(Disconnect, Reliable, Both) \
|
||||
DeclarePacket(KeepAlive, Reliable, Both) \
|
||||
DeclarePacket(LockSteps, Unreliable, Both) \
|
||||
DeclarePacket(LoggingSuccess, Reliable, Server) \
|
||||
DeclarePacket(PlayerJoin, Reliable, Server) \
|
||||
DeclarePacket(PlayerLeave, Reliable, Server) \
|
||||
DeclarePacket(PlayerLogin, Reliable, Client) \
|
||||
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
@@ -1,17 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file PacketDispatcher.h
|
||||
* \brief File containing the td::protocol::PacketDispatcher class
|
||||
*/
|
||||
|
||||
#include <td/protocol/Dispatcher.h>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
using PacketDispatcher = Dispatcher<PacketType, PacketVisitor, Packet>;
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
@@ -1,19 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
namespace PacketFactory {
|
||||
|
||||
template <typename PacketDerived, typename = typename std::enable_if<std::is_base_of<Packet, PacketDerived>::value>::type>
|
||||
std::unique_ptr<PacketDerived> CreatePacket() {
|
||||
return std::make_unique<PacketDerived>();
|
||||
}
|
||||
|
||||
const std::unique_ptr<Packet>& CreateReadOnlyPacket(PacketType a_Type);
|
||||
|
||||
} // namespace PacketFactory
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
18
include/td/protocol/packet/PacketSerialize.h
Normal file
18
include/td/protocol/packet/PacketSerialize.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/protocol/packet/PacketData.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
|
||||
|
||||
namespace game {
|
||||
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const TeamCastle& a_Castle);
|
||||
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, TeamCastle& a_Castle);
|
||||
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Spawn& a_Spawn);
|
||||
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, Spawn& a_Spawn);
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
@@ -1,21 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <td/common/DataBuffer.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
class Packet;
|
||||
|
||||
using PacketPtr = std::unique_ptr<Packet>;
|
||||
|
||||
namespace PacketSerializer {
|
||||
|
||||
DataBuffer Serialize(const Packet& a_Packet);
|
||||
|
||||
std::unique_ptr<Packet> Deserialize(DataBuffer& a_Data);
|
||||
|
||||
} // namespace PacketSerializer
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
@@ -1,39 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
/**
|
||||
* \file PacketVisitor.h
|
||||
* \brief File containing the td::protocol::PacketVisitor class
|
||||
*/
|
||||
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
#define DeclarePacket(PacketName, ...) \
|
||||
/** This function is called when the packet processed by PacketVisitor::Check is a PacketName */ \
|
||||
virtual void Visit(const packets::PacketName&) {}
|
||||
|
||||
/**
|
||||
* \class PacketVisitor
|
||||
* \brief This class uses double-dispatch in order to find the real type of a packet
|
||||
*/
|
||||
class PacketVisitor : private NonCopyable {
|
||||
protected:
|
||||
PacketVisitor() {}
|
||||
virtual ~PacketVisitor() {}
|
||||
|
||||
public:
|
||||
/**
|
||||
* \brief Calls the right PacketVisitor::Visit method corresponding to the real type of the packet
|
||||
* \param packet the Packet to visit
|
||||
*/
|
||||
void Check(const Packet& packet);
|
||||
|
||||
DeclareAllPacket()
|
||||
};
|
||||
|
||||
#undef DeclarePacket
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
@@ -7,107 +7,83 @@
|
||||
|
||||
#include <td/common/NonCopyable.h>
|
||||
#include <td/protocol/packet/PacketData.h>
|
||||
#include <td/protocol/packet/PacketDeclare.h>
|
||||
|
||||
#include <sp/common/GenericHandler.h>
|
||||
#include <sp/protocol/ConcreteMessage.h>
|
||||
#include <sp/protocol/MessageBase.h>
|
||||
#include <sp/protocol/MessageDispatcher.h>
|
||||
#include <sp/protocol/MessageFactory.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
class PacketVisitor;
|
||||
|
||||
/** A Packet id is 8 bits wide */
|
||||
using PacketID = std::uint8_t;
|
||||
using PeerID = std::uint16_t;
|
||||
|
||||
#define DeclarePacket(PacketName, ...) /** PacketName */ PacketName,
|
||||
|
||||
/**
|
||||
* \enum PacketType
|
||||
* \brief Map a Packet to an id
|
||||
*/
|
||||
enum class PacketType : PacketID {
|
||||
|
||||
DeclareAllPacket()
|
||||
|
||||
/** The number of packets */
|
||||
PACKET_COUNT
|
||||
enum class PacketID : std::uint8_t {
|
||||
ChatMessage = 0,
|
||||
BeginGame,
|
||||
Disconnect,
|
||||
KeepAlive,
|
||||
LockStepRequest,
|
||||
LockStepResponse,
|
||||
LockSteps,
|
||||
LoggingSuccess,
|
||||
PlaceTower,
|
||||
PlayerJoin,
|
||||
PlayerLeave,
|
||||
PlayerList,
|
||||
PlayerLogin,
|
||||
PredictCommand,
|
||||
SpawnTroop,
|
||||
WorldHeader,
|
||||
WorldData,
|
||||
};
|
||||
|
||||
class PacketHandler;
|
||||
|
||||
#undef DeclarePacket
|
||||
|
||||
|
||||
class Packet : private NonCopyable {
|
||||
public:
|
||||
/**
|
||||
* \return The real type of the packet
|
||||
*/
|
||||
virtual PacketType GetType() const = 0;
|
||||
|
||||
/**
|
||||
* \brief The network peer who sent the packet
|
||||
*/
|
||||
PeerID m_Sender;
|
||||
|
||||
private:
|
||||
/** Use a PacketVisitor to make double-dispatch possible */
|
||||
virtual void Accept(PacketVisitor& a_Visitor) const = 0;
|
||||
|
||||
friend class PacketVisitor;
|
||||
};
|
||||
|
||||
using PacketBase = sp::MessageBase<PacketID, PacketHandler>;
|
||||
|
||||
|
||||
template <typename TData, PacketID ID>
|
||||
using PacketMessage = sp::ConcreteMessage<TData, PacketBase, ID>;
|
||||
|
||||
|
||||
namespace packets {
|
||||
|
||||
/**
|
||||
* \class ConcretePacket
|
||||
* \brief A Packet associated with an id and holding data
|
||||
* \tparam PT The packet type
|
||||
* \tparam Data The structure holding the data of the packet (in td::protocol::data namespace)
|
||||
*/
|
||||
template <PacketType PT, typename Data>
|
||||
class ConcretePacket : public Packet {
|
||||
public:
|
||||
/** The type of the struct holding the data */
|
||||
using PacketDataType = Data;
|
||||
|
||||
/** The structure holding the actual data */
|
||||
PacketDataType m_Data;
|
||||
|
||||
/** Construct the packet with data of type PacketDataType */
|
||||
ConcretePacket(const PacketDataType& a_Data = {});
|
||||
|
||||
constexpr PacketType GetType() const override {
|
||||
return PT;
|
||||
};
|
||||
|
||||
private:
|
||||
void Accept(PacketVisitor& a_Visitor) const override;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// define TD_INSTANCIATE_PACKETS
|
||||
// before including this file
|
||||
// if you want to instantiate templates
|
||||
#ifdef TD_INSTANCIATE_PACKETS
|
||||
#define DeclarePacket(PacketName, ...) \
|
||||
using PacketName = ConcretePacket<PacketType::PacketName, pdata::PacketName>; \
|
||||
template class ConcretePacket<PacketType::PacketName, pdata::PacketName>;
|
||||
#else
|
||||
#define DeclarePacket(PacketName, ...) /** Defines the PacketName packet */ \
|
||||
using PacketName = ConcretePacket<PacketType::PacketName, pdata::PacketName>;
|
||||
#endif
|
||||
|
||||
DeclareAllPacket()
|
||||
|
||||
#undef DeclarePacket
|
||||
using BeginGamePacket = PacketMessage<pdata::BeginGame, PacketID::BeginGame>;
|
||||
using ChatMessagePacket = PacketMessage<pdata::ChatMessage, PacketID::ChatMessage>;
|
||||
using DisconnectPacket = PacketMessage<pdata::Disconnect, PacketID::Disconnect>;
|
||||
using KeepAlivePacket = PacketMessage<pdata::KeepAlive, PacketID::KeepAlive>;
|
||||
using LockStepRequestPacket = PacketMessage<pdata::LockStepRequest, PacketID::LockStepRequest>;
|
||||
using LockStepResponsePacket = PacketMessage<pdata::LockStepResponse, PacketID::LockStepResponse>;
|
||||
using LockStepsPacket = PacketMessage<pdata::LockSteps, PacketID::LockSteps>;
|
||||
using LoggingSuccessPacket = PacketMessage<pdata::LoggingSuccess, PacketID::LoggingSuccess>;
|
||||
using PlaceTowerPacket = PacketMessage<pdata::PlaceTower, PacketID::PlaceTower>;
|
||||
using PlayerJoinPacket = PacketMessage<pdata::PlayerJoin, PacketID::PlayerJoin>;
|
||||
using PlayerLeavePacket = PacketMessage<pdata::PlayerLeave, PacketID::PlayerLeave>;
|
||||
using PlayerListPacket = PacketMessage<pdata::PlayerList, PacketID::PlayerList>;
|
||||
using PlayerLoginPacket = PacketMessage<pdata::PlayerLogin, PacketID::PlayerLogin>;
|
||||
using PredictCommandPacket = PacketMessage<pdata::PredictCommand, PacketID::PredictCommand>;
|
||||
using SpawnTroopPacket = PacketMessage<pdata::SpawnTroop, PacketID::SpawnTroop>;
|
||||
using WorldHeaderPacket = PacketMessage<pdata::WorldHeader, PacketID::WorldHeader>;
|
||||
using WorldDataPacket = PacketMessage<pdata::WorldData, PacketID::WorldData>;
|
||||
|
||||
} // namespace packets
|
||||
|
||||
using AllPackets = std::tuple<packets::BeginGamePacket, packets::ChatMessagePacket, packets::DisconnectPacket,
|
||||
packets::KeepAlivePacket, packets::LockStepRequestPacket, packets::LockStepResponsePacket, packets::LockStepsPacket,
|
||||
packets::LoggingSuccessPacket, packets::PlaceTowerPacket, packets::PlayerJoinPacket, packets::PlayerLeavePacket,
|
||||
packets::PlayerListPacket, packets::PlayerLoginPacket, packets::PredictCommandPacket, packets::SpawnTroopPacket,
|
||||
packets::WorldHeaderPacket, packets::WorldDataPacket>;
|
||||
|
||||
class PacketHandler : public sp::GenericHandler<AllPackets> {
|
||||
public:
|
||||
void HandleBase(const PacketBase& a_Packet) {
|
||||
a_Packet.Dispatch(*this);
|
||||
}
|
||||
};
|
||||
|
||||
using PacketDispatcher = sp::MessageDispatcher<PacketBase>;
|
||||
|
||||
using PacketFactory = sp::MessageFactory<PacketBase, AllPackets>;
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
|
||||
41
include/td/render/Camera.h
Normal file
41
include/td/render/Camera.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/Maths.h>
|
||||
#include <td/misc/Signal.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
class Camera {
|
||||
private:
|
||||
Mat4f m_ViewMatrix;
|
||||
Mat4f m_ProjectionMatrix;
|
||||
Mat4f m_InvViewMatrix;
|
||||
Mat4f m_InvProjectionMatrix;
|
||||
|
||||
float m_CamDistance = 25.0f;
|
||||
Vec3f m_CamPos{0, m_CamDistance, 0};
|
||||
Vec2f m_CamLook{};
|
||||
|
||||
float m_Yaw = -PI / 2.0f;
|
||||
float m_Pitch = -PI / 2.0f + 0.0000001f;
|
||||
|
||||
public:
|
||||
utils::Signal<> OnPerspectiveChange;
|
||||
utils::Signal<> OnViewChange;
|
||||
|
||||
const Mat4f& GetViewMatrix() const {
|
||||
return m_ViewMatrix;
|
||||
}
|
||||
|
||||
const Mat4f& GetProjectionMatrix() const {
|
||||
return m_ProjectionMatrix;
|
||||
}
|
||||
|
||||
void UpdatePerspective(float a_AspectRatio);
|
||||
|
||||
void SetCamPos(const Vec3f& a_NewPos);
|
||||
};
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
8
include/td/render/OpenGL.h
Normal file
8
include/td/render/OpenGL.h
Normal file
@@ -0,0 +1,8 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef TD_GL_LOADER_GLEW
|
||||
#include <GL/glew.h>
|
||||
#else
|
||||
#include <glbinding/gl/gl.h>
|
||||
using namespace gl;
|
||||
#endif
|
||||
81
include/td/render/Renderer.h
Normal file
81
include/td/render/Renderer.h
Normal file
@@ -0,0 +1,81 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <td/render/Camera.h>
|
||||
#include <td/render/loader/GLLoader.h>
|
||||
#include <td/render/shader/CameraShaderProgram.h>
|
||||
#include <td/misc/SlotGuard.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
class BasicRenderer {
|
||||
public:
|
||||
virtual void Render(float a_Lerp) = 0;
|
||||
virtual ~BasicRenderer() {}
|
||||
|
||||
void Render(const GL::VertexArray& a_Vao);
|
||||
};
|
||||
|
||||
template <typename TShader>
|
||||
class Renderer : public BasicRenderer, private utils::SlotGuard {
|
||||
protected:
|
||||
std::unique_ptr<TShader> m_Shader;
|
||||
Camera& m_Camera;
|
||||
|
||||
public:
|
||||
Renderer(Camera& a_Camera);
|
||||
virtual ~Renderer() {}
|
||||
|
||||
template <typename T>
|
||||
float Lerp(const T& a_Mob, float a_LerpFactor, const std::function<float(const T&)>& a_MemberGetter) {
|
||||
return static_cast<float>(maths::Lerp(a_MemberGetter(a_Mob), a_MemberGetter(*a_Mob.m_Next), a_LerpFactor));
|
||||
}
|
||||
};
|
||||
|
||||
class RenderPipeline {
|
||||
private:
|
||||
std::vector<std::unique_ptr<BasicRenderer>> m_Renderers;
|
||||
|
||||
public:
|
||||
RenderPipeline();
|
||||
virtual ~RenderPipeline() {}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
T& AddRenderer(Args&&... args) {
|
||||
auto ptr = std::make_unique<T>(args...);
|
||||
auto rawPtr = ptr.get();
|
||||
m_Renderers.push_back(std::move(ptr));
|
||||
return *rawPtr;
|
||||
}
|
||||
|
||||
void Clear() {
|
||||
m_Renderers.clear();
|
||||
}
|
||||
|
||||
void Render(float a_Lerp) {
|
||||
for (auto& renderer : m_Renderers) {
|
||||
renderer->Render(a_Lerp);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template <typename TShader>
|
||||
Renderer<TShader>::Renderer(Camera& a_Camera) : m_Shader(std::make_unique<TShader>()), m_Camera(a_Camera) {
|
||||
Connect(a_Camera.OnPerspectiveChange, [this](){
|
||||
m_Shader->Start();
|
||||
m_Shader->SetProjectionMatrix(m_Camera.GetProjectionMatrix());
|
||||
});
|
||||
|
||||
Connect(a_Camera.OnViewChange, [this]() {
|
||||
m_Shader->Start();
|
||||
m_Shader->SetViewMatrix(m_Camera.GetViewMatrix());
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
86
include/td/render/loader/GLLoader.h
Normal file
86
include/td/render/loader/GLLoader.h
Normal file
@@ -0,0 +1,86 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/common/NonCopyable.h>
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace td {
|
||||
namespace GL {
|
||||
|
||||
struct VertexAttribPointer {
|
||||
unsigned int m_Index;
|
||||
unsigned int m_Size;
|
||||
unsigned int m_Offset;
|
||||
};
|
||||
|
||||
class ElementBuffer : private sp::NonCopyable {
|
||||
private:
|
||||
unsigned int m_ID = 0;
|
||||
std::size_t m_TriangleCount;
|
||||
|
||||
public:
|
||||
ElementBuffer(ElementBuffer&& other) {
|
||||
std::swap(m_ID, other.m_ID);
|
||||
m_TriangleCount = other.m_TriangleCount;
|
||||
}
|
||||
|
||||
explicit ElementBuffer(const std::vector<unsigned int>& indicies);
|
||||
~ElementBuffer();
|
||||
|
||||
void Bind() const;
|
||||
void Unbind() const;
|
||||
|
||||
std::size_t GetTriangleCount() const {
|
||||
return m_TriangleCount;
|
||||
}
|
||||
};
|
||||
|
||||
class VertexBuffer : private sp::NonCopyable {
|
||||
private:
|
||||
unsigned int m_ID = 0, m_DataStride;
|
||||
std::vector<VertexAttribPointer> m_VertexAttribs;
|
||||
|
||||
public:
|
||||
VertexBuffer(VertexBuffer&& other) {
|
||||
std::swap(m_ID, other.m_ID);
|
||||
m_VertexAttribs = std::move(other.m_VertexAttribs);
|
||||
m_DataStride = other.m_DataStride;
|
||||
}
|
||||
|
||||
VertexBuffer(const std::vector<float>& data, unsigned int stride);
|
||||
~VertexBuffer();
|
||||
|
||||
void Bind() const;
|
||||
void Unbind() const;
|
||||
void AddVertexAttribPointer(unsigned int index, unsigned int coordinateSize, unsigned int offset);
|
||||
void BindVertexAttribs() const;
|
||||
};
|
||||
|
||||
class VertexArray : private sp::NonCopyable {
|
||||
private:
|
||||
unsigned int m_ID = 0;
|
||||
ElementBuffer m_ElementBuffer;
|
||||
std::vector<VertexBuffer> m_VertexBuffers; // use to destroy vbos when become unused
|
||||
public:
|
||||
VertexArray(VertexArray&& other) : m_ElementBuffer(std::move(other.m_ElementBuffer)) {
|
||||
std::swap(m_ID, other.m_ID);
|
||||
m_VertexBuffers = std::move(other.m_VertexBuffers);
|
||||
}
|
||||
|
||||
VertexArray(ElementBuffer&& indicies);
|
||||
~VertexArray();
|
||||
|
||||
std::size_t GetVertexCount() const {
|
||||
return m_ElementBuffer.GetTriangleCount();
|
||||
}
|
||||
|
||||
void BindVertexBuffer(VertexBuffer&& vbo);
|
||||
void Bind() const;
|
||||
void Unbind() const;
|
||||
|
||||
private:
|
||||
void BindElementArrayBuffer();
|
||||
};
|
||||
|
||||
} // namespace GL
|
||||
} // namespace td
|
||||
25
include/td/render/loader/WorldLoader.h
Normal file
25
include/td/render/loader/WorldLoader.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/game/World.h>
|
||||
#include <td/render/loader/GLLoader.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
namespace WorldLoader {
|
||||
|
||||
struct RenderData {
|
||||
std::vector<float> positions;
|
||||
std::vector<float> colors;
|
||||
};
|
||||
|
||||
GL::VertexArray LoadMobModel();
|
||||
GL::VertexArray LoadWorldModel(const td::game::World* world);
|
||||
GL::VertexArray LoadTileSelectModel();
|
||||
RenderData LoadTowerModel(const game::TowerPtr& tower);
|
||||
|
||||
} // namespace WorldLoader
|
||||
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
23
include/td/render/renderer/EntityRenderer.h
Normal file
23
include/td/render/renderer/EntityRenderer.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/render/Renderer.h>
|
||||
#include <td/render/shader/EntityShader.h>
|
||||
#include <td/game/World.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
class EntityRenderer : public Renderer<shader::EntityShader> {
|
||||
private:
|
||||
game::WorldPtr m_World;
|
||||
std::unique_ptr<GL::VertexArray> m_EntityVao;
|
||||
|
||||
public:
|
||||
EntityRenderer(Camera& a_Camera, const game::WorldPtr& a_World);
|
||||
virtual ~EntityRenderer();
|
||||
|
||||
virtual void Render(float a_Lerp) override;
|
||||
};
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
21
include/td/render/renderer/HotkeysRenderer.h
Normal file
21
include/td/render/renderer/HotkeysRenderer.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/render/Renderer.h>
|
||||
#include <client/PlayerManager.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
/**
|
||||
* \brief This is a debug class
|
||||
*/
|
||||
class HotkeysRenderer : public BasicRenderer {
|
||||
public:
|
||||
virtual void Render(float a_Lerp) override;
|
||||
|
||||
HotkeysRenderer();
|
||||
~HotkeysRenderer() {}
|
||||
};
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
357
include/td/render/renderer/ImHotkey.h
Normal file
357
include/td/render/renderer/ImHotkey.h
Normal file
@@ -0,0 +1,357 @@
|
||||
// ImHotKey v1.0
|
||||
// https://github.com/CedricGuillemet/ImHotKey
|
||||
//
|
||||
// The MIT License(MIT)
|
||||
//
|
||||
// Copyright(c) 2019 Cedric Guillemet
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files(the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions :
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all
|
||||
// copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
// SOFTWARE.
|
||||
//
|
||||
#pragma once
|
||||
|
||||
#define SDL_h_
|
||||
|
||||
/*
|
||||
How to use it?
|
||||
|
||||
// Get some hotkeys composed of:
|
||||
// - hotkey name
|
||||
// - hotkey comment/lib
|
||||
// - hotkey scancodes. Computed by the editor. Store that value in your app.
|
||||
|
||||
static std::vector<ImHotKey::HotKey> hotkeys = { { "Layout", "Reorder nodes in a simpler layout", 0xFFFF261D}
|
||||
,{"Save", "Save the current graph", 0xFFFF1F1D}
|
||||
,{"Load", "Load an existing graph file", 0xFFFF181D}
|
||||
,{"Play/Stop", "Play or stop the animation from the current graph", 0xFFFFFF3F}
|
||||
,{"SetKey", "Make a new animation key with the current parameters values at the current time", 0xFFFFFF1F}
|
||||
};
|
||||
|
||||
// The editor is a modal window. bring it with something like that
|
||||
if (ImGui::Button("Edit Hotkeys"))
|
||||
{
|
||||
ImGui::OpenPopup("HotKeys Editor");
|
||||
}
|
||||
ImHotKey::Edit(hotkeys.data(), hotkeys.size(), "HotKeys Editor");
|
||||
|
||||
// ImHotKey also provides a way to retrieve HotKey
|
||||
int hotkey = ImHotKey::GetHotKey(hotkeys.data(), hotkeys.size());
|
||||
if (hotkey != -1)
|
||||
{
|
||||
// handle the hotkey index!
|
||||
}
|
||||
|
||||
Awesome, you are done!
|
||||
To help you integrate in your app, you can get a text (like "Ctrl + O") to integrate in your menu
|
||||
static void GetHotKeyLib(unsigned int functionKeys, char* buffer, size_t bufferSize);
|
||||
|
||||
*/
|
||||
|
||||
#include "imgui.h"
|
||||
#include "imgui_internal.h"
|
||||
|
||||
namespace ImHotKey
|
||||
{
|
||||
struct HotKey
|
||||
{
|
||||
const char *functionName;
|
||||
const char *functionLib;
|
||||
unsigned int functionKeys;
|
||||
};
|
||||
|
||||
struct Key
|
||||
{
|
||||
const char* lib = nullptr;
|
||||
unsigned int order;
|
||||
unsigned int scanCodePage1 = 0; // win32 scancode
|
||||
unsigned int scanCodePage7 = 0; // HID (SDL,...)
|
||||
float offset = 0;
|
||||
float width = 40;
|
||||
};
|
||||
|
||||
static const Key Keys[6][18] = {
|
||||
{ {"Esc", 4, 0x1, 0x29, 18}, {"F1", 5, 0x3B, 0x3A, 18}, {"F2", 6, 0x3C, 0x3B}, {"F3", 7, 0x3D, 0x3C}, {"F4", 8, 0x3E, 0x3D}, {"F5", 9, 0x3F, 0x3E, 24}, {"F6", 10, 0x40, 0x3F}, {"F7", 11, 0x41, 0x40}, {"F8", 12, 0x42, 0x41}, {"F9", 13, 0x43, 0x42, 24}, {"F10", 14, 0x44, 0x43}, {"F11", 15, 0x57, 0x44}, {"F12", 16, 0x58, 0x45}, {"PrSn", 17, 0x37, 0x46, 24}, {"ScLk", 18, 0x46}, {"Brk", 19, 126, 0x47} },
|
||||
{ {"~", 20, 0x29, 0x35}, {"1", 21, 0x2, 0x1E}, {"2", 22, 0x3, 0x1F}, {"3", 23, 0x4, 0x20}, {"4", 24, 0x5, 0x21}, {"5", 25, 0x6, 0x22}, {"6", 26, 0x7, 0x23}, {"7", 27, 0x8, 0x24}, {"8", 28, 0x9, 0x25}, {"9", 29, 0xA, 0x26}, {"0", 30, 0xB, 0x27}, {"-", 31, 0xC, 0x2D}, {"+", 32, 0xD, 0x2E},{"Backspace", 33, 0xE, 0x2A, 0, 80}, {"Ins", 34, 0x52, 0x49, 24}, {"Hom", 35, 0x47, 0x4A}, {"PgU", 36, 0x49, 0x4B} },
|
||||
{ {"Tab", 3, 0xF, 0x2B, 0, 60}, {"Q", 37, 0x10, 0x14}, {"W", 38, 0x11, 0x1A}, {"E", 39, 0x12, 0x08}, {"R", 40, 0x13, 0x15}, {"T", 41, 0x14, 0x17}, {"Y", 42, 0x15, 0x1C}, {"U", 43, 0x16, 0x18}, {"I", 44, 0x17, 0x0C}, {"O", 45, 0x18, 0x12}, {"P", 46, 0x19, 0x13}, {"[", 47, 0x1A, 0x2F}, {"]", 48, 0x1B, 0x30}, {"|", 49, 0x2B, 0x31, 0, 60}, {"Del", 50, 0x53, 0x4C, 24}, {"End", 51, 0x4F, 0x4D}, {"PgD", 52, 0x51, 0x4E} },
|
||||
{ {"Caps Lock", 53, 0x3A, 0x39, 0, 80}, {"A", 54, 0x1E, 0x04}, {"S", 55, 0x1F, 0x16}, {"D", 56, 0x20, 0x07}, {"F", 57, 0x21, 0x09}, {"G", 58, 0x22, 0x0A}, {"H", 59, 0x23, 0x0B}, {"J", 60, 0x24, 0x0D}, {"K", 61, 0x25, 0x0E}, {"L", 62, 0x26, 0x0F}, {";", 63, 0x27, 0x33}, {"'", 64, 0x28, 0x34}, {"Ret", 65, 0x1C, 0X28, 0, 84} },
|
||||
{ {"Shift", 2, 0x2A, 0xE1, 0, 104}, {"Z", 66, 0x2C, 0x1D}, {"X", 67, 0x2D, 0x1B}, {"C", 68, 0x2E, 0x06}, {"V", 69, 0x2F, 0x19}, {"B", 70, 0x30, 0x05}, {"N", 71, 0x31, 0x11}, {"M", 72, 0x32, 0x10}, {",", 73, 0x33, 0x36}, {".", 74, 0x34, 0x37}, {"/", 75, 0x35, 0x38}, {"Shift", 2, 0x2A, 0xE5, 0, 104}, {"Up", 76, 0x48, 0x52, 68} },
|
||||
{ {"Ctrl", 0, 0x1D, 0xE0, 0, 60}, {"Alt", 1, 0x38, 0xE2, 68, 60}, {"Space", 77, 0x39, 0X2c, 0, 260}, {"Alt", 1, 0x38, 0xE6, 0, 60}, {"Ctrl", 0, 0x1D, 0xE4, 68, 60}, {"Left", 78, 0x4B, 0x50, 24}, {"Down", 79, 0x50, 0x51}, {"Right", 80, 0x4D, 0x52} }
|
||||
};
|
||||
|
||||
static const Key& GetKeyForScanCode(unsigned int scancode)
|
||||
{
|
||||
for (unsigned int y = 0; y < 6; y++)
|
||||
{
|
||||
int x = 0;
|
||||
while (Keys[y][x].lib)
|
||||
{
|
||||
#ifdef SDL_h_
|
||||
if (Keys[y][x].scanCodePage7 == scancode)
|
||||
#elif WIN32
|
||||
if (Keys[y][x].scanCodePage1 == scancode)
|
||||
#else
|
||||
#error
|
||||
#endif
|
||||
return Keys[y][x];
|
||||
x++;
|
||||
}
|
||||
}
|
||||
return Keys[0][0];
|
||||
}
|
||||
|
||||
static unsigned int GetOrderedScanCodes(unsigned char scanCodes[4], unsigned char order[4])
|
||||
{
|
||||
for (int pass = 0; pass < 2; pass++)
|
||||
{
|
||||
for (int o = 0; o < 3; o++)
|
||||
{
|
||||
if (order[o] > order[o + 1])
|
||||
{
|
||||
ImSwap(order[o], order[o + 1]); ImSwap(scanCodes[o], scanCodes[o + 1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return (scanCodes[3] << 24) + (scanCodes[2] << 16) + (scanCodes[1] << 8) + scanCodes[0];
|
||||
}
|
||||
|
||||
static void HotKeySPrintf(char* buffer, size_t bufferSize, const char* fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
va_start(args, fmt);
|
||||
ImFormatStringV(buffer, bufferSize, fmt, args);
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
static void GetHotKeyLib(unsigned int functionKeys, char* buffer, size_t bufferSize, const char *functionLib = nullptr)
|
||||
{
|
||||
static const char* str[4] = { "%s", "%s + %s", "%s + %s +%s", "%s + %s + %s + %s" };
|
||||
static const char* strLib[4] = { "%s (%s)", "%s (%s + %s)", "%s (%s + %s +%s)", "%s (%s + %s + %s + %s)" };
|
||||
static const char* lib[4];
|
||||
int scanCodeCount = 0;
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
unsigned char scanCode = (unsigned char)(functionKeys >> i * 8);
|
||||
if (scanCode == 0xFF)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
lib[scanCodeCount] = GetKeyForScanCode(scanCode).lib;
|
||||
scanCodeCount++;
|
||||
}
|
||||
if (!scanCodeCount)
|
||||
{
|
||||
buffer[0] = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (functionLib)
|
||||
{
|
||||
const char* fmt = strLib[scanCodeCount - 1];
|
||||
HotKeySPrintf(buffer, bufferSize, fmt, functionLib, lib[0], lib[1], lib[2], lib[3]);
|
||||
}
|
||||
else
|
||||
{
|
||||
const char* fmt = str[scanCodeCount - 1];
|
||||
HotKeySPrintf(buffer, bufferSize, fmt, lib[0], lib[1], lib[2], lib[3]);
|
||||
}
|
||||
}
|
||||
|
||||
static void Edit(HotKey *hotkey, size_t hotkeyCount, const char *popupModal)
|
||||
{
|
||||
static int editingHotkey = -1;
|
||||
if (!hotkeyCount)
|
||||
return;
|
||||
static bool keyDown[512] = {};
|
||||
|
||||
ImGui::SetNextWindowSize(ImVec2(1060, 400));
|
||||
if (!ImGui::BeginPopupModal(popupModal, NULL, ImGuiWindowFlags_NoResize))
|
||||
return;
|
||||
|
||||
ImGui::BeginChildFrame(127, ImVec2(220, -1));
|
||||
for(size_t i = 0;i< hotkeyCount;i++)
|
||||
{
|
||||
char hotKeyLib[128];
|
||||
GetHotKeyLib(hotkey[i].functionKeys, hotKeyLib, sizeof(hotKeyLib), hotkey[i].functionName);
|
||||
if (ImGui::Selectable(hotKeyLib, editingHotkey == int(i)) || editingHotkey == -1)
|
||||
{
|
||||
editingHotkey = int(i);
|
||||
memset(keyDown, 0, sizeof(keyDown));
|
||||
for (int j = 0; j < 4; j++)
|
||||
{
|
||||
int scan = (hotkey[editingHotkey].functionKeys >> (8 * j)) & 0xFF;
|
||||
if (scan != 0xFF)
|
||||
{
|
||||
keyDown[scan] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
ImGui::EndChildFrame();
|
||||
ImGui::SameLine();
|
||||
ImGui::BeginGroup();
|
||||
|
||||
for (int i = ImGuiKey_NamedKey_BEGIN; i < ImGuiKey_NamedKey_END; i++)
|
||||
{
|
||||
if (ImGui::IsKeyPressed(ImGuiKey(i), false))
|
||||
{
|
||||
int imKey;
|
||||
#ifdef SDL_h_
|
||||
imKey = i;
|
||||
#elif WIN32
|
||||
imKey = MapVirtualKeyA(i, MAPVK_VK_TO_VSC);
|
||||
#else
|
||||
imKey = i;
|
||||
#endif
|
||||
keyDown[imKey - ImGuiKey_NamedKey_BEGIN] = !keyDown[imKey - ImGuiKey_NamedKey_BEGIN];
|
||||
}
|
||||
}
|
||||
for (unsigned int y = 0; y < 6; y++)
|
||||
{
|
||||
int x = 0;
|
||||
ImGui::BeginGroup();
|
||||
while (Keys[y][x].lib)
|
||||
{
|
||||
const Key& key = Keys[y][x];
|
||||
const float ofs = key.offset + (x?4.f:0.f);
|
||||
|
||||
const float width = key.width;
|
||||
if (x)
|
||||
{
|
||||
ImGui::SameLine(0.f, ofs);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (ofs >= 1.f)
|
||||
{
|
||||
ImGui::Indent(ofs);
|
||||
}
|
||||
}
|
||||
#ifdef SDL_h_
|
||||
bool& butSwtch = keyDown[key.scanCodePage7];
|
||||
#elif WIN32
|
||||
bool& butSwtch = keyDown[key.scanCodePage1];
|
||||
#else
|
||||
#error
|
||||
#endif
|
||||
ImGui::PushStyleColor(ImGuiCol_Button, butSwtch ? 0xFF1040FF : 0x80000000);
|
||||
if (ImGui::Button(Keys[y][x].lib, ImVec2(width, 40)))
|
||||
{
|
||||
butSwtch = !butSwtch;
|
||||
}
|
||||
ImGui::PopStyleColor();
|
||||
x++;
|
||||
}
|
||||
ImGui::EndGroup();
|
||||
}
|
||||
ImGui::InvisibleButton("space", ImVec2(10, 55));
|
||||
ImGui::BeginChildFrame(18, ImVec2(540, 40));
|
||||
ImGui::Text("%s :", hotkey[editingHotkey].functionName);
|
||||
ImGui::SameLine();
|
||||
ImGui::TextWrapped("%s", hotkey[editingHotkey].functionLib);
|
||||
ImGui::EndChildFrame();
|
||||
ImGui::SameLine();
|
||||
int keyDownCount = 0;
|
||||
for (auto d : keyDown)
|
||||
{
|
||||
keyDownCount += d ? 1 : 0;
|
||||
}
|
||||
if (ImGui::Button("Clear", ImVec2(80, 40)))
|
||||
{
|
||||
memset(keyDown, 0, sizeof(keyDown));
|
||||
}
|
||||
ImGui::SameLine();
|
||||
if (keyDownCount && keyDownCount < 5)
|
||||
{
|
||||
if (ImGui::Button("Set", ImVec2(80, 40)))
|
||||
{
|
||||
unsigned char scanCodes[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
unsigned char order[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
int scanCodeCount = 0;
|
||||
hotkey[editingHotkey].functionKeys = 0;
|
||||
for (int i = 1; i < sizeof(keyDown); i++)
|
||||
{
|
||||
if (keyDown[i])
|
||||
{
|
||||
scanCodes[scanCodeCount] = (unsigned char)i;
|
||||
order[scanCodeCount] = (unsigned char)GetKeyForScanCode(i).order;
|
||||
scanCodeCount++;
|
||||
}
|
||||
}
|
||||
|
||||
hotkey[editingHotkey].functionKeys = GetOrderedScanCodes(scanCodes, order);
|
||||
}
|
||||
ImGui::SameLine(0.f, 20.f);
|
||||
}
|
||||
else
|
||||
{
|
||||
ImGui::SameLine(0.f, 100.f);
|
||||
}
|
||||
|
||||
if (ImGui::Button("Done", ImVec2(80, 40))) { ImGui::CloseCurrentPopup(); }
|
||||
ImGui::EndGroup();
|
||||
ImGui::EndPopup();
|
||||
}
|
||||
|
||||
static int GetHotKey(HotKey *hotkey, size_t hotkeyCount)
|
||||
{
|
||||
static unsigned int lastHotKey = 0xFFFFFFFF;
|
||||
unsigned char scanCodes[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
unsigned char order[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
|
||||
int scanCodeCount = 0;
|
||||
for (int i = ImGuiKey_Aliases_BEGIN; i < ImGuiKey_Aliases_END; i++)
|
||||
{
|
||||
if (ImGui::IsKeyDown(ImGuiKey(i)))
|
||||
{
|
||||
int imKey;
|
||||
#ifdef SDL_h_
|
||||
imKey = i - ImGuiKey_NamedKey_BEGIN;
|
||||
#elif WIN32
|
||||
imKey = MapVirtualKeyA(i, MAPVK_VK_TO_VSC);
|
||||
#else
|
||||
imKey = i;
|
||||
#endif
|
||||
scanCodes[scanCodeCount] = (unsigned char)imKey;
|
||||
order[scanCodeCount] = (unsigned char)GetKeyForScanCode(imKey).order;
|
||||
scanCodeCount++;
|
||||
if (scanCodeCount == 4)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int newHotKey = GetOrderedScanCodes(scanCodes, order);
|
||||
|
||||
if (scanCodeCount)
|
||||
{
|
||||
if (newHotKey != lastHotKey)
|
||||
{
|
||||
for (size_t i = 0; i < hotkeyCount; i++)
|
||||
{
|
||||
if (hotkey[i].functionKeys == newHotKey)
|
||||
{
|
||||
lastHotKey = newHotKey;
|
||||
return int(i);
|
||||
}
|
||||
}
|
||||
lastHotKey = 0xFFFFFFFF;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
lastHotKey = 0xFFFFFFFF;
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
27
include/td/render/renderer/PlayerListRenderer.h
Normal file
27
include/td/render/renderer/PlayerListRenderer.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/render/Renderer.h>
|
||||
#include <client/PlayerManager.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
/**
|
||||
* \brief This is a debug class
|
||||
*/
|
||||
class PlayerListRenderer : public BasicRenderer {
|
||||
private:
|
||||
const client::PlayerManager& m_Players;
|
||||
public:
|
||||
utils::Signal<> OnPlayerCreate;
|
||||
// utils::Signal<> OnRequestPOV;
|
||||
utils::Signal<PlayerID> OnPlayerKick;
|
||||
|
||||
virtual void Render(float a_Lerp) override;
|
||||
|
||||
PlayerListRenderer(const client::PlayerManager& a_Players);
|
||||
~PlayerListRenderer() {}
|
||||
};
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
23
include/td/render/renderer/TowerRenderer.h
Normal file
23
include/td/render/renderer/TowerRenderer.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/render/Renderer.h>
|
||||
#include <td/render/shader/EntityShader.h>
|
||||
#include <td/game/World.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
class TowerRenderer : public Renderer<shader::EntityShader> {
|
||||
private:
|
||||
game::WorldPtr m_World;
|
||||
std::unique_ptr<GL::VertexArray> m_EntityVao;
|
||||
|
||||
public:
|
||||
TowerRenderer(Camera& a_Camera, const game::WorldPtr& a_World);
|
||||
virtual ~TowerRenderer();
|
||||
|
||||
virtual void Render(float a_Lerp) override;
|
||||
};
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
23
include/td/render/renderer/WorldRenderer.h
Normal file
23
include/td/render/renderer/WorldRenderer.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/game/World.h>
|
||||
#include <td/render/Renderer.h>
|
||||
#include <td/render/loader/GLLoader.h>
|
||||
#include <td/render/shader/WorldShader.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
class WorldRenderer : public Renderer<shader::WorldShader> {
|
||||
private:
|
||||
std::unique_ptr<GL::VertexArray> m_WorldVao;
|
||||
|
||||
public:
|
||||
WorldRenderer(Camera& a_Camera, const game::WorldPtr& a_World);
|
||||
virtual ~WorldRenderer();
|
||||
|
||||
virtual void Render(float a_Lerp) override;
|
||||
};
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
23
include/td/render/shader/CameraShaderProgram.h
Normal file
23
include/td/render/shader/CameraShaderProgram.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/render/shader/ShaderProgram.h>
|
||||
|
||||
namespace td {
|
||||
namespace shader {
|
||||
|
||||
class CameraShaderProgram : public ShaderProgram {
|
||||
private:
|
||||
unsigned int m_LocationProjection = 0, m_LocationView = 0;
|
||||
|
||||
public:
|
||||
CameraShaderProgram() {}
|
||||
|
||||
void SetProjectionMatrix(const Mat4f& proj) const;
|
||||
void SetViewMatrix(const Mat4f& view) const;
|
||||
|
||||
virtual void GetAllUniformLocation();
|
||||
};
|
||||
|
||||
|
||||
} // namespace shader
|
||||
} // namespace td
|
||||
24
include/td/render/shader/EntityShader.h
Normal file
24
include/td/render/shader/EntityShader.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/render/shader/CameraShaderProgram.h>
|
||||
|
||||
namespace td {
|
||||
namespace shader {
|
||||
|
||||
class EntityShader : public CameraShaderProgram {
|
||||
private:
|
||||
unsigned int m_LocationPosition = 0;
|
||||
unsigned int m_LocationColorEffect = 0;
|
||||
|
||||
protected:
|
||||
virtual void GetAllUniformLocation();
|
||||
|
||||
public:
|
||||
EntityShader();
|
||||
|
||||
void SetColorEffect(const Vec3f& color);
|
||||
void SetModelPos(const Vec3f& pos) const;
|
||||
};
|
||||
|
||||
} // namespace shader
|
||||
} // namespace td
|
||||
43
include/td/render/shader/ShaderProgram.h
Executable file
43
include/td/render/shader/ShaderProgram.h
Executable file
@@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <td/Maths.h>
|
||||
#include <td/render/OpenGL.h>
|
||||
|
||||
namespace td {
|
||||
namespace shader {
|
||||
|
||||
class ShaderProgram {
|
||||
public:
|
||||
ShaderProgram();
|
||||
virtual ~ShaderProgram();
|
||||
|
||||
void Start() const;
|
||||
void Stop() const;
|
||||
|
||||
protected:
|
||||
void LoadProgramFile(const std::string& vertexFile, const std::string& fragmentFile);
|
||||
void LoadProgram(const std::string& vertexSource, const std::string& fragmentSource);
|
||||
|
||||
virtual void GetAllUniformLocation() = 0;
|
||||
int GetUniformLocation(const std::string& uniformName) const;
|
||||
|
||||
void LoadFloat(unsigned int location, float value) const;
|
||||
void LoadInt(unsigned int location, int value) const;
|
||||
void LoadVector(unsigned int location, const Vec2f& vector) const;
|
||||
void LoadVector(unsigned int location, const Vec3f& vector) const;
|
||||
void LoadBoolean(unsigned int location, bool value) const;
|
||||
void LoadMat4(unsigned int location, const Mat4f& mat) const;
|
||||
void CleanUp() const;
|
||||
|
||||
private:
|
||||
unsigned int m_ProgramID;
|
||||
unsigned int m_VertexShaderID;
|
||||
unsigned int m_FragmentShaderID;
|
||||
|
||||
unsigned int LoadShaderFromFile(const std::string& file, GLenum type);
|
||||
unsigned int LoadShader(const std::string& source, GLenum type);
|
||||
};
|
||||
|
||||
} // namespace shader
|
||||
} // namespace td
|
||||
14
include/td/render/shader/WorldShader.h
Normal file
14
include/td/render/shader/WorldShader.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/render/shader/CameraShaderProgram.h>
|
||||
|
||||
namespace td {
|
||||
namespace shader {
|
||||
|
||||
class WorldShader : public CameraShaderProgram {
|
||||
public:
|
||||
WorldShader();
|
||||
};
|
||||
|
||||
} // namespace shader
|
||||
} // namespace td
|
||||
70
include/td/simulation/ClientSimulation.h
Normal file
70
include/td/simulation/ClientSimulation.h
Normal file
@@ -0,0 +1,70 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/game/World.h>
|
||||
#include <optional>
|
||||
#include <td/misc/Signal.h>
|
||||
|
||||
namespace td {
|
||||
namespace sim {
|
||||
|
||||
using GameHistory = std::vector<td::protocol::LockStep>;
|
||||
using GameBuffer = std::vector<std::optional<td::protocol::LockStep>>;
|
||||
|
||||
// TODO: OnEnd signal
|
||||
class ClientSimulation : public protocol::PacketHandler {
|
||||
private:
|
||||
std::uint64_t m_StepTime;
|
||||
std::shared_ptr<game::World> m_World;
|
||||
GameBuffer m_History;
|
||||
float m_CurrentTime;
|
||||
StepTime m_CurrentStep;
|
||||
|
||||
std::shared_ptr<WorldSnapshot> m_LastSnapshot;
|
||||
StepTime m_LastValidStep;
|
||||
|
||||
static const protocol::LockStep EMPTY_LOCKSTEP;
|
||||
|
||||
using protocol::PacketHandler::Handle;
|
||||
|
||||
public:
|
||||
utils::Signal<const std::vector<StepTime>&> OnMissingLockSteps;
|
||||
|
||||
/**
|
||||
* \brief Replay constructor
|
||||
* \param a_StepTime in ms
|
||||
*/
|
||||
ClientSimulation(std::shared_ptr<game::World> a_World, GameHistory&& a_History, std::uint64_t a_StepTime);
|
||||
|
||||
/**
|
||||
* \brief Live update constructor (continuous game updates)
|
||||
* \param a_StepTime in ms
|
||||
*/
|
||||
ClientSimulation(std::shared_ptr<game::World> a_World, std::uint64_t a_StepTime, const std::vector<protocol::LockStep>& a_FirstSteps);
|
||||
|
||||
/**
|
||||
* \return the progress [0-1] between two steps
|
||||
*/
|
||||
float Update(float a_Delta);
|
||||
|
||||
virtual void Handle(const protocol::packets::LockStepsPacket& a_LockSteps) override;
|
||||
virtual void Handle(const protocol::packets::LockStepResponsePacket& a_LockSteps) override;
|
||||
|
||||
private:
|
||||
/**
|
||||
* \returns false if the empty lockstep was used
|
||||
*/
|
||||
bool Step();
|
||||
|
||||
/**
|
||||
* \brief Ticks a_Count times
|
||||
*/
|
||||
std::size_t FastForward(std::size_t a_Count);
|
||||
|
||||
/**
|
||||
* \brief Tries to recompute simulation if needed (for example in late command receival)
|
||||
*/
|
||||
void FastReplay();
|
||||
};
|
||||
|
||||
} // namespace sim
|
||||
} // namespace td
|
||||
27
include/td/simulation/CommandApply.h
Normal file
27
include/td/simulation/CommandApply.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/game/World.h>
|
||||
#include <td/protocol/command/Commands.h>
|
||||
|
||||
namespace td {
|
||||
namespace sim {
|
||||
|
||||
class CommandApply : public protocol::CommandHandler {
|
||||
private:
|
||||
const game::World& m_World;
|
||||
WorldSnapshot& m_Snapshot;
|
||||
|
||||
public:
|
||||
CommandApply(const game::World& a_World, WorldSnapshot& a_Snapshot);
|
||||
|
||||
virtual void Handle(const protocol::commands::EndCommand& a_End) override;
|
||||
virtual void Handle(const protocol::commands::PlaceTowerCommand& a_PlaceTower) override;
|
||||
virtual void Handle(const protocol::commands::PlayerJoinCommand& a_PlayerJoin) override;
|
||||
virtual void Handle(const protocol::commands::SpawnTroopCommand& a_SpawnTroop) override;
|
||||
virtual void Handle(const protocol::commands::TeamChangeCommand& a_TeamChange) override;
|
||||
virtual void Handle(const protocol::commands::UpgradeTowerCommand& a_UpgradeTower) override;
|
||||
virtual void Handle(const protocol::commands::UseItemCommand& a_UseItem) override;
|
||||
};
|
||||
|
||||
} // namespace sim
|
||||
} // namespace td
|
||||
41
include/td/simulation/ServerSimulation.h
Normal file
41
include/td/simulation/ServerSimulation.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <mutex>
|
||||
#include <td/game/World.h>
|
||||
|
||||
namespace td {
|
||||
namespace sim {
|
||||
|
||||
// TODO: OnEnd signal
|
||||
/**
|
||||
* \note This class is thread safe
|
||||
*/
|
||||
class ServerSimulation : public protocol::CommandHandler {
|
||||
private:
|
||||
std::mutex m_Mutex;
|
||||
game::World& m_World;
|
||||
std::uint64_t m_StepTime;
|
||||
std::uint64_t m_CurrentTime;
|
||||
std::vector<protocol::LockStep> m_History;
|
||||
|
||||
using protocol::CommandHandler::Handle;
|
||||
|
||||
public:
|
||||
ServerSimulation(game::World& a_World, std::uint64_t a_StepTime);
|
||||
|
||||
protocol::packets::LockStepsPacket Update();
|
||||
|
||||
protocol::packets::LockStepsPacket MakePacket();
|
||||
|
||||
std::vector<protocol::LockStep> GetFirstLocksteps();
|
||||
|
||||
// no checks are done !
|
||||
|
||||
virtual void Handle(const protocol::commands::SpawnTroopCommand& a_SpawnTroop) override;
|
||||
virtual void Handle(const protocol::commands::PlaceTowerCommand& a_PlaceTower) override;
|
||||
|
||||
protocol::packets::LockStepResponsePacket GetResponse(const protocol::packets::LockStepRequestPacket& a_Missing) const;
|
||||
};
|
||||
|
||||
} // namespace sim
|
||||
} // namespace td
|
||||
18
include/td/simulation/WorldSnapshot.h
Normal file
18
include/td/simulation/WorldSnapshot.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/game/WorldTypes.h>
|
||||
#include <td/game/Mobs.h>
|
||||
|
||||
namespace td {
|
||||
namespace sim {
|
||||
|
||||
struct WorldSnapshot {
|
||||
game::MobList m_Mobs;
|
||||
|
||||
game::TowerList m_Towers;
|
||||
|
||||
game::TeamList m_Teams;
|
||||
};
|
||||
|
||||
} // namespace sim
|
||||
} // namespace td
|
||||
41
include/td/simulation/WorldTicker.h
Normal file
41
include/td/simulation/WorldTicker.h
Normal file
@@ -0,0 +1,41 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/protocol/command/Commands.h>
|
||||
#include <td/simulation/WorldSnapshot.h>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
class World;
|
||||
}
|
||||
|
||||
namespace sim {
|
||||
|
||||
class IWorldSystem {
|
||||
public:
|
||||
virtual void Tick(const game::World& a_World, WorldSnapshot& a_State, FpFloat a_Delta) = 0;
|
||||
virtual ~IWorldSystem() = default;
|
||||
};
|
||||
|
||||
class WorldTicker {
|
||||
private:
|
||||
std::vector<std::unique_ptr<IWorldSystem>> m_Systems;
|
||||
|
||||
public:
|
||||
WorldTicker();
|
||||
|
||||
WorldSnapshot NextStep(
|
||||
const game::World& a_World, WorldSnapshot& a_PreviousState, const protocol::LockStep& a_LockStep, FpFloat a_Delta);
|
||||
|
||||
private:
|
||||
void ApplySteps(const game::World& a_World, WorldSnapshot& a_State, const protocol::LockStep& a_LockStep);
|
||||
void Tick(const game::World& a_World, WorldSnapshot& a_State, FpFloat a_Delta);
|
||||
WorldSnapshot CreateNext(WorldSnapshot& a_PreviousState);
|
||||
|
||||
template <typename T>
|
||||
void AddSystem() {
|
||||
m_Systems.push_back(std::make_unique<T>());
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace sim
|
||||
} // namespace td
|
||||
14
include/td/simulation/system/EntityMove.h
Normal file
14
include/td/simulation/system/EntityMove.h
Normal file
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/simulation/WorldTicker.h>
|
||||
|
||||
namespace td {
|
||||
namespace sim {
|
||||
|
||||
class EntityMove : public IWorldSystem {
|
||||
public:
|
||||
virtual void Tick(const game::World& a_World, WorldSnapshot& a_State, FpFloat a_Delta) override;
|
||||
};
|
||||
|
||||
} // namespace sim
|
||||
} // namespace td
|
||||
25
src/client/Client.cpp
Normal file
25
src/client/Client.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <client/Client.h>
|
||||
|
||||
#include <client/state/LoggingState.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
Client::Client(const std::shared_ptr<IClientSocket>& a_Socket) : m_Socket(a_Socket), m_Players(a_Socket) {
|
||||
// ChangeState<LoggingState>(a_PlayerName);
|
||||
}
|
||||
|
||||
Client::~Client() {
|
||||
Disconnect();
|
||||
}
|
||||
|
||||
void Client::SendPacket(const protocol::PacketBase& a_Packet) {
|
||||
m_Socket->Send(a_Packet);
|
||||
}
|
||||
|
||||
void Client::Disconnect() {
|
||||
m_Socket->Disconnect();
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
16
src/client/ClientState.cpp
Normal file
16
src/client/ClientState.cpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#include <client/ClientState.h>
|
||||
#include <client/Client.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
ClientState::ClientState(Client& a_Client) : Client::State(a_Client) {
|
||||
Connect(m_StateMachine.m_Socket->OnReceive, std::bind(&ClientState::HandleBase, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
void ClientState::SendPacket(const protocol::PacketBase& a_Packet) {
|
||||
m_StateMachine.m_Socket->Send(a_Packet);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
44
src/client/PlayerManager.cpp
Normal file
44
src/client/PlayerManager.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include <client/PlayerManager.h>
|
||||
|
||||
#include <client/IClientSocket.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
PlayerManager::PlayerManager(const std::shared_ptr<IClientSocket>& a_Socket) : m_Socket(a_Socket) {
|
||||
a_Socket->OnReceive.Connect(std::bind(&PlayerManager::HandleBase, this, std::placeholders::_1));
|
||||
OnPlayerJoin.Connect([this](const PlayerInfo& a_Player){
|
||||
std::cout << "[Client " << this << "] " << a_Player.m_PlayerName << " joined !\n";
|
||||
});
|
||||
OnPlayerLeave.Connect([this](const PlayerID a_Player){
|
||||
std::cout << "[Client " << this << "] " << GetPlayer(a_Player).m_PlayerName << "(" << +a_Player << ") left !\n";
|
||||
});
|
||||
}
|
||||
|
||||
PlayerManager::~PlayerManager() {}
|
||||
|
||||
PlayerInfo PlayerManager::GetPlayer(PlayerID a_Player) {
|
||||
return m_Players.at(a_Player);
|
||||
}
|
||||
|
||||
void PlayerManager::Handle(const protocol::packets::PlayerJoinPacket& a_Packet) {
|
||||
m_Players.emplace(a_Packet->m_Player.m_PlayerId, a_Packet->m_Player);
|
||||
OnPlayerJoin(a_Packet->m_Player);
|
||||
}
|
||||
|
||||
void PlayerManager::Handle(const protocol::packets::PlayerListPacket& a_Packet) {
|
||||
for (auto pInfo : a_Packet->m_Players) {
|
||||
m_Players.emplace(pInfo.m_PlayerId, pInfo);
|
||||
OnPlayerJoin(pInfo);
|
||||
}
|
||||
}
|
||||
|
||||
void PlayerManager::Handle(const protocol::packets::PlayerLeavePacket& a_Packet) {
|
||||
OnPlayerLeave(a_Packet->m_PlayerId);
|
||||
m_Players.erase(a_Packet->m_PlayerId);
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
23
src/client/socket/FakeSocket.cpp
Normal file
23
src/client/socket/FakeSocket.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <client/socket/FakeSocket.h>
|
||||
#include <server/socket/FakeSocket.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
void FakeSocket::Send(const protocol::PacketBase& a_Packet) {
|
||||
m_Server->OnReceivePeer(m_PeerId, a_Packet);
|
||||
}
|
||||
|
||||
std::shared_ptr<FakeSocket> FakeSocket::Connect(const std::shared_ptr<server::FakeSocket>& a_Server) {
|
||||
auto socket = std::make_shared<FakeSocket>(Private());
|
||||
socket->m_Server = a_Server;
|
||||
socket->m_PeerId = a_Server->ConnectFakePeer(socket);
|
||||
return socket;
|
||||
}
|
||||
|
||||
void FakeSocket::Disconnect() {
|
||||
m_Server->DisconnectFakePeer(m_PeerId);
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
26
src/client/state/GameState.cpp
Normal file
26
src/client/state/GameState.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <client/state/GameState.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
GameState::GameState(Client& a_Client, const std::shared_ptr<game::World>& a_World, std::uint64_t a_StepTime, const std::vector<protocol::LockStep> a_FirstSteps) :
|
||||
ClientState(a_Client), m_World(a_World), m_Simulation(a_World, a_StepTime, a_FirstSteps) {
|
||||
m_Simulation.OnMissingLockSteps.Connect([this](const std::vector<td::StepTime>& a_MissingSteps) {
|
||||
SendPacket(protocol::packets::LockStepRequestPacket(a_MissingSteps));
|
||||
});
|
||||
}
|
||||
|
||||
void GameState::Handle(const protocol::packets::LockStepsPacket& a_LockStep) {
|
||||
m_Simulation.Handle(a_LockStep);
|
||||
}
|
||||
|
||||
void GameState::Handle(const protocol::packets::LockStepResponsePacket& a_LockStep) {
|
||||
m_Simulation.Handle(a_LockStep);
|
||||
}
|
||||
|
||||
void GameState::Update(float a_Delta) {
|
||||
m_CurrentLerp = m_Simulation.Update(a_Delta);
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
27
src/client/state/LobbyState.cpp
Normal file
27
src/client/state/LobbyState.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <client/state/LobbyState.h>
|
||||
|
||||
#include <client/state/GameState.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
LobbyState::LobbyState(Client& a_Client) : ClientState(a_Client), m_World(std::make_shared<game::World>()) {}
|
||||
|
||||
LobbyState::~LobbyState() {}
|
||||
|
||||
void LobbyState::Handle(const protocol::packets::WorldHeaderPacket& a_Packet) {
|
||||
m_World->LoadMap(*a_Packet);
|
||||
}
|
||||
|
||||
void LobbyState::Handle(const protocol::packets::WorldDataPacket& a_Packet) {
|
||||
m_World->LoadMap(*a_Packet);
|
||||
}
|
||||
|
||||
void LobbyState::Handle(const protocol::packets::BeginGamePacket& a_Packet) {
|
||||
ChangeState<GameState>(m_World, STEP_TIME, a_Packet->m_FirstLocksteps);
|
||||
}
|
||||
|
||||
void LobbyState::Update(float a_Delta) {}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
27
src/client/state/LoggingState.cpp
Normal file
27
src/client/state/LoggingState.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <client/state/LoggingState.h>
|
||||
|
||||
#include <client/state/LobbyState.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
LoggingState::LoggingState(Client& a_Client, const std::string& a_PlayerName) : ClientState(a_Client) {
|
||||
SendPacket(td::protocol::packets::PlayerLoginPacket(a_PlayerName));
|
||||
}
|
||||
|
||||
void LoggingState::Update(float a_Delta) {
|
||||
}
|
||||
|
||||
LoggingState::~LoggingState() {}
|
||||
|
||||
void LoggingState::Handle(const protocol::packets::PlayerJoinPacket& a_Packet) {
|
||||
// TODO: check if id matches client id
|
||||
}
|
||||
|
||||
void LoggingState::Handle(const protocol::packets::LoggingSuccessPacket& a_Packet) {
|
||||
m_StateMachine.m_Id = a_Packet->m_PlayerId;
|
||||
ChangeState<LobbyState>();
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
27
src/main.cpp
27
src/main.cpp
@@ -1,21 +1,18 @@
|
||||
#include <iostream>
|
||||
// #include <td/protocol/packet/PacketVisitor.h>
|
||||
#include <td/game/GameHistory.h>
|
||||
#include <td/protocol/command/CommandDispatcher.h>
|
||||
#include <td/protocol/command/CommandFactory.h>
|
||||
#include <td/protocol/command/CommandSerializer.h>
|
||||
#include <td/protocol/command/CommandVisitor.h>
|
||||
|
||||
class Test : public td::protocol::CommandVisitor {};
|
||||
#include <chrono>
|
||||
#include <td/display/state/DebugWorldState.h>
|
||||
#include <td/misc/Time.h>
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
// Test visitor;
|
||||
// td::protocol::packets::ChatMessage chat{{"coucou"}};
|
||||
// visitor.Check(chat);
|
||||
// init GL context
|
||||
td::Display display(1920, 1080, "Tower-Defense 2");
|
||||
|
||||
display.ChangeState<td::DebugWorldState>();
|
||||
|
||||
td::Timer timer;
|
||||
while (!display.IsCloseRequested()) {
|
||||
display.PollEvents();
|
||||
display.Update(timer.GetDelta());
|
||||
}
|
||||
|
||||
td::protocol::commands::UpgradeTower com{{1, 2}};
|
||||
std::cout << (unsigned)com.GetType() << std::endl;
|
||||
td::protocol::CommandDispatcher disptacher;
|
||||
return 0;
|
||||
}
|
||||
|
||||
56
src/server/IServerSocket.cpp
Normal file
56
src/server/IServerSocket.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <server/IServerSocket.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
IServerSocket::IServerSocket() {
|
||||
}
|
||||
|
||||
void IServerSocket::RegisterHandler(const PlayerPacketHandler& a_Handler) {
|
||||
m_Handlers.push_back(a_Handler);
|
||||
}
|
||||
|
||||
void IServerSocket::UnregisterHandler(const PlayerPacketHandler& a_Handler) {
|
||||
auto it = std::find_if(m_Handlers.begin(), m_Handlers.end(),
|
||||
[&a_Handler](PlayerPacketHandler& handler) { return a_Handler.template target<PlayerPacketHandlerType>() == handler.template target<PlayerPacketHandlerType>(); });
|
||||
m_Handlers.erase(it);
|
||||
}
|
||||
|
||||
void IServerSocket::OnConnectPeer(PeerID a_PeerId) {
|
||||
// here, the client is not a player yet (we need to wait for auth)
|
||||
m_Ids.AddConnection(a_PeerId);
|
||||
OnPlayerConnect(m_Ids.GetPlayerId(a_PeerId));
|
||||
}
|
||||
|
||||
void IServerSocket::OnDisconnectPeer(PeerID a_PeerId) {
|
||||
OnPlayerDisconnect(m_Ids.GetPlayerId(a_PeerId));
|
||||
m_Ids.RemovePeer(a_PeerId);
|
||||
}
|
||||
|
||||
void IServerSocket::OnReceivePeer(PeerID a_PeerId, const protocol::PacketBase& a_Packet) {
|
||||
auto playerId = m_Ids.GetPlayerId(a_PeerId);
|
||||
|
||||
for (const auto& factory : m_Handlers) {
|
||||
auto handler = factory(playerId);
|
||||
a_Packet.Dispatch(*handler);
|
||||
}
|
||||
|
||||
OnReceive(playerId, a_Packet);
|
||||
}
|
||||
|
||||
void IServerSocket::Send(PlayerID a_PlayerId, const protocol::PacketBase& a_Packet) {
|
||||
SendPeer(m_Ids.GetPeerId(a_PlayerId), a_Packet);
|
||||
}
|
||||
|
||||
void IServerSocket::Broadcast(const protocol::PacketBase& a_Packet) {
|
||||
for (auto [peerId, playerId] : m_Ids) {
|
||||
SendPeer(peerId, a_Packet);
|
||||
}
|
||||
}
|
||||
|
||||
void IServerSocket::Disconnect(PlayerID a_PlayerId) {
|
||||
OnDisconnectPeer(m_Ids.GetPeerId(a_PlayerId));
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
62
src/server/PlayerManager.cpp
Normal file
62
src/server/PlayerManager.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <server/PlayerManager.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <server/IServerSocket.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
PlayerManager::ConnectionHandler::ConnectionHandler(PlayerManager& a_PlayerManager, PlayerID a_Player) :
|
||||
m_PlayerManager(a_PlayerManager), m_Player(a_Player) {}
|
||||
|
||||
void PlayerManager::ConnectionHandler::Handle(const protocol::packets::PlayerLoginPacket& a_Packet) {
|
||||
PlayerInfo pInfo{m_Player, a_Packet->m_PlayerName};
|
||||
|
||||
auto& socket = *m_PlayerManager.m_Socket;
|
||||
auto& players = m_PlayerManager.m_Players;
|
||||
|
||||
std::vector<PlayerInfo> playerInfos;
|
||||
playerInfos.reserve(players.size());
|
||||
for (auto& [id, player] : players) {
|
||||
playerInfos.push_back(player);
|
||||
}
|
||||
|
||||
socket.Send(m_Player, protocol::packets::LoggingSuccessPacket(m_Player));
|
||||
socket.Send(m_Player, protocol::packets::PlayerListPacket(playerInfos));
|
||||
socket.Broadcast(protocol::packets::PlayerJoinPacket(pInfo));
|
||||
|
||||
players.emplace(m_Player, pInfo);
|
||||
m_PlayerManager.OnPlayerJoin(m_Player, pInfo);
|
||||
std::cout << "[Server] " << a_Packet->m_PlayerName << " joined !\n";
|
||||
}
|
||||
|
||||
void PlayerManager::ConnectionHandler::Handle(const protocol::packets::DisconnectPacket& a_Packet) {
|
||||
m_PlayerManager.Disconnect(m_Player);
|
||||
}
|
||||
|
||||
PlayerManager::PlayerManager(const std::shared_ptr<IServerSocket>& a_Socket) : m_Socket(a_Socket) {
|
||||
a_Socket->RegisterHandler([this](PlayerID a_PlayerId) { return std::make_unique<ConnectionHandler>(*this, a_PlayerId); });
|
||||
a_Socket->OnPlayerDisconnect.Connect(std::bind(&PlayerManager::Disconnect, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
void PlayerManager::Disconnect(PlayerID a_Player) {
|
||||
if (!m_Players.contains(a_Player))
|
||||
return;
|
||||
std::cout << "[Server] " << +a_Player << " wants to disconnect !\n";
|
||||
m_Socket->Broadcast(protocol::packets::PlayerLeavePacket(a_Player));
|
||||
m_Players.erase(a_Player);
|
||||
}
|
||||
|
||||
PlayerManager::~PlayerManager() {}
|
||||
|
||||
void PlayerManager::RemovePlayer(PlayerID a_Player) {
|
||||
m_Socket->Disconnect(a_Player);
|
||||
Disconnect(a_Player);
|
||||
}
|
||||
|
||||
PlayerInfo PlayerManager::GetPlayer(PlayerID a_Player) {
|
||||
return m_Players.at(a_Player);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
22
src/server/Server.cpp
Normal file
22
src/server/Server.cpp
Normal file
@@ -0,0 +1,22 @@
|
||||
#include <server/Server.h>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include <server/state/LobbyState.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
Server::Server(const std::shared_ptr<IServerSocket>& a_Socket) : m_Socket(a_Socket), m_Players(a_Socket), m_LastMspt(0) {
|
||||
ChangeState<LobbyState>();
|
||||
}
|
||||
|
||||
void Server::Update(float a_Delta) {
|
||||
auto before = std::chrono::system_clock::now();
|
||||
StateMachine<Server, void, float>::Update(a_Delta);
|
||||
m_LastMspt = std::chrono::duration<float, std::chrono::milliseconds::period>(std::chrono::system_clock::now() - before).count();
|
||||
// std::cout << "Tick : " << m_LastMspt << "ms\n";
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
26
src/server/ServerState.cpp
Normal file
26
src/server/ServerState.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <server/Server.h>
|
||||
#include <server/ServerState.h>
|
||||
|
||||
#include <td/common/StateMachine.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
ServerState::ServerState(Server& a_Server) : Server::State(a_Server) {
|
||||
Connect(m_StateMachine.m_Socket->OnReceive, std::bind(&ServerState::HandlePacket, this, std::placeholders::_1, std::placeholders::_2));
|
||||
Connect(m_StateMachine.m_Players.OnPlayerJoin, std::bind(&ServerState::OnPlayerJoin, this, std::placeholders::_1, std::placeholders::_2));
|
||||
Connect(m_StateMachine.m_Players.OnPlayerLeave, std::bind(&ServerState::OnPlayerLeave, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
ServerState::~ServerState() {}
|
||||
|
||||
void ServerState::SendPacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) {
|
||||
m_StateMachine.m_Socket->Send(a_Id, a_Packet);
|
||||
}
|
||||
|
||||
void ServerState::BroadcastPacket(const protocol::PacketBase& a_Packet) {
|
||||
m_StateMachine.m_Socket->Broadcast(a_Packet);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user