Compare commits
23 Commits
39580c15c5
...
3D
| Author | SHA1 | Date | |
|---|---|---|---|
|
1ab2377f8e
|
|||
|
45c9e5180f
|
|||
|
19ebe7b64f
|
|||
|
d9baf7c5ef
|
|||
|
701dd6b120
|
|||
|
2225151f72
|
|||
|
aef0cf4d95
|
|||
|
550ff3aeec
|
|||
|
127fa1fcb8
|
|||
|
d1d2b63be8
|
|||
|
62c5c762f9
|
|||
|
1d436aa1c3
|
|||
|
73dd2dabfa
|
|||
|
5b6254c690
|
|||
|
688b6e93ea
|
|||
|
d64c366f4b
|
|||
|
7d58b881b2
|
|||
|
20acbc0499
|
|||
|
4fe2e25029
|
|||
|
fd08833f3f
|
|||
|
165ebf7b2e
|
|||
|
a02cb2b309
|
|||
|
bd56fb0646
|
2
.vscode/c_cpp_properties.json
vendored
2
.vscode/c_cpp_properties.json
vendored
@@ -2,7 +2,7 @@
|
||||
"configurations": [
|
||||
{
|
||||
"name": "TD2",
|
||||
"cppStandard": "c++17",
|
||||
"cppStandard": "c++20",
|
||||
"includePath": [
|
||||
"include"
|
||||
],
|
||||
|
||||
1721
assets/zombie.fbx
Normal file
1721
assets/zombie.fbx
Normal file
File diff suppressed because it is too large
Load Diff
BIN
assets/zombie.png
Normal file
BIN
assets/zombie.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 994 B |
@@ -1,23 +1,40 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/IClientSocket.h>
|
||||
#include <client/PlayerManager.h>
|
||||
#include <td/common/StateMachine.h>
|
||||
#include <optional>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class ClientState;
|
||||
class LoggingState;
|
||||
|
||||
class Client : public StateMachine<Client, void, float> {
|
||||
private:
|
||||
std::shared_ptr<IClientSocket> m_Socket;
|
||||
PlayerManager m_Players;
|
||||
std::optional<PlayerID> m_Id;
|
||||
|
||||
public:
|
||||
Client(const std::shared_ptr<IClientSocket>& a_Socket, const std::string& a_PlayerName);
|
||||
Client(const std::shared_ptr<IClientSocket>& a_Socket);
|
||||
~Client();
|
||||
|
||||
void SendPacket(const protocol::PacketBase& a_Packet);
|
||||
|
||||
void Disconnect();
|
||||
|
||||
const PlayerManager& GetPlayers() const {
|
||||
return m_Players;
|
||||
}
|
||||
|
||||
const std::optional<PlayerID> GetId() const {
|
||||
return m_Id;
|
||||
}
|
||||
|
||||
friend class ClientState;
|
||||
friend class LoggingState;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
|
||||
@@ -14,6 +14,7 @@ class IClientSocket {
|
||||
utils::Signal<const protocol::PacketBase&> OnReceive;
|
||||
|
||||
virtual void Send(const protocol::PacketBase& a_Packet) = 0;
|
||||
virtual void Disconnect() = 0;
|
||||
|
||||
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);
|
||||
|
||||
virtual void Send(const protocol::PacketBase& a_Packet) override;
|
||||
virtual void Disconnect() override;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
|
||||
@@ -9,12 +9,13 @@ namespace client {
|
||||
|
||||
class GameState : public ClientState {
|
||||
private:
|
||||
std::shared_ptr<game::World> m_World;
|
||||
game::WorldPtr m_World;
|
||||
sim::ClientSimulation m_Simulation;
|
||||
float m_CurrentLerp;
|
||||
float m_ElapsedTime;
|
||||
|
||||
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() {}
|
||||
|
||||
virtual void Update(float a_Delta) override;
|
||||
@@ -23,6 +24,14 @@ class GameState : public ClientState {
|
||||
return m_CurrentLerp;
|
||||
}
|
||||
|
||||
float GetElapsedTime() const {
|
||||
return m_ElapsedTime;
|
||||
}
|
||||
|
||||
game::WorldPtr GetWorld() const {
|
||||
return m_World;
|
||||
}
|
||||
|
||||
virtual void Handle(const protocol::packets::LockStepsPacket& a_LockStep) override;
|
||||
virtual void Handle(const protocol::packets::LockStepResponsePacket& a_LockStep) override;
|
||||
};
|
||||
|
||||
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();
|
||||
|
||||
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::LoggingSuccessPacket& a_Packet) override;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <server/Server.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class IServerSocket;
|
||||
|
||||
class ConnectionHandler : public protocol::PacketHandler {
|
||||
private:
|
||||
IServerSocket& m_Server;
|
||||
PlayerID m_Player;
|
||||
|
||||
public:
|
||||
ConnectionHandler(IServerSocket& a_Server, PlayerID a_Player);
|
||||
~ConnectionHandler() = default;
|
||||
|
||||
virtual void Handle(const protocol::packets::PlayerLoginPacket& a_Packet) override;
|
||||
virtual void Handle(const protocol::packets::DisconnectPacket& a_Packet) override;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
@@ -12,8 +12,8 @@ class IServerSocket {
|
||||
using PlayerPacketHandlerType = std::unique_ptr<protocol::PacketHandler>(PlayerID);
|
||||
using PlayerPacketHandler = std::function<PlayerPacketHandlerType>;
|
||||
|
||||
utils::Signal<PlayerID, const protocol::PlayerInfo&> OnPlayerJoin;
|
||||
utils::Signal<PlayerID> OnPlayerLeave;
|
||||
utils::Signal<PlayerID> OnPlayerConnect;
|
||||
utils::Signal<PlayerID> OnPlayerDisconnect;
|
||||
utils::Signal<PlayerID, const protocol::PacketBase&> OnReceive;
|
||||
|
||||
void Send(PlayerID a_PlayerId, const protocol::PacketBase& a_Packet);
|
||||
|
||||
44
include/server/PlayerManager.h
Normal file
44
include/server/PlayerManager.h
Normal file
@@ -0,0 +1,44 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/misc/Signal.h>
|
||||
#include <td/protocol/packet/Packets.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
class IServerSocket;
|
||||
|
||||
class PlayerManager {
|
||||
private:
|
||||
std::map<PlayerID, PlayerInfo> m_Players;
|
||||
std::shared_ptr<IServerSocket> m_Socket;
|
||||
|
||||
public:
|
||||
utils::Signal<PlayerID, const PlayerInfo&> OnPlayerJoin;
|
||||
utils::Signal<PlayerID> OnPlayerLeave;
|
||||
|
||||
PlayerManager(const std::shared_ptr<IServerSocket>& a_Socket);
|
||||
~PlayerManager();
|
||||
|
||||
void RemovePlayer(PlayerID a_Player);
|
||||
PlayerInfo GetPlayer(PlayerID a_Player);
|
||||
|
||||
private:
|
||||
void Disconnect(PlayerID a_Player);
|
||||
|
||||
class ConnectionHandler : public protocol::PacketHandler {
|
||||
private:
|
||||
PlayerManager& m_PlayerManager;
|
||||
PlayerID m_Player;
|
||||
|
||||
public:
|
||||
ConnectionHandler(PlayerManager& a_PlayerManager, PlayerID a_Player);
|
||||
~ConnectionHandler() = default;
|
||||
|
||||
virtual void Handle(const protocol::packets::PlayerLoginPacket& a_Packet) override;
|
||||
virtual void Handle(const protocol::packets::DisconnectPacket& a_Packet) override;
|
||||
};
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <server/IServerSocket.h>
|
||||
#include <td/common/StateMachine.h>
|
||||
#include <server/PlayerManager.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
@@ -11,9 +12,17 @@ class ServerState;
|
||||
class Server : public StateMachine<Server, void, float> {
|
||||
private:
|
||||
std::shared_ptr<IServerSocket> m_Socket;
|
||||
PlayerManager m_Players;
|
||||
float m_LastMspt;
|
||||
|
||||
public:
|
||||
Server(const std::shared_ptr<IServerSocket>& a_Socket) : m_Socket(a_Socket) {}
|
||||
Server(const std::shared_ptr<IServerSocket>& a_Socket);
|
||||
|
||||
virtual void Update(float a_Delta);
|
||||
|
||||
const PlayerManager& GetPlayers() const {
|
||||
return m_Players;
|
||||
}
|
||||
|
||||
friend class ServerState;
|
||||
};
|
||||
|
||||
@@ -10,7 +10,7 @@ class ServerState : public Server::State, private utils::SlotGuard {
|
||||
public:
|
||||
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) = 0;
|
||||
virtual void Update(float a_Delta) = 0;
|
||||
virtual void OnPlayerJoin(PlayerID a_Id, const td::protocol::PlayerInfo& a_Info) {}
|
||||
virtual void OnPlayerJoin(PlayerID a_Id, const td::PlayerInfo& a_Info) {}
|
||||
virtual void OnPlayerLeave(PlayerID a_Id) {}
|
||||
|
||||
ServerState(Server& a_Server);
|
||||
|
||||
@@ -22,6 +22,8 @@ class GameState : public ServerState {
|
||||
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) override;
|
||||
virtual void Update(float a_Delta) override;
|
||||
|
||||
virtual void OnPlayerJoin(PlayerID a_Id, const td::PlayerInfo& a_Info) override;
|
||||
|
||||
friend class GameStateHandler;
|
||||
};
|
||||
|
||||
|
||||
@@ -10,9 +10,11 @@ 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(Server& a_Server);
|
||||
~LobbyState() {}
|
||||
|
||||
virtual void OnPlayerJoin(PlayerID a_Id, const td::PlayerInfo& a_Info) override;
|
||||
|
||||
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) override;
|
||||
virtual void Update(float a_Delta) override;
|
||||
};
|
||||
|
||||
@@ -183,6 +183,51 @@ T Lerp(T v0, T v1, T t) {
|
||||
|
||||
} // namespace maths
|
||||
|
||||
template<typename T>
|
||||
Vec2<T> operator+(const Vec2<T>& vect, const Vec2<T>& other) {
|
||||
return {vect.x + other.x, vect.y + other.y};
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Vec2<T> operator- (const Vec2<T>& vect) {
|
||||
return { -vect.x, -vect.y };
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Vec2<T> operator- (const Vec2<T>& vect, const Vec2<T>& other) {
|
||||
return vect + (-other);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Vec3<T> operator- (const Vec3<T>& vect) {
|
||||
return { -vect.x, -vect.y, -vect.z };
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Vec3<T> operator+ (const Vec3<T>& vect, const Vec3<T>& other) {
|
||||
return { vect.x + other.x, vect.y + other.y, vect.z + other.z };
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Vec3<T> operator- (const Vec3<T>& vect, const Vec3<T>& other) {
|
||||
return vect + (-other);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Vec4<T> operator- (const Vec4<T>& vect) {
|
||||
return { -vect.x, -vect.y, -vect.z, -vect.w };
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Vec4<T> operator+ (const Vec4<T>& vect, const Vec4<T>& other) {
|
||||
return { vect.x + other.x, vect.y + other.y, vect.z + other.z, vect.w + other.w };
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
Vec4<T> operator- (const Vec4<T>& vect, const Vec4<T>& other) {
|
||||
return vect + (-other);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Vec2<T>& a_Vec) {
|
||||
return a_Buffer << a_Vec.x << a_Vec.y;
|
||||
|
||||
@@ -78,6 +78,11 @@ enum class Direction : std::uint8_t {
|
||||
NegativeY = 1 << 3,
|
||||
};
|
||||
|
||||
struct PlayerInfo {
|
||||
PlayerID m_PlayerId;
|
||||
std::string m_PlayerName;
|
||||
};
|
||||
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const EntityCoords& a_Coords);
|
||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const FpFloat& a_Float);
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
#include <td/misc/Signal.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
@@ -16,27 +16,34 @@ class StateMachine {
|
||||
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)...);
|
||||
void ChangeState(Args... args) {
|
||||
m_StateMachine.template ChangeState<T>(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
protected:
|
||||
TDerived& m_StateMachine;
|
||||
};
|
||||
|
||||
utils::Signal<State&> OnStateChange;
|
||||
|
||||
StateMachine() {}
|
||||
StateMachine(StateMachine&&) = default;
|
||||
virtual ~StateMachine() {}
|
||||
|
||||
virtual TReturn Update(TArgs... args) {
|
||||
assert(m_State && "You must change state at least once before updating !");
|
||||
return m_State->Update(args...);
|
||||
return m_State->Update(std::forward<TArgs>(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());
|
||||
void ChangeState(Args... args) {
|
||||
auto* currentState = m_State.get();
|
||||
auto newState = std::make_unique<T>(static_cast<TDerived&>(*this), std::forward<Args>(args)...);
|
||||
// This allows chaining
|
||||
if (m_State.get() == currentState) {
|
||||
m_State = std::move(newState);
|
||||
OnStateChange(*m_State);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
#include <td/display/Display.h>
|
||||
#include <td/misc/SlotGuard.h>
|
||||
#include <client/ClientState.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
class DisplayState : public Display::State, private utils::SlotGuard {
|
||||
public:
|
||||
DisplayState(Display& a_Display);
|
||||
@@ -13,4 +15,5 @@ class DisplayState : public Display::State, private utils::SlotGuard {
|
||||
virtual void OnAspectRatioChange(float a_Ratio) {}
|
||||
virtual void OnKeyDown(SDL_Keycode a_Key) {}
|
||||
};
|
||||
|
||||
} // namespace td
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <td/render/Renderer.h>
|
||||
#include <td/simulation/ClientSimulation.h>
|
||||
#include <client/state/GameState.h>
|
||||
#include <server/socket/FakeSocket.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
@@ -13,10 +14,15 @@ 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;
|
||||
std::unique_ptr<client::Client> m_Client;
|
||||
client::GameState* m_ClientState;
|
||||
|
||||
std::vector<std::unique_ptr<client::Client>> m_FakeClients;
|
||||
std::shared_ptr<server::FakeSocket> m_ServerSocket;
|
||||
|
||||
unsigned int m_PlaySpeed = 1;
|
||||
|
||||
public:
|
||||
DebugWorldState(Display& a_Display);
|
||||
~DebugWorldState();
|
||||
|
||||
@@ -77,7 +77,7 @@ public:
|
||||
struct TeamList {
|
||||
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::WorldData& worldData);
|
||||
|
||||
protocol::packets::WorldHeaderPacket GetPacketHeader() const;
|
||||
protocol::packets::WorldDataPacket GetPacketData() const;
|
||||
|
||||
bool LoadMapFromFile(const std::string& fileName);
|
||||
bool SaveMap(const std::string& fileName) const;
|
||||
|
||||
@@ -102,7 +105,7 @@ class World {
|
||||
return m_CurrentState->m_Teams[TeamColor::Blue];
|
||||
}
|
||||
const Team& GetBlueTeam() const {
|
||||
return m_CurrentState->m_Teams[TeamColor::Red];
|
||||
return m_CurrentState->m_Teams[TeamColor::Blue];
|
||||
}
|
||||
|
||||
Team& GetTeam(TeamColor team) {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <td/common/NonCopyable.h>
|
||||
#include <vector>
|
||||
|
||||
@@ -15,7 +16,7 @@ template <typename... Args>
|
||||
class SignalRaw : private NonCopyable {
|
||||
public:
|
||||
using FnType = void(Args...);
|
||||
using CallBack = std::function<FnType>;
|
||||
using CallBack = std::shared_ptr<std::function<FnType>>;
|
||||
|
||||
private:
|
||||
std::vector<CallBack> m_Callbacks;
|
||||
@@ -26,14 +27,13 @@ class SignalRaw : private NonCopyable {
|
||||
}
|
||||
|
||||
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>(); });
|
||||
auto it = std::find(m_Callbacks.begin(), m_Callbacks.end(), a_Callback);
|
||||
m_Callbacks.erase(it);
|
||||
}
|
||||
|
||||
void operator()(Args... args) const {
|
||||
for (const CallBack& callback : m_Callbacks) {
|
||||
callback(args...);
|
||||
callback->operator()(args...);
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -46,6 +46,7 @@ class Signal {
|
||||
public:
|
||||
using SignalBase = SignalRaw<Args...>;
|
||||
using CallBack = typename SignalBase::CallBack;
|
||||
using CallBackRaw = typename CallBack::element_type;
|
||||
using SignalPtr = std::shared_ptr<SignalBase>;
|
||||
class ConnectionGuard;
|
||||
|
||||
@@ -56,12 +57,15 @@ class Signal {
|
||||
Signal() : m_Signal(std::make_shared<SignalBase>()) {}
|
||||
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) {
|
||||
Connect(a_Callback);
|
||||
m_Signal->Connect(a_Callback);
|
||||
return std::make_unique<ConnectionGuard>(*this, a_Callback);
|
||||
}
|
||||
|
||||
@@ -88,7 +92,8 @@ class Signal<Args...>::ConnectionGuard : public Connection {
|
||||
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(const Signal<Args...>& a_Signal, const CallBack& a_Callback) :
|
||||
m_Signal(a_Signal.m_Signal), m_Callback(a_Callback) {}
|
||||
|
||||
~ConnectionGuard() {
|
||||
if (!m_Signal.expired())
|
||||
|
||||
@@ -19,8 +19,9 @@ class SlotGuard {
|
||||
* \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 Connect(Signal<Args...> a_Signal, const typename Signal<Args...>::CallBack::element_type& a_Callback) {
|
||||
auto ptr = std::make_shared<typename Signal<Args...>::CallBack::element_type>(a_Callback);
|
||||
m_Connections.push_back(a_Signal.ConnectSafe(ptr));
|
||||
}
|
||||
|
||||
void Disconnect() {
|
||||
|
||||
@@ -16,11 +16,6 @@
|
||||
namespace td {
|
||||
namespace protocol {
|
||||
|
||||
struct PlayerInfo {
|
||||
PlayerID m_PlayerId;
|
||||
std::string m_PlayerName;
|
||||
};
|
||||
|
||||
struct MapData {
|
||||
|
||||
};
|
||||
@@ -72,8 +67,6 @@ struct ChatMessage {
|
||||
std::string m_Text;
|
||||
};
|
||||
|
||||
// TODO: handle players joining in the first second
|
||||
|
||||
struct BeginGame {
|
||||
std::vector<PlayerInfo> m_BlueTeam;
|
||||
std::vector<PlayerInfo> m_RedTeam;
|
||||
|
||||
@@ -35,6 +35,7 @@ class Camera {
|
||||
void UpdatePerspective(float a_AspectRatio);
|
||||
|
||||
void SetCamPos(const Vec3f& a_NewPos);
|
||||
const Vec3f& GetCamPos() const;
|
||||
};
|
||||
|
||||
} // namespace render
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <td/misc/SlotGuard.h>
|
||||
#include <td/render/Camera.h>
|
||||
#include <td/render/loader/FbxLoader.h>
|
||||
#include <td/render/loader/GLLoader.h>
|
||||
#include <td/render/shader/CameraShaderProgram.h>
|
||||
#include <td/misc/SlotGuard.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
@@ -15,6 +16,7 @@ class BasicRenderer {
|
||||
virtual ~BasicRenderer() {}
|
||||
|
||||
void Render(const GL::VertexArray& a_Vao);
|
||||
void Render(const Model& a_Model);
|
||||
};
|
||||
|
||||
template <typename TShader>
|
||||
@@ -42,8 +44,11 @@ class RenderPipeline {
|
||||
virtual ~RenderPipeline() {}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
void AddRenderer(Args&&... args) {
|
||||
m_Renderers.push_back(std::make_unique<T>(args...));
|
||||
T& AddRenderer(Args&&... args) {
|
||||
auto ptr = std::make_unique<T>(args...);
|
||||
auto rawPtr = ptr.get();
|
||||
m_Renderers.push_back(std::move(ptr));
|
||||
return *rawPtr;
|
||||
}
|
||||
|
||||
void Clear() {
|
||||
|
||||
19
include/td/render/loader/FbxLoader.h
Normal file
19
include/td/render/loader/FbxLoader.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/render/loader/GLLoader.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace td {
|
||||
|
||||
struct Model {
|
||||
std::vector<std::unique_ptr<GL::VertexArray>> m_Vaos;
|
||||
};
|
||||
|
||||
namespace ModelLoader {
|
||||
|
||||
Model LoadModel(const std::string& fileName);
|
||||
|
||||
}
|
||||
}
|
||||
31
include/td/render/loader/TextureLoader.h
Normal file
31
include/td/render/loader/TextureLoader.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <sp/common/NonCopyable.h>
|
||||
#include <string>
|
||||
|
||||
namespace td {
|
||||
namespace GL {
|
||||
|
||||
class Texture : private sp::NonCopyable {
|
||||
public:
|
||||
Texture(const char* a_Data, int a_Width, int a_Height, int a_Comp);
|
||||
Texture(Texture&& a_Other);
|
||||
|
||||
~Texture();
|
||||
|
||||
void Bind() const;
|
||||
void Unbind() const;
|
||||
|
||||
private:
|
||||
unsigned int m_ID;
|
||||
};
|
||||
|
||||
} // namespace GL
|
||||
|
||||
namespace TextureLoader {
|
||||
|
||||
GL::Texture LoadTexture(const std::string& fileName);
|
||||
|
||||
}
|
||||
|
||||
} // namespace td
|
||||
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <td/render/loader/TextureLoader.h>
|
||||
#include <td/render/loader/FbxLoader.h>
|
||||
#include <td/render/Renderer.h>
|
||||
#include <td/render/shader/EntityShader.h>
|
||||
#include <td/game/World.h>
|
||||
@@ -10,7 +12,8 @@ namespace render {
|
||||
class EntityRenderer : public Renderer<shader::EntityShader> {
|
||||
private:
|
||||
game::WorldPtr m_World;
|
||||
std::unique_ptr<GL::VertexArray> m_EntityVao;
|
||||
Model m_EntityModel;
|
||||
std::unique_ptr<GL::Texture> m_EntityTexture;
|
||||
|
||||
public:
|
||||
EntityRenderer(Camera& a_Camera, const game::WorldPtr& a_World);
|
||||
|
||||
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
|
||||
21
include/td/render/renderer/TimerRenderer.h
Normal file
21
include/td/render/renderer/TimerRenderer.h
Normal file
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include "client/state/GameState.h"
|
||||
#include <td/render/Renderer.h>
|
||||
#include <client/PlayerManager.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
class TimerRenderer : public BasicRenderer {
|
||||
private:
|
||||
const client::GameState& m_State;
|
||||
public:
|
||||
virtual void Render(float a_Lerp) override;
|
||||
|
||||
TimerRenderer(const client::GameState& a_State);
|
||||
~TimerRenderer() {}
|
||||
};
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
@@ -17,6 +17,9 @@ class WorldRenderer : public Renderer<shader::WorldShader> {
|
||||
virtual ~WorldRenderer();
|
||||
|
||||
virtual void Render(float a_Lerp) override;
|
||||
|
||||
private:
|
||||
void UpdateControls();
|
||||
};
|
||||
|
||||
} // namespace render
|
||||
|
||||
@@ -39,7 +39,7 @@ class ClientSimulation : public protocol::PacketHandler {
|
||||
* \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);
|
||||
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
|
||||
|
||||
@@ -16,7 +16,7 @@ class ServerSimulation : public protocol::CommandHandler {
|
||||
game::World& m_World;
|
||||
std::uint64_t m_StepTime;
|
||||
std::uint64_t m_CurrentTime;
|
||||
std::vector<td::protocol::LockStep> m_History;
|
||||
std::vector<protocol::LockStep> m_History;
|
||||
|
||||
using protocol::CommandHandler::Handle;
|
||||
|
||||
@@ -27,6 +27,8 @@ class ServerSimulation : public protocol::CommandHandler {
|
||||
|
||||
protocol::packets::LockStepsPacket MakePacket();
|
||||
|
||||
std::vector<protocol::LockStep> GetFirstLocksteps();
|
||||
|
||||
// no checks are done !
|
||||
|
||||
virtual void Handle(const protocol::commands::SpawnTroopCommand& a_SpawnTroop) override;
|
||||
|
||||
@@ -5,13 +5,21 @@
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
Client::Client(const std::shared_ptr<IClientSocket>& a_Socket, const std::string& a_PlayerName) : m_Socket(a_Socket) {
|
||||
ChangeState<LoggingState>(a_PlayerName);
|
||||
Client::Client(const std::shared_ptr<IClientSocket>& a_Socket) : m_Socket(a_Socket), m_Players(a_Socket) {
|
||||
// ChangeState<LoggingState>(a_PlayerName);
|
||||
}
|
||||
|
||||
Client::~Client() {
|
||||
Disconnect();
|
||||
}
|
||||
|
||||
void Client::SendPacket(const protocol::PacketBase& a_Packet) {
|
||||
m_Socket->Send(a_Packet);
|
||||
}
|
||||
|
||||
void Client::Disconnect() {
|
||||
m_Socket->Disconnect();
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
void FakeSocket::Disconnect() {
|
||||
m_Server->DisconnectFakePeer(m_PeerId);
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
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) {
|
||||
GameState::GameState(Client& a_Client, const std::shared_ptr<game::World>& a_World, std::uint64_t a_StepTime, const std::vector<protocol::LockStep> a_FirstSteps) :
|
||||
ClientState(a_Client), m_World(a_World), m_Simulation(a_World, a_StepTime, a_FirstSteps) {
|
||||
m_Simulation.OnMissingLockSteps.Connect([this](const std::vector<td::StepTime>& a_MissingSteps) {
|
||||
SendPacket(protocol::packets::LockStepRequestPacket(a_MissingSteps));
|
||||
});
|
||||
@@ -20,6 +20,7 @@ void GameState::Handle(const protocol::packets::LockStepResponsePacket& a_LockSt
|
||||
|
||||
void GameState::Update(float a_Delta) {
|
||||
m_CurrentLerp = m_Simulation.Update(a_Delta);
|
||||
m_ElapsedTime += a_Delta;
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
|
||||
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 <iostream>
|
||||
#include <client/state/LobbyState.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
@@ -9,10 +9,18 @@ LoggingState::LoggingState(Client& a_Client, const std::string& a_PlayerName) :
|
||||
SendPacket(td::protocol::packets::PlayerLoginPacket(a_PlayerName));
|
||||
}
|
||||
|
||||
void LoggingState::Update(float a_Delta) {
|
||||
}
|
||||
|
||||
LoggingState::~LoggingState() {}
|
||||
|
||||
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
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
#include <server/ConnectionHandler.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
ConnectionHandler::ConnectionHandler(IServerSocket& a_Server, PlayerID a_Player) : m_Server(a_Server), m_Player(a_Player) {}
|
||||
|
||||
void ConnectionHandler::Handle(const protocol::packets::PlayerLoginPacket& a_Packet) {
|
||||
protocol::PlayerInfo pInfo{m_Player, a_Packet->m_PlayerName};
|
||||
|
||||
// TODO
|
||||
std::vector<protocol::PlayerInfo> players;
|
||||
|
||||
m_Server.Send(m_Player, protocol::packets::LoggingSuccessPacket(m_Player));
|
||||
m_Server.Send(m_Player, protocol::packets::PlayerListPacket(players));
|
||||
m_Server.Broadcast(protocol::packets::PlayerJoinPacket(pInfo));
|
||||
|
||||
m_Server.OnPlayerJoin(m_Player, pInfo);
|
||||
std::cout << "[Server] " << a_Packet->m_PlayerName << " tried to join !\n";
|
||||
}
|
||||
|
||||
void ConnectionHandler::Handle(const protocol::packets::DisconnectPacket& a_Packet) {
|
||||
std::cout << "[Server] " << +m_Player << " wants to disconnect !\n";
|
||||
m_Server.Disconnect(m_Player);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
@@ -1,12 +1,9 @@
|
||||
#include <server/IServerSocket.h>
|
||||
|
||||
#include <server/ConnectionHandler.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
IServerSocket::IServerSocket() {
|
||||
RegisterHandler([this](PlayerID a_PlayerId) { return std::make_unique<ConnectionHandler>(*this, a_PlayerId); });
|
||||
}
|
||||
|
||||
void IServerSocket::RegisterHandler(const PlayerPacketHandler& a_Handler) {
|
||||
@@ -22,10 +19,11 @@ void IServerSocket::UnregisterHandler(const PlayerPacketHandler& a_Handler) {
|
||||
void IServerSocket::OnConnectPeer(PeerID a_PeerId) {
|
||||
// here, the client is not a player yet (we need to wait for auth)
|
||||
m_Ids.AddConnection(a_PeerId);
|
||||
OnPlayerConnect(m_Ids.GetPlayerId(a_PeerId));
|
||||
}
|
||||
|
||||
void IServerSocket::OnDisconnectPeer(PeerID a_PeerId) {
|
||||
OnPlayerLeave(m_Ids.GetPlayerId(a_PeerId));
|
||||
OnPlayerDisconnect(m_Ids.GetPlayerId(a_PeerId));
|
||||
m_Ids.RemovePeer(a_PeerId);
|
||||
}
|
||||
|
||||
|
||||
62
src/server/PlayerManager.cpp
Normal file
62
src/server/PlayerManager.cpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <server/PlayerManager.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <server/IServerSocket.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
PlayerManager::ConnectionHandler::ConnectionHandler(PlayerManager& a_PlayerManager, PlayerID a_Player) :
|
||||
m_PlayerManager(a_PlayerManager), m_Player(a_Player) {}
|
||||
|
||||
void PlayerManager::ConnectionHandler::Handle(const protocol::packets::PlayerLoginPacket& a_Packet) {
|
||||
PlayerInfo pInfo{m_Player, a_Packet->m_PlayerName};
|
||||
|
||||
auto& socket = *m_PlayerManager.m_Socket;
|
||||
auto& players = m_PlayerManager.m_Players;
|
||||
|
||||
std::vector<PlayerInfo> playerInfos;
|
||||
playerInfos.reserve(players.size());
|
||||
for (auto& [id, player] : players) {
|
||||
playerInfos.push_back(player);
|
||||
}
|
||||
|
||||
socket.Send(m_Player, protocol::packets::LoggingSuccessPacket(m_Player));
|
||||
socket.Send(m_Player, protocol::packets::PlayerListPacket(playerInfos));
|
||||
socket.Broadcast(protocol::packets::PlayerJoinPacket(pInfo));
|
||||
|
||||
players.emplace(m_Player, pInfo);
|
||||
m_PlayerManager.OnPlayerJoin(m_Player, pInfo);
|
||||
std::cout << "[Server] " << a_Packet->m_PlayerName << " joined !\n";
|
||||
}
|
||||
|
||||
void PlayerManager::ConnectionHandler::Handle(const protocol::packets::DisconnectPacket& a_Packet) {
|
||||
m_PlayerManager.Disconnect(m_Player);
|
||||
}
|
||||
|
||||
PlayerManager::PlayerManager(const std::shared_ptr<IServerSocket>& a_Socket) : m_Socket(a_Socket) {
|
||||
a_Socket->RegisterHandler([this](PlayerID a_PlayerId) { return std::make_unique<ConnectionHandler>(*this, a_PlayerId); });
|
||||
a_Socket->OnPlayerDisconnect.Connect(std::bind(&PlayerManager::Disconnect, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
void PlayerManager::Disconnect(PlayerID a_Player) {
|
||||
if (!m_Players.contains(a_Player))
|
||||
return;
|
||||
std::cout << "[Server] " << +a_Player << " wants to disconnect !\n";
|
||||
m_Socket->Broadcast(protocol::packets::PlayerLeavePacket(a_Player));
|
||||
m_Players.erase(a_Player);
|
||||
}
|
||||
|
||||
PlayerManager::~PlayerManager() {}
|
||||
|
||||
void PlayerManager::RemovePlayer(PlayerID a_Player) {
|
||||
m_Socket->Disconnect(a_Player);
|
||||
Disconnect(a_Player);
|
||||
}
|
||||
|
||||
PlayerInfo PlayerManager::GetPlayer(PlayerID a_Player) {
|
||||
return m_Players.at(a_Player);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
@@ -1,9 +1,22 @@
|
||||
#include <server/Server.h>
|
||||
|
||||
#include <chrono>
|
||||
|
||||
#include <server/state/LobbyState.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
Server::Server(const std::shared_ptr<IServerSocket>& a_Socket) : m_Socket(a_Socket), m_Players(a_Socket), m_LastMspt(0) {
|
||||
ChangeState<LobbyState>();
|
||||
}
|
||||
|
||||
void Server::Update(float a_Delta) {
|
||||
auto before = std::chrono::system_clock::now();
|
||||
StateMachine<Server, void, float>::Update(a_Delta);
|
||||
m_LastMspt = std::chrono::duration<float, std::chrono::milliseconds::period>(std::chrono::system_clock::now() - before).count();
|
||||
// std::cout << "Tick : " << m_LastMspt << "ms\n";
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
|
||||
@@ -8,8 +8,8 @@ namespace server {
|
||||
|
||||
ServerState::ServerState(Server& a_Server) : Server::State(a_Server) {
|
||||
Connect(m_StateMachine.m_Socket->OnReceive, std::bind(&ServerState::HandlePacket, this, std::placeholders::_1, std::placeholders::_2));
|
||||
Connect(m_StateMachine.m_Socket->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.OnPlayerJoin, std::bind(&ServerState::OnPlayerJoin, this, std::placeholders::_1, std::placeholders::_2));
|
||||
Connect(m_StateMachine.m_Players.OnPlayerLeave, std::bind(&ServerState::OnPlayerLeave, this, std::placeholders::_1));
|
||||
}
|
||||
|
||||
ServerState::~ServerState() {}
|
||||
|
||||
@@ -1,13 +1,18 @@
|
||||
#include <server/state/GameState.h>
|
||||
#include <server/state/GameStateHandler.h>
|
||||
#include <td/protocol/packet/PacketSerialize.h>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
GameState::GameState(Server& a_Server, const std::shared_ptr<game::World>& a_World) : ServerState(a_Server), m_World(a_World), m_Simulation(*m_World, STEP_TIME), m_Time(0) {
|
||||
std::cout << "Switched to Game state !\n";
|
||||
GameState::GameState(Server& a_Server, const std::shared_ptr<game::World>& a_World) :
|
||||
ServerState(a_Server), m_World(a_World), m_Simulation(*m_World, STEP_TIME), m_Time(0) {
|
||||
std::cout << "[Server] Switched to Game state !\n";
|
||||
BroadcastPacket(a_World->GetPacketHeader());
|
||||
BroadcastPacket(a_World->GetPacketData());
|
||||
BroadcastPacket(protocol::packets::BeginGamePacket());
|
||||
BroadcastPacket(m_Simulation.MakePacket());
|
||||
}
|
||||
|
||||
@@ -20,12 +25,23 @@ 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);
|
||||
while (m_Time > stepTimeSecond) {
|
||||
m_Time -= stepTimeSecond;
|
||||
auto lockStepPacket = m_Simulation.Update();
|
||||
BroadcastPacket(lockStepPacket);
|
||||
}
|
||||
}
|
||||
|
||||
void GameState::OnPlayerJoin(PlayerID a_Id, const td::PlayerInfo& a_Info) {
|
||||
SendPacket(a_Id, m_World->GetPacketHeader());
|
||||
SendPacket(a_Id, m_World->GetPacketData());
|
||||
|
||||
// TODO: real teams
|
||||
std::vector<PlayerInfo> team;
|
||||
std::vector<protocol::LockStep> locksteps = m_Simulation.GetFirstLocksteps();
|
||||
|
||||
BroadcastPacket(protocol::packets::BeginGamePacket(team, team, locksteps));
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
|
||||
@@ -1,18 +1,60 @@
|
||||
#include <server/state/LobbyState.h>
|
||||
#include <server/state/GameState.h>
|
||||
#include <server/state/LobbyState.h>
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <td/protocol/packet/PacketSerialize.h>
|
||||
|
||||
#include <sp/common/DataBuffer.h>
|
||||
#include <sp/extensions/Compress.h>
|
||||
#include <sp/io/MessageStream.h>
|
||||
#include <sp/io/StdIo.h>
|
||||
|
||||
namespace td {
|
||||
namespace server {
|
||||
|
||||
void 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) {
|
||||
m_StateMachine.ChangeState<GameState>(m_World);
|
||||
game::WorldPtr GetWorld() {
|
||||
auto comp = std::make_shared<sp::ZlibCompress>();
|
||||
|
||||
std::ifstream fStream("test/tdmap.tdmap2");
|
||||
auto out = std::make_shared<sp::StdInput>(fStream);
|
||||
|
||||
sp::MessageStream<protocol::PacketFactory> stream(std::move(out), std::move(comp));
|
||||
|
||||
auto header = stream.ReadConcreteMessage<protocol::packets::WorldHeaderPacket>();
|
||||
auto data = stream.ReadConcreteMessage<protocol::packets::WorldDataPacket>();
|
||||
|
||||
auto w = std::make_shared<game::World>();
|
||||
|
||||
w->LoadMap(**header);
|
||||
w->LoadMap(**data);
|
||||
|
||||
// Save(*header, *data);
|
||||
|
||||
return w;
|
||||
}
|
||||
|
||||
LobbyState::LobbyState(Server& a_Server) : ServerState(a_Server) {}
|
||||
|
||||
void LobbyState::HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) {}
|
||||
|
||||
void LobbyState::OnPlayerJoin(PlayerID a_Id, const td::PlayerInfo& a_Info) {
|
||||
m_StateMachine.ChangeState<GameState>(GetWorld());
|
||||
}
|
||||
|
||||
void LobbyState::Update(float a_Delta) {}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
|
||||
@@ -1,96 +1,83 @@
|
||||
#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/PlayerListRenderer.h>
|
||||
#include <td/render/renderer/TimerRenderer.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 <server/state/LobbyState.h>
|
||||
|
||||
#include <client/Client.h>
|
||||
#include <client/socket/FakeSocket.h>
|
||||
#include <client/state/GameState.h>
|
||||
#include <client/state/LoggingState.h>
|
||||
|
||||
#include <td/display/Display.h>
|
||||
#include <td/display/state/DebugWorldState.h>
|
||||
|
||||
namespace td {
|
||||
|
||||
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) {
|
||||
// server
|
||||
game::WorldPtr serverWorld = GetWorld();
|
||||
auto serverFakeSocket = std::make_shared<server::FakeSocket>();
|
||||
m_Server = std::make_unique<server::Server>(serverFakeSocket);
|
||||
m_ServerSocket = std::make_shared<server::FakeSocket>();
|
||||
m_Server = std::make_unique<server::Server>(m_ServerSocket);
|
||||
|
||||
// client
|
||||
game::WorldPtr clientWorld = GetWorld();
|
||||
auto clientFakeSocket = client::FakeSocket::Connect(serverFakeSocket);
|
||||
m_Client = std::make_unique<client::Client>(clientFakeSocket, "Player0");
|
||||
auto clientFakeSocket = client::FakeSocket::Connect(m_ServerSocket);
|
||||
m_Client = std::make_unique<client::Client>(clientFakeSocket);
|
||||
|
||||
// TODO: make it better
|
||||
m_Client->OnStateChange.Connect([this](client::Client::State& a_State) {
|
||||
if (auto gameState = dynamic_cast<client::GameState*>(&a_State)) {
|
||||
// render
|
||||
auto clientWorld = gameState->GetWorld();
|
||||
m_Renderer.AddRenderer<render::WorldRenderer>(m_Camera, clientWorld);
|
||||
m_Renderer.AddRenderer<render::EntityRenderer>(m_Camera, clientWorld);
|
||||
m_Renderer.AddRenderer<render::TowerRenderer>(m_Camera, clientWorld);
|
||||
m_Renderer.AddRenderer<render::TimerRenderer>(*gameState);
|
||||
|
||||
auto& list = m_Renderer.AddRenderer<render::PlayerListRenderer>(m_Client->GetPlayers());
|
||||
|
||||
list.OnPlayerCreate.Connect([this]() {
|
||||
auto newSocket = client::FakeSocket::Connect(m_ServerSocket);
|
||||
auto newClient = std::make_unique<client::Client>(newSocket);
|
||||
newClient->ChangeState<client::LoggingState>("Bot");
|
||||
m_FakeClients.push_back(std::move(newClient));
|
||||
});
|
||||
|
||||
list.OnPlayerKick.Connect([this](PlayerID a_Player) {
|
||||
auto it = std::find_if(m_FakeClients.begin(), m_FakeClients.end(), [a_Player](auto& clientPtr) {
|
||||
if (!clientPtr->GetId().has_value())
|
||||
return false;
|
||||
return clientPtr->GetId().value() == a_Player;
|
||||
});
|
||||
m_FakeClients.erase(it);
|
||||
});
|
||||
|
||||
// update state
|
||||
m_ClientState = gameState;
|
||||
}
|
||||
});
|
||||
|
||||
m_Client->ChangeState<client::LoggingState>("Player0");
|
||||
|
||||
// camera
|
||||
m_Camera.SetCamPos({77, 7, 13});
|
||||
m_Camera.UpdatePerspective(m_StateMachine.GetAspectRatio());
|
||||
|
||||
// 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_Server->Update(a_Delta * m_PlaySpeed);
|
||||
m_Client->Update(a_Delta * m_PlaySpeed);
|
||||
if (m_ClientState)
|
||||
m_Renderer.Render(m_ClientState->GetCurrentLerp());
|
||||
}
|
||||
|
||||
@@ -100,10 +87,27 @@ void DebugWorldState::OnAspectRatioChange(float a_Ratio) {
|
||||
|
||||
void DebugWorldState::OnKeyDown(SDL_Keycode a_Key) {
|
||||
// temporary tests
|
||||
if (a_Key == SDLK_A) {
|
||||
constexpr int SECONDS = 10;
|
||||
switch (a_Key) {
|
||||
case SDLK_A:
|
||||
m_Client->SendPacket(td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1));
|
||||
} else if (a_Key == SDLK_Z) {
|
||||
break;
|
||||
|
||||
case SDLK_Z:
|
||||
m_Client->SendPacket(td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13)));
|
||||
break;
|
||||
|
||||
case SDLK_F:
|
||||
m_Server->Update(SECONDS);
|
||||
m_Client->Update(SECONDS);
|
||||
break;
|
||||
|
||||
case SDLK_P:
|
||||
m_PlaySpeed = 1 - m_PlaySpeed;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
#include <cassert>
|
||||
#include <td/Maths.h>
|
||||
#include <td/game/World.h>
|
||||
#include <td/game/WorldTypes.h>
|
||||
|
||||
#include <td/protocol/packet/PacketSerialize.h>
|
||||
#include <td/simulation/WorldTicker.h>
|
||||
|
||||
namespace td {
|
||||
@@ -62,6 +66,16 @@ bool World::LoadMap(const protocol::pdata::WorldHeader& a_WorldHeader) {
|
||||
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) {
|
||||
m_Chunks = a_WorldData.m_Chunks;
|
||||
return true;
|
||||
@@ -78,5 +92,14 @@ void World::ResetSnapshots(std::shared_ptr<sim::WorldSnapshot>& a_Current, std::
|
||||
m_NextState = a_Next;
|
||||
}
|
||||
|
||||
TilePtr World::GetTile(std::int32_t x, std::int32_t y) const {
|
||||
ChunkCoord coords{static_cast<std::int16_t>(x / Chunk::ChunkWidth), static_cast<std::int16_t>(y / Chunk::ChunkHeight)};
|
||||
auto it = m_Chunks.find(coords);
|
||||
assert(it != m_Chunks.end());
|
||||
auto chunk = it->second;
|
||||
Vec2i inchunkCoords{x % Chunk::ChunkWidth, y % Chunk::ChunkHeight};
|
||||
return GetTilePtr(chunk->GetTileIndex(inchunkCoords.y * Chunk::ChunkWidth + inchunkCoords.x));
|
||||
}
|
||||
|
||||
} // namespace game
|
||||
} // namespace td
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include "td/Maths.h"
|
||||
#include <td/render/Camera.h>
|
||||
|
||||
#include <cmath>
|
||||
@@ -24,5 +25,9 @@ void Camera::SetCamPos(const Vec3f& a_NewPos) {
|
||||
OnViewChange();
|
||||
}
|
||||
|
||||
const Vec3f& Camera::GetCamPos() const {
|
||||
return m_CamPos;
|
||||
}
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
|
||||
@@ -8,10 +8,17 @@ namespace render {
|
||||
void BasicRenderer::Render(const GL::VertexArray& a_Vao) {
|
||||
a_Vao.Bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, a_Vao.GetVertexCount());
|
||||
// glDrawElements(GL_TRIANGLES, a_Vao.GetVertexCount(), GL_UNSIGNED_INT, nullptr);
|
||||
a_Vao.Unbind();
|
||||
}
|
||||
|
||||
void BasicRenderer::Render(const Model& a_Model) {
|
||||
for (const auto& vao : a_Model.m_Vaos) {
|
||||
vao->Bind();
|
||||
glDrawElements(GL_TRIANGLES, vao->GetVertexCount(), GL_UNSIGNED_INT, nullptr);
|
||||
vao->Unbind();
|
||||
}
|
||||
}
|
||||
|
||||
RenderPipeline::RenderPipeline() {
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glEnable(GL_BLEND);
|
||||
|
||||
128
src/td/render/loader/FbxLoader.cpp
Normal file
128
src/td/render/loader/FbxLoader.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
#include <td/render/loader/FbxLoader.h>
|
||||
|
||||
#include <assimp/Importer.hpp> // C++ importer interface
|
||||
#include <assimp/postprocess.h> // Post processing flags
|
||||
#include <assimp/scene.h> // Output data structure
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <sp/common/DataBuffer.h>
|
||||
#include <td/render/loader/GLLoader.h>
|
||||
#include <vector>
|
||||
|
||||
#define VERTEX_SIZE 3
|
||||
#define UV_SIZE 2
|
||||
|
||||
#define VERTEX_POSITION_INDEX 0
|
||||
#define VERTEX_UV_INDEX 1
|
||||
#define VERTEX_NORMAL_INDEX 2
|
||||
|
||||
namespace td {
|
||||
namespace ModelLoader {
|
||||
|
||||
static std::unique_ptr<GL::VertexArray> ProcessMesh(aiMesh* mesh, const aiScene* scene, const aiMatrix4x4& transform) {
|
||||
std::vector<float> positions;
|
||||
std::vector<float> textureCoords;
|
||||
std::vector<float> normals;
|
||||
|
||||
std::vector<unsigned int> indicies;
|
||||
|
||||
aiFace* faces = mesh->mFaces;
|
||||
std::size_t faceNumber = mesh->mNumFaces;
|
||||
|
||||
for (std::size_t j = 0; j < faceNumber; j++) {
|
||||
std::size_t offset = indicies.size();
|
||||
std::size_t numIndices = faces[j].mNumIndices;
|
||||
|
||||
indicies.resize(indicies.size() + numIndices);
|
||||
std::memcpy(indicies.data() + offset, faces[j].mIndices, numIndices * sizeof(unsigned int));
|
||||
}
|
||||
|
||||
std::size_t vertNumber = mesh->mNumVertices;
|
||||
aiVector3D* vertecies = mesh->mVertices;
|
||||
|
||||
for (std::size_t j = 0; j < vertNumber; j++) {
|
||||
aiVector3D vertex = transform * vertecies[j];
|
||||
|
||||
positions.push_back(vertex.x);
|
||||
positions.push_back(vertex.y);
|
||||
positions.push_back(vertex.z);
|
||||
}
|
||||
|
||||
if (mesh->HasNormals()) {
|
||||
aiVector3D* vertexNormals = mesh->mNormals;
|
||||
|
||||
for (std::size_t j = 0; j < vertNumber; j++) {
|
||||
aiVector3D normal = transform * vertexNormals[j];
|
||||
|
||||
normals.push_back(normal.x);
|
||||
normals.push_back(normal.y);
|
||||
normals.push_back(normal.z);
|
||||
}
|
||||
}
|
||||
|
||||
aiVector3D** vertexTexture = mesh->mTextureCoords;
|
||||
|
||||
for (std::size_t j = 0; j < vertNumber; j++) {
|
||||
textureCoords.push_back(vertexTexture[0][j].x);
|
||||
textureCoords.push_back(vertexTexture[0][j].y);
|
||||
}
|
||||
|
||||
GL::VertexBuffer positionVBO(positions, VERTEX_SIZE);
|
||||
positionVBO.AddVertexAttribPointer(VERTEX_POSITION_INDEX, VERTEX_SIZE, 0);
|
||||
|
||||
GL::VertexBuffer textureVBO(textureCoords, UV_SIZE);
|
||||
textureVBO.AddVertexAttribPointer(VERTEX_UV_INDEX, UV_SIZE, 0);
|
||||
|
||||
GL::VertexBuffer normalVBO(normals, VERTEX_SIZE);
|
||||
normalVBO.AddVertexAttribPointer(VERTEX_NORMAL_INDEX, VERTEX_SIZE, 0);
|
||||
|
||||
auto Vao = std::make_unique<GL::VertexArray>(GL::ElementBuffer{indicies});
|
||||
Vao->Bind();
|
||||
Vao->BindVertexBuffer(std::move(positionVBO));
|
||||
Vao->BindVertexBuffer(std::move(textureVBO));
|
||||
Vao->BindVertexBuffer(std::move(normalVBO));
|
||||
Vao->Unbind();
|
||||
|
||||
return Vao;
|
||||
}
|
||||
|
||||
static void ProcessNode(
|
||||
aiNode* node, const aiScene* scene, std::vector<std::unique_ptr<GL::VertexArray>>& meshes, const aiMatrix4x4& transform) {
|
||||
// recursive
|
||||
for (unsigned int i = 0; i < node->mNumChildren; i++) {
|
||||
ProcessNode(node->mChildren[i], scene, meshes, transform * node->mTransformation);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < node->mNumMeshes; i++) {
|
||||
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
|
||||
meshes.push_back(ProcessMesh(mesh, scene, transform * node->mTransformation));
|
||||
}
|
||||
}
|
||||
|
||||
Model LoadModel(const std::string& fileName) {
|
||||
// DataBuffer fileData = utils::AssetsManager::GetAsset(fileName);
|
||||
sp::DataBuffer fileData;
|
||||
fileData.ReadFile(fileName);
|
||||
|
||||
Assimp::Importer importer;
|
||||
|
||||
const aiScene* scene = importer.ReadFile(fileName, aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_SortByPType |
|
||||
aiProcess_OptimizeGraph | aiProcess_OptimizeMeshes | aiProcess_GlobalScale | aiProcess_FlipUVs);
|
||||
|
||||
if (!scene) {
|
||||
std::cerr << "[ModelLoader] Failed to load model !\n";
|
||||
return {};
|
||||
}
|
||||
|
||||
// utils::LOGD(utils::Format("[ModelLoader]\tModel nodes : %i", scene->mRootNode->mNumMeshes));
|
||||
|
||||
Model model;
|
||||
ProcessNode(scene->mRootNode, scene, model.m_Vaos, {});
|
||||
|
||||
std::cout << "Loaded " << model.m_Vaos.size() << " vaos !\n";
|
||||
|
||||
return model;
|
||||
}
|
||||
|
||||
} // namespace ModelLoader
|
||||
} // namespace td
|
||||
89
src/td/render/loader/TextureLoader.cpp
Normal file
89
src/td/render/loader/TextureLoader.cpp
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* TextureLoader.cpp
|
||||
*
|
||||
* Created on: 15 nov. 2020
|
||||
* Author: simon
|
||||
*/
|
||||
|
||||
// #include "render/loader/TextureLoader.h"
|
||||
// #include "render/loader/stb_image.h"
|
||||
// #include "render/GL.h"
|
||||
// #include "misc/Log.h"
|
||||
// #include "misc/Format.h"
|
||||
|
||||
#include <td/render/loader/TextureLoader.h>
|
||||
|
||||
#include <stdexcept>
|
||||
#include <td/misc/Log.h>
|
||||
#include <utility>
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include <stb_image.h>
|
||||
|
||||
#include <td/render/OpenGL.h>
|
||||
|
||||
namespace td {
|
||||
namespace TextureLoader {
|
||||
|
||||
GL::Texture LoadTexture(const std::string& fileName) {
|
||||
|
||||
int width, height, comp;
|
||||
|
||||
unsigned char* image = stbi_load(fileName.c_str(), &width, &height, &comp, STBI_rgb_alpha);
|
||||
|
||||
if (image == nullptr) {
|
||||
utils::LOGE("Erreur lors du chargement de la texture !");
|
||||
throw std::runtime_error("Failed to load texture");
|
||||
}
|
||||
|
||||
GL::Texture texture(reinterpret_cast<const char*>(image), width, height, comp);
|
||||
|
||||
stbi_image_free(image);
|
||||
|
||||
// utils::LOGD(utils::format("Texture %s chargée !", fileName.c_str()));
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
} // namespace TextureLoader
|
||||
|
||||
namespace GL {
|
||||
|
||||
Texture::Texture(const char* textureData, int width, int height, int comp) {
|
||||
glGenTextures(1, &m_ID);
|
||||
|
||||
Bind();
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
assert(comp == 3 || comp == 4);
|
||||
|
||||
if (comp == 3)
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, textureData);
|
||||
else
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
|
||||
|
||||
Unbind();
|
||||
}
|
||||
|
||||
Texture::Texture(Texture&& a_Other) {
|
||||
std::swap(m_ID, a_Other.m_ID);
|
||||
}
|
||||
|
||||
Texture::~Texture() {
|
||||
glDeleteTextures(1, &m_ID);
|
||||
}
|
||||
|
||||
void Texture::Bind() const {
|
||||
glBindTexture(GL_TEXTURE_2D, m_ID);
|
||||
}
|
||||
|
||||
void Texture::Unbind() const {
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
|
||||
|
||||
} // namespace GL
|
||||
|
||||
} // namespace td
|
||||
@@ -1,3 +1,6 @@
|
||||
#include "td/render/Renderer.h"
|
||||
#include "td/render/loader/TextureLoader.h"
|
||||
#include <memory>
|
||||
#include <td/render/renderer/EntityRenderer.h>
|
||||
|
||||
#include <td/render/loader/WorldLoader.h>
|
||||
@@ -6,9 +9,10 @@ namespace td {
|
||||
namespace render {
|
||||
|
||||
EntityRenderer::EntityRenderer(Camera& a_Camera, const game::WorldPtr& a_World) : Renderer(a_Camera), m_World(a_World) {
|
||||
m_EntityVao = std::make_unique<GL::VertexArray>(WorldLoader::LoadMobModel());
|
||||
m_EntityModel = ModelLoader::LoadModel("assets/zombie.fbx");
|
||||
m_EntityTexture = std::make_unique<GL::Texture>(TextureLoader::LoadTexture("assets/zombie.png"));
|
||||
m_Shader->Start();
|
||||
m_Shader->SetColorEffect({1, 0, 1});
|
||||
m_Shader->SetColorEffect({1, 1, 1});
|
||||
}
|
||||
|
||||
EntityRenderer::~EntityRenderer() {}
|
||||
@@ -22,8 +26,9 @@ void EntityRenderer::Render(float a_Lerp) {
|
||||
float x = Lerp<game::Mob>(*mob, a_Lerp, [](const game::Mob& a_Mob) { return static_cast<float>(a_Mob.m_Position.x); });
|
||||
float z = Lerp<game::Mob>(*mob, a_Lerp, [](const game::Mob& a_Mob) { return static_cast<float>(a_Mob.m_Position.y); });
|
||||
|
||||
m_Shader->SetModelPos({x, 1, z});
|
||||
Renderer::Render(*m_EntityVao);
|
||||
m_Shader->SetModelPos({x, .001, z});
|
||||
m_EntityTexture->Bind();
|
||||
Renderer::Render(m_EntityModel);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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
|
||||
18
src/td/render/renderer/TimerRenderer.cpp
Normal file
18
src/td/render/renderer/TimerRenderer.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include <client/state/GameState.h>
|
||||
#include <td/render/renderer/TimerRenderer.h>
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
namespace td {
|
||||
namespace render {
|
||||
|
||||
void TimerRenderer::Render(float a_Lerp) {
|
||||
ImGui::Begin("Timer");
|
||||
ImGui::Text("Time : %02d:%02d", static_cast<int>(m_State.GetElapsedTime()) / 60, static_cast<int>(m_State.GetElapsedTime()) % 60);
|
||||
ImGui::End();
|
||||
}
|
||||
|
||||
TimerRenderer::TimerRenderer(const client::GameState& a_State) : m_State(a_State) {}
|
||||
|
||||
} // namespace render
|
||||
} // namespace td
|
||||
@@ -18,7 +18,7 @@ TowerRenderer::~TowerRenderer() {}
|
||||
void TowerRenderer::Render(float a_Lerp) {
|
||||
m_Shader->Start();
|
||||
for (const auto& tower : m_World->GetTowers()) {
|
||||
m_Shader->SetModelPos({tower->GetCenterX(), 1, tower->GetCenterY()});
|
||||
m_Shader->SetModelPos({tower->GetCenterX(), .001, tower->GetCenterY()});
|
||||
Renderer::Render(*m_EntityVao);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <td/Maths.h>
|
||||
#include <td/render/renderer/WorldRenderer.h>
|
||||
|
||||
#include <td/render/loader/WorldLoader.h>
|
||||
@@ -13,7 +14,17 @@ WorldRenderer::WorldRenderer(Camera& a_Camera, const game::WorldPtr& a_World) :
|
||||
|
||||
WorldRenderer::~WorldRenderer() {}
|
||||
|
||||
void WorldRenderer::UpdateControls() {
|
||||
if (ImGui::IsMouseDown(ImGuiMouseButton_Left)) {
|
||||
constexpr float sensitivity = 1.0f;
|
||||
float delta = ImGui::GetIO().DeltaTime;
|
||||
auto mouseDelta = ImGui::GetIO().MouseDelta;
|
||||
m_Camera.SetCamPos(m_Camera.GetCamPos() + Vec3f{-mouseDelta.x * delta * sensitivity, 0, -mouseDelta.y * delta * sensitivity});
|
||||
}
|
||||
}
|
||||
|
||||
void WorldRenderer::Render(float a_Lerp) {
|
||||
UpdateControls();
|
||||
m_Shader->Start();
|
||||
Renderer::Render(*m_WorldVao);
|
||||
}
|
||||
|
||||
@@ -53,12 +53,16 @@ static const char vertexSource[] = R"(
|
||||
#version 330
|
||||
|
||||
layout(location = 0) in vec3 position;
|
||||
layout(location = 1) in vec2 texCoords;
|
||||
|
||||
uniform mat4 viewMatrix;
|
||||
uniform mat4 projectionMatrix;
|
||||
uniform vec3 modelPosition;
|
||||
|
||||
out vec2 pass_textureCoords;
|
||||
|
||||
void main(void){
|
||||
pass_textureCoords = texCoords;
|
||||
gl_Position = projectionMatrix * viewMatrix * vec4(position + modelPosition, 1.0);
|
||||
}
|
||||
)";
|
||||
@@ -66,19 +70,23 @@ void main(void){
|
||||
static const char fragmentSource[] = R"(
|
||||
#version 330
|
||||
|
||||
in vec2 pass_textureCoords;
|
||||
|
||||
out vec4 out_color;
|
||||
|
||||
uniform vec3 ColorEffect;
|
||||
uniform sampler2D textureSampler;
|
||||
|
||||
void main(void){
|
||||
|
||||
vec4 color = vec4(ColorEffect, 1.0);
|
||||
vec4 color = vec4(ColorEffect, 1.0) * texture(textureSampler, pass_textureCoords);
|
||||
|
||||
if (color.a <= 0.1)
|
||||
discard;
|
||||
|
||||
out_color = color;
|
||||
|
||||
|
||||
}
|
||||
)";
|
||||
#endif
|
||||
|
||||
@@ -30,21 +30,29 @@ ClientSimulation::ClientSimulation(std::shared_ptr<game::World> a_World, GameHis
|
||||
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_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) {}
|
||||
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) {
|
||||
// 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);
|
||||
while (m_CurrentTime > stepTimeSecond) {
|
||||
m_CurrentTime -= stepTimeSecond;
|
||||
Step();
|
||||
}
|
||||
return (float)m_CurrentTime / stepTimeSecond;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <td/Types.h>
|
||||
#include <td/simulation/CommandApply.h>
|
||||
|
||||
namespace td {
|
||||
@@ -24,6 +25,10 @@ void CommandApply::Handle(const protocol::commands::PlayerJoinCommand& a_PlayerJ
|
||||
void CommandApply::Handle(const protocol::commands::SpawnTroopCommand& a_SpawnTroop) {
|
||||
auto zombie = std::make_shared<game::Zombie>();
|
||||
zombie->m_Position = a_SpawnTroop->m_Position;
|
||||
|
||||
// TODO: make it spawn dependant
|
||||
zombie->m_Direction = Direction::PositiveY;
|
||||
|
||||
m_Snapshot.m_Mobs.push_back(zombie);
|
||||
}
|
||||
|
||||
|
||||
@@ -47,6 +47,16 @@ protocol::packets::LockStepResponsePacket ServerSimulation::GetResponse(
|
||||
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 td
|
||||
|
||||
@@ -1,11 +1,61 @@
|
||||
#include <td/Maths.h>
|
||||
#include <td/Types.h>
|
||||
#include <td/game/WorldTypes.h>
|
||||
#include <td/simulation/system/EntityMove.h>
|
||||
|
||||
#include <td/game/World.h>
|
||||
|
||||
namespace td {
|
||||
namespace sim {
|
||||
|
||||
static Vec2i GetUnitDirection(Direction a_Direction) {
|
||||
switch (a_Direction) {
|
||||
case Direction::PositiveX:
|
||||
return {1, 0};
|
||||
case Direction::NegativeX:
|
||||
return {-1, 0};
|
||||
case Direction::PositiveY:
|
||||
return {0, 1};
|
||||
case Direction::NegativeY:
|
||||
return {0, -1};
|
||||
}
|
||||
return {0, 0};
|
||||
}
|
||||
|
||||
class DirectionTileVisitor : public game::TileHandler {
|
||||
private:
|
||||
Direction m_Direction;
|
||||
|
||||
public:
|
||||
DirectionTileVisitor() {}
|
||||
|
||||
virtual void Handle(const game::EmptyTile& a_Tile) override {}
|
||||
|
||||
virtual void Handle(const game::TowerTile& a_Tile) override {}
|
||||
|
||||
virtual void Handle(const game::DecorationTile& a_Tile) override {}
|
||||
|
||||
virtual void Handle(const game::WalkableTile& a_Tile) override {
|
||||
m_Direction = a_Tile->m_Direction;
|
||||
}
|
||||
|
||||
const Direction GetDirection() {
|
||||
return m_Direction;
|
||||
}
|
||||
};
|
||||
|
||||
void EntityMove::Tick(const game::World& a_World, WorldSnapshot& a_State, FpFloat a_Delta) {
|
||||
for (auto& mob : a_State.m_Mobs) {
|
||||
mob->m_Position.x += a_Delta;
|
||||
auto tile = a_World.GetTile(static_cast<std::int32_t>(mob->m_Position.x), static_cast<std::int32_t>(mob->m_Position.y));
|
||||
Direction direction = mob->m_Direction;
|
||||
if (tile) {
|
||||
DirectionTileVisitor visitor;
|
||||
tile->Dispatch(visitor);
|
||||
direction = visitor.GetDirection();
|
||||
}
|
||||
auto directVector = GetUnitDirection(direction);
|
||||
mob->m_Position.x += directVector.x * a_Delta;
|
||||
mob->m_Position.y += directVector.y * a_Delta;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
11
xmake.lua
11
xmake.lua
@@ -3,26 +3,29 @@ 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("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", "assimp", "stb")
|
||||
|
||||
set_languages("c++17")
|
||||
set_languages("c++20")
|
||||
|
||||
set_warnings("all")
|
||||
|
||||
if is_mode("release") then
|
||||
set_warnings("all", "error")
|
||||
else
|
||||
set_policy("build.sanitizer.address", true)
|
||||
set_policy("build.sanitizer.leak", true)
|
||||
set_policy("build.sanitizer.undefined", true)
|
||||
end
|
||||
|
||||
target("Tower-Defense2")
|
||||
add_includedirs("include", {public = true})
|
||||
set_kind("binary")
|
||||
add_files("src/**.cpp")
|
||||
add_packages("libsdl3", "imgui", "glew", "splib", "zlib", "fpm", "enet6", {public = true})
|
||||
add_packages("libsdl3", "imgui", "glew", "splib", "zlib", "fpm", "assimp", "enet6", "stb", {public = true})
|
||||
set_rundir(".")
|
||||
add_defines("TD_GL_LOADER_GLEW")
|
||||
|
||||
|
||||
|
||||
-- Tests
|
||||
for _, file in ipairs(os.files("test/**.cpp")) do
|
||||
local name = path.basename(file)
|
||||
|
||||
Reference in New Issue
Block a user