Compare commits
79 Commits
b788caafa6
...
imguizmo
| Author | SHA1 | Date | |
|---|---|---|---|
|
3faf55884b
|
|||
|
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 | |||
| cd03175b98 |
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'
|
||||||
4
.gitignore
vendored
4
.gitignore
vendored
@@ -8,4 +8,6 @@ build/
|
|||||||
|
|
||||||
.vscode
|
.vscode
|
||||||
|
|
||||||
imgui.ini
|
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 🏗️👷⚙️
|
||||||
5
assets/stats.json
Normal file
5
assets/stats.json
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
{
|
||||||
|
"Zombie" : {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
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
|
||||||
10
include/td/Constants.h
Normal file
10
include/td/Constants.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
constexpr int TPS = 20;
|
||||||
|
constexpr float STEP_PERIOD = 1.0f / static_cast<float>(TPS);
|
||||||
|
|
||||||
|
} // namespace td
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cstdint>
|
#include <sp/common/DataBufferOperators.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
@@ -18,7 +18,7 @@ struct Vec2 {
|
|||||||
T g;
|
T g;
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr Vec2(T X = 0, T Y = 0) : x(X), y(Y) {}
|
constexpr Vec2(T X = T(0), T Y = T(0)) : x(X), y(Y) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@@ -176,8 +176,42 @@ Mat4f Look(const Vec3f& eye, const Vec3f& center, const Vec3f& up);
|
|||||||
|
|
||||||
Mat4f Inverse(const Mat4f& mat);
|
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
|
} // 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
|
} // namespace td
|
||||||
|
|||||||
@@ -2,11 +2,20 @@
|
|||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <fpm/fixed.hpp>
|
#include <fpm/fixed.hpp>
|
||||||
|
#include <td/Maths.h>
|
||||||
|
|
||||||
|
namespace sp {
|
||||||
|
class DataBuffer;
|
||||||
|
} // namespace sp
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
using FpFloat = fpm::fixed_16_16;
|
using FpFloat = fpm::fixed_16_16;
|
||||||
|
|
||||||
|
using StepTime = std::uint16_t;
|
||||||
|
|
||||||
|
constexpr int STEP_TIME = 50;
|
||||||
|
|
||||||
enum class TeamColor : std::int8_t {
|
enum class TeamColor : std::int8_t {
|
||||||
None = -1,
|
None = -1,
|
||||||
Blue,
|
Blue,
|
||||||
@@ -31,7 +40,7 @@ enum class EntityType : std::uint8_t {
|
|||||||
Zombie = 0,
|
Zombie = 0,
|
||||||
Spider,
|
Spider,
|
||||||
Pigman,
|
Pigman,
|
||||||
Skeleton,
|
Skelon,
|
||||||
Creeper,
|
Creeper,
|
||||||
Silverfish,
|
Silverfish,
|
||||||
Blaze,
|
Blaze,
|
||||||
@@ -56,15 +65,9 @@ using TowerID = std::uint16_t;
|
|||||||
using PlayerID = std::uint8_t;
|
using PlayerID = std::uint8_t;
|
||||||
using EntityID = std::uint16_t;
|
using EntityID = std::uint16_t;
|
||||||
|
|
||||||
struct TowerCoords {
|
using TowerCoords = Vec2<std::int16_t>;
|
||||||
std::int16_t x;
|
|
||||||
std::int16_t y;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct EntityCoords {
|
using EntityCoords = Vec2<FpFloat>;
|
||||||
FpFloat x;
|
|
||||||
FpFloat y;
|
|
||||||
};
|
|
||||||
|
|
||||||
using PeerID = std::uint16_t;
|
using PeerID = std::uint16_t;
|
||||||
|
|
||||||
@@ -75,4 +78,15 @@ enum class Direction : std::uint8_t {
|
|||||||
NegativeY = 1 << 3,
|
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
|
} // namespace td
|
||||||
|
|||||||
@@ -48,6 +48,10 @@ class Array {
|
|||||||
return m_Data[a_Index];
|
return m_Data[a_Index];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const T& operator[](std::size_t a_Index) const {
|
||||||
|
return m_Data[a_Index];
|
||||||
|
}
|
||||||
|
|
||||||
~Array() {
|
~Array() {
|
||||||
delete [] m_Data;
|
delete [] m_Data;
|
||||||
}
|
}
|
||||||
|
|||||||
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
|
||||||
@@ -2,34 +2,14 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#include <SDL3/SDL_keycode.h>
|
||||||
#include <SDL3/SDL_video.h>
|
#include <SDL3/SDL_video.h>
|
||||||
|
#include <td/common/StateMachine.h>
|
||||||
|
#include <td/misc/Signal.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
class Display {
|
class Display : public StateMachine<Display, void, float> {
|
||||||
public:
|
|
||||||
Display(int a_Width, int a_Height, const std::string& a_Title);
|
|
||||||
~Display();
|
|
||||||
|
|
||||||
void PollEvents();
|
|
||||||
void Update();
|
|
||||||
|
|
||||||
bool IsCloseRequested() {
|
|
||||||
return m_ShouldClose;
|
|
||||||
}
|
|
||||||
|
|
||||||
float GetAspectRatio() {
|
|
||||||
return m_AspectRatio;
|
|
||||||
}
|
|
||||||
|
|
||||||
int GetWindowWidth() {
|
|
||||||
return m_LastWidth;
|
|
||||||
}
|
|
||||||
|
|
||||||
int GetWindowHeight() {
|
|
||||||
return m_LastHeight;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SDL_Window* m_Window;
|
SDL_Window* m_Window;
|
||||||
SDL_GLContext m_GLContext;
|
SDL_GLContext m_GLContext;
|
||||||
@@ -38,6 +18,33 @@ class Display {
|
|||||||
float m_AspectRatio;
|
float m_AspectRatio;
|
||||||
|
|
||||||
bool m_ShouldClose;
|
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
|
} // 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
|
||||||
@@ -1,37 +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 = std::uint16_t;
|
|
||||||
|
|
||||||
std::vector<std::optional<protocol::LockStep>> m_History;
|
|
||||||
|
|
||||||
HistorySizeType m_Cursor;
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
const protocol::LockStep& GetNextStep();
|
|
||||||
|
|
||||||
bool HasNextStep() const;
|
|
||||||
|
|
||||||
void FromPacket(td::protocol::pdata::LockSteps&& a_Steps);
|
|
||||||
|
|
||||||
td::protocol::packets::LockStepsPacket ToPacket(HistorySizeType a_StartIndex);
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace game
|
|
||||||
} // namespace td
|
|
||||||
@@ -9,10 +9,14 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace td {
|
#include <sp/protocol/ConcreteMessage.h>
|
||||||
namespace game {
|
#include <sp/common/GenericHandler.h>
|
||||||
|
#include <sp/protocol/MessageFactory.h>
|
||||||
|
|
||||||
struct WalkableTile;
|
namespace td {
|
||||||
|
using Vec2fp = Vec2<FpFloat>;
|
||||||
|
|
||||||
|
namespace game {
|
||||||
|
|
||||||
enum class EffectType : std::uint8_t {
|
enum class EffectType : std::uint8_t {
|
||||||
Slowness = 0,
|
Slowness = 0,
|
||||||
@@ -62,151 +66,33 @@ const MobStats* GetMobStats(MobType type, std::uint8_t level);
|
|||||||
const TowerImmunities& GetMobTowerImmunities(MobType type, std::uint8_t level);
|
const TowerImmunities& GetMobTowerImmunities(MobType type, std::uint8_t level);
|
||||||
const EffectImmunities& GetMobEffectImmunities(MobType type, std::uint8_t level);
|
const EffectImmunities& GetMobEffectImmunities(MobType type, std::uint8_t level);
|
||||||
|
|
||||||
class Mob : public utils::shape::Rectangle {
|
class MobHandler;
|
||||||
protected:
|
|
||||||
float m_Health;
|
|
||||||
|
|
||||||
private:
|
struct MobData {};
|
||||||
|
|
||||||
|
class Mob : public sp::MessageBase<MobType, MobHandler> {
|
||||||
|
public:
|
||||||
MobID m_ID;
|
MobID m_ID;
|
||||||
PlayerID m_Sender;
|
|
||||||
MobLevel m_Level;
|
MobLevel m_Level;
|
||||||
|
PlayerID m_Sender;
|
||||||
|
float m_Health;
|
||||||
|
Vec2fp m_Position;
|
||||||
Direction m_Direction;
|
Direction m_Direction;
|
||||||
std::vector<EffectDuration> m_Effects;
|
std::vector<EffectDuration> m_Effects;
|
||||||
const Tower* m_LastDamage; // the last tower that damaged the mob
|
const Tower* m_LastDamage; // the last tower that damaged the mob
|
||||||
float m_HitCooldown;
|
float m_HitCooldown;
|
||||||
|
|
||||||
// utils::Timer m_EffectFireTimer;
|
|
||||||
// utils::Timer m_EffectPoisonTimer;
|
|
||||||
// utils::Timer m_EffectHealTimer;
|
|
||||||
|
|
||||||
TeamCastle* m_CastleTarget;
|
TeamCastle* m_CastleTarget;
|
||||||
// utils::CooldownTimer m_AttackTimer;
|
// utils::CooldownTimer m_AttackTimer;
|
||||||
|
|
||||||
public:
|
MobPtr m_Next;
|
||||||
Mob(MobID id, MobLevel level, PlayerID sender) : m_Sender(sender), m_Level(level), m_HitCooldown(0), m_CastleTarget(nullptr) {}
|
|
||||||
|
|
||||||
virtual ~Mob() {}
|
Mob() {}
|
||||||
|
|
||||||
virtual MobType GetType() const = 0;
|
Mob& operator=(const Mob& a_Other) = default;
|
||||||
|
|
||||||
virtual void Tick(std::uint64_t delta, World* world) {}
|
|
||||||
|
|
||||||
virtual bool OnDeath(World* world) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
MobID GetMobID() const {
|
|
||||||
return m_ID;
|
|
||||||
}
|
|
||||||
const TowerImmunities& GetTowerImmunities() const {
|
|
||||||
return GetMobTowerImmunities(GetType(), m_Level);
|
|
||||||
}
|
|
||||||
const EffectImmunities& GetEffectImmunities() const {
|
|
||||||
return GetMobEffectImmunities(GetType(), m_Level);
|
|
||||||
}
|
|
||||||
PlayerID GetSender() const {
|
|
||||||
return m_Sender;
|
|
||||||
}
|
|
||||||
MobLevel GetLevel() const {
|
|
||||||
return m_Level;
|
|
||||||
}
|
|
||||||
const MobStats* GetStats() const {
|
|
||||||
return GetMobStats(GetType(), m_Level);
|
|
||||||
}
|
|
||||||
void SetHealth(float newHealth) {
|
|
||||||
m_Health = newHealth;
|
|
||||||
}
|
|
||||||
float GetHealth() const {
|
|
||||||
return m_Health;
|
|
||||||
}
|
|
||||||
bool IsDead() const {
|
|
||||||
return m_Health <= 0;
|
|
||||||
}
|
|
||||||
bool IsAlive() const {
|
|
||||||
return m_Health > 0;
|
|
||||||
}
|
|
||||||
const Tower* GetLastDamageTower() {
|
|
||||||
return m_LastDamage;
|
|
||||||
}
|
|
||||||
bool HasReachedEnemyCastle() {
|
|
||||||
return m_CastleTarget != nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Damage(float dmg, const Tower* damager) {
|
|
||||||
m_Health = std::max(0.0f, m_Health - dmg);
|
|
||||||
m_LastDamage = damager;
|
|
||||||
m_HitCooldown = 0.1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Heal(float heal) {
|
|
||||||
m_Health = std::min(static_cast<float>(GetStats()->m_MaxLife), m_Health + heal);
|
|
||||||
}
|
|
||||||
|
|
||||||
void SetMobReachedCastle(TeamCastle* castle) {
|
|
||||||
m_CastleTarget = castle;
|
|
||||||
} // used when mob is in front of the castle
|
|
||||||
|
|
||||||
bool IsImmuneTo(TowerType type);
|
|
||||||
|
|
||||||
bool IsImmuneTo(EffectType type);
|
|
||||||
void AddEffect(EffectType type, float durationSec, Tower* tower);
|
|
||||||
bool HasEffect(EffectType type);
|
|
||||||
|
|
||||||
bool HasTakenDamage() {
|
|
||||||
return m_HitCooldown > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// returns a float between 0 and 1 excluded
|
|
||||||
float GetTileX() {
|
|
||||||
return GetCenterX() - static_cast<float>(static_cast<std::int32_t>(GetCenterX()));
|
|
||||||
}
|
|
||||||
|
|
||||||
// returns a float between 0 and 1 excluded
|
|
||||||
float GetTileY() {
|
|
||||||
return GetCenterY() - static_cast<float>(static_cast<std::int32_t>(GetCenterY()));
|
|
||||||
}
|
|
||||||
|
|
||||||
Direction GetDirection() const {
|
|
||||||
return m_Direction;
|
|
||||||
}
|
|
||||||
void SetDirection(Direction dir) {
|
|
||||||
m_Direction = dir;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void InitMob() {
|
|
||||||
m_Health = static_cast<float>(GetStats()->m_MaxLife);
|
|
||||||
SetSize(GetStats()->m_Size.x, GetStats()->m_Size.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
void UpdateEffects(std::uint64_t delta, World* world);
|
|
||||||
void AttackCastle(std::uint64_t delta, World* world);
|
|
||||||
void Move(std::uint64_t delta, World* world);
|
|
||||||
void Walk(std::uint64_t delta, World* world);
|
|
||||||
void MoveBack(const TeamCastle& castle, World* world);
|
|
||||||
void ChangeDirection(const WalkableTile& tile, World* world);
|
|
||||||
bool IsTouchingCastle(const TeamCastle& castle) const;
|
|
||||||
EffectDuration& GetEffect(EffectType type);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::shared_ptr<Mob> MobPtr;
|
template <MobType ID>
|
||||||
|
using ConcreteMob = sp::ConcreteMessage<MobData, Mob, ID>;
|
||||||
template <MobType MT>
|
|
||||||
class ConcreteMob : public Mob {
|
|
||||||
public:
|
|
||||||
ConcreteMob(MobID id, std::uint8_t level, PlayerID sender) : Mob(id, level, sender) {
|
|
||||||
InitMob();
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~ConcreteMob() {}
|
|
||||||
|
|
||||||
virtual void Tick(std::uint64_t delta, World* world) {}
|
|
||||||
|
|
||||||
virtual constexpr MobType GetType() const {
|
|
||||||
return MT;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
using Zombie = ConcreteMob<MobType::Zombie>;
|
using Zombie = ConcreteMob<MobType::Zombie>;
|
||||||
using Spider = ConcreteMob<MobType::Spider>;
|
using Spider = ConcreteMob<MobType::Spider>;
|
||||||
@@ -219,16 +105,17 @@ using Witch = ConcreteMob<MobType::Witch>;
|
|||||||
using Slime = ConcreteMob<MobType::Slime>;
|
using Slime = ConcreteMob<MobType::Slime>;
|
||||||
using Giant = ConcreteMob<MobType::Giant>;
|
using Giant = ConcreteMob<MobType::Giant>;
|
||||||
|
|
||||||
namespace MobFactory {
|
using AllMobs = std::tuple<Zombie, Spider, Skeleton, PigMan, Creeper, Silverfish, Blaze, Witch, Slime, Giant>;
|
||||||
|
|
||||||
MobPtr CreateMob(MobID id, MobType type, std::uint8_t level, PlayerID sender);
|
class MobHandler : public sp::GenericHandler<AllMobs> {};
|
||||||
std::string GetMobName(MobType type);
|
|
||||||
|
|
||||||
} // namespace MobFactory
|
using MobFactory = sp::MessageFactory<Mob, AllMobs>;
|
||||||
|
|
||||||
class MobListener {
|
class MobListener {
|
||||||
public:
|
public:
|
||||||
virtual void OnMobSpawn(Mob* mob) {}
|
virtual void OnMobSpawn(Mob* mob) {
|
||||||
|
MobHandler h;
|
||||||
|
}
|
||||||
virtual void OnMobDie(Mob* mob) {}
|
virtual void OnMobDie(Mob* mob) {}
|
||||||
|
|
||||||
virtual void OnMobDamage(Mob* target, float damage, Tower* damager) {}
|
virtual void OnMobDamage(Mob* target, float damage, Tower* damager) {}
|
||||||
@@ -237,6 +124,8 @@ class MobListener {
|
|||||||
virtual void OnMobCastleDamage(Mob* damager, TeamCastle* enemyCastle, float damage) {}
|
virtual void OnMobCastleDamage(Mob* damager, TeamCastle* enemyCastle, float damage) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using MobList = std::vector<MobPtr>;
|
||||||
|
|
||||||
// typedef utils::ObjectNotifier<MobListener> MobNotifier;
|
// typedef utils::ObjectNotifier<MobListener> MobNotifier;
|
||||||
|
|
||||||
} // namespace game
|
} // namespace game
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ class Spawn : public utils::shape::Rectangle {
|
|||||||
private:
|
private:
|
||||||
Direction m_Direction;
|
Direction m_Direction;
|
||||||
public:
|
public:
|
||||||
Spawn() {
|
Spawn() : m_Direction(Direction::PositiveX) {
|
||||||
SetWidth(5);
|
SetWidth(5);
|
||||||
SetHeight(5);
|
SetHeight(5);
|
||||||
}
|
}
|
||||||
@@ -31,23 +31,17 @@ class Team;
|
|||||||
|
|
||||||
class TeamCastle : public utils::shape::Rectangle {
|
class TeamCastle : public utils::shape::Rectangle {
|
||||||
private:
|
private:
|
||||||
const Team* m_Team;
|
|
||||||
float m_Life;
|
float m_Life;
|
||||||
public:
|
public:
|
||||||
static constexpr int CastleMaxLife = 1000;
|
static constexpr int CastleMaxLife = 1000;
|
||||||
|
|
||||||
TeamCastle(const Team* team) : m_Team(team), m_Life(CastleMaxLife) {
|
TeamCastle() : m_Life(CastleMaxLife) {
|
||||||
SetWidth(5);
|
SetWidth(5);
|
||||||
SetHeight(5);
|
SetHeight(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
TeamCastle() : TeamCastle(nullptr) {}
|
|
||||||
|
|
||||||
float GetLife() const { return m_Life; }
|
float GetLife() const { return m_Life; }
|
||||||
|
|
||||||
const Team* GetTeam() const { return m_Team; }
|
|
||||||
void SetTeam(const Team* team) { m_Team = team; }
|
|
||||||
|
|
||||||
void SetLife(float life) { m_Life = life; }
|
void SetLife(float life) { m_Life = life; }
|
||||||
void Damage(float damage) { m_Life = std::max(0.0f, m_Life - damage); }
|
void Damage(float damage) { m_Life = std::max(0.0f, m_Life - damage); }
|
||||||
|
|
||||||
@@ -80,7 +74,29 @@ public:
|
|||||||
std::uint8_t GetPlayerCount() const;
|
std::uint8_t GetPlayerCount() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::array<Team, 2> TeamList;
|
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 game
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -1,12 +1,16 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#include <td/misc/Shapes.h>
|
#include <td/misc/Shapes.h>
|
||||||
|
|
||||||
#include <td/Types.h>
|
#include <td/Types.h>
|
||||||
|
|
||||||
|
#include <sp/common/GenericHandler.h>
|
||||||
|
#include <sp/protocol/ConcreteMessage.h>
|
||||||
|
#include <sp/protocol/MessageFactory.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace game {
|
namespace game {
|
||||||
|
|
||||||
@@ -15,252 +19,129 @@ class Mob;
|
|||||||
|
|
||||||
typedef std::shared_ptr<Mob> MobPtr;
|
typedef std::shared_ptr<Mob> MobPtr;
|
||||||
|
|
||||||
enum class TowerType : std::uint8_t {
|
|
||||||
Archer = 0,
|
|
||||||
Ice,
|
|
||||||
Sorcerer,
|
|
||||||
Zeus,
|
|
||||||
Mage,
|
|
||||||
Artillery,
|
|
||||||
Quake,
|
|
||||||
Poison,
|
|
||||||
|
|
||||||
Leach,
|
|
||||||
Turret,
|
|
||||||
Necromancer,
|
|
||||||
|
|
||||||
TowerCount
|
|
||||||
};
|
|
||||||
|
|
||||||
enum class TowerSize : std::uint8_t {
|
enum class TowerSize : std::uint8_t {
|
||||||
Little = 3, // 3x3
|
Little = 3, // 3x3
|
||||||
Big = 5, // 5x5
|
Big = 5, // 5x5
|
||||||
};
|
};
|
||||||
|
|
||||||
enum class TowerPath : std::uint8_t {
|
enum class TowerPath : std::uint8_t {
|
||||||
Top = 0,
|
Top = 0,
|
||||||
Base, // Base Path
|
Base, // Base Path
|
||||||
Bottom
|
Bottom
|
||||||
};
|
};
|
||||||
|
|
||||||
class TowerStats {
|
class TowerStats {
|
||||||
private:
|
private:
|
||||||
float m_Rate;
|
float m_Rate;
|
||||||
float m_Damage;
|
float m_Damage;
|
||||||
std::uint8_t m_Range;
|
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; }
|
public:
|
||||||
float GetDamage() const { return m_Damage; }
|
TowerStats(float rate, float damage, std::uint8_t range) : m_Rate(rate), m_Damage(damage), m_Range(range) {}
|
||||||
std::uint8_t GetRange() const { return m_Range; }
|
|
||||||
|
float GetDamageRate() const {
|
||||||
|
return m_Rate;
|
||||||
|
}
|
||||||
|
float GetDamage() const {
|
||||||
|
return m_Damage;
|
||||||
|
}
|
||||||
|
std::uint8_t GetRange() const {
|
||||||
|
return m_Range;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class TowerLevel {
|
class TowerLevel {
|
||||||
private:
|
private:
|
||||||
// 1, 2, 3, 4
|
// 1, 2, 3, 4
|
||||||
std::uint8_t m_Level : 3;
|
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)
|
// 0 : base path 1 : top path (if there is bottom path) 2 : bottom path (if there is top path)
|
||||||
TowerPath m_Path : 2;
|
TowerPath m_Path : 2;
|
||||||
public:
|
|
||||||
|
public:
|
||||||
TowerLevel() : m_Level(1), m_Path(TowerPath::Base) {}
|
TowerLevel() : m_Level(1), m_Path(TowerPath::Base) {}
|
||||||
TowerLevel(std::uint8_t level, TowerPath path) : m_Level(level), m_Path(path) {}
|
TowerLevel(std::uint8_t level, TowerPath path) : m_Level(level), m_Path(path) {}
|
||||||
|
|
||||||
std::uint8_t GetLevel() const { return m_Level; }
|
std::uint8_t GetLevel() const {
|
||||||
TowerPath GetPath() const { return m_Path; }
|
return m_Level;
|
||||||
|
}
|
||||||
|
TowerPath GetPath() const {
|
||||||
|
return m_Path;
|
||||||
|
}
|
||||||
|
|
||||||
void SetLevel(std::uint8_t level) { m_Level = level; }
|
void SetLevel(std::uint8_t level) {
|
||||||
void SetPath(TowerPath path) { m_Path = path; }
|
m_Level = level;
|
||||||
|
}
|
||||||
|
void SetPath(TowerPath path) {
|
||||||
|
m_Path = path;
|
||||||
|
}
|
||||||
|
|
||||||
// operator to sort maps
|
// operator to sort maps
|
||||||
friend bool operator<(const TowerLevel& level, const TowerLevel& other) {
|
friend bool operator<(const TowerLevel& level, const TowerLevel& other) {
|
||||||
return level.GetLevel() + static_cast<std::uint8_t>(level.GetPath()) * 4 <
|
return level.GetLevel() + static_cast<std::uint8_t>(level.GetPath()) * 4 <
|
||||||
other.GetLevel() + static_cast<std::uint8_t>(other.GetPath()) * 4;
|
other.GetLevel() + static_cast<std::uint8_t>(other.GetPath()) * 4;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const TowerStats* GetTowerStats(TowerType type, TowerLevel level);
|
using TowerID = std::uint16_t;
|
||||||
|
|
||||||
typedef std::uint16_t TowerID;
|
class TowerHandler;
|
||||||
|
|
||||||
class Tower : public utils::shape::Circle {
|
class Tower : public utils::shape::Circle, public sp::MessageBase<TowerType, TowerHandler> {
|
||||||
private:
|
public:
|
||||||
TowerID m_ID;
|
TowerID m_ID;
|
||||||
TowerType m_Type;
|
|
||||||
TowerLevel m_Level{};
|
TowerLevel m_Level{};
|
||||||
PlayerID m_Builder;
|
PlayerID m_Builder;
|
||||||
protected:
|
|
||||||
// utils::CooldownTimer m_Timer;
|
public:
|
||||||
public:
|
Tower() : m_ID(0), m_Level({}), m_Builder(0) {}
|
||||||
Tower(TowerID id, TowerType type, std::int32_t x, std::int32_t y, PlayerID builder) : utils::shape::Circle(x + 0.5f, y + 0.5f, 0), m_ID(id), m_Type(type), m_Builder(builder)
|
|
||||||
// m_Timer(GetStats()->GetDamageRate() * 1000)
|
|
||||||
{ // converting seconds to millis
|
|
||||||
SetRadius(GetStats()->GetRange());
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual TowerType GetType() const = 0;
|
virtual TowerType GetType() const = 0;
|
||||||
virtual TowerSize GetSize() const = 0;
|
virtual TowerSize GetSize() const = 0;
|
||||||
virtual void Tick(std::uint64_t delta, World* world) = 0;
|
virtual void Tick(std::uint64_t delta, World* world) = 0;
|
||||||
|
};
|
||||||
|
|
||||||
void Upgrade(std::uint8_t level, TowerPath path) {
|
struct TowerData {};
|
||||||
m_Level.SetLevel(level);
|
|
||||||
m_Level.SetPath(path);
|
template <TowerType Type, TowerSize Size>
|
||||||
// m_Timer.SetCooldown(GetStats()->GetDamageRate() * 1000); // converting seconds to millis
|
class ConcreteTower : public sp::ConcreteMessage<TowerData, Tower, Type, false> {
|
||||||
// m_Timer.Reset();
|
public:
|
||||||
SetRadius(GetStats()->GetRange());
|
using HandlerType = typename sp::ConcreteMessage<TowerData, Tower, Type, false>::HandlerType;
|
||||||
|
|
||||||
|
virtual TowerSize GetSize() const override {
|
||||||
|
return Size;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::uint16_t GetID() const { return m_ID; }
|
virtual TowerType GetType() const override {
|
||||||
const TowerLevel& GetLevel() const { return m_Level; }
|
return Type;
|
||||||
const TowerStats* GetStats() const { return GetTowerStats(m_Type, m_Level); }
|
|
||||||
PlayerID GetBuilder() const { return m_Builder; }
|
|
||||||
|
|
||||||
bool IsMobInRange(MobPtr mob);
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef std::shared_ptr<Tower> TowerPtr;
|
|
||||||
|
|
||||||
namespace TowerFactory {
|
|
||||||
|
|
||||||
TowerPtr CreateTower(TowerType type, TowerID id, std::int32_t x, std::int32_t y, PlayerID builder);
|
|
||||||
std::string GetTowerName(TowerType type);
|
|
||||||
|
|
||||||
} // namespace TowerFactory
|
|
||||||
|
|
||||||
|
|
||||||
class TowerInfo {
|
|
||||||
private:
|
|
||||||
std::string m_Name, m_Description;
|
|
||||||
bool m_IsBigTower;
|
|
||||||
public:
|
|
||||||
TowerInfo(std::string&& name, std::string&& description, bool big) : m_Name(std::move(name)),
|
|
||||||
m_Description(std::move(description)), m_IsBigTower(big) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string& GetName() const { return m_Name; }
|
virtual void Tick(std::uint64_t delta, World* world) override {}
|
||||||
const std::string& GetDescription() const { return m_Description; }
|
|
||||||
|
|
||||||
bool IsBigTower() const { return m_IsBigTower; }
|
virtual void Dispatch(HandlerType& handler) const override {
|
||||||
|
handler.Handle(*this);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const TowerInfo& GetTowerInfo(TowerType type);
|
using TowerPtr = std::shared_ptr<Tower>;
|
||||||
|
|
||||||
// ---------- Little Towers ----------
|
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>;
|
||||||
|
|
||||||
class LittleTower : public Tower {
|
using AllTowers = std::tuple<ArcherTower, ArtilleryTower, IceTower, LeachTower, MageTower, NecromancerTower, PoisonTower, QuakeTower,
|
||||||
public:
|
SorcererTower, TurretTower, ZeusTower>;
|
||||||
LittleTower(TowerID id, TowerType type, std::uint16_t x, std::uint16_t y, PlayerID builder) : Tower(id, type, x, y, builder) {}
|
|
||||||
|
|
||||||
virtual TowerSize GetSize() const { return TowerSize::Little; }
|
using TowerFactory = sp::MessageFactory<Tower, AllTowers>;
|
||||||
|
|
||||||
virtual TowerType GetType() const = 0;
|
class TowerHandler : public sp::GenericHandler<AllTowers> {};
|
||||||
virtual void Tick(std::uint64_t delta, World* world) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ArcherTower : public LittleTower {
|
} // namespace game
|
||||||
public:
|
} // namespace td
|
||||||
ArcherTower(TowerID id, std::uint16_t x, std::uint16_t y, PlayerID builder) : LittleTower(id, GetType(), x, y, builder) {}
|
|
||||||
|
|
||||||
constexpr static float ExplosionRadius = 1.5f;
|
|
||||||
constexpr static float FireDurationSec = 10.0f;
|
|
||||||
|
|
||||||
virtual TowerType GetType() const { return TowerType::Archer; }
|
|
||||||
virtual void Tick(std::uint64_t delta, World* world);
|
|
||||||
};
|
|
||||||
|
|
||||||
class IceTower : public LittleTower {
|
|
||||||
public:
|
|
||||||
IceTower(TowerID id, std::uint16_t x, std::uint16_t y, PlayerID builder) : LittleTower(id, GetType(), x, y, builder) {}
|
|
||||||
|
|
||||||
virtual TowerType GetType() const { return TowerType::Ice; }
|
|
||||||
virtual void Tick(std::uint64_t delta, World* world);
|
|
||||||
};
|
|
||||||
|
|
||||||
class MageTower : public LittleTower {
|
|
||||||
public:
|
|
||||||
MageTower(TowerID id, std::uint16_t x, std::uint16_t y, PlayerID builder) : LittleTower(id, GetType(), x, y, builder) {}
|
|
||||||
|
|
||||||
virtual TowerType GetType() const { return TowerType::Mage; }
|
|
||||||
virtual void Tick(std::uint64_t delta, World* world);
|
|
||||||
};
|
|
||||||
|
|
||||||
class PoisonTower : public LittleTower {
|
|
||||||
public:
|
|
||||||
PoisonTower(TowerID id, std::uint16_t x, std::uint16_t y, PlayerID builder) : LittleTower(id, GetType(), x, y, builder) {}
|
|
||||||
|
|
||||||
virtual TowerType GetType() const { return TowerType::Poison; }
|
|
||||||
virtual void Tick(std::uint64_t delta, World* world);
|
|
||||||
};
|
|
||||||
|
|
||||||
class QuakeTower : public LittleTower {
|
|
||||||
public:
|
|
||||||
QuakeTower(TowerID id, std::uint16_t x, std::uint16_t y, PlayerID builder) : LittleTower(id, GetType(), x, y, builder) {}
|
|
||||||
|
|
||||||
virtual TowerType GetType() const { return TowerType::Quake; }
|
|
||||||
virtual void Tick(std::uint64_t delta, World* world);
|
|
||||||
};
|
|
||||||
|
|
||||||
class ArtilleryTower : public LittleTower {
|
|
||||||
public:
|
|
||||||
ArtilleryTower(TowerID id, std::uint16_t x, std::uint16_t y, PlayerID builder) : LittleTower(id, GetType(), x, y, builder) {}
|
|
||||||
|
|
||||||
virtual TowerType GetType() const { return TowerType::Artillery; }
|
|
||||||
virtual void Tick(std::uint64_t delta, World* world);
|
|
||||||
};
|
|
||||||
|
|
||||||
class SorcererTower : public LittleTower {
|
|
||||||
public:
|
|
||||||
SorcererTower(TowerID id, std::uint16_t x, std::uint16_t y, PlayerID builder) : LittleTower(id, GetType(), x, y, builder) {}
|
|
||||||
|
|
||||||
virtual TowerType GetType() const { return TowerType::Sorcerer; }
|
|
||||||
virtual void Tick(std::uint64_t delta, World* world);
|
|
||||||
};
|
|
||||||
|
|
||||||
class ZeusTower : public LittleTower {
|
|
||||||
public:
|
|
||||||
ZeusTower(TowerID id, std::uint16_t x, std::uint16_t y, PlayerID builder) : LittleTower(id, GetType(), x, y, builder) {}
|
|
||||||
|
|
||||||
virtual TowerType GetType() const { return TowerType::Zeus; }
|
|
||||||
virtual void Tick(std::uint64_t delta, World* world);
|
|
||||||
};
|
|
||||||
|
|
||||||
// ---------- Big Towers ----------
|
|
||||||
|
|
||||||
class BigTower : public Tower {
|
|
||||||
public:
|
|
||||||
BigTower(TowerID id, TowerType type, std::uint16_t x, std::uint16_t y, PlayerID builder) : Tower(id, type, x, y, builder) {}
|
|
||||||
|
|
||||||
virtual TowerSize GetSize() const { return TowerSize::Big; }
|
|
||||||
|
|
||||||
virtual TowerType GetType() const = 0;
|
|
||||||
virtual void Tick(std::uint64_t delta, World* world) = 0;
|
|
||||||
};
|
|
||||||
|
|
||||||
class TurretTower : public BigTower {
|
|
||||||
public:
|
|
||||||
TurretTower(TowerID id, std::uint16_t x, std::uint16_t y, PlayerID builder) : BigTower(id, GetType(), x, y, builder) {}
|
|
||||||
|
|
||||||
virtual TowerType GetType() const { return TowerType::Turret; }
|
|
||||||
virtual void Tick(std::uint64_t delta, World* world);
|
|
||||||
};
|
|
||||||
|
|
||||||
class NecromancerTower : public BigTower {
|
|
||||||
public:
|
|
||||||
NecromancerTower(TowerID id, std::uint16_t x, std::uint16_t y, PlayerID builder) : BigTower(id, GetType(), x, y, builder) {}
|
|
||||||
|
|
||||||
virtual TowerType GetType() const { return TowerType::Necromancer; }
|
|
||||||
virtual void Tick(std::uint64_t delta, World* world);
|
|
||||||
};
|
|
||||||
|
|
||||||
class LeachTower : public BigTower {
|
|
||||||
public:
|
|
||||||
LeachTower(TowerID id, std::uint16_t x, std::uint16_t y, PlayerID builder) : BigTower(id, GetType(), x, y, builder) {}
|
|
||||||
|
|
||||||
virtual TowerType GetType() const { return TowerType::Leach; }
|
|
||||||
virtual void Tick(std::uint64_t delta, World* world);
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace game
|
|
||||||
} // namespace td
|
|
||||||
|
|||||||
@@ -2,40 +2,43 @@
|
|||||||
|
|
||||||
#include <td/game/WorldTypes.h>
|
#include <td/game/WorldTypes.h>
|
||||||
#include <td/protocol/packet/Packets.h>
|
#include <td/protocol/packet/Packets.h>
|
||||||
|
#include <td/simulation/WorldTicker.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace game {
|
namespace game {
|
||||||
|
|
||||||
class World {
|
class World {
|
||||||
protected:
|
protected:
|
||||||
|
// header
|
||||||
TowerTileColorPalette m_TowerPlacePalette;
|
TowerTileColorPalette m_TowerPlacePalette;
|
||||||
Color m_WalkablePalette;
|
Color m_WalkablePalette;
|
||||||
std::vector<Color> m_DecorationPalette;
|
std::vector<Color> m_DecorationPalette;
|
||||||
Color m_Background;
|
Color m_Background;
|
||||||
|
|
||||||
std::unordered_map<ChunkCoord, ChunkPtr> m_Chunks;
|
|
||||||
|
|
||||||
SpawnColorPalette m_SpawnColorPalette;
|
SpawnColorPalette m_SpawnColorPalette;
|
||||||
|
|
||||||
TilePalette m_TilePalette;
|
TilePalette m_TilePalette;
|
||||||
|
|
||||||
MobList m_Mobs;
|
//data
|
||||||
|
ChunkList m_Chunks;
|
||||||
|
|
||||||
TowerList m_Towers;
|
std::shared_ptr<sim::WorldSnapshot> m_CurrentState;
|
||||||
|
std::shared_ptr<sim::WorldSnapshot> m_NextState;
|
||||||
|
|
||||||
TeamList m_Teams;
|
private:
|
||||||
|
sim::WorldTicker m_Ticker;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
World();
|
World();
|
||||||
|
World(World&&) = default;
|
||||||
|
|
||||||
bool LoadMap(const protocol::pdata::WorldHeader& worldHeader);
|
bool LoadMap(const protocol::pdata::WorldHeader& worldHeader);
|
||||||
bool LoadMap(const protocol::pdata::WorldData& worldData);
|
bool LoadMap(const protocol::pdata::WorldData& worldData);
|
||||||
|
|
||||||
|
protocol::packets::WorldHeaderPacket GetPacketHeader() const;
|
||||||
|
protocol::packets::WorldDataPacket GetPacketData() const;
|
||||||
|
|
||||||
bool LoadMapFromFile(const std::string& fileName);
|
bool LoadMapFromFile(const std::string& fileName);
|
||||||
bool SaveMap(const std::string& fileName) const;
|
bool SaveMap(const std::string& fileName) const;
|
||||||
|
|
||||||
void Tick(std::uint64_t delta);
|
|
||||||
|
|
||||||
void SpawnMobAt(MobID id, MobType type, std::uint8_t level, PlayerID sender, float x, float y, Direction dir);
|
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 PlaceTowerAt(TowerID id, TowerType type, std::int32_t x, std::int32_t y, PlayerID builder);
|
||||||
@@ -62,7 +65,7 @@ class World {
|
|||||||
|
|
||||||
TilePtr GetTilePtr(TileIndex index) const {
|
TilePtr GetTilePtr(TileIndex index) const {
|
||||||
if (index == 0)
|
if (index == 0)
|
||||||
return nullptr;
|
return TilePtr(nullptr);
|
||||||
return m_TilePalette.at(index - 1);
|
return m_TilePalette.at(index - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -71,7 +74,7 @@ class World {
|
|||||||
|
|
||||||
TowerPtr GetTower(const Vec2f& position) const; // returns null if no tower is here
|
TowerPtr GetTower(const Vec2f& position) const; // returns null if no tower is here
|
||||||
|
|
||||||
const std::unordered_map<ChunkCoord, ChunkPtr>& GetChunks() const {
|
const ChunkList& GetChunks() const {
|
||||||
return m_Chunks;
|
return m_Chunks;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,55 +86,57 @@ class World {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const MobList& GetMobList() const {
|
const MobList& GetMobList() const {
|
||||||
return m_Mobs;
|
return m_CurrentState->m_Mobs;
|
||||||
}
|
}
|
||||||
MobList& GetMobList() {
|
MobList& GetMobList() {
|
||||||
return m_Mobs;
|
return m_CurrentState->m_Mobs;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Color* GetTileColor(TilePtr tile) const;
|
const Color* GetTileColor(const TilePtr& tile) const;
|
||||||
|
|
||||||
Team& GetRedTeam() {
|
Team& GetRedTeam() {
|
||||||
return m_Teams[static_cast<std::uint8_t>(TeamColor::Red)];
|
return m_CurrentState->m_Teams[TeamColor::Red];
|
||||||
}
|
}
|
||||||
const Team& GetRedTeam() const {
|
const Team& GetRedTeam() const {
|
||||||
return m_Teams[static_cast<std::uint8_t>(TeamColor::Red)];
|
return m_CurrentState->m_Teams[TeamColor::Red];
|
||||||
}
|
}
|
||||||
|
|
||||||
Team& GetBlueTeam() {
|
Team& GetBlueTeam() {
|
||||||
return m_Teams[static_cast<std::uint8_t>(TeamColor::Blue)];
|
return m_CurrentState->m_Teams[TeamColor::Blue];
|
||||||
}
|
}
|
||||||
const Team& GetBlueTeam() const {
|
const Team& GetBlueTeam() const {
|
||||||
return m_Teams[static_cast<std::uint8_t>(TeamColor::Red)];
|
return m_CurrentState->m_Teams[TeamColor::Blue];
|
||||||
}
|
}
|
||||||
|
|
||||||
Team& GetTeam(TeamColor team) {
|
Team& GetTeam(TeamColor team) {
|
||||||
return m_Teams[static_cast<std::uint8_t>(team)];
|
return m_CurrentState->m_Teams[team];
|
||||||
}
|
}
|
||||||
const Team& GetTeam(TeamColor team) const {
|
const Team& GetTeam(TeamColor team) const {
|
||||||
return m_Teams[static_cast<std::uint8_t>(team)];
|
return m_CurrentState->m_Teams[team];
|
||||||
}
|
}
|
||||||
|
|
||||||
const TeamList& GetTeams() const {
|
const TeamList& GetTeams() const {
|
||||||
return m_Teams;
|
return m_CurrentState->m_Teams;
|
||||||
}
|
}
|
||||||
|
|
||||||
const TowerList& GetTowers() const {
|
const TowerList& GetTowers() const {
|
||||||
return m_Towers;
|
return m_CurrentState->m_Towers;
|
||||||
}
|
}
|
||||||
|
|
||||||
TowerPtr GetTowerById(TowerID tower);
|
TowerPtr GetTowerById(TowerID tower);
|
||||||
|
|
||||||
const Player* GetPlayerById(PlayerID id) const;
|
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:
|
private:
|
||||||
void TickMobs(std::uint64_t delta);
|
void TickMobs(std::uint64_t delta);
|
||||||
void CleanDeadMobs();
|
void CleanDeadMobs();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
using WorldPtr = std::shared_ptr<World>;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace game
|
} // namespace game
|
||||||
} // namespace td
|
} // namespace td
|
||||||
@@ -1,20 +1,15 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <td/Maths.h>
|
#include <td/Maths.h>
|
||||||
#include <td/game/Mobs.h>
|
|
||||||
#include <td/game/Team.h>
|
#include <td/game/Team.h>
|
||||||
#include <td/game/Towers.h>
|
#include <td/game/Towers.h>
|
||||||
|
|
||||||
|
#include <sp/io/SerializableMessage.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace game {
|
namespace game {
|
||||||
struct ChunkCoord {
|
|
||||||
std::int16_t x, y;
|
|
||||||
|
|
||||||
friend bool operator==(const td::game::ChunkCoord& first, const td::game::ChunkCoord& other) {
|
|
||||||
return first.x == other.x && first.y == other.y;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
using ChunkCoord = Vec2<std::int16_t>;
|
||||||
|
|
||||||
class Game;
|
class Game;
|
||||||
|
|
||||||
@@ -37,36 +32,44 @@ static constexpr Color RED{255, 0, 0};
|
|||||||
static constexpr Color GREEN{0, 255, 0};
|
static constexpr Color GREEN{0, 255, 0};
|
||||||
static constexpr Color BLUE{0, 0, 255};
|
static constexpr Color BLUE{0, 0, 255};
|
||||||
|
|
||||||
struct Tile {
|
class TileHandler;
|
||||||
virtual TileType GetType() const = 0;
|
|
||||||
|
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 TowerTile : Tile {
|
struct WalkableTileData {
|
||||||
std::uint8_t color_palette_ref;
|
Direction m_Direction;
|
||||||
TeamColor team_owner;
|
|
||||||
|
|
||||||
virtual TileType GetType() const {
|
|
||||||
return TileType::Tower;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct WalkableTile : Tile {
|
struct DecorationTileData {
|
||||||
Direction direction;
|
std::uint16_t m_ColorPaletteRef;
|
||||||
|
|
||||||
virtual TileType GetType() const {
|
|
||||||
return TileType::Walk;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
} // namespace data
|
||||||
|
|
||||||
struct DecorationTile : Tile {
|
using EmptyTile = ConcreteTile<TileType::None, data::EmptyData>;
|
||||||
std::uint16_t color_palette_ref;
|
using TowerTile = ConcreteTile<TileType::Tower, data::TowerTileData>;
|
||||||
|
using WalkableTile = ConcreteTile<TileType::Walk, data::WalkableTileData>;
|
||||||
|
using DecorationTile = ConcreteTile<TileType::Decoration, data::DecorationTileData>;
|
||||||
|
|
||||||
virtual TileType GetType() const {
|
using AllTiles = std::tuple<EmptyTile, TowerTile, WalkableTile, DecorationTile>;
|
||||||
return TileType::Decoration;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef std::shared_ptr<Tile> TilePtr;
|
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::vector<std::uint16_t> ChunkPalette;
|
||||||
|
|
||||||
typedef std::shared_ptr<WalkableTile> WalkableTilePtr;
|
typedef std::shared_ptr<WalkableTile> WalkableTilePtr;
|
||||||
@@ -76,18 +79,14 @@ typedef std::uint32_t TileIndex;
|
|||||||
// 32 x 32 area
|
// 32 x 32 area
|
||||||
struct Chunk {
|
struct Chunk {
|
||||||
enum { ChunkWidth = 32, ChunkHeight = 32, ChunkSize = ChunkWidth * ChunkHeight };
|
enum { ChunkWidth = 32, ChunkHeight = 32, ChunkSize = ChunkWidth * ChunkHeight };
|
||||||
typedef std::array<std::uint16_t, ChunkSize> ChunkData;
|
using ChunkData = std::array<std::uint16_t, ChunkSize>;
|
||||||
|
using ChunkPackedData = std::vector<uint64_t>;
|
||||||
|
|
||||||
// stores index of tile palette
|
// stores index of tile palette
|
||||||
ChunkData tiles{0};
|
ChunkPalette m_Palette;
|
||||||
ChunkPalette palette;
|
ChunkPackedData m_Data;
|
||||||
|
|
||||||
TileIndex GetTileIndex(std::uint16_t tileNumber) const {
|
TileIndex GetTileIndex(std::uint16_t tileNumber) const;
|
||||||
TileIndex chunkPaletteIndex = tiles.at(tileNumber);
|
|
||||||
if (chunkPaletteIndex == 0) // index 0 means empty tile index 1 = first tile
|
|
||||||
return 0;
|
|
||||||
return palette.at(chunkPaletteIndex);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef std::shared_ptr<Chunk> ChunkPtr;
|
typedef std::shared_ptr<Chunk> ChunkPtr;
|
||||||
@@ -96,12 +95,11 @@ typedef std::array<Color, 2> TowerTileColorPalette;
|
|||||||
|
|
||||||
typedef std::vector<TilePtr> TilePalette;
|
typedef std::vector<TilePtr> TilePalette;
|
||||||
|
|
||||||
typedef std::vector<MobPtr> MobList;
|
|
||||||
|
|
||||||
typedef std::array<Color, 2> SpawnColorPalette;
|
typedef std::array<Color, 2> SpawnColorPalette;
|
||||||
|
|
||||||
typedef std::vector<TowerPtr> TowerList;
|
typedef std::vector<TowerPtr> TowerList;
|
||||||
|
|
||||||
|
using ChunkList = std::unordered_map<ChunkCoord, ChunkPtr>;
|
||||||
|
|
||||||
} // namespace game
|
} // namespace game
|
||||||
|
|
||||||
@@ -116,4 +114,4 @@ struct hash<td::game::ChunkCoord> {
|
|||||||
return std::hash<std::int16_t>()(key.x << 16 | key.y);
|
return std::hash<std::int16_t>()(key.x << 16 | key.y);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
} // namespace std
|
} // namespace std
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ private:
|
|||||||
Point m_Center;
|
Point m_Center;
|
||||||
float m_Width, m_Height;
|
float m_Width, m_Height;
|
||||||
public:
|
public:
|
||||||
Rectangle() {}
|
Rectangle() : m_Center(), m_Width(0), m_Height(0) {}
|
||||||
|
|
||||||
const Point& GetCenter() const { return m_Center; }
|
const Point& GetCenter() const { return m_Center; }
|
||||||
float GetCenterX() const { return m_Center.GetX(); }
|
float GetCenterX() const { return m_Center.GetX(); }
|
||||||
@@ -92,5 +92,5 @@ public:
|
|||||||
};
|
};
|
||||||
|
|
||||||
} // namespace shape
|
} // namespace shape
|
||||||
} // namespace utils
|
} // namespace utils
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -2,30 +2,104 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
#include <td/common/NonCopyable.h>
|
#include <td/common/NonCopyable.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace utils {
|
namespace utils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief Signal class
|
||||||
|
*/
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
class Signal : private NonCopyable {
|
class SignalRaw : private NonCopyable {
|
||||||
private:
|
public:
|
||||||
using CallBack = std::function<void(Args...)>;
|
using FnType = void(Args...);
|
||||||
|
using CallBack = std::shared_ptr<std::function<FnType>>;
|
||||||
|
|
||||||
|
private:
|
||||||
std::vector<CallBack> m_Callbacks;
|
std::vector<CallBack> m_Callbacks;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void Connect(CallBack&& a_Callback) {
|
void Connect(const CallBack& a_Callback) {
|
||||||
m_Callbacks.push_back(std::move(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 {
|
void operator()(Args... args) const {
|
||||||
for (const CallBack& callback : m_Callbacks) {
|
for (const CallBack& callback : m_Callbacks) {
|
||||||
callback(args...);
|
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 utils
|
||||||
} // namespace td
|
} // 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
include/td/network/EnetClient.h
Normal file
1
include/td/network/EnetClient.h
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#pragma once
|
||||||
1
include/td/network/EnetConnection.h
Normal file
1
include/td/network/EnetConnection.h
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#pragma once
|
||||||
1
include/td/network/EnetServer.h
Normal file
1
include/td/network/EnetServer.h
Normal file
@@ -0,0 +1 @@
|
|||||||
|
#pragma once
|
||||||
@@ -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
|
|
||||||
@@ -6,14 +6,16 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <sp/common/GenericHandler.h>
|
||||||
#include <sp/protocol/ConcreteMessage.h>
|
#include <sp/protocol/ConcreteMessage.h>
|
||||||
#include <sp/protocol/MessageDispatcher.h>
|
#include <sp/protocol/MessageDispatcher.h>
|
||||||
#include <sp/protocol/MessageFactory.h>
|
#include <sp/protocol/MessageFactory.h>
|
||||||
#include <sp/protocol/MessageHandler.h>
|
|
||||||
#include <td/Types.h>
|
#include <td/Types.h>
|
||||||
#include <td/common/NonCopyable.h>
|
#include <td/common/NonCopyable.h>
|
||||||
#include <td/protocol/command/CommandData.h>
|
#include <td/protocol/command/CommandData.h>
|
||||||
|
|
||||||
|
#include <sp/io/SerializableMessage.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace protocol {
|
namespace protocol {
|
||||||
|
|
||||||
@@ -33,7 +35,7 @@ class CommandHandler;
|
|||||||
using CommandBase = sp::MessageBase<CommandID, CommandHandler>;
|
using CommandBase = sp::MessageBase<CommandID, CommandHandler>;
|
||||||
|
|
||||||
template <typename TData, CommandID ID>
|
template <typename TData, CommandID ID>
|
||||||
using CommandMessage = sp::ConcreteMessage<TData, CommandID, ID, CommandHandler>;
|
using CommandMessage = sp::ConcreteMessage<TData, CommandBase, ID>;
|
||||||
|
|
||||||
|
|
||||||
namespace commands {
|
namespace commands {
|
||||||
@@ -51,13 +53,15 @@ using UseItemCommand = CommandMessage<cdata::UseItem, CommandID::UseItem>;
|
|||||||
using AllCommands = std::tuple<commands::EndCommand, commands::PlaceTowerCommand, commands::PlayerJoinCommand,
|
using AllCommands = std::tuple<commands::EndCommand, commands::PlaceTowerCommand, commands::PlayerJoinCommand,
|
||||||
commands::SpawnTroopCommand, commands::TeamChangeCommand, commands::UpgradeTowerCommand, commands::UseItemCommand>;
|
commands::SpawnTroopCommand, commands::TeamChangeCommand, commands::UpgradeTowerCommand, commands::UseItemCommand>;
|
||||||
|
|
||||||
class CommandHandler : public sp::MessageHandler<AllCommands> {};
|
class CommandHandler : public sp::GenericHandler<AllCommands> {};
|
||||||
|
|
||||||
using CommandDispatcher = sp::MessageDispatcher<CommandBase>;
|
using CommandDispatcher = sp::MessageDispatcher<CommandBase>;
|
||||||
|
|
||||||
using CommandFactory = sp::MessageFactory<CommandBase, AllCommands>;
|
using CommandFactory = sp::MessageFactory<CommandBase, AllCommands>;
|
||||||
|
|
||||||
using LockStep = std::vector<std::shared_ptr<CommandBase>>;
|
using CommandPtr = sp::SerializableMessage<CommandFactory>;
|
||||||
|
|
||||||
|
using LockStep = std::vector<CommandPtr>;
|
||||||
|
|
||||||
} // namespace protocol
|
} // namespace protocol
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -7,32 +7,31 @@
|
|||||||
#include <td/game/WorldTypes.h>
|
#include <td/game/WorldTypes.h>
|
||||||
|
|
||||||
// Make it dynamic ?
|
// Make it dynamic ?
|
||||||
|
#ifdef NDEBUG
|
||||||
#define LOCKSTEP_BUFFER_SIZE 10
|
#define LOCKSTEP_BUFFER_SIZE 10
|
||||||
|
#else
|
||||||
|
#define LOCKSTEP_BUFFER_SIZE 1
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace protocol {
|
namespace protocol {
|
||||||
|
|
||||||
struct PlayerInfo {
|
|
||||||
PlayerID m_PlayerId;
|
|
||||||
std::string m_PlayerName;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct MapData {
|
struct MapData {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace pdata {
|
namespace pdata {
|
||||||
|
|
||||||
/** Client attempts to login (very basic) */
|
|
||||||
struct PlayerLogin {
|
|
||||||
std::string m_PlayerName;
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Server indicates success */
|
/** Server indicates success */
|
||||||
struct LoggingSuccess {
|
struct LoggingSuccess {
|
||||||
PlayerID m_PlayerId;
|
PlayerID m_PlayerId;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Client attempts to login (very basic) */
|
||||||
|
struct PlayerLogin {
|
||||||
|
std::string m_PlayerName;
|
||||||
|
};
|
||||||
|
|
||||||
/** Player joins the lobby */
|
/** Player joins the lobby */
|
||||||
struct PlayerJoin {
|
struct PlayerJoin {
|
||||||
PlayerInfo m_Player;
|
PlayerInfo m_Player;
|
||||||
@@ -43,6 +42,16 @@ struct PlayerLeave {
|
|||||||
PlayerID m_PlayerId;
|
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 */
|
/** Keep alive */
|
||||||
struct KeepAlive {
|
struct KeepAlive {
|
||||||
std::uint64_t m_KeepAliveId;
|
std::uint64_t m_KeepAliveId;
|
||||||
@@ -58,8 +67,6 @@ struct ChatMessage {
|
|||||||
std::string m_Text;
|
std::string m_Text;
|
||||||
};
|
};
|
||||||
|
|
||||||
// TODO: handle players joining in the first second
|
|
||||||
|
|
||||||
struct BeginGame {
|
struct BeginGame {
|
||||||
std::vector<PlayerInfo> m_BlueTeam;
|
std::vector<PlayerInfo> m_BlueTeam;
|
||||||
std::vector<PlayerInfo> m_RedTeam;
|
std::vector<PlayerInfo> m_RedTeam;
|
||||||
@@ -68,8 +75,8 @@ struct BeginGame {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct LockSteps {
|
struct LockSteps {
|
||||||
std::uint16_t m_FirstFrameNumber;
|
StepTime m_FirstFrameNumber;
|
||||||
Array<LockStep, LOCKSTEP_BUFFER_SIZE> m_LockSteps;
|
std::array<LockStep, LOCKSTEP_BUFFER_SIZE> m_LockSteps;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct WorldHeader {
|
struct WorldHeader {
|
||||||
@@ -87,7 +94,26 @@ struct WorldHeader {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct WorldData {
|
struct WorldData {
|
||||||
std::unordered_map<game::ChunkCoord, game::ChunkPtr> m_Chunks;
|
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 pdata
|
||||||
|
|||||||
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
|
||||||
@@ -8,11 +8,11 @@
|
|||||||
#include <td/common/NonCopyable.h>
|
#include <td/common/NonCopyable.h>
|
||||||
#include <td/protocol/packet/PacketData.h>
|
#include <td/protocol/packet/PacketData.h>
|
||||||
|
|
||||||
|
#include <sp/common/GenericHandler.h>
|
||||||
#include <sp/protocol/ConcreteMessage.h>
|
#include <sp/protocol/ConcreteMessage.h>
|
||||||
#include <sp/protocol/MessageBase.h>
|
#include <sp/protocol/MessageBase.h>
|
||||||
#include <sp/protocol/MessageDispatcher.h>
|
#include <sp/protocol/MessageDispatcher.h>
|
||||||
#include <sp/protocol/MessageFactory.h>
|
#include <sp/protocol/MessageFactory.h>
|
||||||
#include <sp/protocol/MessageHandler.h>
|
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace protocol {
|
namespace protocol {
|
||||||
@@ -22,11 +22,17 @@ enum class PacketID : std::uint8_t {
|
|||||||
BeginGame,
|
BeginGame,
|
||||||
Disconnect,
|
Disconnect,
|
||||||
KeepAlive,
|
KeepAlive,
|
||||||
|
LockStepRequest,
|
||||||
|
LockStepResponse,
|
||||||
LockSteps,
|
LockSteps,
|
||||||
LoggingSuccess,
|
LoggingSuccess,
|
||||||
|
PlaceTower,
|
||||||
PlayerJoin,
|
PlayerJoin,
|
||||||
PlayerLeave,
|
PlayerLeave,
|
||||||
|
PlayerList,
|
||||||
PlayerLogin,
|
PlayerLogin,
|
||||||
|
PredictCommand,
|
||||||
|
SpawnTroop,
|
||||||
WorldHeader,
|
WorldHeader,
|
||||||
WorldData,
|
WorldData,
|
||||||
};
|
};
|
||||||
@@ -37,7 +43,7 @@ using PacketBase = sp::MessageBase<PacketID, PacketHandler>;
|
|||||||
|
|
||||||
|
|
||||||
template <typename TData, PacketID ID>
|
template <typename TData, PacketID ID>
|
||||||
using PacketMessage = sp::ConcreteMessage<TData, PacketID, ID, PacketHandler>;
|
using PacketMessage = sp::ConcreteMessage<TData, PacketBase, ID>;
|
||||||
|
|
||||||
|
|
||||||
namespace packets {
|
namespace packets {
|
||||||
@@ -46,22 +52,34 @@ using BeginGamePacket = PacketMessage<pdata::BeginGame, PacketID::BeginGame>;
|
|||||||
using ChatMessagePacket = PacketMessage<pdata::ChatMessage, PacketID::ChatMessage>;
|
using ChatMessagePacket = PacketMessage<pdata::ChatMessage, PacketID::ChatMessage>;
|
||||||
using DisconnectPacket = PacketMessage<pdata::Disconnect, PacketID::Disconnect>;
|
using DisconnectPacket = PacketMessage<pdata::Disconnect, PacketID::Disconnect>;
|
||||||
using KeepAlivePacket = PacketMessage<pdata::KeepAlive, PacketID::KeepAlive>;
|
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 LockStepsPacket = PacketMessage<pdata::LockSteps, PacketID::LockSteps>;
|
||||||
using LoggingSuccessPacket = PacketMessage<pdata::LoggingSuccess, PacketID::LoggingSuccess>;
|
using LoggingSuccessPacket = PacketMessage<pdata::LoggingSuccess, PacketID::LoggingSuccess>;
|
||||||
|
using PlaceTowerPacket = PacketMessage<pdata::PlaceTower, PacketID::PlaceTower>;
|
||||||
using PlayerJoinPacket = PacketMessage<pdata::PlayerJoin, PacketID::PlayerJoin>;
|
using PlayerJoinPacket = PacketMessage<pdata::PlayerJoin, PacketID::PlayerJoin>;
|
||||||
using PlayerLeavePacket = PacketMessage<pdata::PlayerLeave, PacketID::PlayerLeave>;
|
using PlayerLeavePacket = PacketMessage<pdata::PlayerLeave, PacketID::PlayerLeave>;
|
||||||
|
using PlayerListPacket = PacketMessage<pdata::PlayerList, PacketID::PlayerList>;
|
||||||
using PlayerLoginPacket = PacketMessage<pdata::PlayerLogin, PacketID::PlayerLogin>;
|
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 WorldHeaderPacket = PacketMessage<pdata::WorldHeader, PacketID::WorldHeader>;
|
||||||
using WorldDataPacket = PacketMessage<pdata::WorldData, PacketID::WorldData>;
|
using WorldDataPacket = PacketMessage<pdata::WorldData, PacketID::WorldData>;
|
||||||
|
|
||||||
|
|
||||||
} // namespace packets
|
} // namespace packets
|
||||||
|
|
||||||
using AllPackets = std::tuple<packets::BeginGamePacket, packets::ChatMessagePacket, packets::DisconnectPacket,
|
using AllPackets = std::tuple<packets::BeginGamePacket, packets::ChatMessagePacket, packets::DisconnectPacket,
|
||||||
packets::KeepAlivePacket, packets::LockStepsPacket, packets::LoggingSuccessPacket, packets::PlayerJoinPacket,
|
packets::KeepAlivePacket, packets::LockStepRequestPacket, packets::LockStepResponsePacket, packets::LockStepsPacket,
|
||||||
packets::PlayerLeavePacket, packets::PlayerLoginPacket, packets::WorldHeaderPacket, packets::WorldDataPacket>;
|
packets::LoggingSuccessPacket, packets::PlaceTowerPacket, packets::PlayerJoinPacket, packets::PlayerLeavePacket,
|
||||||
|
packets::PlayerListPacket, packets::PlayerLoginPacket, packets::PredictCommandPacket, packets::SpawnTroopPacket,
|
||||||
|
packets::WorldHeaderPacket, packets::WorldDataPacket>;
|
||||||
|
|
||||||
class PacketHandler : public sp::MessageHandler<AllPackets> {};
|
class PacketHandler : public sp::GenericHandler<AllPackets> {
|
||||||
|
public:
|
||||||
|
void HandleBase(const PacketBase& a_Packet) {
|
||||||
|
a_Packet.Dispatch(*this);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
using PacketDispatcher = sp::MessageDispatcher<PacketBase>;
|
using PacketDispatcher = sp::MessageDispatcher<PacketBase>;
|
||||||
|
|
||||||
|
|||||||
@@ -4,19 +4,21 @@
|
|||||||
#include <td/render/Camera.h>
|
#include <td/render/Camera.h>
|
||||||
#include <td/render/loader/GLLoader.h>
|
#include <td/render/loader/GLLoader.h>
|
||||||
#include <td/render/shader/CameraShaderProgram.h>
|
#include <td/render/shader/CameraShaderProgram.h>
|
||||||
|
#include <td/misc/SlotGuard.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace render {
|
namespace render {
|
||||||
|
|
||||||
class BasicRenderer {
|
class BasicRenderer {
|
||||||
public:
|
public:
|
||||||
virtual void Render() = 0;
|
virtual void Render(float a_Lerp) = 0;
|
||||||
|
virtual ~BasicRenderer() {}
|
||||||
|
|
||||||
void Render(const GL::VertexArray& a_Vao);
|
void Render(const GL::VertexArray& a_Vao);
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename TShader>
|
template <typename TShader>
|
||||||
class Renderer : public BasicRenderer {
|
class Renderer : public BasicRenderer, private utils::SlotGuard {
|
||||||
protected:
|
protected:
|
||||||
std::unique_ptr<TShader> m_Shader;
|
std::unique_ptr<TShader> m_Shader;
|
||||||
Camera& m_Camera;
|
Camera& m_Camera;
|
||||||
@@ -24,6 +26,11 @@ class Renderer : public BasicRenderer {
|
|||||||
public:
|
public:
|
||||||
Renderer(Camera& a_Camera);
|
Renderer(Camera& a_Camera);
|
||||||
virtual ~Renderer() {}
|
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 {
|
class RenderPipeline {
|
||||||
@@ -32,20 +39,23 @@ class RenderPipeline {
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
RenderPipeline();
|
RenderPipeline();
|
||||||
~RenderPipeline() = default;
|
virtual ~RenderPipeline() {}
|
||||||
|
|
||||||
template <typename T, typename... Args>
|
template <typename T, typename... Args>
|
||||||
void AddRenderer(Args&&... args) {
|
T& AddRenderer(Args&&... args) {
|
||||||
m_Renderers.push_back(std::make_unique<T>(args...));
|
auto ptr = std::make_unique<T>(args...);
|
||||||
|
auto rawPtr = ptr.get();
|
||||||
|
m_Renderers.push_back(std::move(ptr));
|
||||||
|
return *rawPtr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Clear() {
|
void Clear() {
|
||||||
m_Renderers.clear();
|
m_Renderers.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Render() {
|
void Render(float a_Lerp) {
|
||||||
for (auto& renderer : m_Renderers) {
|
for (auto& renderer : m_Renderers) {
|
||||||
renderer->Render();
|
renderer->Render(a_Lerp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -56,12 +66,12 @@ class RenderPipeline {
|
|||||||
|
|
||||||
template <typename TShader>
|
template <typename TShader>
|
||||||
Renderer<TShader>::Renderer(Camera& a_Camera) : m_Shader(std::make_unique<TShader>()), m_Camera(a_Camera) {
|
Renderer<TShader>::Renderer(Camera& a_Camera) : m_Shader(std::make_unique<TShader>()), m_Camera(a_Camera) {
|
||||||
a_Camera.OnPerspectiveChange.Connect([this]() {
|
Connect(a_Camera.OnPerspectiveChange, [this](){
|
||||||
m_Shader->Start();
|
m_Shader->Start();
|
||||||
m_Shader->SetProjectionMatrix(m_Camera.GetProjectionMatrix());
|
m_Shader->SetProjectionMatrix(m_Camera.GetProjectionMatrix());
|
||||||
});
|
});
|
||||||
|
|
||||||
a_Camera.OnViewChange.Connect([this]() {
|
Connect(a_Camera.OnViewChange, [this]() {
|
||||||
m_Shader->Start();
|
m_Shader->Start();
|
||||||
m_Shader->SetViewMatrix(m_Camera.GetViewMatrix());
|
m_Shader->SetViewMatrix(m_Camera.GetViewMatrix());
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ struct RenderData {
|
|||||||
GL::VertexArray LoadMobModel();
|
GL::VertexArray LoadMobModel();
|
||||||
GL::VertexArray LoadWorldModel(const td::game::World* world);
|
GL::VertexArray LoadWorldModel(const td::game::World* world);
|
||||||
GL::VertexArray LoadTileSelectModel();
|
GL::VertexArray LoadTileSelectModel();
|
||||||
RenderData LoadTowerModel(game::TowerPtr tower);
|
RenderData LoadTowerModel(const game::TowerPtr& tower);
|
||||||
|
|
||||||
} // namespace WorldLoader
|
} // namespace WorldLoader
|
||||||
|
|
||||||
|
|||||||
@@ -9,14 +9,14 @@ namespace render {
|
|||||||
|
|
||||||
class EntityRenderer : public Renderer<shader::EntityShader> {
|
class EntityRenderer : public Renderer<shader::EntityShader> {
|
||||||
private:
|
private:
|
||||||
const game::World& m_World;
|
game::WorldPtr m_World;
|
||||||
std::unique_ptr<GL::VertexArray> m_EntityVao;
|
std::unique_ptr<GL::VertexArray> m_EntityVao;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EntityRenderer(Camera& a_Camera, const game::World& a_World);
|
EntityRenderer(Camera& a_Camera, const game::WorldPtr& a_World);
|
||||||
virtual ~EntityRenderer();
|
virtual ~EntityRenderer();
|
||||||
|
|
||||||
virtual void Render() override;
|
virtual void Render(float a_Lerp) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace render
|
} // namespace render
|
||||||
|
|||||||
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
|
||||||
@@ -10,14 +10,13 @@ namespace render {
|
|||||||
|
|
||||||
class WorldRenderer : public Renderer<shader::WorldShader> {
|
class WorldRenderer : public Renderer<shader::WorldShader> {
|
||||||
private:
|
private:
|
||||||
const game::World& m_World;
|
|
||||||
std::unique_ptr<GL::VertexArray> m_WorldVao;
|
std::unique_ptr<GL::VertexArray> m_WorldVao;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
WorldRenderer(Camera& a_Camera, const game::World& a_World);
|
WorldRenderer(Camera& a_Camera, const game::WorldPtr& a_World);
|
||||||
virtual ~WorldRenderer();
|
virtual ~WorldRenderer();
|
||||||
|
|
||||||
virtual void Render() override;
|
virtual void Render(float a_Lerp) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace render
|
} // namespace render
|
||||||
|
|||||||
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
|
||||||
219
src/main.cpp
219
src/main.cpp
@@ -1,224 +1,17 @@
|
|||||||
#include <iostream>
|
#include <chrono>
|
||||||
|
#include <td/display/state/MainMenuState.h>
|
||||||
#include <sp/common/DataBuffer.h>
|
#include <td/misc/Time.h>
|
||||||
#include <sp/extensions/Compress.h>
|
|
||||||
#include <td/input/Display.h>
|
|
||||||
|
|
||||||
#include <td/game/World.h>
|
|
||||||
#include <td/protocol/packet/Packets.h>
|
|
||||||
|
|
||||||
#include <td/render/renderer/WorldRenderer.h>
|
|
||||||
#include <td/render/renderer/EntityRenderer.h>
|
|
||||||
|
|
||||||
namespace td {
|
|
||||||
namespace game {
|
|
||||||
|
|
||||||
sp::DataBuffer& operator>>(sp::DataBuffer& buffer, game::TilePtr& tile) {
|
|
||||||
game::TileType tileType;
|
|
||||||
buffer >> tileType;
|
|
||||||
switch (tileType) {
|
|
||||||
case game::TileType::Tower: {
|
|
||||||
auto tilePtr = std::make_shared<game::TowerTile>();
|
|
||||||
buffer >> tilePtr->color_palette_ref >> tilePtr->team_owner;
|
|
||||||
tile = tilePtr;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case game::TileType::Walk: {
|
|
||||||
auto tilePtr = std::make_shared<game::WalkableTile>();
|
|
||||||
buffer >> tilePtr->direction;
|
|
||||||
tile = tilePtr;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case game::TileType::Decoration: {
|
|
||||||
auto tilePtr = std::make_shared<game::DecorationTile>();
|
|
||||||
buffer >> tilePtr->color_palette_ref;
|
|
||||||
tile = tilePtr;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return buffer;
|
|
||||||
}
|
|
||||||
} // namespace game
|
|
||||||
} // namespace td
|
|
||||||
|
|
||||||
namespace sp {
|
|
||||||
namespace details {
|
|
||||||
|
|
||||||
template <>
|
|
||||||
void ReadMessage(DataBuffer& a_Buffer, td::protocol::pdata::WorldHeader& a_Header) {
|
|
||||||
a_Buffer >> a_Header.m_TowerPlacePalette >> a_Header.m_WalkablePalette;
|
|
||||||
|
|
||||||
std::uint16_t decoPaletteSize;
|
|
||||||
a_Buffer >> decoPaletteSize;
|
|
||||||
|
|
||||||
std::size_t decoPalletteSizeByte = decoPaletteSize * sizeof(td::Color);
|
|
||||||
|
|
||||||
a_Header.m_DecorationPalette.resize(decoPaletteSize);
|
|
||||||
|
|
||||||
memcpy(reinterpret_cast<std::uint8_t*>(a_Header.m_DecorationPalette.data()), a_Buffer.data() + a_Buffer.GetReadOffset(),
|
|
||||||
decoPalletteSizeByte);
|
|
||||||
|
|
||||||
a_Buffer.SetReadOffset(a_Buffer.GetReadOffset() + decoPalletteSizeByte);
|
|
||||||
|
|
||||||
a_Buffer >> a_Header.m_Background;
|
|
||||||
|
|
||||||
td::utils::shape::Rectangle redCastle, blueCastle;
|
|
||||||
|
|
||||||
a_Buffer >> a_Header.m_RedSpawn >> redCastle;
|
|
||||||
a_Buffer >> a_Header.m_BlueSpawn >> blueCastle;
|
|
||||||
|
|
||||||
a_Header.m_RedCastle.SetShape(redCastle);
|
|
||||||
a_Header.m_BlueCastle.SetShape(blueCastle);
|
|
||||||
|
|
||||||
std::uint64_t tilePaletteSize;
|
|
||||||
a_Buffer >> tilePaletteSize;
|
|
||||||
|
|
||||||
a_Header.m_TilePalette.reserve(tilePaletteSize);
|
|
||||||
|
|
||||||
for (std::uint64_t tileNumber = 0; tileNumber < tilePaletteSize; tileNumber++) {
|
|
||||||
td::game::TilePtr tile;
|
|
||||||
a_Buffer >> tile;
|
|
||||||
a_Header.m_TilePalette.push_back(tile);
|
|
||||||
}
|
|
||||||
|
|
||||||
a_Buffer >> a_Header.m_SpawnColorPalette;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef std::vector<uint64_t> ChunkPackedData;
|
|
||||||
|
|
||||||
const int BITS_IN_BYTE = 8;
|
|
||||||
const int BITS_IN_LONG = BITS_IN_BYTE * sizeof(std::uint64_t);
|
|
||||||
|
|
||||||
static unsigned int countBits(unsigned int number) {
|
|
||||||
// log function in base 2
|
|
||||||
// take only integer part
|
|
||||||
return static_cast<unsigned int>(std::log2(number) + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
template <>
|
|
||||||
void ReadMessage(DataBuffer& a_Buffer, td::protocol::pdata::WorldData& a_WorldData) {
|
|
||||||
std::uint64_t chunkCount;
|
|
||||||
a_Buffer >> chunkCount;
|
|
||||||
|
|
||||||
for (std::uint64_t chunkNumber = 0; chunkNumber < chunkCount; chunkNumber++) {
|
|
||||||
td::game::ChunkPtr chunk = std::make_shared<td::game::Chunk>();
|
|
||||||
|
|
||||||
td::game::ChunkCoord chunkCoords;
|
|
||||||
a_Buffer >> chunkCoords.x >> chunkCoords.y;
|
|
||||||
|
|
||||||
std::uint64_t chunkPaletteSize;
|
|
||||||
// std::reverse(reinterpret_cast<std::uint8_t*>(&chunkPaletteSize), reinterpret_cast<std::uint8_t*>(&chunkPaletteSize) + 4);
|
|
||||||
a_Buffer >> chunkPaletteSize;
|
|
||||||
|
|
||||||
td::game::ChunkPalette chunkPalette(chunkPaletteSize);
|
|
||||||
|
|
||||||
memcpy(reinterpret_cast<void*>(chunkPalette.data()), a_Buffer.data() + a_Buffer.GetReadOffset(),
|
|
||||||
chunkPaletteSize * sizeof(td::game::ChunkPalette::value_type));
|
|
||||||
a_Buffer.SetReadOffset(a_Buffer.GetReadOffset() + chunkPaletteSize * sizeof(td::game::ChunkPalette::value_type));
|
|
||||||
|
|
||||||
chunk->palette = chunkPalette;
|
|
||||||
|
|
||||||
std::uint8_t bitsPerTile = countBits(chunkPaletteSize);
|
|
||||||
|
|
||||||
// A bitmask that contains bitsPerTile set bits
|
|
||||||
td::game::Chunk::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1);
|
|
||||||
|
|
||||||
ChunkPackedData chunkData(td::game::Chunk::ChunkSize / (BITS_IN_BYTE * sizeof(ChunkPackedData::value_type) / bitsPerTile), 0);
|
|
||||||
|
|
||||||
memcpy(reinterpret_cast<void*>(chunkData.data()), a_Buffer.data() + a_Buffer.GetReadOffset(),
|
|
||||||
chunkData.size() * sizeof(ChunkPackedData::value_type));
|
|
||||||
a_Buffer.SetReadOffset(a_Buffer.GetReadOffset() + chunkData.size() * sizeof(ChunkPackedData::value_type));
|
|
||||||
|
|
||||||
for (unsigned int tileNumber = 0; tileNumber < td::game::Chunk::ChunkSize; tileNumber++) {
|
|
||||||
std::size_t startLong = (tileNumber * bitsPerTile) / BITS_IN_LONG;
|
|
||||||
std::size_t startOffset = (tileNumber * bitsPerTile) % BITS_IN_LONG;
|
|
||||||
std::size_t endLong = ((tileNumber + 1) * bitsPerTile - 1) / BITS_IN_LONG;
|
|
||||||
|
|
||||||
td::game::Chunk::ChunkData::value_type value;
|
|
||||||
if (startLong == endLong) {
|
|
||||||
value = (chunkData[startLong] >> startOffset);
|
|
||||||
} else {
|
|
||||||
int endOffset = BITS_IN_LONG - startOffset;
|
|
||||||
value = (chunkData[startLong] >> startOffset | chunkData[endLong] << endOffset);
|
|
||||||
}
|
|
||||||
value &= individualValueMask;
|
|
||||||
|
|
||||||
chunk->tiles[tileNumber] = value;
|
|
||||||
}
|
|
||||||
a_WorldData.m_Chunks.insert({chunkCoords, chunk});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace details
|
|
||||||
} // namespace sp
|
|
||||||
|
|
||||||
class WorldApply : public td::protocol::PacketHandler {
|
|
||||||
private:
|
|
||||||
td::game::World& m_World;
|
|
||||||
public:
|
|
||||||
WorldApply(td::game::World& a_World) : m_World(a_World) {}
|
|
||||||
|
|
||||||
void Handle(const td::protocol::pdata::WorldHeader& a_Header) override {
|
|
||||||
m_World.LoadMap(a_Header);
|
|
||||||
}
|
|
||||||
void Handle(const td::protocol::pdata::WorldData& a_Data) override {
|
|
||||||
m_World.LoadMap(a_Data);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
td::game::World GetWorld() {
|
|
||||||
sp::DataBuffer buffer;
|
|
||||||
buffer.ReadFile("test/tdmap.tdmap2");
|
|
||||||
|
|
||||||
sp::DataBuffer buffer1 = sp::zlib::Decompress(buffer, 84);
|
|
||||||
buffer.SetReadOffset(buffer.GetReadOffset() + 83);
|
|
||||||
sp::DataBuffer buffer2 = sp::zlib::Decompress(buffer, 511);
|
|
||||||
|
|
||||||
td::protocol::packets::WorldHeaderPacket header;
|
|
||||||
header.Read(buffer1);
|
|
||||||
|
|
||||||
td::protocol::packets::WorldDataPacket data;
|
|
||||||
data.Read(buffer2);
|
|
||||||
|
|
||||||
td::game::World w;
|
|
||||||
WorldApply wa(w);
|
|
||||||
|
|
||||||
td::protocol::PacketDispatcher d;
|
|
||||||
d.RegisterHandler(td::protocol::PacketID::WorldData, &wa);
|
|
||||||
d.RegisterHandler(td::protocol::PacketID::WorldHeader, &wa);
|
|
||||||
|
|
||||||
d.Dispatch(header);
|
|
||||||
d.Dispatch(data);
|
|
||||||
|
|
||||||
return w;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
td::game::World w = GetWorld();
|
|
||||||
|
|
||||||
// init GL context
|
// init GL context
|
||||||
td::Display display(1920, 1080, "Tower-Defense 2");
|
td::Display display(1920, 1080, "Tower-Defense 2");
|
||||||
|
|
||||||
td::render::Camera cam;
|
display.ChangeState<td::MainMenuState>();
|
||||||
|
|
||||||
auto mob = std::make_shared<td::game::Zombie>(0, 0, 0);
|
|
||||||
mob->SetCenter({77, 13});
|
|
||||||
w.GetMobList().push_back(mob);
|
|
||||||
|
|
||||||
td::render::RenderPipeline renderer;
|
|
||||||
renderer.AddRenderer<td::render::WorldRenderer>(cam, w);
|
|
||||||
renderer.AddRenderer<td::render::EntityRenderer>(cam, w);
|
|
||||||
|
|
||||||
cam.SetCamPos({77, 25, 13});
|
|
||||||
cam.UpdatePerspective(display.GetAspectRatio());
|
|
||||||
|
|
||||||
|
td::Timer timer;
|
||||||
while (!display.IsCloseRequested()) {
|
while (!display.IsCloseRequested()) {
|
||||||
display.PollEvents();
|
display.PollEvents();
|
||||||
renderer.Render();
|
display.Update(timer.GetDelta());
|
||||||
display.Update();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
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
|
||||||
48
src/server/socket/FakeSocket.cpp
Normal file
48
src/server/socket/FakeSocket.cpp
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
#include <server/socket/FakeSocket.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace server {
|
||||||
|
|
||||||
|
void FakeSocket::SendPeer(PeerID a_Peer, const protocol::PacketBase& a_Packet) {
|
||||||
|
auto socket = m_Clients.at(a_Peer);
|
||||||
|
assert(socket.has_value());
|
||||||
|
assert(!socket.value().expired());
|
||||||
|
socket.value().lock()->OnReceive(a_Packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
void FakeSocket::ReceiveFromFakePeer(PeerID a_Peer, const protocol::PacketBase& a_Packet) {
|
||||||
|
OnReceivePeer(a_Peer, a_Packet);
|
||||||
|
}
|
||||||
|
|
||||||
|
PeerID FakeSocket::ConnectFakePeer(const std::shared_ptr<client::FakeSocket>& a_Socket) {
|
||||||
|
int peerId = GetNextFreeId();
|
||||||
|
if (peerId == -1) {
|
||||||
|
peerId = m_Clients.size();
|
||||||
|
m_Clients.push_back(a_Socket);
|
||||||
|
} else {
|
||||||
|
m_Clients.emplace(m_Clients.begin() + peerId, a_Socket);
|
||||||
|
}
|
||||||
|
a_Socket->OnConnect();
|
||||||
|
OnConnectPeer(peerId);
|
||||||
|
return peerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FakeSocket::DisconnectFakePeer(PeerID a_Peer) {
|
||||||
|
auto socket = m_Clients.at(a_Peer);
|
||||||
|
assert(socket.has_value());
|
||||||
|
assert(!socket.value().expired());
|
||||||
|
socket.value().lock()->OnDisconnect();
|
||||||
|
OnDisconnectPeer(a_Peer);
|
||||||
|
}
|
||||||
|
|
||||||
|
int FakeSocket::GetNextFreeId() {
|
||||||
|
auto it = std::find_if(m_Clients.begin(), m_Clients.end(), [](const auto& a_Value) { return !a_Value.has_value(); });
|
||||||
|
|
||||||
|
if (it == m_Clients.end())
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return std::distance(m_Clients.begin(), it);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace server
|
||||||
|
} // namespace td
|
||||||
47
src/server/state/GameState.cpp
Normal file
47
src/server/state/GameState.cpp
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include <server/state/GameState.h>
|
||||||
|
#include <server/state/GameStateHandler.h>
|
||||||
|
#include <td/protocol/packet/PacketSerialize.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace server {
|
||||||
|
|
||||||
|
GameState::GameState(Server& a_Server, const std::shared_ptr<game::World>& a_World) :
|
||||||
|
ServerState(a_Server), m_World(a_World), m_Simulation(*m_World, STEP_TIME), m_Time(0) {
|
||||||
|
std::cout << "[Server] Switched to Game state !\n";
|
||||||
|
BroadcastPacket(a_World->GetPacketHeader());
|
||||||
|
BroadcastPacket(a_World->GetPacketData());
|
||||||
|
BroadcastPacket(protocol::packets::BeginGamePacket());
|
||||||
|
BroadcastPacket(m_Simulation.MakePacket());
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameState::HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) {
|
||||||
|
GameStateHandler handler(*this, a_Id);
|
||||||
|
a_Packet.Dispatch(handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameState::Update(float a_Delta) {
|
||||||
|
// TODO: don't make STEP_TIME constant
|
||||||
|
static const float stepTimeSecond = static_cast<float>(STEP_TIME) / 1000.0f;
|
||||||
|
m_Time += a_Delta;
|
||||||
|
if (m_Time > stepTimeSecond) {
|
||||||
|
m_Time = std::fmod(m_Time, stepTimeSecond);
|
||||||
|
auto lockStepPacket = m_Simulation.Update();
|
||||||
|
BroadcastPacket(lockStepPacket);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameState::OnPlayerJoin(PlayerID a_Id, const td::PlayerInfo& a_Info) {
|
||||||
|
SendPacket(a_Id, m_World->GetPacketHeader());
|
||||||
|
SendPacket(a_Id, m_World->GetPacketData());
|
||||||
|
|
||||||
|
// TODO: real teams
|
||||||
|
std::vector<PlayerInfo> team;
|
||||||
|
std::vector<protocol::LockStep> locksteps = m_Simulation.GetFirstLocksteps();
|
||||||
|
|
||||||
|
BroadcastPacket(protocol::packets::BeginGamePacket(team, team, locksteps));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace server
|
||||||
|
} // namespace td
|
||||||
28
src/server/state/GameStateHandler.cpp
Normal file
28
src/server/state/GameStateHandler.cpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#include <server/state/GameState.h>
|
||||||
|
#include <server/state/GameStateHandler.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace server {
|
||||||
|
|
||||||
|
GameStateHandler::GameStateHandler(GameState& a_GameState, PlayerID a_PlayerId) : m_GameState(a_GameState), m_PlayerId(a_PlayerId) {}
|
||||||
|
|
||||||
|
// TODO: redo this
|
||||||
|
void GameStateHandler::Handle(const protocol::packets::SpawnTroopPacket& a_Packet) {
|
||||||
|
static const EntityCoords DEFAULT_POS(td::FpFloat(77), td::FpFloat(13));
|
||||||
|
|
||||||
|
td::protocol::commands::SpawnTroopCommand spawn(*a_Packet->m_Type, *a_Packet->m_Level, DEFAULT_POS, m_PlayerId);
|
||||||
|
m_GameState.m_Simulation.Handle(spawn);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: and this
|
||||||
|
void GameStateHandler::Handle(const protocol::packets::PlaceTowerPacket& a_Packet) {
|
||||||
|
td::protocol::commands::PlaceTowerCommand place(a_Packet->m_Type, m_PlayerId, a_Packet->m_Position);
|
||||||
|
m_GameState.m_Simulation.Handle(place);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameStateHandler::Handle(const protocol::packets::LockStepRequestPacket& a_Packet) {
|
||||||
|
m_GameState.SendPacket(m_PlayerId, m_GameState.m_Simulation.GetResponse(a_Packet));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace server
|
||||||
|
} // namespace td
|
||||||
60
src/server/state/LobbyState.cpp
Normal file
60
src/server/state/LobbyState.cpp
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
#include <server/state/GameState.h>
|
||||||
|
#include <server/state/LobbyState.h>
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
|
#include <td/protocol/packet/PacketSerialize.h>
|
||||||
|
|
||||||
|
#include <sp/common/DataBuffer.h>
|
||||||
|
#include <sp/extensions/Compress.h>
|
||||||
|
#include <sp/io/MessageStream.h>
|
||||||
|
#include <sp/io/StdIo.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace server {
|
||||||
|
|
||||||
|
void Save(const protocol::PacketBase& header, const protocol::PacketBase& data) {
|
||||||
|
auto comp = std::make_shared<sp::ZlibCompress>();
|
||||||
|
|
||||||
|
std::ofstream fStream("test/tdmap.tdmap3");
|
||||||
|
auto out = std::make_shared<sp::StdOuput>(fStream);
|
||||||
|
|
||||||
|
sp::MessageStream<protocol::PacketFactory> stream(std::move(out), std::move(comp));
|
||||||
|
|
||||||
|
stream.WriteMessage(header, false);
|
||||||
|
stream.WriteMessage(data, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
game::WorldPtr GetWorld() {
|
||||||
|
auto comp = std::make_shared<sp::ZlibCompress>();
|
||||||
|
|
||||||
|
std::ifstream fStream("test/tdmap.tdmap2");
|
||||||
|
auto out = std::make_shared<sp::StdInput>(fStream);
|
||||||
|
|
||||||
|
sp::MessageStream<protocol::PacketFactory> stream(std::move(out), std::move(comp));
|
||||||
|
|
||||||
|
auto header = stream.ReadConcreteMessage<protocol::packets::WorldHeaderPacket>();
|
||||||
|
auto data = stream.ReadConcreteMessage<protocol::packets::WorldDataPacket>();
|
||||||
|
|
||||||
|
auto w = std::make_shared<game::World>();
|
||||||
|
|
||||||
|
w->LoadMap(**header);
|
||||||
|
w->LoadMap(**data);
|
||||||
|
|
||||||
|
// Save(*header, *data);
|
||||||
|
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
LobbyState::LobbyState(Server& a_Server) : ServerState(a_Server) {}
|
||||||
|
|
||||||
|
void LobbyState::HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) {}
|
||||||
|
|
||||||
|
void LobbyState::OnPlayerJoin(PlayerID a_Id, const td::PlayerInfo& a_Info) {
|
||||||
|
m_StateMachine.ChangeState<GameState>(GetWorld());
|
||||||
|
}
|
||||||
|
|
||||||
|
void LobbyState::Update(float a_Delta) {}
|
||||||
|
|
||||||
|
} // namespace server
|
||||||
|
} // namespace td
|
||||||
29
src/td/Types.cpp
Normal file
29
src/td/Types.cpp
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
#include <td/Types.h>
|
||||||
|
|
||||||
|
#include <sp/common/DataBuffer.h>
|
||||||
|
#include <sp/common/ByteSwapping.h>
|
||||||
|
#include <sp/common/DataBufferOperators.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const EntityCoords& a_Coords) {
|
||||||
|
return a_Buffer << a_Coords.x << a_Coords.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const FpFloat& a_Float) {
|
||||||
|
auto raw = a_Float.raw_value();
|
||||||
|
return a_Buffer << raw;
|
||||||
|
}
|
||||||
|
|
||||||
|
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, EntityCoords& a_Coords) {
|
||||||
|
return a_Buffer >> a_Coords.x >> a_Coords.y;
|
||||||
|
}
|
||||||
|
|
||||||
|
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, FpFloat& a_Float) {
|
||||||
|
auto raw = a_Float.raw_value();
|
||||||
|
a_Buffer >> raw;
|
||||||
|
a_Float = FpFloat::from_raw_value(raw);
|
||||||
|
return a_Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace td
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
#include <td/input/Display.h>
|
#include <td/display/Display.h>
|
||||||
|
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
@@ -8,11 +8,15 @@
|
|||||||
|
|
||||||
#include <td/misc/Format.h>
|
#include <td/misc/Format.h>
|
||||||
#include <td/misc/Log.h>
|
#include <td/misc/Log.h>
|
||||||
|
#include <td/display/ImGuiTheme.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
Display::Display(int a_Width, int a_Height, const std::string& a_Title) :
|
Display::Display(int a_Width, int a_Height, const std::string& a_Title) :
|
||||||
m_LastWidth(0), m_LastHeight(0), m_AspectRatio(1), m_ShouldClose(false) {
|
m_LastWidth(0), m_LastHeight(0), m_AspectRatio(1), m_ShouldClose(false) {
|
||||||
|
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||||
|
utils::LOGE(utils::Format("Could not initialize SDL! SDL error: %s", SDL_GetError()));
|
||||||
|
}
|
||||||
|
|
||||||
m_Window = SDL_CreateWindow(a_Title.c_str(), a_Width, a_Height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
|
m_Window = SDL_CreateWindow(a_Title.c_str(), a_Width, a_Height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
|
||||||
|
|
||||||
@@ -38,6 +42,9 @@ Display::Display(int a_Width, int a_Height, const std::string& a_Title) :
|
|||||||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
||||||
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
||||||
|
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
|
||||||
|
|
||||||
m_GLContext = SDL_GL_CreateContext(m_Window);
|
m_GLContext = SDL_GL_CreateContext(m_Window);
|
||||||
|
|
||||||
if (!m_GLContext) {
|
if (!m_GLContext) {
|
||||||
@@ -46,6 +53,8 @@ Display::Display(int a_Width, int a_Height, const std::string& a_Title) :
|
|||||||
|
|
||||||
int major, minor, mask;
|
int major, minor, mask;
|
||||||
int r, g, b, a, depth;
|
int r, g, b, a, depth;
|
||||||
|
int mBuffers, mSamples;
|
||||||
|
|
||||||
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &mask);
|
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &mask);
|
||||||
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
|
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
|
||||||
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
|
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
|
||||||
@@ -57,6 +66,9 @@ Display::Display(int a_Width, int a_Height, const std::string& a_Title) :
|
|||||||
|
|
||||||
SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depth);
|
SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depth);
|
||||||
|
|
||||||
|
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &mBuffers);
|
||||||
|
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &mSamples);
|
||||||
|
|
||||||
const char* mask_desc;
|
const char* mask_desc;
|
||||||
|
|
||||||
if (mask & SDL_GL_CONTEXT_PROFILE_CORE) {
|
if (mask & SDL_GL_CONTEXT_PROFILE_CORE) {
|
||||||
@@ -72,6 +84,8 @@ Display::Display(int a_Width, int a_Height, const std::string& a_Title) :
|
|||||||
utils::LOG(utils::Format(
|
utils::LOG(utils::Format(
|
||||||
"GL Context : %i.%i %s, Color : R:%i G:%i B:%i A:%i, Depth bits : %i", major, minor, mask_desc, r, g, b, a, depth));
|
"GL Context : %i.%i %s, Color : R:%i G:%i B:%i A:%i, Depth bits : %i", major, minor, mask_desc, r, g, b, a, depth));
|
||||||
|
|
||||||
|
utils::LOG(utils::Format("MultiSamples : Buffers : %i, Samples : %i", mBuffers, mSamples));
|
||||||
|
|
||||||
SDL_GL_MakeCurrent(m_Window, m_GLContext);
|
SDL_GL_MakeCurrent(m_Window, m_GLContext);
|
||||||
|
|
||||||
GLenum error = glewInit();
|
GLenum error = glewInit();
|
||||||
@@ -81,6 +95,9 @@ Display::Display(int a_Width, int a_Height, const std::string& a_Title) :
|
|||||||
|
|
||||||
// WindowResizeEvent(WindowWidth, WindowHeight);
|
// WindowResizeEvent(WindowWidth, WindowHeight);
|
||||||
|
|
||||||
|
// vsync
|
||||||
|
SDL_GL_SetSwapInterval(1);
|
||||||
|
|
||||||
// Setup Dear ImGui context
|
// Setup Dear ImGui context
|
||||||
IMGUI_CHECKVERSION();
|
IMGUI_CHECKVERSION();
|
||||||
ImGui::CreateContext();
|
ImGui::CreateContext();
|
||||||
@@ -96,9 +113,11 @@ Display::Display(int a_Width, int a_Height, const std::string& a_Title) :
|
|||||||
// Setup scaling
|
// Setup scaling
|
||||||
float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
|
float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
|
||||||
// ImGuiStyle& style = ImGui::GetStyle();
|
// ImGuiStyle& style = ImGui::GetStyle();
|
||||||
// style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing this
|
// style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing
|
||||||
|
// this
|
||||||
// // requires resetting Style + calling this again)
|
// // requires resetting Style + calling this again)
|
||||||
// style.FontSizeBase = 13 * main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We leave
|
// style.FontSizeBase = 13 * main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We
|
||||||
|
// leave
|
||||||
// // both here for documentation purpose)
|
// // both here for documentation purpose)
|
||||||
|
|
||||||
|
|
||||||
@@ -109,8 +128,12 @@ Display::Display(int a_Width, int a_Height, const std::string& a_Title) :
|
|||||||
// Setup Platform/Renderer backends
|
// Setup Platform/Renderer backends
|
||||||
ImGui_ImplSDL3_InitForOpenGL(m_Window, m_GLContext);
|
ImGui_ImplSDL3_InitForOpenGL(m_Window, m_GLContext);
|
||||||
ImGui_ImplOpenGL3_Init("#version 330");
|
ImGui_ImplOpenGL3_Init("#version 330");
|
||||||
|
LoadTheme();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Display::Close() {
|
||||||
|
m_ShouldClose = true;
|
||||||
|
}
|
||||||
|
|
||||||
void Display::PollEvents() {
|
void Display::PollEvents() {
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
@@ -119,12 +142,21 @@ void Display::PollEvents() {
|
|||||||
case SDL_EVENT_QUIT:
|
case SDL_EVENT_QUIT:
|
||||||
case SDL_EVENT_WINDOW_CLOSE_REQUESTED: {
|
case SDL_EVENT_WINDOW_CLOSE_REQUESTED: {
|
||||||
m_ShouldClose = true;
|
m_ShouldClose = true;
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case SDL_EVENT_WINDOW_RESIZED: {
|
case SDL_EVENT_WINDOW_RESIZED: {
|
||||||
m_LastWidth = event.window.data1;
|
m_LastWidth = event.window.data1;
|
||||||
m_LastHeight = event.window.data2;
|
m_LastHeight = event.window.data2;
|
||||||
m_AspectRatio = (float)m_LastWidth / m_LastHeight;
|
m_AspectRatio = (float)m_LastWidth / m_LastHeight;
|
||||||
|
OnAspectRatioChange(m_AspectRatio);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case SDL_EVENT_KEY_DOWN: {
|
||||||
|
if (!event.key.repeat)
|
||||||
|
OnKeyDown(event.key.key);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
@@ -138,7 +170,11 @@ void Display::PollEvents() {
|
|||||||
ImGui::NewFrame();
|
ImGui::NewFrame();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Display::Update() {
|
void Display::Update(float a_Delta) {
|
||||||
|
StateMachine::Update(a_Delta);
|
||||||
|
#ifndef NDEBUG
|
||||||
|
ImGui::ShowDemoWindow();
|
||||||
|
#endif
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
ImGuiIO& io = ImGui::GetIO();
|
||||||
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
|
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
|
||||||
10
src/td/display/DisplayState.cpp
Normal file
10
src/td/display/DisplayState.cpp
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#include <td/display/DisplayState.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
DisplayState::DisplayState(Display& a_Display) : Display::State(a_Display) {
|
||||||
|
Connect(m_StateMachine.OnKeyDown, std::bind(&DisplayState::OnKeyDown, this, std::placeholders::_1));
|
||||||
|
Connect(m_StateMachine.OnAspectRatioChange, std::bind(&DisplayState::OnAspectRatioChange, this, std::placeholders::_1));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace td
|
||||||
88
src/td/display/ImGuiTheme.cpp
Normal file
88
src/td/display/ImGuiTheme.cpp
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
#include <td/display/ImGuiTheme.h>
|
||||||
|
|
||||||
|
#include <imgui.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
void LoadTheme() {
|
||||||
|
|
||||||
|
static const bool bStyleDark_ = true;
|
||||||
|
static const float alpha_ = 0.8f;
|
||||||
|
|
||||||
|
ImGuiStyle& style = ImGui::GetStyle();
|
||||||
|
|
||||||
|
// light style from Pacôme Danhiez (user itamago) https://github.com/ocornut/imgui/pull/511#issuecomment-175719267
|
||||||
|
style.Alpha = 1.0f;
|
||||||
|
style.FrameRounding = 3.0f;
|
||||||
|
style.Colors[ImGuiCol_Text] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_TextDisabled] = ImVec4(0.60f, 0.60f, 0.60f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_WindowBg] = ImVec4(0.94f, 0.94f, 0.94f, 0.94f);
|
||||||
|
// style.Colors[ImGuiCol_ChildWindowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
|
||||||
|
style.Colors[ImGuiCol_PopupBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.94f);
|
||||||
|
style.Colors[ImGuiCol_Border] = ImVec4(0.00f, 0.00f, 0.00f, 0.39f);
|
||||||
|
style.Colors[ImGuiCol_BorderShadow] = ImVec4(1.00f, 1.00f, 1.00f, 0.10f);
|
||||||
|
style.Colors[ImGuiCol_FrameBg] = ImVec4(1.00f, 1.00f, 1.00f, 0.94f);
|
||||||
|
style.Colors[ImGuiCol_FrameBgHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);
|
||||||
|
style.Colors[ImGuiCol_FrameBgActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);
|
||||||
|
style.Colors[ImGuiCol_TitleBg] = ImVec4(0.96f, 0.96f, 0.96f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_TitleBgCollapsed] = ImVec4(1.00f, 1.00f, 1.00f, 0.51f);
|
||||||
|
style.Colors[ImGuiCol_TitleBgActive] = ImVec4(0.82f, 0.82f, 0.82f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_MenuBarBg] = ImVec4(0.86f, 0.86f, 0.86f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_ScrollbarBg] = ImVec4(0.98f, 0.98f, 0.98f, 0.53f);
|
||||||
|
style.Colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.69f, 0.69f, 0.69f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.59f, 0.59f, 0.59f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.49f, 0.49f, 0.49f, 1.00f);
|
||||||
|
// style.Colors[ImGuiCol_ComboBg] = ImVec4(0.86f, 0.86f, 0.86f, 0.99f);
|
||||||
|
style.Colors[ImGuiCol_CheckMark] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_SliderGrab] = ImVec4(0.24f, 0.52f, 0.88f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_SliderGrabActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_Button] = ImVec4(0.26f, 0.59f, 0.98f, 0.40f);
|
||||||
|
style.Colors[ImGuiCol_ButtonHovered] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_ButtonActive] = ImVec4(0.06f, 0.53f, 0.98f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_Header] = ImVec4(0.26f, 0.59f, 0.98f, 0.31f);
|
||||||
|
style.Colors[ImGuiCol_HeaderHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.80f);
|
||||||
|
style.Colors[ImGuiCol_HeaderActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
|
||||||
|
// style.Colors[ImGuiCol_Column] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f);
|
||||||
|
// style.Colors[ImGuiCol_ColumnHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.78f);
|
||||||
|
// style.Colors[ImGuiCol_ColumnActive] = ImVec4(0.26f, 0.59f, 0.98f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_ResizeGrip] = ImVec4(1.00f, 1.00f, 1.00f, 0.50f);
|
||||||
|
style.Colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.26f, 0.59f, 0.98f, 0.67f);
|
||||||
|
style.Colors[ImGuiCol_ResizeGripActive] = ImVec4(0.26f, 0.59f, 0.98f, 0.95f);
|
||||||
|
// style.Colors[ImGuiCol_CloseButton] = ImVec4(0.59f, 0.59f, 0.59f, 0.50f);
|
||||||
|
// style.Colors[ImGuiCol_CloseButtonHovered] = ImVec4(0.98f, 0.39f, 0.36f, 1.00f);
|
||||||
|
// style.Colors[ImGuiCol_CloseButtonActive] = ImVec4(0.98f, 0.39f, 0.36f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_PlotLines] = ImVec4(0.39f, 0.39f, 0.39f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.43f, 0.35f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_PlotHistogram] = ImVec4(0.90f, 0.70f, 0.00f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.60f, 0.00f, 1.00f);
|
||||||
|
style.Colors[ImGuiCol_TextSelectedBg] = ImVec4(0.26f, 0.59f, 0.98f, 0.35f);
|
||||||
|
// style.Colors[ImGuiCol_ModalWindowDarkening] = ImVec4(0.20f, 0.20f, 0.20f, 0.35f);
|
||||||
|
|
||||||
|
if (bStyleDark_) {
|
||||||
|
for (int i = 0; i <= ImGuiCol_COUNT; i++) {
|
||||||
|
ImVec4& col = style.Colors[i];
|
||||||
|
float H, S, V;
|
||||||
|
ImGui::ColorConvertRGBtoHSV(col.x, col.y, col.z, H, S, V);
|
||||||
|
|
||||||
|
if (S < 0.1f) {
|
||||||
|
V = 1.0f - V;
|
||||||
|
}
|
||||||
|
ImGui::ColorConvertHSVtoRGB(H, S, V, col.x, col.y, col.z);
|
||||||
|
if (col.w < 1.00f) {
|
||||||
|
col.w *= alpha_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (int i = 0; i <= ImGuiCol_COUNT; i++) {
|
||||||
|
ImVec4& col = style.Colors[i];
|
||||||
|
if (col.w < 1.00f) {
|
||||||
|
col.x *= alpha_;
|
||||||
|
col.y *= alpha_;
|
||||||
|
col.z *= alpha_;
|
||||||
|
col.w *= alpha_;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace td
|
||||||
16
src/td/display/menu/CreatePartyMenu.cpp
Normal file
16
src/td/display/menu/CreatePartyMenu.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include <td/display/menu/CreatePartyMenu.h>
|
||||||
|
|
||||||
|
#include <imgui.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
CreatePartyMenu::CreatePartyMenu(MainMenuState& a_MainMenu) : MainMenuState::Menu(a_MainMenu) {}
|
||||||
|
|
||||||
|
CreatePartyMenu::~CreatePartyMenu() {}
|
||||||
|
|
||||||
|
void CreatePartyMenu::Update() {
|
||||||
|
ImGui::Text("CreatePartyMenu");
|
||||||
|
m_StateStack.RenderBackButton();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace td
|
||||||
16
src/td/display/menu/JoinPartyMenu.cpp
Normal file
16
src/td/display/menu/JoinPartyMenu.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include <td/display/menu/JoinPartyMenu.h>
|
||||||
|
|
||||||
|
#include <imgui.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
JoinPartyMenu::JoinPartyMenu(MainMenuState& a_MainMenu) : MainMenuState::Menu(a_MainMenu) {}
|
||||||
|
|
||||||
|
JoinPartyMenu::~JoinPartyMenu() {}
|
||||||
|
|
||||||
|
void JoinPartyMenu::Update() {
|
||||||
|
ImGui::Text("JoinPartyMenu");
|
||||||
|
m_StateStack.RenderBackButton();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace td
|
||||||
28
src/td/display/menu/MainMenu.cpp
Normal file
28
src/td/display/menu/MainMenu.cpp
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
#include <td/display/menu/MainMenu.h>
|
||||||
|
|
||||||
|
#include <imgui.h>
|
||||||
|
#include <td/display/menu/CreatePartyMenu.h>
|
||||||
|
#include <td/display/menu/JoinPartyMenu.h>
|
||||||
|
#include <td/display/menu/SettingsMenu.h>
|
||||||
|
#include <td/display/state/DebugWorldState.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
MainMenu::MainMenu(MainMenuState& a_MainMenu) : MainMenuState::Menu(a_MainMenu) {}
|
||||||
|
|
||||||
|
MainMenu::~MainMenu() {}
|
||||||
|
|
||||||
|
void MainMenu::Update() {
|
||||||
|
if (ImGui::Button("Create Party"))
|
||||||
|
m_StateStack.PushState<CreatePartyMenu>();
|
||||||
|
if (ImGui::Button("Join Party"))
|
||||||
|
m_StateStack.PushState<JoinPartyMenu>();
|
||||||
|
if (ImGui::Button("Settings"))
|
||||||
|
m_StateStack.PushState<SettingsMenu>();
|
||||||
|
#ifndef NDEBUG
|
||||||
|
if (ImGui::Button("Debug world"))
|
||||||
|
m_StateStack.ChangeState<DebugWorldState>();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace td
|
||||||
16
src/td/display/menu/SettingsMenu.cpp
Normal file
16
src/td/display/menu/SettingsMenu.cpp
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
#include <td/display/menu/SettingsMenu.h>
|
||||||
|
|
||||||
|
#include <imgui.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
SettingsMenu::SettingsMenu(MainMenuState& a_MainMenu) : MainMenuState::Menu(a_MainMenu) {}
|
||||||
|
|
||||||
|
SettingsMenu::~SettingsMenu() {}
|
||||||
|
|
||||||
|
void SettingsMenu::Update() {
|
||||||
|
ImGui::Text("SettingsMenu");
|
||||||
|
m_StateStack.RenderBackButton();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace td
|
||||||
104
src/td/display/state/DebugWorldState.cpp
Normal file
104
src/td/display/state/DebugWorldState.cpp
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
#include <td/display/state/DebugWorldState.h>
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include <td/game/World.h>
|
||||||
|
#include <td/protocol/packet/Packets.h>
|
||||||
|
#include <td/render/renderer/EntityRenderer.h>
|
||||||
|
#include <td/render/renderer/PlayerListRenderer.h>
|
||||||
|
#include <td/render/renderer/TowerRenderer.h>
|
||||||
|
#include <td/render/renderer/WorldRenderer.h>
|
||||||
|
|
||||||
|
#include <server/Server.h>
|
||||||
|
#include <server/socket/FakeSocket.h>
|
||||||
|
#include <server/state/LobbyState.h>
|
||||||
|
|
||||||
|
#include <client/Client.h>
|
||||||
|
#include <client/socket/FakeSocket.h>
|
||||||
|
#include <client/state/GameState.h>
|
||||||
|
#include <client/state/LoggingState.h>
|
||||||
|
|
||||||
|
#include <td/display/Display.h>
|
||||||
|
#include <td/display/state/DebugWorldState.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
DebugWorldState::DebugWorldState(Display& a_Display) : DisplayState(a_Display) {
|
||||||
|
// server
|
||||||
|
m_ServerSocket = std::make_shared<server::FakeSocket>();
|
||||||
|
m_Server = std::make_unique<server::Server>(m_ServerSocket);
|
||||||
|
|
||||||
|
// client
|
||||||
|
auto clientFakeSocket = client::FakeSocket::Connect(m_ServerSocket);
|
||||||
|
m_Client = std::make_unique<client::Client>(clientFakeSocket);
|
||||||
|
|
||||||
|
// TODO: make it better
|
||||||
|
m_Client->OnStateChange.Connect([this](client::Client::State& a_State) {
|
||||||
|
if (auto gameState = dynamic_cast<client::GameState*>(&a_State)) {
|
||||||
|
// render
|
||||||
|
auto clientWorld = gameState->GetWorld();
|
||||||
|
m_Renderer.AddRenderer<render::WorldRenderer>(m_Camera, clientWorld);
|
||||||
|
m_Renderer.AddRenderer<render::EntityRenderer>(m_Camera, clientWorld);
|
||||||
|
m_Renderer.AddRenderer<render::TowerRenderer>(m_Camera, clientWorld);
|
||||||
|
|
||||||
|
auto& list = m_Renderer.AddRenderer<render::PlayerListRenderer>(m_Client->GetPlayers());
|
||||||
|
|
||||||
|
list.OnPlayerCreate.Connect([this]() {
|
||||||
|
auto newSocket = client::FakeSocket::Connect(m_ServerSocket);
|
||||||
|
auto newClient = std::make_unique<client::Client>(newSocket);
|
||||||
|
newClient->ChangeState<client::LoggingState>("Bot");
|
||||||
|
m_FakeClients.push_back(std::move(newClient));
|
||||||
|
});
|
||||||
|
|
||||||
|
list.OnPlayerKick.Connect([this](PlayerID a_Player) {
|
||||||
|
auto it = std::find_if(m_FakeClients.begin(), m_FakeClients.end(), [a_Player](auto& clientPtr){
|
||||||
|
if (!clientPtr->GetId().has_value())
|
||||||
|
return false;
|
||||||
|
return clientPtr->GetId().value() == a_Player;
|
||||||
|
});
|
||||||
|
m_FakeClients.erase(it);
|
||||||
|
});
|
||||||
|
|
||||||
|
// update state
|
||||||
|
m_ClientState = gameState;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
m_Client->ChangeState<client::LoggingState>("Player0");
|
||||||
|
|
||||||
|
// camera
|
||||||
|
m_Camera.SetCamPos({77, 7, 13});
|
||||||
|
m_Camera.UpdatePerspective(m_StateMachine.GetAspectRatio());
|
||||||
|
}
|
||||||
|
|
||||||
|
void DebugWorldState::Update(float a_Delta) {
|
||||||
|
m_Server->Update(a_Delta);
|
||||||
|
m_Client->Update(a_Delta);
|
||||||
|
if (m_ClientState)
|
||||||
|
m_Renderer.Render(m_ClientState->GetCurrentLerp());
|
||||||
|
}
|
||||||
|
|
||||||
|
void DebugWorldState::OnAspectRatioChange(float a_Ratio) {
|
||||||
|
m_Camera.UpdatePerspective(a_Ratio);
|
||||||
|
}
|
||||||
|
|
||||||
|
void DebugWorldState::OnKeyDown(SDL_Keycode a_Key) {
|
||||||
|
// temporary tests
|
||||||
|
switch (a_Key) {
|
||||||
|
case SDLK_A:
|
||||||
|
m_Client->SendPacket(td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1));
|
||||||
|
break;
|
||||||
|
|
||||||
|
case SDLK_Z:
|
||||||
|
m_Client->SendPacket(td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13)));
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
DebugWorldState::~DebugWorldState() {}
|
||||||
|
|
||||||
|
} // namespace td
|
||||||
42
src/td/display/state/MainMenuState.cpp
Normal file
42
src/td/display/state/MainMenuState.cpp
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
#include <td/display/state/MainMenuState.h>
|
||||||
|
|
||||||
|
#include <imgui.h>
|
||||||
|
#include <td/display/menu/MainMenu.h>
|
||||||
|
#include <td/display/state/DebugWorldState.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
MainMenuState::MainMenuState(Display& a_Display) : DisplayState(a_Display) {
|
||||||
|
PushState<MainMenu>();
|
||||||
|
}
|
||||||
|
|
||||||
|
MainMenuState::~MainMenuState() {}
|
||||||
|
|
||||||
|
static int GetWindowFullScreenFlags() {
|
||||||
|
return ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoBackground;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void SetNextWindowFullScreen() {
|
||||||
|
const ImGuiViewport* viewport = ImGui::GetMainViewport();
|
||||||
|
ImGui::SetNextWindowPos(viewport->WorkPos);
|
||||||
|
ImGui::SetNextWindowSize(viewport->WorkSize);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainMenuState::Update(float a_Delta) {
|
||||||
|
SetNextWindowFullScreen();
|
||||||
|
ImGui::Begin("MainWindow", nullptr, GetWindowFullScreenFlags());
|
||||||
|
MainMenuStateStack::Update();
|
||||||
|
ImGui::End();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainMenuState::RenderBackButton() {
|
||||||
|
if (ImGui::Button("Back"))
|
||||||
|
PopState();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MainMenuState::OnKeyDown(SDL_Keycode a_Key) {
|
||||||
|
if (a_Key == SDLK_ESCAPE)
|
||||||
|
PopState();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace td
|
||||||
@@ -1,44 +0,0 @@
|
|||||||
#include <td/game/GameHistory.h>
|
|
||||||
|
|
||||||
namespace td {
|
|
||||||
namespace game {
|
|
||||||
|
|
||||||
GameHistory::GameHistory() : m_History(std::numeric_limits<HistorySizeType>::max()), m_Cursor(0) {}
|
|
||||||
|
|
||||||
void GameHistory::SetLockStep(HistorySizeType a_Index, protocol::LockStep&& a_LockStep) {
|
|
||||||
m_History[a_Index] = std::move(a_LockStep);
|
|
||||||
}
|
|
||||||
|
|
||||||
const protocol::LockStep& GameHistory::GetLockStep(HistorySizeType a_Index) const {
|
|
||||||
return *m_History[a_Index];
|
|
||||||
}
|
|
||||||
|
|
||||||
bool GameHistory::HasLockStep(HistorySizeType a_Index) const {
|
|
||||||
return m_History[a_Index].has_value();
|
|
||||||
}
|
|
||||||
|
|
||||||
const protocol::LockStep& GameHistory::GetNextStep() {
|
|
||||||
return GetLockStep(m_Cursor++);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool GameHistory::HasNextStep() const {
|
|
||||||
return HasLockStep(m_Cursor);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GameHistory::FromPacket(td::protocol::pdata::LockSteps&& a_Steps) {
|
|
||||||
for (int i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) {
|
|
||||||
protocol::LockStep& step = a_Steps.m_LockSteps[i];
|
|
||||||
SetLockStep(i + a_Steps.m_FirstFrameNumber, std::move(step));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
td::protocol::packets::LockStepsPacket GameHistory::ToPacket(HistorySizeType a_StartIndex) {
|
|
||||||
Array<protocol::LockStep, LOCKSTEP_BUFFER_SIZE> steps;
|
|
||||||
for (int i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) {
|
|
||||||
steps[i] = GetLockStep(a_StartIndex + i);
|
|
||||||
}
|
|
||||||
return {a_StartIndex, std::move(steps)};
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace game
|
|
||||||
} // namespace td
|
|
||||||
@@ -1,31 +1,44 @@
|
|||||||
#include <td/game/World.h>
|
#include <td/game/World.h>
|
||||||
|
|
||||||
|
#include <td/simulation/WorldTicker.h>
|
||||||
|
#include <td/protocol/packet/PacketSerialize.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace game {
|
namespace game {
|
||||||
|
|
||||||
World::World() : m_Teams{Team{TeamColor::Red}, Team{TeamColor::Blue}} {
|
class ColorTileVisitor : public TileHandler {
|
||||||
|
private:
|
||||||
}
|
const World& m_World;
|
||||||
|
const Color* m_Result;
|
||||||
|
|
||||||
const Color* World::GetTileColor(TilePtr tile) const {
|
public:
|
||||||
switch (tile->GetType()) {
|
ColorTileVisitor(const World& a_World) : m_World(a_World), m_Result(nullptr) {}
|
||||||
case TileType::Tower: {
|
|
||||||
TowerTile* towerTile = dynamic_cast<TowerTile*>(tile.get());
|
virtual void Handle(const EmptyTile& a_Tile) override {}
|
||||||
return &m_TowerPlacePalette[towerTile->color_palette_ref];
|
|
||||||
}
|
virtual void Handle(const TowerTile& a_Tile) override {
|
||||||
case TileType::Walk: {
|
m_Result = &m_World.GetTowerTileColorPalette()[a_Tile->m_ColorPaletteRef];
|
||||||
return &m_WalkablePalette;
|
|
||||||
}
|
|
||||||
case TileType::Decoration: {
|
|
||||||
DecorationTile* towerTile = dynamic_cast<DecorationTile*>(tile.get());
|
|
||||||
return &m_DecorationPalette[towerTile->color_palette_ref];
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default: {
|
|
||||||
return nullptr;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nullptr;
|
|
||||||
|
virtual void Handle(const WalkableTile& a_Tile) override {
|
||||||
|
m_Result = &m_World.GetWalkableTileColor();
|
||||||
|
}
|
||||||
|
|
||||||
|
virtual void Handle(const DecorationTile& a_Tile) override {
|
||||||
|
m_Result = &m_World.GetDecorationPalette()[a_Tile->m_ColorPaletteRef];
|
||||||
|
}
|
||||||
|
|
||||||
|
const Color* GetResult() {
|
||||||
|
return m_Result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
World::World() : m_CurrentState(std::make_shared<sim::WorldSnapshot>()), m_NextState(m_CurrentState) {}
|
||||||
|
|
||||||
|
const Color* World::GetTileColor(const TilePtr& tile) const {
|
||||||
|
ColorTileVisitor visitor(*this);
|
||||||
|
tile->Dispatch(visitor);
|
||||||
|
return visitor.GetResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool World::LoadMap(const protocol::pdata::WorldHeader& a_WorldHeader) {
|
bool World::LoadMap(const protocol::pdata::WorldHeader& a_WorldHeader) {
|
||||||
@@ -40,20 +53,41 @@ bool World::LoadMap(const protocol::pdata::WorldHeader& a_WorldHeader) {
|
|||||||
m_SpawnColorPalette = a_WorldHeader.m_SpawnColorPalette;
|
m_SpawnColorPalette = a_WorldHeader.m_SpawnColorPalette;
|
||||||
|
|
||||||
GetRedTeam().GetCastle() = a_WorldHeader.m_RedCastle;
|
GetRedTeam().GetCastle() = a_WorldHeader.m_RedCastle;
|
||||||
GetRedTeam().GetCastle().SetTeam(&GetRedTeam());
|
// GetRedTeam().GetCastle().SetTeam(&GetRedTeam());
|
||||||
|
|
||||||
GetBlueTeam().GetCastle() = a_WorldHeader.m_BlueCastle;
|
GetBlueTeam().GetCastle() = a_WorldHeader.m_BlueCastle;
|
||||||
GetBlueTeam().GetCastle().SetTeam(&GetBlueTeam());
|
// GetBlueTeam().GetCastle().SetTeam(&GetBlueTeam());
|
||||||
|
|
||||||
m_TilePalette = a_WorldHeader.m_TilePalette;
|
m_TilePalette = a_WorldHeader.m_TilePalette;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protocol::packets::WorldHeaderPacket World::GetPacketHeader() const {
|
||||||
|
return protocol::packets::WorldHeaderPacket(m_TowerPlacePalette, m_WalkablePalette, m_DecorationPalette, m_Background,
|
||||||
|
m_SpawnColorPalette, m_TilePalette, GetRedTeam().GetSpawn(), GetBlueTeam().GetSpawn(), GetRedTeam().GetCastle(),
|
||||||
|
GetBlueTeam().GetCastle());
|
||||||
|
}
|
||||||
|
|
||||||
|
protocol::packets::WorldDataPacket World::GetPacketData() const {
|
||||||
|
return protocol::packets::WorldDataPacket(m_Chunks);
|
||||||
|
}
|
||||||
|
|
||||||
bool World::LoadMap(const protocol::pdata::WorldData& a_WorldData) {
|
bool World::LoadMap(const protocol::pdata::WorldData& a_WorldData) {
|
||||||
m_Chunks = a_WorldData.m_Chunks;
|
m_Chunks = a_WorldData.m_Chunks;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::shared_ptr<sim::WorldSnapshot>& World::Tick(const protocol::LockStep& a_LockStep, FpFloat a_Delta) {
|
||||||
|
m_CurrentState = m_NextState;
|
||||||
|
m_NextState = std::make_shared<sim::WorldSnapshot>(m_Ticker.NextStep(*this, *m_NextState, a_LockStep, a_Delta));
|
||||||
|
return m_CurrentState;
|
||||||
|
}
|
||||||
|
|
||||||
|
void World::ResetSnapshots(std::shared_ptr<sim::WorldSnapshot>& a_Current, std::shared_ptr<sim::WorldSnapshot>& a_Next) {
|
||||||
|
m_CurrentState = a_Current;
|
||||||
|
m_NextState = a_Next;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace game
|
} // namespace game
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
37
src/td/game/WorldTypes.cpp
Normal file
37
src/td/game/WorldTypes.cpp
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#include <td/game/WorldTypes.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace game {
|
||||||
|
|
||||||
|
const int BITS_IN_BYTE = 8;
|
||||||
|
const int BITS_IN_LONG = BITS_IN_BYTE * sizeof(std::uint64_t);
|
||||||
|
|
||||||
|
static unsigned int countBits(unsigned int number) {
|
||||||
|
// log function in base 2
|
||||||
|
// take only integer part
|
||||||
|
return static_cast<unsigned int>(std::log2(number) + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TileIndex Chunk::GetTileIndex(std::uint16_t tileNumber) const {
|
||||||
|
const std::uint8_t bitsPerTile = countBits(m_Palette.size());
|
||||||
|
|
||||||
|
const std::size_t startLong = (tileNumber * bitsPerTile) / BITS_IN_LONG;
|
||||||
|
const std::size_t startOffset = (tileNumber * bitsPerTile) % BITS_IN_LONG;
|
||||||
|
const std::size_t endLong = ((tileNumber + 1) * bitsPerTile - 1) / BITS_IN_LONG;
|
||||||
|
|
||||||
|
const Chunk::ChunkData::value_type individualValueMask = ((1 << bitsPerTile) - 1);
|
||||||
|
|
||||||
|
td::game::Chunk::ChunkData::value_type value;
|
||||||
|
if (startLong == endLong) {
|
||||||
|
value = (m_Data[startLong] >> startOffset);
|
||||||
|
} else {
|
||||||
|
int endOffset = BITS_IN_LONG - startOffset;
|
||||||
|
value = (m_Data[startLong] >> startOffset | m_Data[endLong] << endOffset);
|
||||||
|
}
|
||||||
|
value &= individualValueMask;
|
||||||
|
|
||||||
|
return m_Palette.at(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace game
|
||||||
|
} // namespace td
|
||||||
9
src/td/protocol/packet/ChunkSerialize.cpp
Normal file
9
src/td/protocol/packet/ChunkSerialize.cpp
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
#include <td/protocol/packet/PacketSerialize.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace game {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace game
|
||||||
|
} // namespace td
|
||||||
33
src/td/protocol/packet/PacketSerialize.cpp
Normal file
33
src/td/protocol/packet/PacketSerialize.cpp
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
#include <td/protocol/packet/PacketData.h>
|
||||||
|
#include <td/protocol/packet/PacketSerialize.h>
|
||||||
|
|
||||||
|
#include <sp/common/DataBufferOperators.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace game {
|
||||||
|
|
||||||
|
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const TeamCastle& a_Castle) {
|
||||||
|
return a_Buffer << a_Castle.GetCenterX() << a_Castle.GetCenterY();
|
||||||
|
}
|
||||||
|
|
||||||
|
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, TeamCastle& a_Castle) {
|
||||||
|
float x, y;
|
||||||
|
a_Buffer >> x >> y;
|
||||||
|
a_Castle.SetCenter({x, y});
|
||||||
|
return a_Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Spawn& a_Spawn) {
|
||||||
|
return a_Buffer << a_Spawn.GetCenterX() << a_Spawn.GetCenterY();
|
||||||
|
}
|
||||||
|
|
||||||
|
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, Spawn& a_Spawn) {
|
||||||
|
float x, y;
|
||||||
|
a_Buffer >> x >> y;
|
||||||
|
a_Spawn.SetCenter({x, y});
|
||||||
|
return a_Buffer;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace game
|
||||||
|
} // 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