Compare commits
12 Commits
165ebf7b2e
...
hotkeymenu
| Author | SHA1 | Date | |
|---|---|---|---|
|
a5d7cc20ed
|
|||
|
d1d2b63be8
|
|||
|
62c5c762f9
|
|||
|
1d436aa1c3
|
|||
|
73dd2dabfa
|
|||
|
5b6254c690
|
|||
|
688b6e93ea
|
|||
|
d64c366f4b
|
|||
|
7d58b881b2
|
|||
|
20acbc0499
|
|||
|
4fe2e25029
|
|||
|
fd08833f3f
|
2
.vscode/c_cpp_properties.json
vendored
2
.vscode/c_cpp_properties.json
vendored
@@ -2,7 +2,7 @@
|
|||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "TD2",
|
"name": "TD2",
|
||||||
"cppStandard": "c++17",
|
"cppStandard": "c++20",
|
||||||
"includePath": [
|
"includePath": [
|
||||||
"include"
|
"include"
|
||||||
],
|
],
|
||||||
|
|||||||
@@ -1,23 +1,40 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <client/IClientSocket.h>
|
#include <client/IClientSocket.h>
|
||||||
|
#include <client/PlayerManager.h>
|
||||||
#include <td/common/StateMachine.h>
|
#include <td/common/StateMachine.h>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace client {
|
namespace client {
|
||||||
|
|
||||||
class ClientState;
|
class ClientState;
|
||||||
|
class LoggingState;
|
||||||
|
|
||||||
class Client : public StateMachine<Client, void, float> {
|
class Client : public StateMachine<Client, void, float> {
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<IClientSocket> m_Socket;
|
std::shared_ptr<IClientSocket> m_Socket;
|
||||||
|
PlayerManager m_Players;
|
||||||
|
std::optional<PlayerID> m_Id;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Client(const std::shared_ptr<IClientSocket>& a_Socket, const std::string& a_PlayerName);
|
Client(const std::shared_ptr<IClientSocket>& a_Socket);
|
||||||
|
~Client();
|
||||||
|
|
||||||
void SendPacket(const protocol::PacketBase& a_Packet);
|
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 ClientState;
|
||||||
|
friend class LoggingState;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace client
|
} // namespace client
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ class IClientSocket {
|
|||||||
utils::Signal<const protocol::PacketBase&> OnReceive;
|
utils::Signal<const protocol::PacketBase&> OnReceive;
|
||||||
|
|
||||||
virtual void Send(const protocol::PacketBase& a_Packet) = 0;
|
virtual void Send(const protocol::PacketBase& a_Packet) = 0;
|
||||||
|
virtual void Disconnect() = 0;
|
||||||
|
|
||||||
IClientSocket() {}
|
IClientSocket() {}
|
||||||
virtual ~IClientSocket() {}
|
virtual ~IClientSocket() {}
|
||||||
|
|||||||
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
|
||||||
@@ -27,6 +27,7 @@ class FakeSocket : public IClientSocket {
|
|||||||
void ReceiveFromFakePeer(PeerID a_Peer, const protocol::PacketBase& a_Packet);
|
void ReceiveFromFakePeer(PeerID a_Peer, const protocol::PacketBase& a_Packet);
|
||||||
|
|
||||||
virtual void Send(const protocol::PacketBase& a_Packet) override;
|
virtual void Send(const protocol::PacketBase& a_Packet) override;
|
||||||
|
virtual void Disconnect() override;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace client
|
} // namespace client
|
||||||
|
|||||||
@@ -9,12 +9,12 @@ namespace client {
|
|||||||
|
|
||||||
class GameState : public ClientState {
|
class GameState : public ClientState {
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<game::World> m_World;
|
game::WorldPtr m_World;
|
||||||
sim::ClientSimulation m_Simulation;
|
sim::ClientSimulation m_Simulation;
|
||||||
float m_CurrentLerp;
|
float m_CurrentLerp;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
GameState(Client& a_Client, const std::shared_ptr<game::World>& a_World, std::uint64_t a_StepTime);
|
GameState(Client& a_Client, const game::WorldPtr& a_World, std::uint64_t a_StepTime, const std::vector<protocol::LockStep> a_FirstSteps);
|
||||||
~GameState() {}
|
~GameState() {}
|
||||||
|
|
||||||
virtual void Update(float a_Delta) override;
|
virtual void Update(float a_Delta) override;
|
||||||
@@ -23,6 +23,10 @@ class GameState : public ClientState {
|
|||||||
return m_CurrentLerp;
|
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::LockStepsPacket& a_LockStep) override;
|
||||||
virtual void Handle(const protocol::packets::LockStepResponsePacket& a_LockStep) override;
|
virtual void Handle(const protocol::packets::LockStepResponsePacket& a_LockStep) override;
|
||||||
};
|
};
|
||||||
|
|||||||
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
|
||||||
@@ -12,9 +12,10 @@ class LoggingState : public ClientState {
|
|||||||
LoggingState(Client& a_Client, const std::string& a_PlayerName);
|
LoggingState(Client& a_Client, const std::string& a_PlayerName);
|
||||||
~LoggingState();
|
~LoggingState();
|
||||||
|
|
||||||
virtual void Update(float a_Delta) override {}
|
virtual void Update(float a_Delta) override;
|
||||||
|
|
||||||
virtual void Handle(const protocol::packets::PlayerJoinPacket& a_Packet) override;
|
virtual void Handle(const protocol::packets::PlayerJoinPacket& a_Packet) override;
|
||||||
|
virtual void Handle(const protocol::packets::LoggingSuccessPacket& a_Packet) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace client
|
} // namespace client
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ class IServerSocket {
|
|||||||
using PlayerPacketHandlerType = std::unique_ptr<protocol::PacketHandler>(PlayerID);
|
using PlayerPacketHandlerType = std::unique_ptr<protocol::PacketHandler>(PlayerID);
|
||||||
using PlayerPacketHandler = std::function<PlayerPacketHandlerType>;
|
using PlayerPacketHandler = std::function<PlayerPacketHandlerType>;
|
||||||
|
|
||||||
utils::Signal<PlayerID, const PlayerInfo&> OnPlayerJoin;
|
utils::Signal<PlayerID> OnPlayerConnect;
|
||||||
utils::Signal<PlayerID> OnPlayerLeave;
|
utils::Signal<PlayerID> OnPlayerDisconnect;
|
||||||
utils::Signal<PlayerID, const protocol::PacketBase&> OnReceive;
|
utils::Signal<PlayerID, const protocol::PacketBase&> OnReceive;
|
||||||
|
|
||||||
void Send(PlayerID a_PlayerId, const protocol::PacketBase& a_Packet);
|
void Send(PlayerID a_PlayerId, const protocol::PacketBase& a_Packet);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <td/misc/Signal.h>
|
||||||
#include <td/protocol/packet/Packets.h>
|
#include <td/protocol/packet/Packets.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
@@ -13,6 +14,9 @@ class PlayerManager {
|
|||||||
std::shared_ptr<IServerSocket> m_Socket;
|
std::shared_ptr<IServerSocket> m_Socket;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
utils::Signal<PlayerID, const PlayerInfo&> OnPlayerJoin;
|
||||||
|
utils::Signal<PlayerID> OnPlayerLeave;
|
||||||
|
|
||||||
PlayerManager(const std::shared_ptr<IServerSocket>& a_Socket);
|
PlayerManager(const std::shared_ptr<IServerSocket>& a_Socket);
|
||||||
~PlayerManager();
|
~PlayerManager();
|
||||||
|
|
||||||
@@ -20,14 +24,15 @@ class PlayerManager {
|
|||||||
PlayerInfo GetPlayer(PlayerID a_Player);
|
PlayerInfo GetPlayer(PlayerID a_Player);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void Disconnect(PlayerID a_Player);
|
||||||
|
|
||||||
class ConnectionHandler : public protocol::PacketHandler {
|
class ConnectionHandler : public protocol::PacketHandler {
|
||||||
private:
|
private:
|
||||||
std::map<PlayerID, PlayerInfo>& m_Players;
|
PlayerManager& m_PlayerManager;
|
||||||
IServerSocket& m_Socket;
|
|
||||||
PlayerID m_Player;
|
PlayerID m_Player;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ConnectionHandler(std::map<PlayerID, PlayerInfo>& a_Players, IServerSocket& a_Socket, PlayerID a_Player);
|
ConnectionHandler(PlayerManager& a_PlayerManager, PlayerID a_Player);
|
||||||
~ConnectionHandler() = default;
|
~ConnectionHandler() = default;
|
||||||
|
|
||||||
virtual void Handle(const protocol::packets::PlayerLoginPacket& a_Packet) override;
|
virtual void Handle(const protocol::packets::PlayerLoginPacket& a_Packet) override;
|
||||||
|
|||||||
@@ -20,6 +20,10 @@ class Server : public StateMachine<Server, void, float> {
|
|||||||
|
|
||||||
virtual void Update(float a_Delta);
|
virtual void Update(float a_Delta);
|
||||||
|
|
||||||
|
const PlayerManager& GetPlayers() const {
|
||||||
|
return m_Players;
|
||||||
|
}
|
||||||
|
|
||||||
friend class ServerState;
|
friend class ServerState;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -22,6 +22,8 @@ class GameState : public ServerState {
|
|||||||
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) override;
|
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) override;
|
||||||
virtual void Update(float a_Delta) override;
|
virtual void Update(float a_Delta) override;
|
||||||
|
|
||||||
|
virtual void OnPlayerJoin(PlayerID a_Id, const td::PlayerInfo& a_Info) override;
|
||||||
|
|
||||||
friend class GameStateHandler;
|
friend class GameStateHandler;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -10,9 +10,11 @@ class LobbyState : public ServerState {
|
|||||||
private:
|
private:
|
||||||
std::shared_ptr<game::World> m_World;
|
std::shared_ptr<game::World> m_World;
|
||||||
public:
|
public:
|
||||||
LobbyState(Server& a_Server, const std::shared_ptr<game::World>& a_World);
|
LobbyState(Server& a_Server);
|
||||||
~LobbyState() {}
|
~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 HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) override;
|
||||||
virtual void Update(float a_Delta) override;
|
virtual void Update(float a_Delta) override;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <memory>
|
#include <td/misc/Signal.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
@@ -16,27 +16,34 @@ class StateMachine {
|
|||||||
virtual TReturn Update(TArgs... args) = 0;
|
virtual TReturn Update(TArgs... args) = 0;
|
||||||
|
|
||||||
template <typename T, typename... Args>
|
template <typename T, typename... Args>
|
||||||
T* ChangeState(Args&&... args) {
|
void ChangeState(Args... args) {
|
||||||
return m_StateMachine.template ChangeState<T>(std::forward<Args>(args)...);
|
m_StateMachine.template ChangeState<T>(std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
TDerived& m_StateMachine;
|
TDerived& m_StateMachine;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
utils::Signal<State&> OnStateChange;
|
||||||
|
|
||||||
StateMachine() {}
|
StateMachine() {}
|
||||||
StateMachine(StateMachine&&) = default;
|
StateMachine(StateMachine&&) = default;
|
||||||
virtual ~StateMachine() {}
|
virtual ~StateMachine() {}
|
||||||
|
|
||||||
virtual TReturn Update(TArgs... args) {
|
virtual TReturn Update(TArgs... args) {
|
||||||
assert(m_State && "You must change state at least once before updating !");
|
assert(m_State && "You must change state at least once before updating !");
|
||||||
return m_State->Update(args...);
|
return m_State->Update(std::forward<TArgs>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T, typename... Args>
|
template <typename T, typename... Args>
|
||||||
T* ChangeState(Args&&... args) {
|
void ChangeState(Args... args) {
|
||||||
m_State = std::make_unique<T>(static_cast<TDerived&>(*this), std::forward<Args>(args)...);
|
auto* currentState = m_State.get();
|
||||||
return static_cast<T*>(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:
|
private:
|
||||||
|
|||||||
@@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
#include <td/display/Display.h>
|
#include <td/display/Display.h>
|
||||||
#include <td/misc/SlotGuard.h>
|
#include <td/misc/SlotGuard.h>
|
||||||
|
#include <client/ClientState.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
class DisplayState : public Display::State, private utils::SlotGuard {
|
class DisplayState : public Display::State, private utils::SlotGuard {
|
||||||
public:
|
public:
|
||||||
DisplayState(Display& a_Display);
|
DisplayState(Display& a_Display);
|
||||||
@@ -13,4 +15,5 @@ class DisplayState : public Display::State, private utils::SlotGuard {
|
|||||||
virtual void OnAspectRatioChange(float a_Ratio) {}
|
virtual void OnAspectRatioChange(float a_Ratio) {}
|
||||||
virtual void OnKeyDown(SDL_Keycode a_Key) {}
|
virtual void OnKeyDown(SDL_Keycode a_Key) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include <td/render/Renderer.h>
|
#include <td/render/Renderer.h>
|
||||||
#include <td/simulation/ClientSimulation.h>
|
#include <td/simulation/ClientSimulation.h>
|
||||||
#include <client/state/GameState.h>
|
#include <client/state/GameState.h>
|
||||||
|
#include <server/socket/FakeSocket.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
@@ -13,10 +14,13 @@ class DebugWorldState : public DisplayState {
|
|||||||
private:
|
private:
|
||||||
render::RenderPipeline m_Renderer;
|
render::RenderPipeline m_Renderer;
|
||||||
render::Camera m_Camera;
|
render::Camera m_Camera;
|
||||||
std::unique_ptr<client::Client> m_Client;
|
|
||||||
std::unique_ptr<server::Server> m_Server;
|
std::unique_ptr<server::Server> m_Server;
|
||||||
|
std::unique_ptr<client::Client> m_Client;
|
||||||
client::GameState* m_ClientState;
|
client::GameState* m_ClientState;
|
||||||
|
|
||||||
|
std::vector<std::unique_ptr<client::Client>> m_FakeClients;
|
||||||
|
std::shared_ptr<server::FakeSocket> m_ServerSocket;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DebugWorldState(Display& a_Display);
|
DebugWorldState(Display& a_Display);
|
||||||
~DebugWorldState();
|
~DebugWorldState();
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ public:
|
|||||||
struct TeamList {
|
struct TeamList {
|
||||||
std::array<Team, 2> m_Teams;
|
std::array<Team, 2> m_Teams;
|
||||||
|
|
||||||
TeamList() : m_Teams{Team{TeamColor::Red}, Team{TeamColor::Blue}}{
|
TeamList() : m_Teams{Team{TeamColor::Blue}, Team{TeamColor::Red}}{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,6 +33,9 @@ class World {
|
|||||||
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;
|
||||||
|
|
||||||
@@ -102,7 +105,7 @@ class World {
|
|||||||
return m_CurrentState->m_Teams[TeamColor::Blue];
|
return m_CurrentState->m_Teams[TeamColor::Blue];
|
||||||
}
|
}
|
||||||
const Team& GetBlueTeam() const {
|
const Team& GetBlueTeam() const {
|
||||||
return m_CurrentState->m_Teams[TeamColor::Red];
|
return m_CurrentState->m_Teams[TeamColor::Blue];
|
||||||
}
|
}
|
||||||
|
|
||||||
Team& GetTeam(TeamColor team) {
|
Team& GetTeam(TeamColor team) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
#include <td/common/NonCopyable.h>
|
#include <td/common/NonCopyable.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@@ -15,7 +16,7 @@ template <typename... Args>
|
|||||||
class SignalRaw : private NonCopyable {
|
class SignalRaw : private NonCopyable {
|
||||||
public:
|
public:
|
||||||
using FnType = void(Args...);
|
using FnType = void(Args...);
|
||||||
using CallBack = std::function<FnType>;
|
using CallBack = std::shared_ptr<std::function<FnType>>;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<CallBack> m_Callbacks;
|
std::vector<CallBack> m_Callbacks;
|
||||||
@@ -26,14 +27,13 @@ class SignalRaw : private NonCopyable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Disconnect(const CallBack& a_Callback) {
|
void Disconnect(const CallBack& a_Callback) {
|
||||||
auto it = std::find_if(m_Callbacks.begin(), m_Callbacks.end(),
|
auto it = std::find(m_Callbacks.begin(), m_Callbacks.end(), a_Callback);
|
||||||
[&a_Callback](CallBack& callback) { return a_Callback.template target<FnType>() == callback.template target<FnType>(); });
|
|
||||||
m_Callbacks.erase(it);
|
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...);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -46,6 +46,7 @@ class Signal {
|
|||||||
public:
|
public:
|
||||||
using SignalBase = SignalRaw<Args...>;
|
using SignalBase = SignalRaw<Args...>;
|
||||||
using CallBack = typename SignalBase::CallBack;
|
using CallBack = typename SignalBase::CallBack;
|
||||||
|
using CallBackRaw = typename CallBack::element_type;
|
||||||
using SignalPtr = std::shared_ptr<SignalBase>;
|
using SignalPtr = std::shared_ptr<SignalBase>;
|
||||||
class ConnectionGuard;
|
class ConnectionGuard;
|
||||||
|
|
||||||
@@ -56,12 +57,15 @@ class Signal {
|
|||||||
Signal() : m_Signal(std::make_shared<SignalBase>()) {}
|
Signal() : m_Signal(std::make_shared<SignalBase>()) {}
|
||||||
Signal(const Signal&) = default;
|
Signal(const Signal&) = default;
|
||||||
|
|
||||||
void Connect(const CallBack& a_Callback) {
|
/**
|
||||||
m_Signal->Connect(a_Callback);
|
* \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) {
|
[[nodiscard]] std::unique_ptr<ConnectionGuard> ConnectSafe(const CallBack& a_Callback) {
|
||||||
Connect(a_Callback);
|
m_Signal->Connect(a_Callback);
|
||||||
return std::make_unique<ConnectionGuard>(*this, a_Callback);
|
return std::make_unique<ConnectionGuard>(*this, a_Callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +92,8 @@ class Signal<Args...>::ConnectionGuard : public Connection {
|
|||||||
CallBack m_Callback;
|
CallBack m_Callback;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
ConnectionGuard(const Signal<Args...>& a_Signal, const CallBack& a_Callback) : m_Signal(a_Signal.m_Signal), m_Callback(a_Callback) {}
|
ConnectionGuard(const Signal<Args...>& a_Signal, const CallBack& a_Callback) :
|
||||||
|
m_Signal(a_Signal.m_Signal), m_Callback(a_Callback) {}
|
||||||
|
|
||||||
~ConnectionGuard() {
|
~ConnectionGuard() {
|
||||||
if (!m_Signal.expired())
|
if (!m_Signal.expired())
|
||||||
|
|||||||
@@ -19,8 +19,9 @@ class SlotGuard {
|
|||||||
* \brief Connect a signal to a function (with the same signature)
|
* \brief Connect a signal to a function (with the same signature)
|
||||||
*/
|
*/
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
void Connect(Signal<Args...> a_Signal, const typename Signal<Args...>::CallBack& a_Callback) {
|
void Connect(Signal<Args...> a_Signal, const typename Signal<Args...>::CallBack::element_type& a_Callback) {
|
||||||
m_Connections.push_back(a_Signal.ConnectSafe(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() {
|
void Disconnect() {
|
||||||
|
|||||||
@@ -67,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;
|
||||||
|
|||||||
@@ -42,8 +42,11 @@ class RenderPipeline {
|
|||||||
virtual ~RenderPipeline() {}
|
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() {
|
||||||
|
|||||||
21
include/td/render/renderer/HotkeysRenderer.h
Normal file
21
include/td/render/renderer/HotkeysRenderer.h
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <td/render/Renderer.h>
|
||||||
|
#include <client/PlayerManager.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace render {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief This is a debug class
|
||||||
|
*/
|
||||||
|
class HotkeysRenderer : public BasicRenderer {
|
||||||
|
public:
|
||||||
|
virtual void Render(float a_Lerp) override;
|
||||||
|
|
||||||
|
HotkeysRenderer();
|
||||||
|
~HotkeysRenderer() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace render
|
||||||
|
} // namespace td
|
||||||
357
include/td/render/renderer/ImHotkey.h
Normal file
357
include/td/render/renderer/ImHotkey.h
Normal file
@@ -0,0 +1,357 @@
|
|||||||
|
// ImHotKey v1.0
|
||||||
|
// https://github.com/CedricGuillemet/ImHotKey
|
||||||
|
//
|
||||||
|
// The MIT License(MIT)
|
||||||
|
//
|
||||||
|
// Copyright(c) 2019 Cedric Guillemet
|
||||||
|
//
|
||||||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
// of this software and associated documentation files(the "Software"), to deal
|
||||||
|
// in the Software without restriction, including without limitation the rights
|
||||||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||||
|
// copies of the Software, and to permit persons to whom the Software is
|
||||||
|
// furnished to do so, subject to the following conditions :
|
||||||
|
//
|
||||||
|
// The above copyright notice and this permission notice shall be included in all
|
||||||
|
// copies or substantial portions of the Software.
|
||||||
|
//
|
||||||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
// SOFTWARE.
|
||||||
|
//
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#define SDL_h_
|
||||||
|
|
||||||
|
/*
|
||||||
|
How to use it?
|
||||||
|
|
||||||
|
// Get some hotkeys composed of:
|
||||||
|
// - hotkey name
|
||||||
|
// - hotkey comment/lib
|
||||||
|
// - hotkey scancodes. Computed by the editor. Store that value in your app.
|
||||||
|
|
||||||
|
static std::vector<ImHotKey::HotKey> hotkeys = { { "Layout", "Reorder nodes in a simpler layout", 0xFFFF261D}
|
||||||
|
,{"Save", "Save the current graph", 0xFFFF1F1D}
|
||||||
|
,{"Load", "Load an existing graph file", 0xFFFF181D}
|
||||||
|
,{"Play/Stop", "Play or stop the animation from the current graph", 0xFFFFFF3F}
|
||||||
|
,{"SetKey", "Make a new animation key with the current parameters values at the current time", 0xFFFFFF1F}
|
||||||
|
};
|
||||||
|
|
||||||
|
// The editor is a modal window. bring it with something like that
|
||||||
|
if (ImGui::Button("Edit Hotkeys"))
|
||||||
|
{
|
||||||
|
ImGui::OpenPopup("HotKeys Editor");
|
||||||
|
}
|
||||||
|
ImHotKey::Edit(hotkeys.data(), hotkeys.size(), "HotKeys Editor");
|
||||||
|
|
||||||
|
// ImHotKey also provides a way to retrieve HotKey
|
||||||
|
int hotkey = ImHotKey::GetHotKey(hotkeys.data(), hotkeys.size());
|
||||||
|
if (hotkey != -1)
|
||||||
|
{
|
||||||
|
// handle the hotkey index!
|
||||||
|
}
|
||||||
|
|
||||||
|
Awesome, you are done!
|
||||||
|
To help you integrate in your app, you can get a text (like "Ctrl + O") to integrate in your menu
|
||||||
|
static void GetHotKeyLib(unsigned int functionKeys, char* buffer, size_t bufferSize);
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "imgui.h"
|
||||||
|
#include "imgui_internal.h"
|
||||||
|
|
||||||
|
namespace ImHotKey
|
||||||
|
{
|
||||||
|
struct HotKey
|
||||||
|
{
|
||||||
|
const char *functionName;
|
||||||
|
const char *functionLib;
|
||||||
|
unsigned int functionKeys;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Key
|
||||||
|
{
|
||||||
|
const char* lib = nullptr;
|
||||||
|
unsigned int order;
|
||||||
|
unsigned int scanCodePage1 = 0; // win32 scancode
|
||||||
|
unsigned int scanCodePage7 = 0; // HID (SDL,...)
|
||||||
|
float offset = 0;
|
||||||
|
float width = 40;
|
||||||
|
};
|
||||||
|
|
||||||
|
static const Key Keys[6][18] = {
|
||||||
|
{ {"Esc", 4, 0x1, 0x29, 18}, {"F1", 5, 0x3B, 0x3A, 18}, {"F2", 6, 0x3C, 0x3B}, {"F3", 7, 0x3D, 0x3C}, {"F4", 8, 0x3E, 0x3D}, {"F5", 9, 0x3F, 0x3E, 24}, {"F6", 10, 0x40, 0x3F}, {"F7", 11, 0x41, 0x40}, {"F8", 12, 0x42, 0x41}, {"F9", 13, 0x43, 0x42, 24}, {"F10", 14, 0x44, 0x43}, {"F11", 15, 0x57, 0x44}, {"F12", 16, 0x58, 0x45}, {"PrSn", 17, 0x37, 0x46, 24}, {"ScLk", 18, 0x46}, {"Brk", 19, 126, 0x47} },
|
||||||
|
{ {"~", 20, 0x29, 0x35}, {"1", 21, 0x2, 0x1E}, {"2", 22, 0x3, 0x1F}, {"3", 23, 0x4, 0x20}, {"4", 24, 0x5, 0x21}, {"5", 25, 0x6, 0x22}, {"6", 26, 0x7, 0x23}, {"7", 27, 0x8, 0x24}, {"8", 28, 0x9, 0x25}, {"9", 29, 0xA, 0x26}, {"0", 30, 0xB, 0x27}, {"-", 31, 0xC, 0x2D}, {"+", 32, 0xD, 0x2E},{"Backspace", 33, 0xE, 0x2A, 0, 80}, {"Ins", 34, 0x52, 0x49, 24}, {"Hom", 35, 0x47, 0x4A}, {"PgU", 36, 0x49, 0x4B} },
|
||||||
|
{ {"Tab", 3, 0xF, 0x2B, 0, 60}, {"Q", 37, 0x10, 0x14}, {"W", 38, 0x11, 0x1A}, {"E", 39, 0x12, 0x08}, {"R", 40, 0x13, 0x15}, {"T", 41, 0x14, 0x17}, {"Y", 42, 0x15, 0x1C}, {"U", 43, 0x16, 0x18}, {"I", 44, 0x17, 0x0C}, {"O", 45, 0x18, 0x12}, {"P", 46, 0x19, 0x13}, {"[", 47, 0x1A, 0x2F}, {"]", 48, 0x1B, 0x30}, {"|", 49, 0x2B, 0x31, 0, 60}, {"Del", 50, 0x53, 0x4C, 24}, {"End", 51, 0x4F, 0x4D}, {"PgD", 52, 0x51, 0x4E} },
|
||||||
|
{ {"Caps Lock", 53, 0x3A, 0x39, 0, 80}, {"A", 54, 0x1E, 0x04}, {"S", 55, 0x1F, 0x16}, {"D", 56, 0x20, 0x07}, {"F", 57, 0x21, 0x09}, {"G", 58, 0x22, 0x0A}, {"H", 59, 0x23, 0x0B}, {"J", 60, 0x24, 0x0D}, {"K", 61, 0x25, 0x0E}, {"L", 62, 0x26, 0x0F}, {";", 63, 0x27, 0x33}, {"'", 64, 0x28, 0x34}, {"Ret", 65, 0x1C, 0X28, 0, 84} },
|
||||||
|
{ {"Shift", 2, 0x2A, 0xE1, 0, 104}, {"Z", 66, 0x2C, 0x1D}, {"X", 67, 0x2D, 0x1B}, {"C", 68, 0x2E, 0x06}, {"V", 69, 0x2F, 0x19}, {"B", 70, 0x30, 0x05}, {"N", 71, 0x31, 0x11}, {"M", 72, 0x32, 0x10}, {",", 73, 0x33, 0x36}, {".", 74, 0x34, 0x37}, {"/", 75, 0x35, 0x38}, {"Shift", 2, 0x2A, 0xE5, 0, 104}, {"Up", 76, 0x48, 0x52, 68} },
|
||||||
|
{ {"Ctrl", 0, 0x1D, 0xE0, 0, 60}, {"Alt", 1, 0x38, 0xE2, 68, 60}, {"Space", 77, 0x39, 0X2c, 0, 260}, {"Alt", 1, 0x38, 0xE6, 0, 60}, {"Ctrl", 0, 0x1D, 0xE4, 68, 60}, {"Left", 78, 0x4B, 0x50, 24}, {"Down", 79, 0x50, 0x51}, {"Right", 80, 0x4D, 0x52} }
|
||||||
|
};
|
||||||
|
|
||||||
|
static const Key& GetKeyForScanCode(unsigned int scancode)
|
||||||
|
{
|
||||||
|
for (unsigned int y = 0; y < 6; y++)
|
||||||
|
{
|
||||||
|
int x = 0;
|
||||||
|
while (Keys[y][x].lib)
|
||||||
|
{
|
||||||
|
#ifdef SDL_h_
|
||||||
|
if (Keys[y][x].scanCodePage7 == scancode)
|
||||||
|
#elif WIN32
|
||||||
|
if (Keys[y][x].scanCodePage1 == scancode)
|
||||||
|
#else
|
||||||
|
#error
|
||||||
|
#endif
|
||||||
|
return Keys[y][x];
|
||||||
|
x++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Keys[0][0];
|
||||||
|
}
|
||||||
|
|
||||||
|
static unsigned int GetOrderedScanCodes(unsigned char scanCodes[4], unsigned char order[4])
|
||||||
|
{
|
||||||
|
for (int pass = 0; pass < 2; pass++)
|
||||||
|
{
|
||||||
|
for (int o = 0; o < 3; o++)
|
||||||
|
{
|
||||||
|
if (order[o] > order[o + 1])
|
||||||
|
{
|
||||||
|
ImSwap(order[o], order[o + 1]); ImSwap(scanCodes[o], scanCodes[o + 1]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (scanCodes[3] << 24) + (scanCodes[2] << 16) + (scanCodes[1] << 8) + scanCodes[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
static void HotKeySPrintf(char* buffer, size_t bufferSize, const char* fmt, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
va_start(args, fmt);
|
||||||
|
ImFormatStringV(buffer, bufferSize, fmt, args);
|
||||||
|
va_end(args);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void GetHotKeyLib(unsigned int functionKeys, char* buffer, size_t bufferSize, const char *functionLib = nullptr)
|
||||||
|
{
|
||||||
|
static const char* str[4] = { "%s", "%s + %s", "%s + %s +%s", "%s + %s + %s + %s" };
|
||||||
|
static const char* strLib[4] = { "%s (%s)", "%s (%s + %s)", "%s (%s + %s +%s)", "%s (%s + %s + %s + %s)" };
|
||||||
|
static const char* lib[4];
|
||||||
|
int scanCodeCount = 0;
|
||||||
|
for (int i = 0; i < 4; i++)
|
||||||
|
{
|
||||||
|
unsigned char scanCode = (unsigned char)(functionKeys >> i * 8);
|
||||||
|
if (scanCode == 0xFF)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
lib[scanCodeCount] = GetKeyForScanCode(scanCode).lib;
|
||||||
|
scanCodeCount++;
|
||||||
|
}
|
||||||
|
if (!scanCodeCount)
|
||||||
|
{
|
||||||
|
buffer[0] = 0;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (functionLib)
|
||||||
|
{
|
||||||
|
const char* fmt = strLib[scanCodeCount - 1];
|
||||||
|
HotKeySPrintf(buffer, bufferSize, fmt, functionLib, lib[0], lib[1], lib[2], lib[3]);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
const char* fmt = str[scanCodeCount - 1];
|
||||||
|
HotKeySPrintf(buffer, bufferSize, fmt, lib[0], lib[1], lib[2], lib[3]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Edit(HotKey *hotkey, size_t hotkeyCount, const char *popupModal)
|
||||||
|
{
|
||||||
|
static int editingHotkey = -1;
|
||||||
|
if (!hotkeyCount)
|
||||||
|
return;
|
||||||
|
static bool keyDown[512] = {};
|
||||||
|
|
||||||
|
ImGui::SetNextWindowSize(ImVec2(1060, 400));
|
||||||
|
if (!ImGui::BeginPopupModal(popupModal, NULL, ImGuiWindowFlags_NoResize))
|
||||||
|
return;
|
||||||
|
|
||||||
|
ImGui::BeginChildFrame(127, ImVec2(220, -1));
|
||||||
|
for(size_t i = 0;i< hotkeyCount;i++)
|
||||||
|
{
|
||||||
|
char hotKeyLib[128];
|
||||||
|
GetHotKeyLib(hotkey[i].functionKeys, hotKeyLib, sizeof(hotKeyLib), hotkey[i].functionName);
|
||||||
|
if (ImGui::Selectable(hotKeyLib, editingHotkey == int(i)) || editingHotkey == -1)
|
||||||
|
{
|
||||||
|
editingHotkey = int(i);
|
||||||
|
memset(keyDown, 0, sizeof(keyDown));
|
||||||
|
for (int j = 0; j < 4; j++)
|
||||||
|
{
|
||||||
|
int scan = (hotkey[editingHotkey].functionKeys >> (8 * j)) & 0xFF;
|
||||||
|
if (scan != 0xFF)
|
||||||
|
{
|
||||||
|
keyDown[scan] = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ImGui::EndChildFrame();
|
||||||
|
ImGui::SameLine();
|
||||||
|
ImGui::BeginGroup();
|
||||||
|
|
||||||
|
for (int i = ImGuiKey_NamedKey_BEGIN; i < ImGuiKey_NamedKey_END; i++)
|
||||||
|
{
|
||||||
|
if (ImGui::IsKeyPressed(ImGuiKey(i), false))
|
||||||
|
{
|
||||||
|
int imKey;
|
||||||
|
#ifdef SDL_h_
|
||||||
|
imKey = i;
|
||||||
|
#elif WIN32
|
||||||
|
imKey = MapVirtualKeyA(i, MAPVK_VK_TO_VSC);
|
||||||
|
#else
|
||||||
|
imKey = i;
|
||||||
|
#endif
|
||||||
|
keyDown[imKey - ImGuiKey_NamedKey_BEGIN] = !keyDown[imKey - ImGuiKey_NamedKey_BEGIN];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (unsigned int y = 0; y < 6; y++)
|
||||||
|
{
|
||||||
|
int x = 0;
|
||||||
|
ImGui::BeginGroup();
|
||||||
|
while (Keys[y][x].lib)
|
||||||
|
{
|
||||||
|
const Key& key = Keys[y][x];
|
||||||
|
const float ofs = key.offset + (x?4.f:0.f);
|
||||||
|
|
||||||
|
const float width = key.width;
|
||||||
|
if (x)
|
||||||
|
{
|
||||||
|
ImGui::SameLine(0.f, ofs);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (ofs >= 1.f)
|
||||||
|
{
|
||||||
|
ImGui::Indent(ofs);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#ifdef SDL_h_
|
||||||
|
bool& butSwtch = keyDown[key.scanCodePage7];
|
||||||
|
#elif WIN32
|
||||||
|
bool& butSwtch = keyDown[key.scanCodePage1];
|
||||||
|
#else
|
||||||
|
#error
|
||||||
|
#endif
|
||||||
|
ImGui::PushStyleColor(ImGuiCol_Button, butSwtch ? 0xFF1040FF : 0x80000000);
|
||||||
|
if (ImGui::Button(Keys[y][x].lib, ImVec2(width, 40)))
|
||||||
|
{
|
||||||
|
butSwtch = !butSwtch;
|
||||||
|
}
|
||||||
|
ImGui::PopStyleColor();
|
||||||
|
x++;
|
||||||
|
}
|
||||||
|
ImGui::EndGroup();
|
||||||
|
}
|
||||||
|
ImGui::InvisibleButton("space", ImVec2(10, 55));
|
||||||
|
ImGui::BeginChildFrame(18, ImVec2(540, 40));
|
||||||
|
ImGui::Text("%s :", hotkey[editingHotkey].functionName);
|
||||||
|
ImGui::SameLine();
|
||||||
|
ImGui::TextWrapped("%s", hotkey[editingHotkey].functionLib);
|
||||||
|
ImGui::EndChildFrame();
|
||||||
|
ImGui::SameLine();
|
||||||
|
int keyDownCount = 0;
|
||||||
|
for (auto d : keyDown)
|
||||||
|
{
|
||||||
|
keyDownCount += d ? 1 : 0;
|
||||||
|
}
|
||||||
|
if (ImGui::Button("Clear", ImVec2(80, 40)))
|
||||||
|
{
|
||||||
|
memset(keyDown, 0, sizeof(keyDown));
|
||||||
|
}
|
||||||
|
ImGui::SameLine();
|
||||||
|
if (keyDownCount && keyDownCount < 5)
|
||||||
|
{
|
||||||
|
if (ImGui::Button("Set", ImVec2(80, 40)))
|
||||||
|
{
|
||||||
|
unsigned char scanCodes[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
|
||||||
|
unsigned char order[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
|
||||||
|
int scanCodeCount = 0;
|
||||||
|
hotkey[editingHotkey].functionKeys = 0;
|
||||||
|
for (int i = 1; i < sizeof(keyDown); i++)
|
||||||
|
{
|
||||||
|
if (keyDown[i])
|
||||||
|
{
|
||||||
|
scanCodes[scanCodeCount] = (unsigned char)i;
|
||||||
|
order[scanCodeCount] = (unsigned char)GetKeyForScanCode(i).order;
|
||||||
|
scanCodeCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hotkey[editingHotkey].functionKeys = GetOrderedScanCodes(scanCodes, order);
|
||||||
|
}
|
||||||
|
ImGui::SameLine(0.f, 20.f);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ImGui::SameLine(0.f, 100.f);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ImGui::Button("Done", ImVec2(80, 40))) { ImGui::CloseCurrentPopup(); }
|
||||||
|
ImGui::EndGroup();
|
||||||
|
ImGui::EndPopup();
|
||||||
|
}
|
||||||
|
|
||||||
|
static int GetHotKey(HotKey *hotkey, size_t hotkeyCount)
|
||||||
|
{
|
||||||
|
static unsigned int lastHotKey = 0xFFFFFFFF;
|
||||||
|
unsigned char scanCodes[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
|
||||||
|
unsigned char order[4] = { 0xFF, 0xFF, 0xFF, 0xFF };
|
||||||
|
int scanCodeCount = 0;
|
||||||
|
for (int i = ImGuiKey_Aliases_BEGIN; i < ImGuiKey_Aliases_END; i++)
|
||||||
|
{
|
||||||
|
if (ImGui::IsKeyDown(ImGuiKey(i)))
|
||||||
|
{
|
||||||
|
int imKey;
|
||||||
|
#ifdef SDL_h_
|
||||||
|
imKey = i - ImGuiKey_NamedKey_BEGIN;
|
||||||
|
#elif WIN32
|
||||||
|
imKey = MapVirtualKeyA(i, MAPVK_VK_TO_VSC);
|
||||||
|
#else
|
||||||
|
imKey = i;
|
||||||
|
#endif
|
||||||
|
scanCodes[scanCodeCount] = (unsigned char)imKey;
|
||||||
|
order[scanCodeCount] = (unsigned char)GetKeyForScanCode(imKey).order;
|
||||||
|
scanCodeCount++;
|
||||||
|
if (scanCodeCount == 4)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int newHotKey = GetOrderedScanCodes(scanCodes, order);
|
||||||
|
|
||||||
|
if (scanCodeCount)
|
||||||
|
{
|
||||||
|
if (newHotKey != lastHotKey)
|
||||||
|
{
|
||||||
|
for (size_t i = 0; i < hotkeyCount; i++)
|
||||||
|
{
|
||||||
|
if (hotkey[i].functionKeys == newHotKey)
|
||||||
|
{
|
||||||
|
lastHotKey = newHotKey;
|
||||||
|
return int(i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
lastHotKey = 0xFFFFFFFF;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
lastHotKey = 0xFFFFFFFF;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
};
|
||||||
27
include/td/render/renderer/PlayerListRenderer.h
Normal file
27
include/td/render/renderer/PlayerListRenderer.h
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <td/render/Renderer.h>
|
||||||
|
#include <client/PlayerManager.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace render {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* \brief This is a debug class
|
||||||
|
*/
|
||||||
|
class PlayerListRenderer : public BasicRenderer {
|
||||||
|
private:
|
||||||
|
const client::PlayerManager& m_Players;
|
||||||
|
public:
|
||||||
|
utils::Signal<> OnPlayerCreate;
|
||||||
|
// utils::Signal<> OnRequestPOV;
|
||||||
|
utils::Signal<PlayerID> OnPlayerKick;
|
||||||
|
|
||||||
|
virtual void Render(float a_Lerp) override;
|
||||||
|
|
||||||
|
PlayerListRenderer(const client::PlayerManager& a_Players);
|
||||||
|
~PlayerListRenderer() {}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace render
|
||||||
|
} // namespace td
|
||||||
@@ -39,7 +39,7 @@ class ClientSimulation : public protocol::PacketHandler {
|
|||||||
* \brief Live update constructor (continuous game updates)
|
* \brief Live update constructor (continuous game updates)
|
||||||
* \param a_StepTime in ms
|
* \param a_StepTime in ms
|
||||||
*/
|
*/
|
||||||
ClientSimulation(std::shared_ptr<game::World> a_World, std::uint64_t a_StepTime);
|
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
|
* \return the progress [0-1] between two steps
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class ServerSimulation : public protocol::CommandHandler {
|
|||||||
game::World& m_World;
|
game::World& m_World;
|
||||||
std::uint64_t m_StepTime;
|
std::uint64_t m_StepTime;
|
||||||
std::uint64_t m_CurrentTime;
|
std::uint64_t m_CurrentTime;
|
||||||
std::vector<td::protocol::LockStep> m_History;
|
std::vector<protocol::LockStep> m_History;
|
||||||
|
|
||||||
using protocol::CommandHandler::Handle;
|
using protocol::CommandHandler::Handle;
|
||||||
|
|
||||||
@@ -27,6 +27,8 @@ class ServerSimulation : public protocol::CommandHandler {
|
|||||||
|
|
||||||
protocol::packets::LockStepsPacket MakePacket();
|
protocol::packets::LockStepsPacket MakePacket();
|
||||||
|
|
||||||
|
std::vector<protocol::LockStep> GetFirstLocksteps();
|
||||||
|
|
||||||
// no checks are done !
|
// no checks are done !
|
||||||
|
|
||||||
virtual void Handle(const protocol::commands::SpawnTroopCommand& a_SpawnTroop) override;
|
virtual void Handle(const protocol::commands::SpawnTroopCommand& a_SpawnTroop) override;
|
||||||
|
|||||||
@@ -5,13 +5,21 @@
|
|||||||
namespace td {
|
namespace td {
|
||||||
namespace client {
|
namespace client {
|
||||||
|
|
||||||
Client::Client(const std::shared_ptr<IClientSocket>& a_Socket, const std::string& a_PlayerName) : m_Socket(a_Socket) {
|
Client::Client(const std::shared_ptr<IClientSocket>& a_Socket) : m_Socket(a_Socket), m_Players(a_Socket) {
|
||||||
ChangeState<LoggingState>(a_PlayerName);
|
// ChangeState<LoggingState>(a_PlayerName);
|
||||||
|
}
|
||||||
|
|
||||||
|
Client::~Client() {
|
||||||
|
Disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::SendPacket(const protocol::PacketBase& a_Packet) {
|
void Client::SendPacket(const protocol::PacketBase& a_Packet) {
|
||||||
m_Socket->Send(a_Packet);
|
m_Socket->Send(a_Packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Client::Disconnect() {
|
||||||
|
m_Socket->Disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace client
|
} // namespace client
|
||||||
} // namespace td
|
} // 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
|
||||||
@@ -15,5 +15,9 @@ std::shared_ptr<FakeSocket> FakeSocket::Connect(const std::shared_ptr<server::Fa
|
|||||||
return socket;
|
return socket;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void FakeSocket::Disconnect() {
|
||||||
|
m_Server->DisconnectFakePeer(m_PeerId);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace client
|
} // namespace client
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -3,8 +3,8 @@
|
|||||||
namespace td {
|
namespace td {
|
||||||
namespace client {
|
namespace client {
|
||||||
|
|
||||||
GameState::GameState(Client& a_Client, const std::shared_ptr<game::World>& a_World, std::uint64_t a_StepTime) :
|
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) {
|
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) {
|
m_Simulation.OnMissingLockSteps.Connect([this](const std::vector<td::StepTime>& a_MissingSteps) {
|
||||||
SendPacket(protocol::packets::LockStepRequestPacket(a_MissingSteps));
|
SendPacket(protocol::packets::LockStepRequestPacket(a_MissingSteps));
|
||||||
});
|
});
|
||||||
|
|||||||
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
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
#include <client/state/LoggingState.h>
|
#include <client/state/LoggingState.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <client/state/LobbyState.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace client {
|
namespace client {
|
||||||
@@ -9,10 +9,18 @@ LoggingState::LoggingState(Client& a_Client, const std::string& a_PlayerName) :
|
|||||||
SendPacket(td::protocol::packets::PlayerLoginPacket(a_PlayerName));
|
SendPacket(td::protocol::packets::PlayerLoginPacket(a_PlayerName));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LoggingState::Update(float a_Delta) {
|
||||||
|
}
|
||||||
|
|
||||||
LoggingState::~LoggingState() {}
|
LoggingState::~LoggingState() {}
|
||||||
|
|
||||||
void LoggingState::Handle(const protocol::packets::PlayerJoinPacket& a_Packet) {
|
void LoggingState::Handle(const protocol::packets::PlayerJoinPacket& a_Packet) {
|
||||||
std::cout << "[Client] " << a_Packet->m_Player.m_PlayerName << "(" << +a_Packet->m_Player.m_PlayerId << ") joined !\n";
|
// 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 client
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <td/display/state/MainMenuState.h>
|
#include <td/display/state/DebugWorldState.h>
|
||||||
#include <td/misc/Time.h>
|
#include <td/misc/Time.h>
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
// init GL context
|
// init GL context
|
||||||
td::Display display(1920, 1080, "Tower-Defense 2");
|
td::Display display(1920, 1080, "Tower-Defense 2");
|
||||||
|
|
||||||
display.ChangeState<td::MainMenuState>();
|
display.ChangeState<td::DebugWorldState>();
|
||||||
|
|
||||||
td::Timer timer;
|
td::Timer timer;
|
||||||
while (!display.IsCloseRequested()) {
|
while (!display.IsCloseRequested()) {
|
||||||
|
|||||||
@@ -19,10 +19,11 @@ void IServerSocket::UnregisterHandler(const PlayerPacketHandler& a_Handler) {
|
|||||||
void IServerSocket::OnConnectPeer(PeerID a_PeerId) {
|
void IServerSocket::OnConnectPeer(PeerID a_PeerId) {
|
||||||
// here, the client is not a player yet (we need to wait for auth)
|
// here, the client is not a player yet (we need to wait for auth)
|
||||||
m_Ids.AddConnection(a_PeerId);
|
m_Ids.AddConnection(a_PeerId);
|
||||||
|
OnPlayerConnect(m_Ids.GetPlayerId(a_PeerId));
|
||||||
}
|
}
|
||||||
|
|
||||||
void IServerSocket::OnDisconnectPeer(PeerID a_PeerId) {
|
void IServerSocket::OnDisconnectPeer(PeerID a_PeerId) {
|
||||||
OnPlayerLeave(m_Ids.GetPlayerId(a_PeerId));
|
OnPlayerDisconnect(m_Ids.GetPlayerId(a_PeerId));
|
||||||
m_Ids.RemovePeer(a_PeerId);
|
m_Ids.RemovePeer(a_PeerId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,44 +6,52 @@
|
|||||||
namespace td {
|
namespace td {
|
||||||
namespace server {
|
namespace server {
|
||||||
|
|
||||||
PlayerManager::ConnectionHandler::ConnectionHandler(
|
PlayerManager::ConnectionHandler::ConnectionHandler(PlayerManager& a_PlayerManager, PlayerID a_Player) :
|
||||||
std::map<PlayerID, PlayerInfo>& a_Players, IServerSocket& a_Socket, PlayerID a_Player) :
|
m_PlayerManager(a_PlayerManager), m_Player(a_Player) {}
|
||||||
m_Players(a_Players), m_Socket(a_Socket), m_Player(a_Player) {}
|
|
||||||
|
|
||||||
void PlayerManager::ConnectionHandler::Handle(const protocol::packets::PlayerLoginPacket& a_Packet) {
|
void PlayerManager::ConnectionHandler::Handle(const protocol::packets::PlayerLoginPacket& a_Packet) {
|
||||||
PlayerInfo pInfo{m_Player, a_Packet->m_PlayerName};
|
PlayerInfo pInfo{m_Player, a_Packet->m_PlayerName};
|
||||||
|
|
||||||
std::vector<PlayerInfo> players;
|
auto& socket = *m_PlayerManager.m_Socket;
|
||||||
players.reserve(m_Players.size());
|
auto& players = m_PlayerManager.m_Players;
|
||||||
for (auto& [id, player] : m_Players) {
|
|
||||||
players.push_back(player);
|
std::vector<PlayerInfo> playerInfos;
|
||||||
|
playerInfos.reserve(players.size());
|
||||||
|
for (auto& [id, player] : players) {
|
||||||
|
playerInfos.push_back(player);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_Socket.Send(m_Player, protocol::packets::LoggingSuccessPacket(m_Player));
|
socket.Send(m_Player, protocol::packets::LoggingSuccessPacket(m_Player));
|
||||||
m_Socket.Send(m_Player, protocol::packets::PlayerListPacket(players));
|
socket.Send(m_Player, protocol::packets::PlayerListPacket(playerInfos));
|
||||||
m_Socket.Broadcast(protocol::packets::PlayerJoinPacket(pInfo));
|
socket.Broadcast(protocol::packets::PlayerJoinPacket(pInfo));
|
||||||
|
|
||||||
m_Players.emplace(m_Player, pInfo);
|
players.emplace(m_Player, pInfo);
|
||||||
m_Socket.OnPlayerJoin(m_Player, pInfo);
|
m_PlayerManager.OnPlayerJoin(m_Player, pInfo);
|
||||||
std::cout << "[Server] " << a_Packet->m_PlayerName << " joined !\n";
|
std::cout << "[Server] " << a_Packet->m_PlayerName << " joined !\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerManager::ConnectionHandler::Handle(const protocol::packets::DisconnectPacket& a_Packet) {
|
void PlayerManager::ConnectionHandler::Handle(const protocol::packets::DisconnectPacket& a_Packet) {
|
||||||
std::cout << "[Server] " << +m_Player << " wants to disconnect !\n";
|
m_PlayerManager.Disconnect(m_Player);
|
||||||
m_Socket.Disconnect(m_Player);
|
|
||||||
m_Players.erase(m_Player);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
PlayerManager::PlayerManager(const std::shared_ptr<IServerSocket>& a_Socket) : m_Socket(a_Socket) {
|
PlayerManager::PlayerManager(const std::shared_ptr<IServerSocket>& a_Socket) : m_Socket(a_Socket) {
|
||||||
a_Socket->RegisterHandler(
|
a_Socket->RegisterHandler([this](PlayerID a_PlayerId) { return std::make_unique<ConnectionHandler>(*this, a_PlayerId); });
|
||||||
[this](PlayerID a_PlayerId) { return std::make_unique<ConnectionHandler>(m_Players, *m_Socket, 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() {}
|
PlayerManager::~PlayerManager() {}
|
||||||
|
|
||||||
void PlayerManager::RemovePlayer(PlayerID a_Player) {
|
void PlayerManager::RemovePlayer(PlayerID a_Player) {
|
||||||
m_Socket->Disconnect(a_Player);
|
m_Socket->Disconnect(a_Player);
|
||||||
m_Players.erase(a_Player);
|
Disconnect(a_Player);
|
||||||
}
|
}
|
||||||
|
|
||||||
PlayerInfo PlayerManager::GetPlayer(PlayerID a_Player) {
|
PlayerInfo PlayerManager::GetPlayer(PlayerID a_Player) {
|
||||||
|
|||||||
@@ -2,10 +2,14 @@
|
|||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
|
||||||
|
#include <server/state/LobbyState.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace server {
|
namespace server {
|
||||||
|
|
||||||
Server::Server(const std::shared_ptr<IServerSocket>& a_Socket) : m_Socket(a_Socket), m_Players(a_Socket), m_LastMspt(0) {}
|
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) {
|
void Server::Update(float a_Delta) {
|
||||||
auto before = std::chrono::system_clock::now();
|
auto before = std::chrono::system_clock::now();
|
||||||
|
|||||||
@@ -8,8 +8,8 @@ namespace server {
|
|||||||
|
|
||||||
ServerState::ServerState(Server& a_Server) : Server::State(a_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_Socket->OnReceive, std::bind(&ServerState::HandlePacket, this, std::placeholders::_1, std::placeholders::_2));
|
||||||
Connect(m_StateMachine.m_Socket->OnPlayerJoin, std::bind(&ServerState::OnPlayerJoin, 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_Socket->OnPlayerLeave, std::bind(&ServerState::OnPlayerLeave, this, std::placeholders::_1));
|
Connect(m_StateMachine.m_Players.OnPlayerLeave, std::bind(&ServerState::OnPlayerLeave, this, std::placeholders::_1));
|
||||||
}
|
}
|
||||||
|
|
||||||
ServerState::~ServerState() {}
|
ServerState::~ServerState() {}
|
||||||
|
|||||||
@@ -1,13 +1,18 @@
|
|||||||
#include <server/state/GameState.h>
|
#include <server/state/GameState.h>
|
||||||
#include <server/state/GameStateHandler.h>
|
#include <server/state/GameStateHandler.h>
|
||||||
|
#include <td/protocol/packet/PacketSerialize.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace server {
|
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) {
|
GameState::GameState(Server& a_Server, const std::shared_ptr<game::World>& a_World) :
|
||||||
std::cout << "Switched to Game state !\n";
|
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());
|
BroadcastPacket(m_Simulation.MakePacket());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,5 +32,16 @@ void GameState::Update(float a_Delta) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 server
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -1,18 +1,60 @@
|
|||||||
#include <server/state/LobbyState.h>
|
|
||||||
#include <server/state/GameState.h>
|
#include <server/state/GameState.h>
|
||||||
|
#include <server/state/LobbyState.h>
|
||||||
|
|
||||||
#include <iostream>
|
#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 td {
|
||||||
namespace server {
|
namespace server {
|
||||||
|
|
||||||
void LobbyState::HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
void LobbyState::Update(float a_Delta) {
|
game::WorldPtr GetWorld() {
|
||||||
m_StateMachine.ChangeState<GameState>(m_World);
|
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 server
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -1,97 +1,86 @@
|
|||||||
#include <td/display/state/DebugWorldState.h>
|
#include <td/display/state/DebugWorldState.h>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include <td/game/World.h>
|
#include <td/game/World.h>
|
||||||
#include <td/protocol/packet/PacketSerialize.h>
|
|
||||||
#include <td/protocol/packet/Packets.h>
|
#include <td/protocol/packet/Packets.h>
|
||||||
#include <td/render/renderer/EntityRenderer.h>
|
#include <td/render/renderer/EntityRenderer.h>
|
||||||
|
#include <td/render/renderer/HotkeysRenderer.h>
|
||||||
|
#include <td/render/renderer/PlayerListRenderer.h>
|
||||||
#include <td/render/renderer/TowerRenderer.h>
|
#include <td/render/renderer/TowerRenderer.h>
|
||||||
#include <td/render/renderer/WorldRenderer.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/Server.h>
|
||||||
#include <server/socket/FakeSocket.h>
|
#include <server/socket/FakeSocket.h>
|
||||||
#include <server/state/GameState.h>
|
#include <server/state/LobbyState.h>
|
||||||
|
|
||||||
#include <client/Client.h>
|
#include <client/Client.h>
|
||||||
#include <client/socket/FakeSocket.h>
|
#include <client/socket/FakeSocket.h>
|
||||||
#include <client/state/GameState.h>
|
#include <client/state/GameState.h>
|
||||||
|
#include <client/state/LoggingState.h>
|
||||||
|
|
||||||
#include <td/display/Display.h>
|
#include <td/display/Display.h>
|
||||||
#include <td/display/state/DebugWorldState.h>
|
#include <td/display/state/DebugWorldState.h>
|
||||||
|
|
||||||
|
#include <imgui.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
DebugWorldState::DebugWorldState(Display& a_Display) : DisplayState(a_Display) {
|
DebugWorldState::DebugWorldState(Display& a_Display) : DisplayState(a_Display) {
|
||||||
// server
|
// server
|
||||||
game::WorldPtr serverWorld = GetWorld();
|
m_ServerSocket = std::make_shared<server::FakeSocket>();
|
||||||
auto serverFakeSocket = std::make_shared<server::FakeSocket>();
|
m_Server = std::make_unique<server::Server>(m_ServerSocket);
|
||||||
m_Server = std::make_unique<server::Server>(serverFakeSocket);
|
|
||||||
|
|
||||||
// client
|
// client
|
||||||
game::WorldPtr clientWorld = GetWorld();
|
auto clientFakeSocket = client::FakeSocket::Connect(m_ServerSocket);
|
||||||
auto clientFakeSocket = client::FakeSocket::Connect(serverFakeSocket);
|
m_Client = std::make_unique<client::Client>(clientFakeSocket);
|
||||||
m_Client = std::make_unique<client::Client>(clientFakeSocket, "Player0");
|
|
||||||
|
|
||||||
// render
|
// TODO: make it better
|
||||||
m_Renderer.AddRenderer<render::WorldRenderer>(m_Camera, clientWorld);
|
m_Client->OnStateChange.Connect([this](client::Client::State& a_State) {
|
||||||
m_Renderer.AddRenderer<render::EntityRenderer>(m_Camera, clientWorld);
|
if (auto gameState = dynamic_cast<client::GameState*>(&a_State)) {
|
||||||
m_Renderer.AddRenderer<render::TowerRenderer>(m_Camera, clientWorld);
|
// 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);
|
||||||
|
m_Renderer.AddRenderer<render::HotkeysRenderer>();
|
||||||
|
|
||||||
|
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
|
// camera
|
||||||
m_Camera.SetCamPos({77, 7, 13});
|
m_Camera.SetCamPos({77, 7, 13});
|
||||||
m_Camera.UpdatePerspective(m_StateMachine.GetAspectRatio());
|
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) {
|
void DebugWorldState::Update(float a_Delta) {
|
||||||
m_Server->Update(a_Delta);
|
m_Server->Update(a_Delta);
|
||||||
m_Client->Update(a_Delta);
|
m_Client->Update(a_Delta);
|
||||||
// TODO: m_ClientState might be invalid !
|
if (m_ClientState)
|
||||||
m_Renderer.Render(m_ClientState->GetCurrentLerp());
|
m_Renderer.Render(m_ClientState->GetCurrentLerp());
|
||||||
}
|
}
|
||||||
|
|
||||||
void DebugWorldState::OnAspectRatioChange(float a_Ratio) {
|
void DebugWorldState::OnAspectRatioChange(float a_Ratio) {
|
||||||
@@ -100,10 +89,17 @@ void DebugWorldState::OnAspectRatioChange(float a_Ratio) {
|
|||||||
|
|
||||||
void DebugWorldState::OnKeyDown(SDL_Keycode a_Key) {
|
void DebugWorldState::OnKeyDown(SDL_Keycode a_Key) {
|
||||||
// temporary tests
|
// temporary tests
|
||||||
if (a_Key == SDLK_A) {
|
switch (a_Key) {
|
||||||
m_Client->SendPacket(td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1));
|
case SDLK_A:
|
||||||
} else if (a_Key == SDLK_Z) {
|
m_Client->SendPacket(td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1));
|
||||||
m_Client->SendPacket(td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13)));
|
break;
|
||||||
|
|
||||||
|
case SDLK_Z:
|
||||||
|
m_Client->SendPacket(td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13)));
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#include <td/game/World.h>
|
#include <td/game/World.h>
|
||||||
|
|
||||||
#include <td/simulation/WorldTicker.h>
|
#include <td/simulation/WorldTicker.h>
|
||||||
|
#include <td/protocol/packet/PacketSerialize.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace game {
|
namespace game {
|
||||||
@@ -14,7 +15,7 @@ class ColorTileVisitor : public TileHandler {
|
|||||||
ColorTileVisitor(const World& a_World) : m_World(a_World), m_Result(nullptr) {}
|
ColorTileVisitor(const World& a_World) : m_World(a_World), m_Result(nullptr) {}
|
||||||
|
|
||||||
virtual void Handle(const EmptyTile& a_Tile) override {}
|
virtual void Handle(const EmptyTile& a_Tile) override {}
|
||||||
|
|
||||||
virtual void Handle(const TowerTile& a_Tile) override {
|
virtual void Handle(const TowerTile& a_Tile) override {
|
||||||
m_Result = &m_World.GetTowerTileColorPalette()[a_Tile->m_ColorPaletteRef];
|
m_Result = &m_World.GetTowerTileColorPalette()[a_Tile->m_ColorPaletteRef];
|
||||||
}
|
}
|
||||||
@@ -62,6 +63,16 @@ bool World::LoadMap(const protocol::pdata::WorldHeader& a_WorldHeader) {
|
|||||||
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;
|
||||||
|
|||||||
36
src/td/render/renderer/HotkeysRenderer.cpp
Normal file
36
src/td/render/renderer/HotkeysRenderer.cpp
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
#include <imgui_internal.h>
|
||||||
|
#include <td/render/renderer/HotkeysRenderer.h>
|
||||||
|
|
||||||
|
#include <imgui.h>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
|
#include <td/render/renderer/ImHotkey.h>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace render {
|
||||||
|
|
||||||
|
void HotkeysRenderer::Render(float a_Lerp) {
|
||||||
|
ImGui::Begin("Hotkeys");
|
||||||
|
static std::vector<ImHotKey::HotKey> hotkeys = {{"Layout", "Reorder nodes in a simpler layout", 0xFFFF261D},
|
||||||
|
{"Save", "Save the current graph", 0xFFFF1F1D}, {"Load", "Load an existing graph file", 0xFFFF181D},
|
||||||
|
{"Play/Stop", "Play or stop the animation from the current graph", 0xFFFFFF3F},
|
||||||
|
{"SetKey", "Make a new animation key with the current parameters values at the current time", 0xFFFFFF1F}};
|
||||||
|
|
||||||
|
// The editor is a modal window. bring it with something like that
|
||||||
|
if (ImGui::Button("Edit Hotkeys")) {
|
||||||
|
ImGui::OpenPopup("HotKeys Editor");
|
||||||
|
}
|
||||||
|
ImHotKey::Edit(hotkeys.data(), hotkeys.size(), "HotKeys Editor");
|
||||||
|
|
||||||
|
// ImHotKey also provides a way to retrieve HotKey
|
||||||
|
int hotkey = ImHotKey::GetHotKey(hotkeys.data(), hotkeys.size());
|
||||||
|
if (hotkey != -1) {
|
||||||
|
// handle the hotkey index!
|
||||||
|
}
|
||||||
|
ImGui::End();
|
||||||
|
}
|
||||||
|
|
||||||
|
HotkeysRenderer::HotkeysRenderer() {}
|
||||||
|
|
||||||
|
} // namespace render
|
||||||
|
} // namespace td
|
||||||
32
src/td/render/renderer/PlayerListRenderer.cpp
Normal file
32
src/td/render/renderer/PlayerListRenderer.cpp
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#include <td/render/renderer/PlayerListRenderer.h>
|
||||||
|
|
||||||
|
#include <imgui.h>
|
||||||
|
#include <optional>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
namespace render {
|
||||||
|
|
||||||
|
void PlayerListRenderer::Render(float a_Lerp) {
|
||||||
|
ImGui::Begin("Players");
|
||||||
|
if (ImGui::Button("Add player")) {
|
||||||
|
OnPlayerCreate();
|
||||||
|
}
|
||||||
|
std::optional<PlayerID> kick;
|
||||||
|
for (const auto& [id, player] : m_Players) {
|
||||||
|
ImGui::PushID(id);
|
||||||
|
ImGui::Text("[%i] %s", id, player.m_PlayerName.c_str());
|
||||||
|
ImGui::SameLine();
|
||||||
|
if (ImGui::Button("Kick")){
|
||||||
|
kick = id;
|
||||||
|
}
|
||||||
|
ImGui::PopID();
|
||||||
|
}
|
||||||
|
ImGui::End();
|
||||||
|
if (kick.has_value())
|
||||||
|
OnPlayerKick(*kick);
|
||||||
|
}
|
||||||
|
|
||||||
|
PlayerListRenderer::PlayerListRenderer(const client::PlayerManager& a_Players) : m_Players(a_Players) {}
|
||||||
|
|
||||||
|
} // namespace render
|
||||||
|
} // namespace td
|
||||||
@@ -30,14 +30,22 @@ ClientSimulation::ClientSimulation(std::shared_ptr<game::World> a_World, GameHis
|
|||||||
Step();
|
Step();
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientSimulation::ClientSimulation(std::shared_ptr<game::World> a_World, std::uint64_t a_StepTime) :
|
ClientSimulation::ClientSimulation(
|
||||||
|
std::shared_ptr<game::World> a_World, std::uint64_t a_StepTime, const std::vector<protocol::LockStep>& a_FirstSteps) :
|
||||||
m_StepTime(a_StepTime),
|
m_StepTime(a_StepTime),
|
||||||
m_World(a_World),
|
m_World(a_World),
|
||||||
m_History(std::numeric_limits<StepTime>::max()),
|
m_History(std::numeric_limits<StepTime>::max()),
|
||||||
m_CurrentTime(0),
|
m_CurrentTime(0),
|
||||||
m_CurrentStep(0),
|
m_CurrentStep(0),
|
||||||
m_LastSnapshot(std::make_shared<WorldSnapshot>()),
|
m_LastSnapshot(std::make_shared<WorldSnapshot>()),
|
||||||
m_LastValidStep(0) {}
|
m_LastValidStep(0) {
|
||||||
|
if (!a_FirstSteps.empty()) {
|
||||||
|
for (std::size_t i = 0; i < a_FirstSteps.size(); i++) {
|
||||||
|
m_History[i] = a_FirstSteps[i];
|
||||||
|
}
|
||||||
|
FastForward(a_FirstSteps.size());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
float ClientSimulation::Update(float a_Delta) {
|
float ClientSimulation::Update(float a_Delta) {
|
||||||
// TODO: handle freezes (m_CurrentTime > 2 * m_StepTime)
|
// TODO: handle freezes (m_CurrentTime > 2 * m_StepTime)
|
||||||
|
|||||||
@@ -47,6 +47,16 @@ protocol::packets::LockStepResponsePacket ServerSimulation::GetResponse(
|
|||||||
return {result};
|
return {result};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<protocol::LockStep> ServerSimulation::GetFirstLocksteps() {
|
||||||
|
std::vector<protocol::LockStep> result;
|
||||||
|
if (m_CurrentTime <= LOCKSTEP_BUFFER_SIZE)
|
||||||
|
return result;
|
||||||
|
|
||||||
|
result.reserve(m_CurrentTime);
|
||||||
|
result.insert(result.begin(), m_History.begin(), m_History.begin() + m_CurrentTime);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace sim
|
} // namespace sim
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
16
xmake.lua
16
xmake.lua
@@ -3,18 +3,20 @@ add_rules("mode.debug", "mode.release")
|
|||||||
add_repositories("persson-repo https://git.ale-pri.com/Persson-dev/xmake-repo.git")
|
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("imgui 1.92.0", {configs = {sdl3 = true, opengl3 = true}})
|
||||||
add_requires("libsdl3 3.2.16", "splib 2.3.1", "zlib", "glew", "fpm", "enet6")
|
add_requires("libsdl3 3.2.16", "splib 2.3.2", "zlib", "glew", "fpm", "enet6")
|
||||||
|
|
||||||
set_languages("c++17")
|
set_languages("c++20")
|
||||||
|
|
||||||
set_warnings("all")
|
set_warnings("all")
|
||||||
|
|
||||||
if is_mode("release") then
|
if is_mode("release") then
|
||||||
set_warnings("all", "error")
|
set_warnings("all", "error")
|
||||||
|
else
|
||||||
|
set_policy("build.sanitizer.address", true)
|
||||||
|
set_policy("build.sanitizer.leak", true)
|
||||||
|
set_policy("build.sanitizer.undefined", true)
|
||||||
end
|
end
|
||||||
|
|
||||||
option("valgrind", {description = "Run binary with valgrind", default = false})
|
|
||||||
|
|
||||||
target("Tower-Defense2")
|
target("Tower-Defense2")
|
||||||
add_includedirs("include", {public = true})
|
add_includedirs("include", {public = true})
|
||||||
set_kind("binary")
|
set_kind("binary")
|
||||||
@@ -23,12 +25,6 @@ target("Tower-Defense2")
|
|||||||
set_rundir(".")
|
set_rundir(".")
|
||||||
add_defines("TD_GL_LOADER_GLEW")
|
add_defines("TD_GL_LOADER_GLEW")
|
||||||
|
|
||||||
if has_config("valgrind") then
|
|
||||||
on_run(function (target)
|
|
||||||
os.execv("valgrind", {"-s", "--leak-check=full", target:targetfile()})
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
-- Tests
|
-- Tests
|
||||||
for _, file in ipairs(os.files("test/**.cpp")) do
|
for _, file in ipairs(os.files("test/**.cpp")) do
|
||||||
|
|||||||
Reference in New Issue
Block a user