Compare commits
34 Commits
fa663d0481
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
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
|
|||
| cd03175b98 |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -8,4 +8,6 @@ build/
|
||||
|
||||
.vscode
|
||||
|
||||
imgui.ini
|
||||
imgui.ini
|
||||
|
||||
test/tdmap.tdmap3
|
||||
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" : {
|
||||
|
||||
}
|
||||
}
|
||||
24
include/client/Client.h
Normal file
24
include/client/Client.h
Normal file
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/IClientSocket.h>
|
||||
#include <td/common/StateMachine.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class ClientState;
|
||||
|
||||
class Client : public StateMachine<Client, void, float> {
|
||||
private:
|
||||
std::shared_ptr<IClientSocket> m_Socket;
|
||||
|
||||
public:
|
||||
Client(const std::shared_ptr<IClientSocket>& a_Socket) : m_Socket(a_Socket) {}
|
||||
|
||||
void SendPacket(const protocol::PacketBase& a_Packet);
|
||||
|
||||
friend class ClientState;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
20
include/client/ClientState.h
Normal file
20
include/client/ClientState.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/Client.h>
|
||||
#include <td/misc/SlotGuard.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class ClientState : public Client::State, private utils::SlotGuard {
|
||||
public:
|
||||
ClientState(Client& a_Client);
|
||||
virtual ~ClientState() {}
|
||||
|
||||
protected:
|
||||
void SendPacket(const protocol::PacketBase& a_Packet);
|
||||
virtual void HandlePacket(const protocol::PacketBase& a_Packet) {}
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
23
include/client/IClientSocket.h
Normal file
23
include/client/IClientSocket.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#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;
|
||||
|
||||
IClientSocket() {}
|
||||
virtual ~IClientSocket() {}
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
33
include/client/socket/FakeSocket.h
Normal file
33
include/client/socket/FakeSocket.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#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;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
31
include/client/state/GameState.h
Normal file
31
include/client/state/GameState.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#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:
|
||||
std::shared_ptr<game::World> m_World;
|
||||
sim::ClientSimulation m_Simulation;
|
||||
float m_CurrentLerp;
|
||||
|
||||
public:
|
||||
GameState(Client& a_Client, const std::shared_ptr<game::World>& a_World, std::uint64_t a_StepTime);
|
||||
~GameState() {}
|
||||
|
||||
virtual void Update(float a_Delta) override;
|
||||
|
||||
float GetCurrentLerp() const {
|
||||
return m_CurrentLerp;
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void HandlePacket(const protocol::PacketBase& a_Packet) override;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
33
include/server/IServerSocket.h
Normal file
33
include/server/IServerSocket.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <server/PlayerIds.h>
|
||||
#include <td/misc/Signal.h>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class IServerSocket {
|
||||
public:
|
||||
utils::Signal<PlayerID> OnConnect;
|
||||
utils::Signal<PlayerID> OnDisconnect;
|
||||
utils::Signal<PlayerID, const protocol::PacketBase&> OnReceive;
|
||||
|
||||
void Send(PlayerID a_PlayerId, const protocol::PacketBase& a_Packet);
|
||||
void Broadcast(const protocol::PacketBase& a_Packet);
|
||||
|
||||
IServerSocket() {}
|
||||
virtual ~IServerSocket() {}
|
||||
|
||||
private:
|
||||
PlayerIds m_Ids;
|
||||
|
||||
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
|
||||
22
include/server/Server.h
Normal file
22
include/server/Server.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <server/IServerSocket.h>
|
||||
#include <td/common/StateMachine.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class ServerState;
|
||||
|
||||
class Server : public StateMachine<Server, void, float> {
|
||||
private:
|
||||
std::shared_ptr<IServerSocket> m_Socket;
|
||||
|
||||
public:
|
||||
Server(const std::shared_ptr<IServerSocket>& a_Socket) : m_Socket(a_Socket) {}
|
||||
|
||||
friend class ServerState;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
23
include/server/ServerState.h
Normal file
23
include/server/ServerState.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#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;
|
||||
|
||||
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
|
||||
29
include/server/state/GameState.h
Normal file
29
include/server/state/GameState.h
Normal file
@@ -0,0 +1,29 @@
|
||||
#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;
|
||||
|
||||
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
|
||||
21
include/server/state/LobbyState.h
Normal file
21
include/server/state/LobbyState.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#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, const std::shared_ptr<game::World>& a_World);
|
||||
~LobbyState() {}
|
||||
|
||||
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
|
||||
|
||||
#include <cstdint>
|
||||
#include <sp/common/DataBufferOperators.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
@@ -183,6 +183,35 @@ T Lerp(T v0, T v1, T t) {
|
||||
|
||||
} // 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
|
||||
|
||||
@@ -12,6 +12,10 @@ namespace td {
|
||||
|
||||
using FpFloat = fpm::fixed_16_16;
|
||||
|
||||
using StepTime = std::uint16_t;
|
||||
|
||||
constexpr int STEP_TIME = 50;
|
||||
|
||||
enum class TeamColor : std::int8_t {
|
||||
None = -1,
|
||||
Blue,
|
||||
@@ -61,10 +65,7 @@ using TowerID = std::uint16_t;
|
||||
using PlayerID = std::uint8_t;
|
||||
using EntityID = std::uint16_t;
|
||||
|
||||
struct TowerCoords {
|
||||
std::int16_t x;
|
||||
std::int16_t y;
|
||||
};
|
||||
using TowerCoords = Vec2<std::int16_t>;
|
||||
|
||||
using EntityCoords = Vec2<FpFloat>;
|
||||
|
||||
|
||||
47
include/td/common/StateMachine.h
Normal file
47
include/td/common/StateMachine.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
|
||||
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>
|
||||
T* ChangeState(Args&&... args) {
|
||||
return m_StateMachine.template ChangeState<T>(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
protected:
|
||||
TDerived& m_StateMachine;
|
||||
};
|
||||
|
||||
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(args...);
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
T* ChangeState(Args&&... args) {
|
||||
m_State = std::make_unique<T>(static_cast<TDerived&>(*this), std::forward<Args>(args)...);
|
||||
return static_cast<T*>(m_State.get());
|
||||
}
|
||||
|
||||
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,39 +2,14 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <SDL3/SDL_video.h>
|
||||
#include <SDL3/SDL_keycode.h>
|
||||
#include <SDL3/SDL_video.h>
|
||||
#include <td/common/StateMachine.h>
|
||||
#include <td/misc/Signal.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
class Display {
|
||||
public:
|
||||
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();
|
||||
|
||||
bool IsCloseRequested() {
|
||||
return m_ShouldClose;
|
||||
}
|
||||
|
||||
float GetAspectRatio() {
|
||||
return m_AspectRatio;
|
||||
}
|
||||
|
||||
int GetWindowWidth() {
|
||||
return m_LastWidth;
|
||||
}
|
||||
|
||||
int GetWindowHeight() {
|
||||
return m_LastHeight;
|
||||
}
|
||||
|
||||
class Display : public StateMachine<Display, void, float> {
|
||||
private:
|
||||
SDL_Window* m_Window;
|
||||
SDL_GLContext m_GLContext;
|
||||
@@ -43,6 +18,33 @@ class Display {
|
||||
float m_AspectRatio;
|
||||
|
||||
bool m_ShouldClose;
|
||||
|
||||
public:
|
||||
utils::Signal<float> OnAspectRatioChange;
|
||||
utils::Signal<SDL_Keycode> OnKeyDown;
|
||||
|
||||
Display(int a_Width, int a_Height, const std::string& a_Title);
|
||||
~Display();
|
||||
|
||||
void PollEvents();
|
||||
void Update(float a_Delta) override;
|
||||
void Close();
|
||||
|
||||
bool IsCloseRequested() {
|
||||
return m_ShouldClose;
|
||||
}
|
||||
|
||||
float GetAspectRatio() {
|
||||
return m_AspectRatio;
|
||||
}
|
||||
|
||||
int GetWindowWidth() {
|
||||
return m_LastWidth;
|
||||
}
|
||||
|
||||
int GetWindowHeight() {
|
||||
return m_LastHeight;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace td
|
||||
16
include/td/display/DisplayState.h
Normal file
16
include/td/display/DisplayState.h
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/display/Display.h>
|
||||
#include <td/misc/SlotGuard.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
|
||||
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
|
||||
31
include/td/display/state/DebugWorldState.h
Normal file
31
include/td/display/state/DebugWorldState.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#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>
|
||||
|
||||
namespace td {
|
||||
|
||||
class DebugWorldState : public DisplayState {
|
||||
private:
|
||||
render::RenderPipeline m_Renderer;
|
||||
render::Camera m_Camera;
|
||||
std::unique_ptr<client::Client> m_Client;
|
||||
std::unique_ptr<server::Server> m_Server;
|
||||
client::GameState* m_ClientState;
|
||||
|
||||
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,25 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/simulation/WorldTicker.h>
|
||||
#include <td/game/WorldTypes.h>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
#include <td/simulation/WorldTicker.h>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
class World {
|
||||
protected:
|
||||
// header
|
||||
TowerTileColorPalette m_TowerPlacePalette;
|
||||
Color m_WalkablePalette;
|
||||
std::vector<Color> m_DecorationPalette;
|
||||
Color m_Background;
|
||||
|
||||
ChunkList m_Chunks;
|
||||
|
||||
SpawnColorPalette m_SpawnColorPalette;
|
||||
|
||||
TilePalette m_TilePalette;
|
||||
|
||||
//data
|
||||
ChunkList m_Chunks;
|
||||
|
||||
std::shared_ptr<sim::WorldSnapshot> m_CurrentState;
|
||||
std::shared_ptr<sim::WorldSnapshot> m_NextState;
|
||||
|
||||
@@ -28,6 +28,7 @@ class World {
|
||||
|
||||
public:
|
||||
World();
|
||||
World(World&&) = default;
|
||||
|
||||
bool LoadMap(const protocol::pdata::WorldHeader& worldHeader);
|
||||
bool LoadMap(const protocol::pdata::WorldData& worldData);
|
||||
@@ -61,7 +62,7 @@ class World {
|
||||
|
||||
TilePtr GetTilePtr(TileIndex index) const {
|
||||
if (index == 0)
|
||||
return nullptr;
|
||||
return TilePtr(nullptr);
|
||||
return m_TilePalette.at(index - 1);
|
||||
}
|
||||
|
||||
@@ -130,12 +131,9 @@ class World {
|
||||
private:
|
||||
void TickMobs(std::uint64_t delta);
|
||||
void CleanDeadMobs();
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
using WorldPtr = std::shared_ptr<World>;
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
} // namespace td
|
||||
@@ -67,7 +67,7 @@ using TileFactory = sp::MessageFactory<Tile, AllTiles>;
|
||||
|
||||
class TileHandler : public sp::GenericHandler<AllTiles> {};
|
||||
|
||||
using TilePtr = std::shared_ptr<sp::SerializableMessage<TileFactory>>;
|
||||
using TilePtr = sp::SerializableMessage<TileFactory>;
|
||||
|
||||
// typedef std::shared_ptr<Tile> TilePtr;
|
||||
typedef std::vector<std::uint16_t> ChunkPalette;
|
||||
|
||||
@@ -8,16 +8,27 @@
|
||||
namespace td {
|
||||
namespace utils {
|
||||
|
||||
/**
|
||||
* \brief Signal class
|
||||
*/
|
||||
template <typename... Args>
|
||||
class Signal : private NonCopyable {
|
||||
private:
|
||||
using CallBack = std::function<void(Args...)>;
|
||||
class SignalRaw : private NonCopyable {
|
||||
public:
|
||||
using FnType = void(Args...);
|
||||
using CallBack = std::function<FnType>;
|
||||
|
||||
private:
|
||||
std::vector<CallBack> m_Callbacks;
|
||||
|
||||
public:
|
||||
void Connect(CallBack&& a_Callback) {
|
||||
m_Callbacks.push_back(std::move(a_Callback));
|
||||
void Connect(const CallBack& a_Callback) {
|
||||
m_Callbacks.push_back(a_Callback);
|
||||
}
|
||||
|
||||
void Disconnect(const CallBack& a_Callback) {
|
||||
auto it = std::find_if(m_Callbacks.begin(), m_Callbacks.end(),
|
||||
[&a_Callback](CallBack& callback) { return a_Callback.template target<FnType>() == callback.template target<FnType>(); });
|
||||
m_Callbacks.erase(it);
|
||||
}
|
||||
|
||||
void operator()(Args... args) const {
|
||||
@@ -27,5 +38,63 @@ class Signal : private NonCopyable {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* \brief Memory managed Signal class
|
||||
*/
|
||||
template <typename... Args>
|
||||
class Signal {
|
||||
public:
|
||||
using SignalBase = SignalRaw<Args...>;
|
||||
using CallBack = typename SignalBase::CallBack;
|
||||
using SignalPtr = std::shared_ptr<SignalBase>;
|
||||
class ConnectionGuard;
|
||||
|
||||
private:
|
||||
SignalPtr m_Signal;
|
||||
|
||||
public:
|
||||
Signal() : m_Signal(std::make_shared<SignalBase>()) {}
|
||||
Signal(const Signal&) = default;
|
||||
|
||||
void Connect(const CallBack& a_Callback) {
|
||||
m_Signal->Connect(a_Callback);
|
||||
}
|
||||
|
||||
[[nodiscard]] std::unique_ptr<ConnectionGuard> ConnectSafe(const CallBack& a_Callback) {
|
||||
Connect(a_Callback);
|
||||
return std::make_unique<ConnectionGuard>(*this, a_Callback);
|
||||
}
|
||||
|
||||
void Disconnect(const CallBack& a_Callback) {
|
||||
m_Signal->Disconnect(a_Callback);
|
||||
}
|
||||
|
||||
void operator()(Args... args) const {
|
||||
m_Signal->operator()(args...);
|
||||
}
|
||||
};
|
||||
|
||||
class Connection {
|
||||
public:
|
||||
virtual ~Connection() {}
|
||||
};
|
||||
|
||||
template <typename... Args>
|
||||
class Signal<Args...>::ConnectionGuard : public Connection {
|
||||
private:
|
||||
using CallBack = typename Signal<Args...>::CallBack;
|
||||
|
||||
std::weak_ptr<SignalRaw<Args...>> m_Signal;
|
||||
CallBack m_Callback;
|
||||
|
||||
public:
|
||||
ConnectionGuard(const Signal<Args...>& a_Signal, const CallBack& a_Callback) : m_Signal(a_Signal.m_Signal), m_Callback(a_Callback) {}
|
||||
|
||||
~ConnectionGuard() {
|
||||
if (!m_Signal.expired())
|
||||
m_Signal.lock()->Disconnect(m_Callback);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace utils
|
||||
} // namespace td
|
||||
|
||||
32
include/td/misc/SlotGuard.h
Normal file
32
include/td/misc/SlotGuard.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#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& a_Callback) {
|
||||
m_Connections.push_back(a_Signal.ConnectSafe(a_Callback));
|
||||
}
|
||||
|
||||
void Disconnect() {
|
||||
m_Connections.clear();
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace utils
|
||||
} // 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
|
||||
@@ -21,10 +21,8 @@ struct UpgradeTower {
|
||||
sp::BitField<std::uint8_t, 4> m_Upgrade;
|
||||
};
|
||||
|
||||
using EntityTypeInt = std::uint8_t;
|
||||
|
||||
struct SpawnTroop {
|
||||
sp::BitField<EntityTypeInt, 5> m_Type;
|
||||
sp::BitField<EntityType, 5> m_Type;
|
||||
sp::BitField<std::uint8_t, 3> m_Level;
|
||||
EntityCoords m_Position;
|
||||
PlayerID m_Sender;
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
#include <memory>
|
||||
#include <sp/common/GenericHandler.h>
|
||||
#include <sp/io/SerializableMessage.h>
|
||||
#include <sp/protocol/ConcreteMessage.h>
|
||||
#include <sp/protocol/MessageDispatcher.h>
|
||||
#include <sp/protocol/MessageFactory.h>
|
||||
@@ -15,6 +14,8 @@
|
||||
#include <td/common/NonCopyable.h>
|
||||
#include <td/protocol/command/CommandData.h>
|
||||
|
||||
#include <sp/io/SerializableMessage.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
@@ -58,9 +59,9 @@ using CommandDispatcher = sp::MessageDispatcher<CommandBase>;
|
||||
|
||||
using CommandFactory = sp::MessageFactory<CommandBase, AllCommands>;
|
||||
|
||||
using LockStep = std::vector<std::shared_ptr<CommandBase>>;
|
||||
using CommandPtr = sp::SerializableMessage<CommandFactory>;
|
||||
|
||||
using CommandPtr = std::unique_ptr<sp::SerializableMessage<CommandFactory>>;
|
||||
using LockStep = std::vector<CommandPtr>;
|
||||
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
|
||||
@@ -7,7 +7,11 @@
|
||||
#include <td/game/WorldTypes.h>
|
||||
|
||||
// Make it dynamic ?
|
||||
#ifdef NDEBUG
|
||||
#define LOCKSTEP_BUFFER_SIZE 10
|
||||
#else
|
||||
#define LOCKSTEP_BUFFER_SIZE 1
|
||||
#endif
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
@@ -45,7 +49,7 @@ struct PlayerLeave {
|
||||
|
||||
struct PredictCommand {
|
||||
CommandPtr m_Command;
|
||||
std::uint16_t m_FrameNumber;
|
||||
StepTime m_FrameNumber;
|
||||
};
|
||||
|
||||
/** Keep alive */
|
||||
@@ -73,8 +77,8 @@ struct BeginGame {
|
||||
};
|
||||
|
||||
struct LockSteps {
|
||||
std::uint16_t m_FirstFrameNumber;
|
||||
Array<LockStep, LOCKSTEP_BUFFER_SIZE> m_LockSteps;
|
||||
StepTime m_FirstFrameNumber;
|
||||
std::array<LockStep, LOCKSTEP_BUFFER_SIZE> m_LockSteps;
|
||||
};
|
||||
|
||||
struct WorldHeader {
|
||||
@@ -95,6 +99,25 @@ struct WorldData {
|
||||
game::ChunkList m_Chunks;
|
||||
};
|
||||
|
||||
// TODO: spawn multiple troops at the same time
|
||||
struct SpawnTroop {
|
||||
sp::BitField<EntityType, 5> m_Type;
|
||||
sp::BitField<std::uint8_t, 3> m_Level;
|
||||
};
|
||||
|
||||
struct PlaceTower {
|
||||
TowerType m_Type;
|
||||
TowerCoords m_Position;
|
||||
};
|
||||
|
||||
struct LockStepRequest {
|
||||
std::vector<StepTime> m_Missing;
|
||||
};
|
||||
|
||||
struct LockStepResponse {
|
||||
std::map<StepTime, LockStep> m_Steps;
|
||||
};
|
||||
|
||||
} // namespace pdata
|
||||
} // namespace protocol
|
||||
} // namespace td
|
||||
|
||||
@@ -4,44 +4,15 @@
|
||||
|
||||
namespace td {
|
||||
|
||||
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 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 ChunkPtr& a_Chunk);
|
||||
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, ChunkPtr& a_Chunk);
|
||||
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/protocol/packet/PacketData.h>
|
||||
|
||||
#include <sp/common/GenericHandler.h>
|
||||
#include <sp/protocol/ConcreteMessage.h>
|
||||
#include <sp/protocol/MessageBase.h>
|
||||
#include <sp/protocol/MessageDispatcher.h>
|
||||
#include <sp/protocol/MessageFactory.h>
|
||||
#include <sp/common/GenericHandler.h>
|
||||
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
@@ -22,12 +22,16 @@ enum class PacketID : std::uint8_t {
|
||||
BeginGame,
|
||||
Disconnect,
|
||||
KeepAlive,
|
||||
LockStepRequest,
|
||||
LockStepResponse,
|
||||
LockSteps,
|
||||
LoggingSuccess,
|
||||
PlaceTower,
|
||||
PlayerJoin,
|
||||
PlayerLeave,
|
||||
PlayerLogin,
|
||||
PredictCommand,
|
||||
SpawnTroop,
|
||||
WorldHeader,
|
||||
WorldData,
|
||||
};
|
||||
@@ -47,20 +51,25 @@ using BeginGamePacket = PacketMessage<pdata::BeginGame, PacketID::BeginGame>;
|
||||
using ChatMessagePacket = PacketMessage<pdata::ChatMessage, PacketID::ChatMessage>;
|
||||
using DisconnectPacket = PacketMessage<pdata::Disconnect, PacketID::Disconnect>;
|
||||
using KeepAlivePacket = PacketMessage<pdata::KeepAlive, PacketID::KeepAlive>;
|
||||
using LockStepRequestPacket = PacketMessage<pdata::LockStepRequest, PacketID::LockStepRequest>;
|
||||
using LockStepResponsePacket = PacketMessage<pdata::LockStepResponse, PacketID::LockStepResponse>;
|
||||
using LockStepsPacket = PacketMessage<pdata::LockSteps, PacketID::LockSteps>;
|
||||
using LoggingSuccessPacket = PacketMessage<pdata::LoggingSuccess, PacketID::LoggingSuccess>;
|
||||
using PlaceTowerPacket = PacketMessage<pdata::PlaceTower, PacketID::PlaceTower>;
|
||||
using PlayerJoinPacket = PacketMessage<pdata::PlayerJoin, PacketID::PlayerJoin>;
|
||||
using PlayerLeavePacket = PacketMessage<pdata::PlayerLeave, PacketID::PlayerLeave>;
|
||||
using PlayerLoginPacket = PacketMessage<pdata::PlayerLogin, PacketID::PlayerLogin>;
|
||||
using PredictCommandPacket = PacketMessage<pdata::PredictCommand, PacketID::PredictCommand>;
|
||||
using SpawnTroopPacket = PacketMessage<pdata::SpawnTroop, PacketID::SpawnTroop>;
|
||||
using WorldHeaderPacket = PacketMessage<pdata::WorldHeader, PacketID::WorldHeader>;
|
||||
using WorldDataPacket = PacketMessage<pdata::WorldData, PacketID::WorldData>;
|
||||
|
||||
} // namespace packets
|
||||
|
||||
using AllPackets = std::tuple<packets::BeginGamePacket, packets::ChatMessagePacket, packets::DisconnectPacket,
|
||||
packets::KeepAlivePacket, packets::LockStepsPacket, packets::LoggingSuccessPacket, packets::PlayerJoinPacket,
|
||||
packets::PlayerLeavePacket, packets::PlayerLoginPacket, packets::PredictCommandPacket, packets::WorldHeaderPacket, packets::WorldDataPacket>;
|
||||
packets::KeepAlivePacket, packets::LockStepRequestPacket, packets::LockStepResponsePacket, packets::LockStepsPacket, packets::LoggingSuccessPacket, packets::PlaceTowerPacket,
|
||||
packets::PlayerJoinPacket, packets::PlayerLeavePacket, packets::PlayerLoginPacket, packets::PredictCommandPacket,
|
||||
packets::SpawnTroopPacket, packets::WorldHeaderPacket, packets::WorldDataPacket>;
|
||||
|
||||
class PacketHandler : public sp::GenericHandler<AllPackets> {};
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <td/render/Camera.h>
|
||||
#include <td/render/loader/GLLoader.h>
|
||||
#include <td/render/shader/CameraShaderProgram.h>
|
||||
#include <td/misc/SlotGuard.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
@@ -17,7 +18,7 @@ class BasicRenderer {
|
||||
};
|
||||
|
||||
template <typename TShader>
|
||||
class Renderer : public BasicRenderer {
|
||||
class Renderer : public BasicRenderer, private utils::SlotGuard {
|
||||
protected:
|
||||
std::unique_ptr<TShader> m_Shader;
|
||||
Camera& m_Camera;
|
||||
@@ -62,12 +63,12 @@ class RenderPipeline {
|
||||
|
||||
template <typename TShader>
|
||||
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->SetProjectionMatrix(m_Camera.GetProjectionMatrix());
|
||||
});
|
||||
|
||||
a_Camera.OnViewChange.Connect([this]() {
|
||||
Connect(a_Camera.OnViewChange, [this]() {
|
||||
m_Shader->Start();
|
||||
m_Shader->SetViewMatrix(m_Camera.GetViewMatrix());
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
/**
|
||||
* \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
|
||||
@@ -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
|
||||
@@ -1,62 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/game/World.h>
|
||||
#include <optional>
|
||||
|
||||
namespace td {
|
||||
namespace sim {
|
||||
|
||||
using GameHistory = std::vector<td::protocol::LockStep>;
|
||||
using GameBuffer = std::vector<std::optional<td::protocol::LockStep>>;
|
||||
|
||||
class RealTimeSimulation {
|
||||
private:
|
||||
std::uint64_t m_StepTime;
|
||||
game::World& m_World;
|
||||
GameBuffer m_History;
|
||||
std::uint64_t m_CurrentTime;
|
||||
std::uint64_t m_LastTime;
|
||||
std::size_t m_CurrentStep;
|
||||
|
||||
std::shared_ptr<WorldSnapshot> m_LastSnapshot;
|
||||
std::uint64_t m_LastValidStep;
|
||||
|
||||
static const protocol::LockStep EMPTY_LOCKSTEP;
|
||||
|
||||
public:
|
||||
/**
|
||||
* \brief Replay constructor
|
||||
* \param a_StepTime in ms
|
||||
*/
|
||||
RealTimeSimulation(game::World& a_World, const GameHistory& a_History, std::uint64_t a_StepTime);
|
||||
|
||||
/**
|
||||
* \brief Live update constructor (continuous game updates)
|
||||
* \param a_StepTime in ms
|
||||
*/
|
||||
RealTimeSimulation(game::World& a_World, std::uint64_t a_StepTime);
|
||||
|
||||
/**
|
||||
* \return the progress [0-1] between two steps
|
||||
*/
|
||||
float Update();
|
||||
|
||||
void HandlePacket(const protocol::packets::LockStepsPacket& a_LockSteps);
|
||||
void HandlePacket(const protocol::packets::PredictCommandPacket& a_Predict);
|
||||
|
||||
private:
|
||||
void Step();
|
||||
|
||||
/**
|
||||
* \brief Ticks a_Count times
|
||||
*/
|
||||
void 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
|
||||
39
include/td/simulation/ServerSimulation.h
Normal file
39
include/td/simulation/ServerSimulation.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#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<td::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();
|
||||
|
||||
// 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
|
||||
11
src/client/Client.cpp
Normal file
11
src/client/Client.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <client/Client.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
void Client::SendPacket(const protocol::PacketBase& a_Packet) {
|
||||
m_Socket->Send(a_Packet);
|
||||
}
|
||||
|
||||
} // 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::HandlePacket, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
void ClientState::SendPacket(const protocol::PacketBase& a_Packet) {
|
||||
m_StateMachine.m_Socket->Send(a_Packet);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
19
src/client/socket/FakeSocket.cpp
Normal file
19
src/client/socket/FakeSocket.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#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;
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
41
src/client/state/GameState.cpp
Normal file
41
src/client/state/GameState.cpp
Normal file
@@ -0,0 +1,41 @@
|
||||
#include <client/state/GameState.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class ClientHandler : public protocol::PacketHandler {
|
||||
private:
|
||||
sim::ClientSimulation& m_Simulation;
|
||||
|
||||
using protocol::PacketHandler::Handle;
|
||||
|
||||
public:
|
||||
ClientHandler(sim::ClientSimulation& a_Simulation) : m_Simulation(a_Simulation) {}
|
||||
|
||||
void Handle(const protocol::packets::LockStepsPacket& a_LockStep) {
|
||||
m_Simulation.Handle(a_LockStep);
|
||||
}
|
||||
|
||||
void Handle(const protocol::packets::LockStepResponsePacket& a_LockStep) {
|
||||
m_Simulation.Handle(a_LockStep);
|
||||
}
|
||||
};
|
||||
|
||||
GameState::GameState(Client& a_Client, const std::shared_ptr<game::World>& a_World, std::uint64_t a_StepTime) :
|
||||
ClientState(a_Client), m_World(a_World), m_Simulation(a_World, a_StepTime) {
|
||||
m_Simulation.OnMissingLockSteps.Connect([this](const std::vector<td::StepTime>& a_MissingSteps) {
|
||||
SendPacket(protocol::packets::LockStepRequestPacket(a_MissingSteps));
|
||||
});
|
||||
}
|
||||
|
||||
void GameState::HandlePacket(const protocol::PacketBase& a_Packet) {
|
||||
ClientHandler handler(m_Simulation);
|
||||
a_Packet.Dispatch(handler);
|
||||
}
|
||||
|
||||
void GameState::Update(float a_Delta) {
|
||||
m_CurrentLerp = m_Simulation.Update(a_Delta);
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
133
src/main.cpp
133
src/main.cpp
@@ -1,135 +1,24 @@
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <td/display/state/MainMenuState.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <td/game/World.h>
|
||||
#include <td/input/Display.h>
|
||||
#include <td/protocol/packet/PacketSerialize.h>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
#include <td/render/renderer/EntityRenderer.h>
|
||||
#include <td/render/renderer/TowerRenderer.h>
|
||||
#include <td/render/renderer/WorldRenderer.h>
|
||||
#include <td/simulation/RealTimeSimulation.h>
|
||||
|
||||
#include <sp/common/DataBuffer.h>
|
||||
#include <sp/extensions/Compress.h>
|
||||
#include <sp/io/MessageStream.h>
|
||||
#include <sp/io/StdIo.h>
|
||||
|
||||
class WorldApply : public td::protocol::PacketHandler {
|
||||
private:
|
||||
td::game::World& m_World;
|
||||
using td::protocol::PacketHandler::Handle;
|
||||
|
||||
public:
|
||||
WorldApply(td::game::World& a_World) : m_World(a_World) {}
|
||||
|
||||
void Handle(const td::protocol::packets::WorldHeaderPacket& a_Header) override {
|
||||
m_World.LoadMap(*a_Header);
|
||||
}
|
||||
|
||||
void Handle(const td::protocol::packets::WorldDataPacket& a_Data) override {
|
||||
m_World.LoadMap(*a_Data);
|
||||
}
|
||||
};
|
||||
|
||||
void Save(const td::protocol::PacketBase& header, const td::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<td::protocol::PacketFactory> stream(std::move(out), std::move(comp));
|
||||
|
||||
stream.WriteMessage(header);
|
||||
stream.WriteMessage(data);
|
||||
float GetDelta() {
|
||||
static std::chrono::time_point<std::chrono::system_clock> m_LastTime = std::chrono::system_clock::now();
|
||||
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;
|
||||
}
|
||||
|
||||
td::game::World GetWorld() {
|
||||
auto comp = std::make_shared<sp::ZlibCompress>();
|
||||
|
||||
std::ifstream fStream("test/tdmap.tdmap2");
|
||||
auto out = std::make_shared<sp::StdInput>(fStream);
|
||||
|
||||
sp::MessageStream<td::protocol::PacketFactory> stream(std::move(out), std::move(comp));
|
||||
|
||||
auto header = stream.ReadMessage();
|
||||
auto data = stream.ReadMessage();
|
||||
|
||||
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);
|
||||
|
||||
Save(*header, *data);
|
||||
|
||||
return w;
|
||||
}
|
||||
|
||||
void FastForward(td::game::World& a_World, const td::sim::GameHistory& a_LockSteps) {
|
||||
const td::FpFloat delta = td::FpFloat(1) / td::FpFloat(75);
|
||||
for (const auto& lockstep : a_LockSteps) {
|
||||
a_World.Tick(lockstep, delta);
|
||||
}
|
||||
}
|
||||
|
||||
td::sim::GameHistory GetCustomHistory() {
|
||||
constexpr std::size_t MAX_COUNT = 20 * 60 * 40;
|
||||
|
||||
td::sim::GameHistory gh(MAX_COUNT);
|
||||
|
||||
auto spawn = std::make_shared<td::protocol::commands::SpawnTroopCommand>(0, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0);
|
||||
gh[0].push_back(spawn);
|
||||
|
||||
auto tower = std::make_shared<td::protocol::commands::PlaceTowerCommand>(td::TowerType::Archer, 0, td::TowerCoords{77, 13});
|
||||
gh[0].push_back(tower);
|
||||
|
||||
return gh;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
td::game::World w = GetWorld();
|
||||
|
||||
// init GL context
|
||||
td::Display display(1920, 1080, "Tower-Defense 2");
|
||||
|
||||
td::render::Camera cam;
|
||||
|
||||
display.OnAspectRatioChange.Connect([&cam](float a_AspectRatio) { cam.UpdatePerspective(a_AspectRatio); });
|
||||
|
||||
td::sim::GameHistory gh = GetCustomHistory();
|
||||
|
||||
td::render::RenderPipeline renderer;
|
||||
renderer.AddRenderer<td::render::WorldRenderer>(cam, w);
|
||||
renderer.AddRenderer<td::render::EntityRenderer>(cam, w);
|
||||
renderer.AddRenderer<td::render::TowerRenderer>(cam, w);
|
||||
|
||||
cam.SetCamPos({77, 5, 13});
|
||||
cam.UpdatePerspective(display.GetAspectRatio());
|
||||
|
||||
td::sim::RealTimeSimulation simulation(w, 500);
|
||||
|
||||
display.OnKeyDown.Connect([&simulation](SDL_Keycode key) {
|
||||
if (key == SDLK_A) {
|
||||
auto spawn =
|
||||
std::make_shared<td::protocol::commands::SpawnTroopCommand>(0, 0, td::Vec2fp{td::FpFloat(77), td::FpFloat(13)}, 0);
|
||||
td::Array<td::protocol::LockStep, LOCKSTEP_BUFFER_SIZE> steps{};
|
||||
steps[0].push_back(spawn);
|
||||
td::protocol::packets::LockStepsPacket packet{0, steps};
|
||||
simulation.HandlePacket(packet);
|
||||
}
|
||||
});
|
||||
display.ChangeState<td::MainMenuState>();
|
||||
|
||||
while (!display.IsCloseRequested()) {
|
||||
display.PollEvents();
|
||||
float lerp = simulation.Update();
|
||||
renderer.Render(lerp);
|
||||
display.Update();
|
||||
float delta = GetDelta();
|
||||
display.Update(delta);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
31
src/server/IServerSocket.cpp
Normal file
31
src/server/IServerSocket.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <server/IServerSocket.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
void IServerSocket::OnConnectPeer(PeerID a_PeerId) {
|
||||
m_Ids.AddConnection(a_PeerId);
|
||||
OnConnect(m_Ids.GetPlayerId(a_PeerId));
|
||||
}
|
||||
|
||||
void IServerSocket::OnDisconnectPeer(PeerID a_PeerId) {
|
||||
OnDisconnect(m_Ids.GetPlayerId(a_PeerId));
|
||||
m_Ids.RemovePeer(a_PeerId);
|
||||
}
|
||||
|
||||
void IServerSocket::OnReceivePeer(PeerID a_PeerId, const protocol::PacketBase& a_Packet) {
|
||||
OnReceive(m_Ids.GetPlayerId(a_PeerId), 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);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
9
src/server/Server.cpp
Normal file
9
src/server/Server.cpp
Normal file
@@ -0,0 +1,9 @@
|
||||
#include <server/Server.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
24
src/server/ServerState.cpp
Normal file
24
src/server/ServerState.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <server/ServerState.h>
|
||||
#include <server/Server.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));
|
||||
}
|
||||
|
||||
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
|
||||
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
|
||||
31
src/server/state/GameState.cpp
Normal file
31
src/server/state/GameState.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <server/state/GameState.h>
|
||||
#include <server/state/GameStateHandler.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 << "Switched to Game state !\n";
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
} // 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
|
||||
18
src/server/state/LobbyState.cpp
Normal file
18
src/server/state/LobbyState.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <server/state/LobbyState.h>
|
||||
#include <server/state/GameState.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
void LobbyState::HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) {
|
||||
|
||||
}
|
||||
|
||||
void LobbyState::Update(float a_Delta) {
|
||||
m_StateMachine.ChangeState<GameState>(m_World);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <sp/common/DataBuffer.h>
|
||||
#include <sp/common/ByteSwapping.h>
|
||||
#include <sp/common/DataBufferOperators.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include <td/input/Display.h>
|
||||
#include <td/display/Display.h>
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include <SDL3/SDL.h>
|
||||
@@ -38,6 +38,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_STENCIL_SIZE, 8);
|
||||
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
|
||||
|
||||
m_GLContext = SDL_GL_CreateContext(m_Window);
|
||||
|
||||
if (!m_GLContext) {
|
||||
@@ -46,6 +49,8 @@ Display::Display(int a_Width, int a_Height, const std::string& a_Title) :
|
||||
|
||||
int major, minor, mask;
|
||||
int r, g, b, a, depth;
|
||||
int mBuffers, mSamples;
|
||||
|
||||
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &mask);
|
||||
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
|
||||
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
|
||||
@@ -57,6 +62,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_MULTISAMPLEBUFFERS, &mBuffers);
|
||||
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &mSamples);
|
||||
|
||||
const char* mask_desc;
|
||||
|
||||
if (mask & SDL_GL_CONTEXT_PROFILE_CORE) {
|
||||
@@ -72,6 +80,8 @@ Display::Display(int a_Width, int a_Height, const std::string& a_Title) :
|
||||
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));
|
||||
|
||||
utils::LOG(utils::Format("MultiSamples : Buffers : %i, Samples : %i", mBuffers, mSamples));
|
||||
|
||||
SDL_GL_MakeCurrent(m_Window, m_GLContext);
|
||||
|
||||
GLenum error = glewInit();
|
||||
@@ -81,6 +91,9 @@ Display::Display(int a_Width, int a_Height, const std::string& a_Title) :
|
||||
|
||||
// WindowResizeEvent(WindowWidth, WindowHeight);
|
||||
|
||||
// vsync
|
||||
SDL_GL_SetSwapInterval(1);
|
||||
|
||||
// Setup Dear ImGui context
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
@@ -111,6 +124,9 @@ Display::Display(int a_Width, int a_Height, const std::string& a_Title) :
|
||||
ImGui_ImplOpenGL3_Init("#version 330");
|
||||
}
|
||||
|
||||
void Display::Close() {
|
||||
m_ShouldClose = true;
|
||||
}
|
||||
|
||||
void Display::PollEvents() {
|
||||
SDL_Event event;
|
||||
@@ -131,7 +147,7 @@ void Display::PollEvents() {
|
||||
}
|
||||
|
||||
case SDL_EVENT_KEY_DOWN: {
|
||||
if(!event.key.repeat)
|
||||
if (!event.key.repeat)
|
||||
OnKeyDown(event.key.key);
|
||||
break;
|
||||
}
|
||||
@@ -147,7 +163,11 @@ void Display::PollEvents() {
|
||||
ImGui::NewFrame();
|
||||
}
|
||||
|
||||
void Display::Update() {
|
||||
void Display::Update(float a_Delta) {
|
||||
StateMachine::Update(a_Delta);
|
||||
#ifndef NDEBUG
|
||||
ImGui::ShowDemoWindow();
|
||||
#endif
|
||||
ImGui::Render();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
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
|
||||
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
|
||||
134
src/td/display/state/DebugWorldState.cpp
Normal file
134
src/td/display/state/DebugWorldState.cpp
Normal file
@@ -0,0 +1,134 @@
|
||||
#include <td/display/state/DebugWorldState.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#include <td/game/World.h>
|
||||
#include <td/protocol/packet/PacketSerialize.h>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
#include <td/render/renderer/EntityRenderer.h>
|
||||
#include <td/render/renderer/TowerRenderer.h>
|
||||
#include <td/render/renderer/WorldRenderer.h>
|
||||
|
||||
#include <sp/common/DataBuffer.h>
|
||||
#include <sp/extensions/Compress.h>
|
||||
#include <sp/io/MessageStream.h>
|
||||
#include <sp/io/StdIo.h>
|
||||
|
||||
#include <server/Server.h>
|
||||
#include <server/socket/FakeSocket.h>
|
||||
#include <server/state/GameState.h>
|
||||
|
||||
#include <client/Client.h>
|
||||
#include <client/socket/FakeSocket.h>
|
||||
#include <client/state/GameState.h>
|
||||
|
||||
#include <td/display/Display.h>
|
||||
#include <td/display/state/DebugWorldState.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
// TODO: get rid of this class
|
||||
class WorldApply : public protocol::PacketHandler {
|
||||
private:
|
||||
game::World& m_World;
|
||||
using protocol::PacketHandler::Handle;
|
||||
|
||||
public:
|
||||
WorldApply(game::World& a_World) : m_World(a_World) {}
|
||||
|
||||
void Handle(const protocol::packets::WorldHeaderPacket& a_Header) override {
|
||||
m_World.LoadMap(*a_Header);
|
||||
}
|
||||
|
||||
void Handle(const protocol::packets::WorldDataPacket& a_Data) override {
|
||||
m_World.LoadMap(*a_Data);
|
||||
}
|
||||
};
|
||||
|
||||
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.ReadMessage(protocol::PacketID::WorldHeader);
|
||||
auto data = stream.ReadMessage(protocol::PacketID::WorldData);
|
||||
|
||||
auto w = std::make_shared<game::World>();
|
||||
auto wa = std::make_shared<WorldApply>(*w);
|
||||
|
||||
protocol::PacketDispatcher d;
|
||||
d.RegisterHandler(wa);
|
||||
|
||||
d.Dispatch(*header);
|
||||
d.Dispatch(*data);
|
||||
|
||||
Save(*header, *data);
|
||||
|
||||
return w;
|
||||
}
|
||||
|
||||
DebugWorldState::DebugWorldState(Display& a_Display) : DisplayState(a_Display) {
|
||||
// server
|
||||
game::WorldPtr serverWorld = GetWorld();
|
||||
auto serverFakeSocket = std::make_shared<server::FakeSocket>();
|
||||
m_Server = std::make_unique<server::Server>(serverFakeSocket);
|
||||
|
||||
// client
|
||||
game::WorldPtr clientWorld = GetWorld();
|
||||
auto clientFakeSocket = client::FakeSocket::Connect(serverFakeSocket);
|
||||
m_Client = std::make_unique<client::Client>(clientFakeSocket);
|
||||
|
||||
// render
|
||||
m_Renderer.AddRenderer<render::WorldRenderer>(m_Camera, *clientWorld);
|
||||
m_Renderer.AddRenderer<render::EntityRenderer>(m_Camera, *clientWorld);
|
||||
m_Renderer.AddRenderer<render::TowerRenderer>(m_Camera, *clientWorld);
|
||||
|
||||
// camera
|
||||
m_Camera.SetCamPos({77, 7, 13});
|
||||
m_Camera.UpdatePerspective(m_StateMachine.GetAspectRatio());
|
||||
|
||||
// states
|
||||
m_ClientState = m_Client->ChangeState<client::GameState>(clientWorld, STEP_TIME);
|
||||
m_Server->ChangeState<server::GameState>(serverWorld);
|
||||
}
|
||||
|
||||
void DebugWorldState::Update(float a_Delta) {
|
||||
m_Server->Update(a_Delta);
|
||||
m_Client->Update(a_Delta);
|
||||
// TODO: m_ClientState might be invalid !
|
||||
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
|
||||
if (a_Key == SDLK_A) {
|
||||
m_Client->SendPacket(td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1));
|
||||
} else if (a_Key == SDLK_Z) {
|
||||
m_Client->SendPacket(td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13)));
|
||||
}
|
||||
}
|
||||
|
||||
DebugWorldState::~DebugWorldState() {}
|
||||
|
||||
} // namespace td
|
||||
31
src/td/display/state/MainMenuState.cpp
Normal file
31
src/td/display/state/MainMenuState.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#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() {}
|
||||
|
||||
void MainMenuState::Update(float a_Delta) {
|
||||
ImGui::Begin("MainWindow");
|
||||
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
|
||||
@@ -3,17 +3,7 @@
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const ChunkPtr& a_Chunk) {
|
||||
a_Buffer << a_Chunk->m_Palette;
|
||||
a_Buffer << a_Chunk->m_Data;
|
||||
return a_Buffer;
|
||||
}
|
||||
|
||||
sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, ChunkPtr& a_Chunk) {
|
||||
a_Chunk = std::make_shared<td::game::Chunk>();
|
||||
a_Buffer >> a_Chunk->m_Palette;
|
||||
return a_Buffer >> a_Chunk->m_Data;
|
||||
}
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
@@ -1,6 +1,8 @@
|
||||
#include <td/protocol/packet/PacketData.h>
|
||||
#include <td/protocol/packet/PacketSerialize.h>
|
||||
|
||||
#include <sp/common/DataBufferOperators.h>
|
||||
|
||||
namespace td {
|
||||
namespace game {
|
||||
|
||||
@@ -15,5 +17,17 @@ sp::DataBuffer& operator>>(sp::DataBuffer& a_Buffer, TeamCastle& a_Castle) {
|
||||
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
|
||||
|
||||
@@ -27,7 +27,7 @@ GL::VertexArray LoadWorldModel(const td::game::World* world) {
|
||||
td::game::TileIndex tileIndex = chunk->GetTileIndex(tileNumber);
|
||||
td::game::TilePtr tile = world->GetTilePtr(tileIndex);
|
||||
|
||||
if (tile == nullptr)
|
||||
if (!tile)
|
||||
continue;
|
||||
|
||||
positions.insert(
|
||||
|
||||
@@ -16,7 +16,6 @@ WorldRenderer::~WorldRenderer() {}
|
||||
void WorldRenderer::Render(float a_Lerp) {
|
||||
m_Shader->Start();
|
||||
Renderer::Render(*m_WorldVao);
|
||||
ImGui::ShowDemoWindow();
|
||||
}
|
||||
|
||||
} // namespace render
|
||||
|
||||
126
src/td/simulation/ClientSimulation.cpp
Normal file
126
src/td/simulation/ClientSimulation.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
#include <td/simulation/ClientSimulation.h>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
// TODO: mutex this bad boy
|
||||
|
||||
namespace td {
|
||||
namespace sim {
|
||||
|
||||
const protocol::LockStep ClientSimulation::EMPTY_LOCKSTEP;
|
||||
|
||||
std::uint64_t GetTime() {
|
||||
return static_cast<std::uint64_t>(
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock().now().time_since_epoch()).count());
|
||||
}
|
||||
|
||||
ClientSimulation::ClientSimulation(std::shared_ptr<game::World> a_World, GameHistory&& a_History, std::uint64_t a_StepTime) :
|
||||
m_StepTime(a_StepTime),
|
||||
m_World(a_World),
|
||||
m_CurrentTime(0),
|
||||
m_CurrentStep(0),
|
||||
m_LastSnapshot(std::make_shared<WorldSnapshot>()),
|
||||
m_LastValidStep(0) {
|
||||
m_History.reserve(a_History.size());
|
||||
for (auto&& lockstep : a_History) {
|
||||
m_History.emplace_back(std::move(lockstep));
|
||||
}
|
||||
Step();
|
||||
}
|
||||
|
||||
ClientSimulation::ClientSimulation(std::shared_ptr<game::World> a_World, std::uint64_t a_StepTime) :
|
||||
m_StepTime(a_StepTime),
|
||||
m_World(a_World),
|
||||
m_History(std::numeric_limits<StepTime>::max()),
|
||||
m_CurrentTime(0),
|
||||
m_CurrentStep(0),
|
||||
m_LastSnapshot(std::make_shared<WorldSnapshot>()),
|
||||
m_LastValidStep(0) {}
|
||||
|
||||
float ClientSimulation::Update(float a_Delta) {
|
||||
// TODO: handle freezes (m_CurrentTime > 2 * m_StepTime)
|
||||
static const float stepTimeSecond = static_cast<float>(m_StepTime) / 1000.0f;
|
||||
m_CurrentTime += a_Delta;
|
||||
if (m_CurrentTime > stepTimeSecond) {
|
||||
m_CurrentTime = std::fmod(m_CurrentTime, stepTimeSecond);
|
||||
Step();
|
||||
}
|
||||
return (float)m_CurrentTime / stepTimeSecond;
|
||||
}
|
||||
|
||||
bool ClientSimulation::Step() {
|
||||
const auto& step = m_History[m_CurrentStep];
|
||||
if (step.has_value()) {
|
||||
auto snapshot = m_World->Tick(step.value(), FpFloat(m_StepTime) / FpFloat(1000));
|
||||
if (m_LastValidStep + 1 == m_CurrentStep) {
|
||||
m_LastValidStep = m_CurrentStep;
|
||||
m_LastSnapshot = snapshot;
|
||||
}
|
||||
} else {
|
||||
m_World->Tick(EMPTY_LOCKSTEP, FpFloat(m_StepTime) / FpFloat(1000));
|
||||
std::cout << "Empty tick (" << m_CurrentStep << ") !\n";
|
||||
}
|
||||
m_CurrentStep++;
|
||||
return step.has_value();
|
||||
}
|
||||
|
||||
void ClientSimulation::Handle(const protocol::packets::LockStepsPacket& a_LockSteps) {
|
||||
const auto& steps = a_LockSteps->m_LockSteps;
|
||||
for (std::size_t i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) {
|
||||
m_History[a_LockSteps->m_FirstFrameNumber + i] = steps[i];
|
||||
}
|
||||
|
||||
// to be in the state [0-1]
|
||||
if (a_LockSteps->m_FirstFrameNumber == 0)
|
||||
Step();
|
||||
|
||||
FastReplay();
|
||||
}
|
||||
|
||||
void ClientSimulation::Handle(const protocol::packets::LockStepResponsePacket& a_LockSteps) {
|
||||
for (const auto& [stepTime, lockStep] : a_LockSteps->m_Steps) {
|
||||
m_History[stepTime] = lockStep;
|
||||
}
|
||||
FastReplay();
|
||||
}
|
||||
|
||||
std::size_t ClientSimulation::FastForward(std::size_t a_Count) {
|
||||
std::size_t stepCount;
|
||||
bool hasMissingStep = false;
|
||||
for (std::size_t i = 0; i < a_Count; i++) {
|
||||
if (!Step() && !hasMissingStep) {
|
||||
hasMissingStep = true;
|
||||
stepCount = i + 1;
|
||||
}
|
||||
}
|
||||
std::cout << "Was behind " << (hasMissingStep ? stepCount : a_Count) << " ticks !\n";
|
||||
return hasMissingStep ? stepCount : a_Count;
|
||||
}
|
||||
|
||||
void ClientSimulation::FastReplay() {
|
||||
if (m_LastValidStep + 1 >= m_CurrentStep)
|
||||
return;
|
||||
|
||||
m_World->ResetSnapshots(m_LastSnapshot, m_LastSnapshot);
|
||||
|
||||
const std::size_t stepCount = m_CurrentStep - m_LastValidStep;
|
||||
m_CurrentStep = m_LastValidStep;
|
||||
|
||||
if (FastForward(stepCount) == stepCount)
|
||||
return;
|
||||
|
||||
std::vector<StepTime> missingSteps;
|
||||
missingSteps.reserve(stepCount);
|
||||
|
||||
for (StepTime step = m_LastValidStep + 1; step < m_CurrentStep; step++) {
|
||||
if (!m_History[step].has_value())
|
||||
missingSteps.push_back(step);
|
||||
}
|
||||
|
||||
OnMissingLockSteps(missingSteps);
|
||||
}
|
||||
|
||||
} // namespace sim
|
||||
} // namespace td
|
||||
@@ -1,44 +0,0 @@
|
||||
#include <td/simulation/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(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));
|
||||
}
|
||||
}
|
||||
|
||||
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,93 +0,0 @@
|
||||
#include <td/simulation/RealTimeSimulation.h>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
namespace td {
|
||||
namespace sim {
|
||||
|
||||
const protocol::LockStep RealTimeSimulation::EMPTY_LOCKSTEP;
|
||||
|
||||
std::uint64_t GetTime() {
|
||||
return static_cast<std::uint64_t>(
|
||||
std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock().now().time_since_epoch()).count());
|
||||
}
|
||||
|
||||
RealTimeSimulation::RealTimeSimulation(game::World& a_World, const GameHistory& a_History, std::uint64_t a_StepTime) :
|
||||
m_StepTime(a_StepTime),
|
||||
m_World(a_World),
|
||||
m_CurrentTime(0),
|
||||
m_LastTime(GetTime()),
|
||||
m_CurrentStep(0),
|
||||
m_LastSnapshot(std::make_shared<WorldSnapshot>()),
|
||||
m_LastValidStep(0) {
|
||||
m_History.reserve(a_History.size());
|
||||
for (const auto& lockstep : a_History) {
|
||||
m_History.emplace_back(lockstep);
|
||||
}
|
||||
Step();
|
||||
}
|
||||
|
||||
RealTimeSimulation::RealTimeSimulation(game::World& a_World, std::uint64_t a_StepTime) :
|
||||
m_StepTime(a_StepTime),
|
||||
m_World(a_World),
|
||||
m_History(std::numeric_limits<std::uint16_t>::max()),
|
||||
m_CurrentTime(0),
|
||||
m_LastTime(GetTime()),
|
||||
m_CurrentStep(0),
|
||||
m_LastSnapshot(std::make_shared<WorldSnapshot>()),
|
||||
m_LastValidStep(0) {
|
||||
Step();
|
||||
}
|
||||
|
||||
float RealTimeSimulation::Update() {
|
||||
// TODO: handle freezes (m_CurrentTime > 2 * m_StepTime)
|
||||
m_CurrentTime += GetTime() - m_LastTime;
|
||||
m_LastTime = GetTime();
|
||||
if (m_CurrentTime > m_StepTime) {
|
||||
Step();
|
||||
m_CurrentTime -= m_StepTime;
|
||||
}
|
||||
return (float)m_CurrentTime / (float)m_StepTime;
|
||||
}
|
||||
|
||||
void RealTimeSimulation::Step() {
|
||||
const auto& step = m_History[m_CurrentStep];
|
||||
if (step.has_value()) {
|
||||
m_LastSnapshot = m_World.Tick(step.value(), FpFloat(m_StepTime) / FpFloat(1000));
|
||||
m_LastValidStep = m_CurrentStep;
|
||||
} else {
|
||||
m_World.Tick(EMPTY_LOCKSTEP, FpFloat(m_StepTime) / FpFloat(1000));
|
||||
}
|
||||
m_CurrentStep++;
|
||||
}
|
||||
|
||||
void RealTimeSimulation::HandlePacket(const protocol::packets::LockStepsPacket& a_LockSteps) {
|
||||
const auto& steps = a_LockSteps->m_LockSteps;
|
||||
for (std::size_t i = 0; i < LOCKSTEP_BUFFER_SIZE; i++) {
|
||||
m_History[a_LockSteps->m_FirstFrameNumber + i] = steps[i];
|
||||
}
|
||||
FastReplay();
|
||||
}
|
||||
|
||||
void RealTimeSimulation::HandlePacket(const protocol::packets::PredictCommandPacket& a_Predict) {}
|
||||
|
||||
void RealTimeSimulation::FastForward(std::size_t a_Count) {
|
||||
for (std::size_t i = 0; i < a_Count; i++) {
|
||||
Step();
|
||||
}
|
||||
}
|
||||
|
||||
void RealTimeSimulation::FastReplay() {
|
||||
if (m_LastValidStep >= m_CurrentStep)
|
||||
return;
|
||||
|
||||
m_World.ResetSnapshots(m_LastSnapshot, m_LastSnapshot);
|
||||
|
||||
const std::size_t stepCount = m_CurrentStep - m_LastValidStep;
|
||||
m_CurrentStep = m_LastValidStep;
|
||||
|
||||
FastForward(stepCount);
|
||||
}
|
||||
|
||||
} // namespace sim
|
||||
} // namespace td
|
||||
52
src/td/simulation/ServerSimulation.cpp
Normal file
52
src/td/simulation/ServerSimulation.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <td/simulation/ServerSimulation.h>
|
||||
|
||||
namespace td {
|
||||
namespace sim {
|
||||
|
||||
ServerSimulation::ServerSimulation(game::World& a_World, std::uint64_t a_StepTime) :
|
||||
m_World(a_World), m_StepTime(a_StepTime), m_CurrentTime(0), m_History(std::numeric_limits<StepTime>::max()) {}
|
||||
|
||||
protocol::packets::LockStepsPacket ServerSimulation::Update() {
|
||||
std::lock_guard<std::mutex> lock(m_Mutex);
|
||||
|
||||
m_World.Tick(m_History[m_CurrentTime], FpFloat(m_StepTime) / FpFloat(1000));
|
||||
m_CurrentTime++;
|
||||
|
||||
return MakePacket();
|
||||
}
|
||||
|
||||
protocol::packets::LockStepsPacket ServerSimulation::MakePacket() {
|
||||
std::array<protocol::LockStep, LOCKSTEP_BUFFER_SIZE> nextSteps;
|
||||
std::copy(m_History.begin() + m_CurrentTime, m_History.begin() + m_CurrentTime + nextSteps.size(), nextSteps.begin());
|
||||
return {m_CurrentTime, std::move(nextSteps)};
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void AddToCommandHistory(protocol::LockStep& a_LockStep, const T& a_Cmd) {
|
||||
a_LockStep.push_back({std::make_shared<T>(a_Cmd)});
|
||||
}
|
||||
|
||||
void ServerSimulation::Handle(const protocol::commands::SpawnTroopCommand& a_SpawnTroop) {
|
||||
std::lock_guard<std::mutex> lock(m_Mutex);
|
||||
AddToCommandHistory(m_History[m_CurrentTime + LOCKSTEP_BUFFER_SIZE], a_SpawnTroop);
|
||||
}
|
||||
|
||||
void ServerSimulation::Handle(const protocol::commands::PlaceTowerCommand& a_PlaceTower) {
|
||||
std::lock_guard<std::mutex> lock(m_Mutex);
|
||||
AddToCommandHistory(m_History[m_CurrentTime + LOCKSTEP_BUFFER_SIZE], a_PlaceTower);
|
||||
}
|
||||
|
||||
protocol::packets::LockStepResponsePacket ServerSimulation::GetResponse(
|
||||
const protocol::packets::LockStepRequestPacket& a_Missing) const {
|
||||
std::map<StepTime, protocol::LockStep> result;
|
||||
|
||||
for (StepTime step : a_Missing->m_Missing) {
|
||||
result.emplace(step, m_History[step]);
|
||||
}
|
||||
|
||||
return {result};
|
||||
}
|
||||
|
||||
|
||||
} // namespace sim
|
||||
} // namespace td
|
||||
Binary file not shown.
@@ -3,8 +3,7 @@ add_rules("mode.debug", "mode.release")
|
||||
add_repositories("persson-repo https://git.ale-pri.com/Persson-dev/xmake-repo.git")
|
||||
|
||||
add_requires("imgui 1.92.0", {configs = {sdl3 = true, opengl3 = true}})
|
||||
add_requires("splib 2.1.0", "zlib")
|
||||
add_requires("libsdl3 3.2.16", "glew", "fpm", "enet6")
|
||||
add_requires("libsdl3 3.2.16", "splib 2.3.0", "zlib", "glew", "fpm", "enet6")
|
||||
|
||||
set_languages("c++17")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user