Compare commits
25 Commits
631e14e66e
...
raylib
| Author | SHA1 | Date | |
|---|---|---|---|
|
3db0afa2e2
|
|||
|
44851099d4
|
|||
| 0d84cc7470 | |||
|
d9baf7c5ef
|
|||
|
701dd6b120
|
|||
|
2225151f72
|
|||
|
aef0cf4d95
|
|||
|
550ff3aeec
|
|||
|
127fa1fcb8
|
|||
|
d1d2b63be8
|
|||
|
62c5c762f9
|
|||
|
1d436aa1c3
|
|||
|
73dd2dabfa
|
|||
|
5b6254c690
|
|||
|
688b6e93ea
|
|||
|
d64c366f4b
|
|||
|
7d58b881b2
|
|||
|
20acbc0499
|
|||
|
4fe2e25029
|
|||
|
fd08833f3f
|
|||
|
165ebf7b2e
|
|||
|
a02cb2b309
|
|||
|
bd56fb0646
|
|||
|
39580c15c5
|
|||
|
ee39c1e429
|
3
.gitattributes
vendored
Normal file
3
.gitattributes
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
*.obj filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.png filter=lfs diff=lfs merge=lfs -text
|
||||||
|
*.glb filter=lfs diff=lfs merge=lfs -text
|
||||||
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"
|
||||||
],
|
],
|
||||||
|
|||||||
BIN
assets/turret.obj
LFS
Normal file
BIN
assets/turret.obj
LFS
Normal file
Binary file not shown.
BIN
assets/turret_diffuse.png
LFS
Normal file
BIN
assets/turret_diffuse.png
LFS
Normal file
Binary file not shown.
BIN
assets/zombie.glb
LFS
Normal file
BIN
assets/zombie.glb
LFS
Normal file
Binary file not shown.
BIN
assets/zombie.png
LFS
Normal file
BIN
assets/zombie.png
LFS
Normal file
Binary file not shown.
@@ -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,13 +9,14 @@ 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;
|
||||||
|
float m_ElapsedTime;
|
||||||
|
|
||||||
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 +24,14 @@ class GameState : public ClientState {
|
|||||||
return m_CurrentLerp;
|
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::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
|
||||||
|
|||||||
@@ -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 PlayerPacketHandlerType = std::unique_ptr<protocol::PacketHandler>(PlayerID);
|
||||||
using PlayerPacketHandler = std::function<PlayerPacketHandlerType>;
|
using PlayerPacketHandler = std::function<PlayerPacketHandlerType>;
|
||||||
|
|
||||||
utils::Signal<PlayerID, const protocol::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);
|
||||||
|
|||||||
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 <server/IServerSocket.h>
|
||||||
#include <td/common/StateMachine.h>
|
#include <td/common/StateMachine.h>
|
||||||
|
#include <server/PlayerManager.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace server {
|
namespace server {
|
||||||
@@ -11,9 +12,17 @@ class ServerState;
|
|||||||
class Server : public StateMachine<Server, void, float> {
|
class Server : public StateMachine<Server, void, float> {
|
||||||
private:
|
private:
|
||||||
std::shared_ptr<IServerSocket> m_Socket;
|
std::shared_ptr<IServerSocket> m_Socket;
|
||||||
|
PlayerManager m_Players;
|
||||||
|
float m_LastMspt;
|
||||||
|
|
||||||
public:
|
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;
|
friend class ServerState;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ class ServerState : public Server::State, private utils::SlotGuard {
|
|||||||
public:
|
public:
|
||||||
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) = 0;
|
virtual void HandlePacket(PlayerID a_Id, const protocol::PacketBase& a_Packet) = 0;
|
||||||
virtual void Update(float a_Delta) = 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) {}
|
virtual void OnPlayerLeave(PlayerID a_Id) {}
|
||||||
|
|
||||||
ServerState(Server& a_Server);
|
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 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;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
static constexpr float PI = 3.141592653f;
|
// static constexpr float PI = 3.141592653f;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
struct Vec2 {
|
struct Vec2 {
|
||||||
@@ -183,6 +183,51 @@ T Lerp(T v0, T v1, T t) {
|
|||||||
|
|
||||||
} // namespace maths
|
} // 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>
|
template <typename T>
|
||||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Vec2<T>& a_Vec) {
|
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const Vec2<T>& a_Vec) {
|
||||||
return a_Buffer << a_Vec.x << a_Vec.y;
|
return a_Buffer << a_Vec.x << a_Vec.y;
|
||||||
|
|||||||
@@ -78,6 +78,11 @@ enum class Direction : std::uint8_t {
|
|||||||
NegativeY = 1 << 3,
|
NegativeY = 1 << 3,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct PlayerInfo {
|
||||||
|
PlayerID m_PlayerId;
|
||||||
|
std::string m_PlayerName;
|
||||||
|
};
|
||||||
|
|
||||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const EntityCoords& a_Coords);
|
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const EntityCoords& a_Coords);
|
||||||
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const FpFloat& a_Float);
|
sp::DataBuffer& operator<<(sp::DataBuffer& a_Buffer, const FpFloat& a_Float);
|
||||||
|
|
||||||
|
|||||||
37
include/td/common/Event.h
Normal file
37
include/td/common/Event.h
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
class EventDispatcher;
|
||||||
|
|
||||||
|
class Event {
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
struct WindowResizeEvent {
|
||||||
|
int m_Width;
|
||||||
|
int m_Height;
|
||||||
|
};
|
||||||
|
|
||||||
|
class EventDispatcher {
|
||||||
|
public:
|
||||||
|
EventDispatcher(const Event& event);
|
||||||
|
|
||||||
|
template<typename E>
|
||||||
|
void Dispatch();
|
||||||
|
};
|
||||||
|
|
||||||
|
// template<typename Event>
|
||||||
|
// class ConcreteEvent : public Event {
|
||||||
|
// public:
|
||||||
|
// void Accept(EventDispatcher& a_Dispatcher) {
|
||||||
|
// a_Dispatcher.Handle(*this);
|
||||||
|
// }
|
||||||
|
// };
|
||||||
|
|
||||||
|
void OnEvent(const Event& event) {
|
||||||
|
EventDispatcher dispatcher(event);
|
||||||
|
dispatcher.Dispatch<WindowResizeEvent>();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -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,6 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include <SDL3/SDL_keycode.h>
|
|
||||||
#include <SDL3/SDL_video.h>
|
|
||||||
#include <td/common/StateMachine.h>
|
#include <td/common/StateMachine.h>
|
||||||
#include <td/misc/Signal.h>
|
#include <td/misc/Signal.h>
|
||||||
|
|
||||||
@@ -11,17 +9,15 @@ namespace td {
|
|||||||
|
|
||||||
class Display : public StateMachine<Display, void, float> {
|
class Display : public StateMachine<Display, void, float> {
|
||||||
private:
|
private:
|
||||||
SDL_Window* m_Window;
|
int m_LastWidth;
|
||||||
SDL_GLContext m_GLContext;
|
int m_LastHeight;
|
||||||
|
|
||||||
int m_LastWidth, m_LastHeight;
|
|
||||||
float m_AspectRatio;
|
float m_AspectRatio;
|
||||||
|
|
||||||
bool m_ShouldClose;
|
bool m_ShouldClose;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
utils::Signal<float> OnAspectRatioChange;
|
utils::Signal<float> OnAspectRatioChange;
|
||||||
utils::Signal<SDL_Keycode> OnKeyDown;
|
utils::Signal<int> OnKeyDown;
|
||||||
|
|
||||||
Display(int a_Width, int a_Height, const std::string& a_Title);
|
Display(int a_Width, int a_Height, const std::string& a_Title);
|
||||||
~Display();
|
~Display();
|
||||||
|
|||||||
@@ -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);
|
||||||
@@ -11,6 +13,7 @@ class DisplayState : public Display::State, private utils::SlotGuard {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void OnAspectRatioChange(float a_Ratio) {}
|
virtual void OnAspectRatioChange(float a_Ratio) {}
|
||||||
virtual void OnKeyDown(SDL_Keycode a_Key) {}
|
virtual void OnKeyDown(int a_Key) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -6,17 +6,23 @@
|
|||||||
#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 {
|
||||||
|
|
||||||
class DebugWorldState : public DisplayState {
|
class DebugWorldState : public DisplayState {
|
||||||
private:
|
private:
|
||||||
render::RenderPipeline m_Renderer;
|
render::RenderPipeline m_Renderer;
|
||||||
render::Camera m_Camera;
|
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;
|
||||||
|
|
||||||
|
unsigned int m_PlaySpeed = 1;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DebugWorldState(Display& a_Display);
|
DebugWorldState(Display& a_Display);
|
||||||
~DebugWorldState();
|
~DebugWorldState();
|
||||||
@@ -25,7 +31,7 @@ class DebugWorldState : public DisplayState {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void OnAspectRatioChange(float a_Ratio) override;
|
virtual void OnAspectRatioChange(float a_Ratio) override;
|
||||||
virtual void OnKeyDown(SDL_Keycode a_Key) override;
|
virtual void OnKeyDown(int a_Key) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ class MainMenuState : public DisplayState, public MainMenuStateStack {
|
|||||||
void RenderBackButton();
|
void RenderBackButton();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
virtual void OnKeyDown(SDL_Keycode a_Key) override;
|
virtual void OnKeyDown(int a_Key) override;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -79,9 +79,10 @@ class Mob : public sp::MessageBase<MobType, MobHandler> {
|
|||||||
Vec2fp m_Position;
|
Vec2fp m_Position;
|
||||||
Direction m_Direction;
|
Direction m_Direction;
|
||||||
std::vector<EffectDuration> m_Effects;
|
std::vector<EffectDuration> m_Effects;
|
||||||
const Tower* m_LastDamage; // the last tower that damaged the mob
|
std::shared_ptr<Tower> m_LastDamage; // the last tower that damaged the mob
|
||||||
float m_HitCooldown;
|
float m_HitCooldown;
|
||||||
TeamCastle* m_CastleTarget;
|
std::shared_ptr<TeamCastle> m_CastleTarget;
|
||||||
|
bool m_HasReachedCastle = false;
|
||||||
// utils::CooldownTimer m_AttackTimer;
|
// utils::CooldownTimer m_AttackTimer;
|
||||||
|
|
||||||
MobPtr m_Next;
|
MobPtr m_Next;
|
||||||
|
|||||||
@@ -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) {
|
||||||
|
|||||||
@@ -25,12 +25,12 @@ enum class TileType : std::uint8_t {
|
|||||||
Ice,*/
|
Ice,*/
|
||||||
};
|
};
|
||||||
|
|
||||||
static constexpr Color BLACK{0, 0, 0};
|
// static constexpr Color BLACK(0, 0, 0);
|
||||||
static constexpr Color WHITE{255, 255, 255};
|
// static constexpr Color WHITE(255, 255, 255);
|
||||||
|
|
||||||
static constexpr Color RED{255, 0, 0};
|
// static constexpr Color RED(255, 0, 0);
|
||||||
static constexpr Color GREEN{0, 255, 0};
|
// static constexpr Color GREEN(0, 255, 0);
|
||||||
static constexpr Color BLUE{0, 0, 255};
|
// static constexpr Color BLUE(0, 0, 255);
|
||||||
|
|
||||||
class TileHandler;
|
class TileHandler;
|
||||||
|
|
||||||
|
|||||||
@@ -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()(std::forward<Args>(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,13 +57,16 @@ 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::shared_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_shared<ConnectionGuard>(*this, a_Callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Disconnect(const CallBack& a_Callback) {
|
void Disconnect(const CallBack& 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())
|
||||||
|
|||||||
@@ -12,15 +12,16 @@ namespace utils {
|
|||||||
*/
|
*/
|
||||||
class SlotGuard {
|
class SlotGuard {
|
||||||
private:
|
private:
|
||||||
std::vector<std::unique_ptr<Connection>> m_Connections;
|
std::vector<std::shared_ptr<Connection>> m_Connections;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/**
|
/**
|
||||||
* \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.emplace_back(a_Signal.ConnectSafe(ptr));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Disconnect() {
|
void Disconnect() {
|
||||||
|
|||||||
22
include/td/misc/Time.h
Normal file
22
include/td/misc/Time.h
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
|
namespace td {
|
||||||
|
|
||||||
|
class Timer {
|
||||||
|
private:
|
||||||
|
std::chrono::time_point<std::chrono::system_clock> m_LastTime;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Timer() : m_LastTime(std::chrono::system_clock::now()) {}
|
||||||
|
|
||||||
|
float GetDelta() {
|
||||||
|
auto timeElapsed = std::chrono::system_clock::now() - m_LastTime;
|
||||||
|
float timeSeconds = std::chrono::duration<float, std::chrono::seconds::period>(timeElapsed).count();
|
||||||
|
m_LastTime = std::chrono::system_clock::now();
|
||||||
|
return timeSeconds;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace td
|
||||||
@@ -16,27 +16,22 @@
|
|||||||
namespace td {
|
namespace td {
|
||||||
namespace protocol {
|
namespace protocol {
|
||||||
|
|
||||||
struct PlayerInfo {
|
|
||||||
PlayerID m_PlayerId;
|
|
||||||
std::string m_PlayerName;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct MapData {
|
struct MapData {
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace pdata {
|
namespace pdata {
|
||||||
|
|
||||||
/** Client attempts to login (very basic) */
|
|
||||||
struct PlayerLogin {
|
|
||||||
std::string m_PlayerName;
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Server indicates success */
|
/** Server indicates success */
|
||||||
struct LoggingSuccess {
|
struct LoggingSuccess {
|
||||||
PlayerID m_PlayerId;
|
PlayerID m_PlayerId;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** Client attempts to login (very basic) */
|
||||||
|
struct PlayerLogin {
|
||||||
|
std::string m_PlayerName;
|
||||||
|
};
|
||||||
|
|
||||||
/** Player joins the lobby */
|
/** Player joins the lobby */
|
||||||
struct PlayerJoin {
|
struct PlayerJoin {
|
||||||
PlayerInfo m_Player;
|
PlayerInfo m_Player;
|
||||||
@@ -47,6 +42,11 @@ struct PlayerLeave {
|
|||||||
PlayerID m_PlayerId;
|
PlayerID m_PlayerId;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/** List of players who joined before */
|
||||||
|
struct PlayerList {
|
||||||
|
std::vector<PlayerInfo> m_Players;
|
||||||
|
};
|
||||||
|
|
||||||
struct PredictCommand {
|
struct PredictCommand {
|
||||||
CommandPtr m_Command;
|
CommandPtr m_Command;
|
||||||
StepTime m_FrameNumber;
|
StepTime m_FrameNumber;
|
||||||
@@ -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;
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ enum class PacketID : std::uint8_t {
|
|||||||
PlaceTower,
|
PlaceTower,
|
||||||
PlayerJoin,
|
PlayerJoin,
|
||||||
PlayerLeave,
|
PlayerLeave,
|
||||||
|
PlayerList,
|
||||||
PlayerLogin,
|
PlayerLogin,
|
||||||
PredictCommand,
|
PredictCommand,
|
||||||
SpawnTroop,
|
SpawnTroop,
|
||||||
@@ -58,6 +59,7 @@ using LoggingSuccessPacket = PacketMessage<pdata::LoggingSuccess, PacketID::Logg
|
|||||||
using PlaceTowerPacket = PacketMessage<pdata::PlaceTower, PacketID::PlaceTower>;
|
using PlaceTowerPacket = PacketMessage<pdata::PlaceTower, PacketID::PlaceTower>;
|
||||||
using PlayerJoinPacket = PacketMessage<pdata::PlayerJoin, PacketID::PlayerJoin>;
|
using PlayerJoinPacket = PacketMessage<pdata::PlayerJoin, PacketID::PlayerJoin>;
|
||||||
using PlayerLeavePacket = PacketMessage<pdata::PlayerLeave, PacketID::PlayerLeave>;
|
using PlayerLeavePacket = PacketMessage<pdata::PlayerLeave, PacketID::PlayerLeave>;
|
||||||
|
using PlayerListPacket = PacketMessage<pdata::PlayerList, PacketID::PlayerList>;
|
||||||
using PlayerLoginPacket = PacketMessage<pdata::PlayerLogin, PacketID::PlayerLogin>;
|
using PlayerLoginPacket = PacketMessage<pdata::PlayerLogin, PacketID::PlayerLogin>;
|
||||||
using PredictCommandPacket = PacketMessage<pdata::PredictCommand, PacketID::PredictCommand>;
|
using PredictCommandPacket = PacketMessage<pdata::PredictCommand, PacketID::PredictCommand>;
|
||||||
using SpawnTroopPacket = PacketMessage<pdata::SpawnTroop, PacketID::SpawnTroop>;
|
using SpawnTroopPacket = PacketMessage<pdata::SpawnTroop, PacketID::SpawnTroop>;
|
||||||
@@ -66,11 +68,11 @@ using WorldDataPacket = PacketMessage<pdata::WorldData, PacketID::WorldData>;
|
|||||||
|
|
||||||
} // namespace packets
|
} // namespace packets
|
||||||
|
|
||||||
using AllPackets =
|
using AllPackets = std::tuple<packets::BeginGamePacket, packets::ChatMessagePacket, packets::DisconnectPacket,
|
||||||
std::tuple<packets::BeginGamePacket, packets::ChatMessagePacket, packets::DisconnectPacket, packets::KeepAlivePacket,
|
packets::KeepAlivePacket, packets::LockStepRequestPacket, packets::LockStepResponsePacket, packets::LockStepsPacket,
|
||||||
packets::LockStepRequestPacket, packets::LockStepResponsePacket, packets::LockStepsPacket, packets::LoggingSuccessPacket,
|
packets::LoggingSuccessPacket, packets::PlaceTowerPacket, packets::PlayerJoinPacket, packets::PlayerLeavePacket,
|
||||||
packets::PlaceTowerPacket, packets::PlayerJoinPacket, packets::PlayerLeavePacket, packets::PlayerLoginPacket,
|
packets::PlayerListPacket, packets::PlayerLoginPacket, packets::PredictCommandPacket, packets::SpawnTroopPacket,
|
||||||
packets::PredictCommandPacket, packets::SpawnTroopPacket, packets::WorldHeaderPacket, packets::WorldDataPacket>;
|
packets::WorldHeaderPacket, packets::WorldDataPacket>;
|
||||||
|
|
||||||
class PacketHandler : public sp::GenericHandler<AllPackets> {
|
class PacketHandler : public sp::GenericHandler<AllPackets> {
|
||||||
public:
|
public:
|
||||||
|
|||||||
@@ -1,41 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <td/Maths.h>
|
|
||||||
#include <td/misc/Signal.h>
|
|
||||||
|
|
||||||
namespace td {
|
|
||||||
namespace render {
|
|
||||||
|
|
||||||
class Camera {
|
|
||||||
private:
|
|
||||||
Mat4f m_ViewMatrix;
|
|
||||||
Mat4f m_ProjectionMatrix;
|
|
||||||
Mat4f m_InvViewMatrix;
|
|
||||||
Mat4f m_InvProjectionMatrix;
|
|
||||||
|
|
||||||
float m_CamDistance = 25.0f;
|
|
||||||
Vec3f m_CamPos{0, m_CamDistance, 0};
|
|
||||||
Vec2f m_CamLook{};
|
|
||||||
|
|
||||||
float m_Yaw = -PI / 2.0f;
|
|
||||||
float m_Pitch = -PI / 2.0f + 0.0000001f;
|
|
||||||
|
|
||||||
public:
|
|
||||||
utils::Signal<> OnPerspectiveChange;
|
|
||||||
utils::Signal<> OnViewChange;
|
|
||||||
|
|
||||||
const Mat4f& GetViewMatrix() const {
|
|
||||||
return m_ViewMatrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
const Mat4f& GetProjectionMatrix() const {
|
|
||||||
return m_ProjectionMatrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
void UpdatePerspective(float a_AspectRatio);
|
|
||||||
|
|
||||||
void SetCamPos(const Vec3f& a_NewPos);
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace render
|
|
||||||
} // namespace td
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#ifdef TD_GL_LOADER_GLEW
|
|
||||||
#include <GL/glew.h>
|
|
||||||
#else
|
|
||||||
#include <glbinding/gl/gl.h>
|
|
||||||
using namespace gl;
|
|
||||||
#endif
|
|
||||||
11
include/td/render/RayGui.h
Normal file
11
include/td/render/RayGui.h
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
// extern "C" {
|
||||||
|
// #include <raygui.h>
|
||||||
|
// }
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
#include <rlImGui.h>
|
||||||
|
}
|
||||||
|
|
||||||
|
#include <imgui.h>
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <td/render/Camera.h>
|
#include <raylib-cpp/Camera3D.hpp>
|
||||||
#include <td/render/loader/GLLoader.h>
|
#include <td/render/loader/GLLoader.h>
|
||||||
#include <td/render/shader/CameraShaderProgram.h>
|
#include <td/render/shader/CameraShaderProgram.h>
|
||||||
#include <td/misc/SlotGuard.h>
|
#include <td/misc/SlotGuard.h>
|
||||||
@@ -21,10 +21,10 @@ template <typename TShader>
|
|||||||
class Renderer : public BasicRenderer, private utils::SlotGuard {
|
class Renderer : public BasicRenderer, private utils::SlotGuard {
|
||||||
protected:
|
protected:
|
||||||
std::unique_ptr<TShader> m_Shader;
|
std::unique_ptr<TShader> m_Shader;
|
||||||
Camera& m_Camera;
|
raylib::Camera& m_Camera;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Renderer(Camera& a_Camera);
|
Renderer(raylib::Camera& a_Camera);
|
||||||
virtual ~Renderer() {}
|
virtual ~Renderer() {}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@@ -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() {
|
||||||
@@ -62,16 +65,16 @@ class RenderPipeline {
|
|||||||
|
|
||||||
|
|
||||||
template <typename TShader>
|
template <typename TShader>
|
||||||
Renderer<TShader>::Renderer(Camera& a_Camera) : m_Shader(std::make_unique<TShader>()), m_Camera(a_Camera) {
|
Renderer<TShader>::Renderer(raylib::Camera& a_Camera) : m_Shader(std::make_unique<TShader>()), m_Camera(a_Camera) {
|
||||||
Connect(a_Camera.OnPerspectiveChange, [this](){
|
// Connect(a_Camera.OnPerspectiveChange, [this](){
|
||||||
m_Shader->Start();
|
// // m_Shader->Start();
|
||||||
m_Shader->SetProjectionMatrix(m_Camera.GetProjectionMatrix());
|
// m_Shader->SetProjectionMatrix(m_Camera.GetProjectionMatrix());
|
||||||
});
|
// });
|
||||||
|
|
||||||
Connect(a_Camera.OnViewChange, [this]() {
|
// Connect(a_Camera.OnViewChange, [this]() {
|
||||||
m_Shader->Start();
|
// // m_Shader->Start();
|
||||||
m_Shader->SetViewMatrix(m_Camera.GetViewMatrix());
|
// m_Shader->SetViewMatrix(m_Camera.GetViewMatrix());
|
||||||
});
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace render
|
} // namespace render
|
||||||
|
|||||||
@@ -1,22 +1,14 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <td/game/World.h>
|
#include <td/game/World.h>
|
||||||
#include <td/render/loader/GLLoader.h>
|
#include <raylib-cpp/Mesh.hpp>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace render {
|
namespace render {
|
||||||
|
|
||||||
namespace WorldLoader {
|
namespace WorldLoader {
|
||||||
|
|
||||||
struct RenderData {
|
Mesh LoadWorldModel(const td::game::World* world);
|
||||||
std::vector<float> positions;
|
|
||||||
std::vector<float> colors;
|
|
||||||
};
|
|
||||||
|
|
||||||
GL::VertexArray LoadMobModel();
|
|
||||||
GL::VertexArray LoadWorldModel(const td::game::World* world);
|
|
||||||
GL::VertexArray LoadTileSelectModel();
|
|
||||||
RenderData LoadTowerModel(const game::TowerPtr& tower);
|
|
||||||
|
|
||||||
} // namespace WorldLoader
|
} // namespace WorldLoader
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#include <td/render/Renderer.h>
|
#include <td/render/Renderer.h>
|
||||||
#include <td/render/shader/EntityShader.h>
|
#include <td/render/shader/EntityShader.h>
|
||||||
#include <td/game/World.h>
|
#include <td/game/World.h>
|
||||||
|
#include <raylib-cpp/Model.hpp>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace render {
|
namespace render {
|
||||||
@@ -10,10 +11,11 @@ namespace render {
|
|||||||
class EntityRenderer : public Renderer<shader::EntityShader> {
|
class EntityRenderer : public Renderer<shader::EntityShader> {
|
||||||
private:
|
private:
|
||||||
game::WorldPtr m_World;
|
game::WorldPtr m_World;
|
||||||
std::unique_ptr<GL::VertexArray> m_EntityVao;
|
raylib::Model m_ZombieModel;
|
||||||
|
Texture2D m_ZombieTexture;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EntityRenderer(Camera& a_Camera, const game::WorldPtr& a_World);
|
EntityRenderer(raylib::Camera& a_Camera, const game::WorldPtr& a_World);
|
||||||
virtual ~EntityRenderer();
|
virtual ~EntityRenderer();
|
||||||
|
|
||||||
virtual void Render(float a_Lerp) override;
|
virtual void Render(float a_Lerp) override;
|
||||||
|
|||||||
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
|
||||||
@@ -3,6 +3,8 @@
|
|||||||
#include <td/render/Renderer.h>
|
#include <td/render/Renderer.h>
|
||||||
#include <td/render/shader/EntityShader.h>
|
#include <td/render/shader/EntityShader.h>
|
||||||
#include <td/game/World.h>
|
#include <td/game/World.h>
|
||||||
|
#include <raylib-cpp/Model.hpp>
|
||||||
|
#include <raylib-cpp/Texture.hpp>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace render {
|
namespace render {
|
||||||
@@ -10,10 +12,11 @@ namespace render {
|
|||||||
class TowerRenderer : public Renderer<shader::EntityShader> {
|
class TowerRenderer : public Renderer<shader::EntityShader> {
|
||||||
private:
|
private:
|
||||||
game::WorldPtr m_World;
|
game::WorldPtr m_World;
|
||||||
std::unique_ptr<GL::VertexArray> m_EntityVao;
|
raylib::Model m_TowerModel;
|
||||||
|
raylib::Texture2D m_TowerTexture;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
TowerRenderer(Camera& a_Camera, const game::WorldPtr& a_World);
|
TowerRenderer(raylib::Camera& a_Camera, const game::WorldPtr& a_World);
|
||||||
virtual ~TowerRenderer();
|
virtual ~TowerRenderer();
|
||||||
|
|
||||||
virtual void Render(float a_Lerp) override;
|
virtual void Render(float a_Lerp) override;
|
||||||
|
|||||||
@@ -4,19 +4,23 @@
|
|||||||
#include <td/render/Renderer.h>
|
#include <td/render/Renderer.h>
|
||||||
#include <td/render/loader/GLLoader.h>
|
#include <td/render/loader/GLLoader.h>
|
||||||
#include <td/render/shader/WorldShader.h>
|
#include <td/render/shader/WorldShader.h>
|
||||||
|
#include <raylib-cpp/Model.hpp>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace render {
|
namespace render {
|
||||||
|
|
||||||
class WorldRenderer : public Renderer<shader::WorldShader> {
|
class WorldRenderer : public Renderer<shader::WorldShader> {
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<GL::VertexArray> m_WorldVao;
|
std::unique_ptr<raylib::Model> m_WorldModel;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
WorldRenderer(Camera& a_Camera, const game::WorldPtr& a_World);
|
WorldRenderer(raylib::Camera& a_Camera, const game::WorldPtr& a_World);
|
||||||
virtual ~WorldRenderer();
|
virtual ~WorldRenderer();
|
||||||
|
|
||||||
virtual void Render(float a_Lerp) override;
|
virtual void Render(float a_Lerp) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void UpdateControls();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace render
|
} // namespace render
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <td/render/shader/ShaderProgram.h>
|
#include <td/Maths.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace shader {
|
namespace shader {
|
||||||
|
|
||||||
class CameraShaderProgram : public ShaderProgram {
|
class CameraShaderProgram {
|
||||||
private:
|
private:
|
||||||
unsigned int m_LocationProjection = 0, m_LocationView = 0;
|
unsigned int m_LocationProjection = 0, m_LocationView = 0;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
CameraShaderProgram() {}
|
CameraShaderProgram() {
|
||||||
|
}
|
||||||
|
|
||||||
void SetProjectionMatrix(const Mat4f& proj) const;
|
void SetProjectionMatrix(const Mat4f& proj) const;
|
||||||
void SetViewMatrix(const Mat4f& view) const;
|
void SetViewMatrix(const Mat4f& view) const;
|
||||||
|
|||||||
@@ -1,43 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <td/Maths.h>
|
|
||||||
#include <td/render/OpenGL.h>
|
|
||||||
|
|
||||||
namespace td {
|
|
||||||
namespace shader {
|
|
||||||
|
|
||||||
class ShaderProgram {
|
|
||||||
public:
|
|
||||||
ShaderProgram();
|
|
||||||
virtual ~ShaderProgram();
|
|
||||||
|
|
||||||
void Start() const;
|
|
||||||
void Stop() const;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
void LoadProgramFile(const std::string& vertexFile, const std::string& fragmentFile);
|
|
||||||
void LoadProgram(const std::string& vertexSource, const std::string& fragmentSource);
|
|
||||||
|
|
||||||
virtual void GetAllUniformLocation() = 0;
|
|
||||||
int GetUniformLocation(const std::string& uniformName) const;
|
|
||||||
|
|
||||||
void LoadFloat(unsigned int location, float value) const;
|
|
||||||
void LoadInt(unsigned int location, int value) const;
|
|
||||||
void LoadVector(unsigned int location, const Vec2f& vector) const;
|
|
||||||
void LoadVector(unsigned int location, const Vec3f& vector) const;
|
|
||||||
void LoadBoolean(unsigned int location, bool value) const;
|
|
||||||
void LoadMat4(unsigned int location, const Mat4f& mat) const;
|
|
||||||
void CleanUp() const;
|
|
||||||
|
|
||||||
private:
|
|
||||||
unsigned int m_ProgramID;
|
|
||||||
unsigned int m_VertexShaderID;
|
|
||||||
unsigned int m_FragmentShaderID;
|
|
||||||
|
|
||||||
unsigned int LoadShaderFromFile(const std::string& file, GLenum type);
|
|
||||||
unsigned int LoadShader(const std::string& source, GLenum type);
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace shader
|
|
||||||
} // namespace td
|
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -1,15 +1,19 @@
|
|||||||
#include <client/state/GameState.h>
|
#include <client/state/GameState.h>
|
||||||
|
|
||||||
|
|
||||||
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,
|
||||||
ClientState(a_Client), m_World(a_World), m_Simulation(a_World, 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) {
|
m_Simulation.OnMissingLockSteps.Connect([this](const std::vector<td::StepTime>& a_MissingSteps) {
|
||||||
SendPacket(protocol::packets::LockStepRequestPacket(a_MissingSteps));
|
SendPacket(protocol::packets::LockStepRequestPacket(a_MissingSteps));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
GameState::~GameState() {}
|
||||||
|
|
||||||
void GameState::Handle(const protocol::packets::LockStepsPacket& a_LockStep) {
|
void GameState::Handle(const protocol::packets::LockStepsPacket& a_LockStep) {
|
||||||
m_Simulation.Handle(a_LockStep);
|
m_Simulation.Handle(a_LockStep);
|
||||||
}
|
}
|
||||||
@@ -20,6 +24,7 @@ void GameState::Handle(const protocol::packets::LockStepResponsePacket& a_LockSt
|
|||||||
|
|
||||||
void GameState::Update(float a_Delta) {
|
void GameState::Update(float a_Delta) {
|
||||||
m_CurrentLerp = m_Simulation.Update(a_Delta);
|
m_CurrentLerp = m_Simulation.Update(a_Delta);
|
||||||
|
m_ElapsedTime += a_Delta;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace client
|
} // 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 <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
|
||||||
|
|||||||
14
src/main.cpp
14
src/main.cpp
@@ -1,13 +1,5 @@
|
|||||||
#include <chrono>
|
|
||||||
#include <td/display/state/MainMenuState.h>
|
#include <td/display/state/MainMenuState.h>
|
||||||
|
#include <td/misc/Time.h>
|
||||||
float GetDelta() {
|
|
||||||
static std::chrono::time_point<std::chrono::system_clock> m_LastTime = std::chrono::system_clock::now();
|
|
||||||
auto timeElapsed = std::chrono::system_clock::now() - m_LastTime;
|
|
||||||
float timeSeconds = std::chrono::duration<float, std::chrono::seconds::period>(timeElapsed).count();
|
|
||||||
m_LastTime = std::chrono::system_clock::now();
|
|
||||||
return timeSeconds;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
// init GL context
|
// init GL context
|
||||||
@@ -15,10 +7,10 @@ int main(int argc, char** argv) {
|
|||||||
|
|
||||||
display.ChangeState<td::MainMenuState>();
|
display.ChangeState<td::MainMenuState>();
|
||||||
|
|
||||||
|
td::Timer timer;
|
||||||
while (!display.IsCloseRequested()) {
|
while (!display.IsCloseRequested()) {
|
||||||
display.PollEvents();
|
display.PollEvents();
|
||||||
float delta = GetDelta();
|
display.Update(timer.GetDelta());
|
||||||
display.Update(delta);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -1,24 +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) {
|
|
||||||
std::cout << "[Server] " << a_Packet->m_PlayerName << " tried to join !\n";
|
|
||||||
protocol::PlayerInfo pInfo{m_Player, a_Packet->m_PlayerName};
|
|
||||||
m_Server.OnPlayerJoin(m_Player, pInfo);
|
|
||||||
m_Server.Broadcast(protocol::packets::PlayerJoinPacket(pInfo));
|
|
||||||
// TODO: send already existing players
|
|
||||||
}
|
|
||||||
|
|
||||||
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/IServerSocket.h>
|
||||||
|
|
||||||
#include <server/ConnectionHandler.h>
|
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace server {
|
namespace server {
|
||||||
|
|
||||||
IServerSocket::IServerSocket() {
|
IServerSocket::IServerSocket() {
|
||||||
RegisterHandler([this](PlayerID a_PlayerId) { return std::make_unique<ConnectionHandler>(*this, a_PlayerId); });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void IServerSocket::RegisterHandler(const PlayerPacketHandler& a_Handler) {
|
void IServerSocket::RegisterHandler(const PlayerPacketHandler& a_Handler) {
|
||||||
@@ -22,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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
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 <server/Server.h>
|
||||||
|
|
||||||
|
#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) {
|
||||||
|
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 server
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -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());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,12 +25,23 @@ void GameState::Update(float a_Delta) {
|
|||||||
// TODO: don't make STEP_TIME constant
|
// TODO: don't make STEP_TIME constant
|
||||||
static const float stepTimeSecond = static_cast<float>(STEP_TIME) / 1000.0f;
|
static const float stepTimeSecond = static_cast<float>(STEP_TIME) / 1000.0f;
|
||||||
m_Time += a_Delta;
|
m_Time += a_Delta;
|
||||||
if (m_Time > stepTimeSecond) {
|
while (m_Time > stepTimeSecond) {
|
||||||
m_Time = std::fmod(m_Time, stepTimeSecond);
|
m_Time -= stepTimeSecond;
|
||||||
auto lockStepPacket = m_Simulation.Update();
|
auto lockStepPacket = m_Simulation.Update();
|
||||||
BroadcastPacket(lockStepPacket);
|
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 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,133 +1,34 @@
|
|||||||
#include <td/display/Display.h>
|
#include <td/display/Display.h>
|
||||||
|
|
||||||
#include <GL/glew.h>
|
#include <td/display/ImGuiTheme.h>
|
||||||
#include <SDL3/SDL.h>
|
|
||||||
#include <imgui.h>
|
|
||||||
#include <imgui_impl_opengl3.h>
|
|
||||||
#include <imgui_impl_sdl3.h>
|
|
||||||
|
|
||||||
#include <td/misc/Format.h>
|
#include <td/misc/Format.h>
|
||||||
#include <td/misc/Log.h>
|
#include <td/misc/Log.h>
|
||||||
#include <td/display/ImGuiTheme.h>
|
|
||||||
|
#include <raylib-cpp/raylib.hpp>
|
||||||
|
#include <td/render/RayGui.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
Display::Display(int a_Width, int a_Height, const std::string& a_Title) :
|
Display::Display(int a_Width, int a_Height, const std::string& a_Title) :
|
||||||
m_LastWidth(0), m_LastHeight(0), m_AspectRatio(1), m_ShouldClose(false) {
|
m_LastWidth(0), m_LastHeight(0), m_AspectRatio(1), m_ShouldClose(false) {
|
||||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
|
||||||
utils::LOGE(utils::Format("Could not initialize SDL! SDL error: %s", SDL_GetError()));
|
|
||||||
}
|
|
||||||
|
|
||||||
m_Window = SDL_CreateWindow(a_Title.c_str(), a_Width, a_Height, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
|
InitWindow(a_Width, a_Height, a_Title.c_str());
|
||||||
|
|
||||||
m_LastWidth = a_Width;
|
m_LastWidth = a_Width;
|
||||||
m_LastHeight = a_Height;
|
m_LastHeight = a_Height;
|
||||||
m_AspectRatio = (float)m_LastWidth / m_LastHeight;
|
m_AspectRatio = (float)m_LastWidth / m_LastHeight;
|
||||||
|
|
||||||
// Prepare and create context
|
|
||||||
#ifdef __ANDROID__
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
|
||||||
#else
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
|
||||||
#endif
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 6);
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 0);
|
|
||||||
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
|
||||||
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
|
|
||||||
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8);
|
|
||||||
|
|
||||||
m_GLContext = SDL_GL_CreateContext(m_Window);
|
|
||||||
|
|
||||||
if (!m_GLContext) {
|
|
||||||
utils::LOGE(utils::Format("Could not create context! SDL error: %s", SDL_GetError()));
|
|
||||||
}
|
|
||||||
|
|
||||||
int major, minor, mask;
|
|
||||||
int r, g, b, a, depth;
|
|
||||||
int mBuffers, mSamples;
|
|
||||||
|
|
||||||
SDL_GL_GetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, &mask);
|
|
||||||
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, &major);
|
|
||||||
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, &minor);
|
|
||||||
|
|
||||||
SDL_GL_GetAttribute(SDL_GL_RED_SIZE, &r);
|
|
||||||
SDL_GL_GetAttribute(SDL_GL_GREEN_SIZE, &g);
|
|
||||||
SDL_GL_GetAttribute(SDL_GL_BLUE_SIZE, &b);
|
|
||||||
SDL_GL_GetAttribute(SDL_GL_ALPHA_SIZE, &a);
|
|
||||||
|
|
||||||
SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &depth);
|
|
||||||
|
|
||||||
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLEBUFFERS, &mBuffers);
|
|
||||||
SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &mSamples);
|
|
||||||
|
|
||||||
const char* mask_desc;
|
|
||||||
|
|
||||||
if (mask & SDL_GL_CONTEXT_PROFILE_CORE) {
|
|
||||||
mask_desc = "core";
|
|
||||||
} else if (mask & SDL_GL_CONTEXT_PROFILE_COMPATIBILITY) {
|
|
||||||
mask_desc = "compatibility";
|
|
||||||
} else if (mask & SDL_GL_CONTEXT_PROFILE_ES) {
|
|
||||||
mask_desc = "es";
|
|
||||||
} else {
|
|
||||||
mask_desc = "?";
|
|
||||||
}
|
|
||||||
|
|
||||||
utils::LOG(utils::Format(
|
|
||||||
"GL Context : %i.%i %s, Color : R:%i G:%i B:%i A:%i, Depth bits : %i", major, minor, mask_desc, r, g, b, a, depth));
|
|
||||||
|
|
||||||
utils::LOG(utils::Format("MultiSamples : Buffers : %i, Samples : %i", mBuffers, mSamples));
|
|
||||||
|
|
||||||
SDL_GL_MakeCurrent(m_Window, m_GLContext);
|
|
||||||
|
|
||||||
GLenum error = glewInit();
|
|
||||||
if (error) {
|
|
||||||
utils::LOGE(utils::Format("Error initializing glew : %s", glewGetErrorString(error)));
|
|
||||||
}
|
|
||||||
|
|
||||||
// WindowResizeEvent(WindowWidth, WindowHeight);
|
// WindowResizeEvent(WindowWidth, WindowHeight);
|
||||||
|
|
||||||
// vsync
|
// vsync
|
||||||
SDL_GL_SetSwapInterval(1);
|
SetTargetFPS(60);
|
||||||
|
|
||||||
// Setup Dear ImGui context
|
rlImGuiSetup(true);
|
||||||
IMGUI_CHECKVERSION();
|
|
||||||
ImGui::CreateContext();
|
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
|
||||||
(void)io;
|
|
||||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
|
|
||||||
io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
|
|
||||||
|
|
||||||
// Setup Dear ImGui style
|
float main_scale = 1.5f;
|
||||||
ImGui::StyleColorsDark();
|
|
||||||
// ImGui::StyleColorsLight();
|
|
||||||
|
|
||||||
// Setup scaling
|
ImGui::GetStyle().FontScaleMain = main_scale;
|
||||||
float main_scale = SDL_GetDisplayContentScale(SDL_GetPrimaryDisplay());
|
|
||||||
// ImGuiStyle& style = ImGui::GetStyle();
|
|
||||||
// style.ScaleAllSizes(main_scale); // Bake a fixed style scale. (until we have a solution for dynamic style scaling, changing
|
|
||||||
// this
|
|
||||||
// // requires resetting Style + calling this again)
|
|
||||||
// style.FontSizeBase = 13 * main_scale; // Set initial font scale. (using io.ConfigDpiScaleFonts=true makes this unnecessary. We
|
|
||||||
// leave
|
|
||||||
// // both here for documentation purpose)
|
|
||||||
|
|
||||||
|
|
||||||
ImFontConfig cfg;
|
|
||||||
cfg.SizePixels = 13 * main_scale * 2;
|
|
||||||
io.Fonts->AddFontDefault(&cfg);
|
|
||||||
|
|
||||||
// Setup Platform/Renderer backends
|
|
||||||
ImGui_ImplSDL3_InitForOpenGL(m_Window, m_GLContext);
|
|
||||||
ImGui_ImplOpenGL3_Init("#version 330");
|
|
||||||
LoadTheme();
|
LoadTheme();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -136,62 +37,66 @@ void Display::Close() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Display::PollEvents() {
|
void Display::PollEvents() {
|
||||||
SDL_Event event;
|
// PollInputEvents();
|
||||||
while (SDL_PollEvent(&event)) {
|
// SDL_Event event;
|
||||||
switch (event.type) {
|
// while (SDL_PollEvent(&event)) {
|
||||||
case SDL_EVENT_QUIT:
|
// switch (event.type) {
|
||||||
case SDL_EVENT_WINDOW_CLOSE_REQUESTED: {
|
// case SDL_EVENT_QUIT:
|
||||||
m_ShouldClose = true;
|
// case SDL_EVENT_WINDOW_CLOSE_REQUESTED: {
|
||||||
break;
|
// m_ShouldClose = true;
|
||||||
}
|
// break;
|
||||||
|
// }
|
||||||
|
|
||||||
case SDL_EVENT_WINDOW_RESIZED: {
|
// case SDL_EVENT_WINDOW_RESIZED: {
|
||||||
m_LastWidth = event.window.data1;
|
// m_LastWidth = event.window.data1;
|
||||||
m_LastHeight = event.window.data2;
|
// m_LastHeight = event.window.data2;
|
||||||
m_AspectRatio = (float)m_LastWidth / m_LastHeight;
|
// m_AspectRatio = (float)m_LastWidth / m_LastHeight;
|
||||||
OnAspectRatioChange(m_AspectRatio);
|
// OnAspectRatioChange(m_AspectRatio);
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
|
|
||||||
case SDL_EVENT_KEY_DOWN: {
|
// case SDL_EVENT_KEY_DOWN: {
|
||||||
if (!event.key.repeat)
|
// if (!event.key.repeat)
|
||||||
OnKeyDown(event.key.key);
|
// OnKeyDown(event.key.key);
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
|
|
||||||
default:
|
// default:
|
||||||
break;
|
// break;
|
||||||
}
|
// }
|
||||||
ImGui_ImplSDL3_ProcessEvent(&event);
|
// ImGui_ImplSDL3_ProcessEvent(&event);
|
||||||
}
|
// }
|
||||||
// Start the Dear ImGui frame
|
// // Start the Dear ImGui frame
|
||||||
ImGui_ImplOpenGL3_NewFrame();
|
// ImGui_ImplOpenGL3_NewFrame();
|
||||||
ImGui_ImplSDL3_NewFrame();
|
// ImGui_ImplSDL3_NewFrame();
|
||||||
ImGui::NewFrame();
|
// ImGui::NewFrame();
|
||||||
|
if (WindowShouldClose())
|
||||||
|
m_ShouldClose = true;
|
||||||
|
BeginDrawing();
|
||||||
|
rlImGuiBegin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Display::Update(float a_Delta) {
|
void Display::Update(float a_Delta) {
|
||||||
StateMachine::Update(a_Delta);
|
StateMachine::Update(a_Delta);
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
ImGui::ShowDemoWindow();
|
ImGui::ShowDemoWindow();
|
||||||
#endif
|
#endif
|
||||||
ImGui::Render();
|
ImGui::Render();
|
||||||
ImGuiIO& io = ImGui::GetIO();
|
rlImGuiEnd();
|
||||||
glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
|
EndDrawing();
|
||||||
// glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
|
ClearBackground(DARKGRAY);
|
||||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
// ImGuiIO& io = ImGui::GetIO();
|
||||||
SDL_GL_SwapWindow(m_Window);
|
// glViewport(0, 0, (int)io.DisplaySize.x, (int)io.DisplaySize.y);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
// // glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
|
||||||
|
// ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||||
|
// SDL_GL_SwapWindow(m_Window);
|
||||||
|
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Display::~Display() {
|
Display::~Display() {
|
||||||
ImGui_ImplOpenGL3_Shutdown();
|
rlImGuiShutdown();
|
||||||
ImGui_ImplSDL3_Shutdown();
|
CloseWindow();
|
||||||
ImGui::DestroyContext();
|
|
||||||
SDL_GL_DestroyContext(m_GLContext);
|
|
||||||
SDL_DestroyWindow(m_Window);
|
|
||||||
SDL_Quit();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
#include <td/display/ImGuiTheme.h>
|
#include <td/display/ImGuiTheme.h>
|
||||||
|
|
||||||
#include <imgui.h>
|
#include <td/render/RayGui.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
void LoadTheme() {
|
void LoadTheme() {
|
||||||
|
|
||||||
static const bool bStyleDark_ = true;
|
static const bool bStyleDark_ = true;
|
||||||
static const float alpha_ = 0.8f;
|
static const float alpha_ = 0.8f;
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include <td/display/menu/CreatePartyMenu.h>
|
#include <td/display/menu/CreatePartyMenu.h>
|
||||||
|
|
||||||
#include <imgui.h>
|
#include <td/render/RayGui.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include <td/display/menu/JoinPartyMenu.h>
|
#include <td/display/menu/JoinPartyMenu.h>
|
||||||
|
|
||||||
#include <imgui.h>
|
#include <td/render/RayGui.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include <td/display/menu/MainMenu.h>
|
#include <td/display/menu/MainMenu.h>
|
||||||
|
|
||||||
#include <imgui.h>
|
#include <td/render/RayGui.h>
|
||||||
#include <td/display/menu/CreatePartyMenu.h>
|
#include <td/display/menu/CreatePartyMenu.h>
|
||||||
#include <td/display/menu/JoinPartyMenu.h>
|
#include <td/display/menu/JoinPartyMenu.h>
|
||||||
#include <td/display/menu/SettingsMenu.h>
|
#include <td/display/menu/SettingsMenu.h>
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include <td/display/menu/SettingsMenu.h>
|
#include <td/display/menu/SettingsMenu.h>
|
||||||
|
|
||||||
#include <imgui.h>
|
#include <td/render/RayGui.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
|
|||||||
@@ -1,110 +1,117 @@
|
|||||||
#include <td/display/state/DebugWorldState.h>
|
#include <td/display/state/DebugWorldState.h>
|
||||||
|
|
||||||
#include <chrono>
|
#include <td/display/Display.h>
|
||||||
#include <fstream>
|
#include <td/display/state/DebugWorldState.h>
|
||||||
#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/PlayerListRenderer.h>
|
||||||
|
#include <td/render/renderer/TimerRenderer.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/state/DebugWorldState.h>
|
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
|
|
||||||
void Save(const protocol::PacketBase& header, const protocol::PacketBase& data) {
|
DebugWorldState::DebugWorldState(Display& a_Display) : DisplayState(a_Display), m_ClientState(nullptr) {
|
||||||
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
|
// 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>(static_cast<raylib::Camera&>(m_Camera), clientWorld);
|
||||||
|
m_Renderer.AddRenderer<render::EntityRenderer>(static_cast<raylib::Camera&>(m_Camera), clientWorld);
|
||||||
|
m_Renderer.AddRenderer<render::TowerRenderer>(static_cast<raylib::Camera&>(m_Camera), clientWorld);
|
||||||
|
m_Renderer.AddRenderer<render::TimerRenderer, client::GameState&>(*gameState);
|
||||||
|
|
||||||
|
auto& list = m_Renderer.AddRenderer<render::PlayerListRenderer, const td::client::PlayerManager&>(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 = Camera{{0}};
|
||||||
m_Camera.UpdatePerspective(m_StateMachine.GetAspectRatio());
|
m_Camera.position = (Vector3){77.0f, 7.0f, 13.0f}; // Camera position
|
||||||
|
m_Camera.target = (Vector3){0.0f, 1.0f, -1.0f}; // Camera looking at point
|
||||||
|
m_Camera.up = (Vector3){0.0f, 1.0f, 0.0f}; // Camera up vector (rotation towards target)
|
||||||
|
m_Camera.fovy = 45.0f; // Camera field-of-view Y
|
||||||
|
m_Camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
|
||||||
|
|
||||||
// states
|
// m_Camera.SetCamPos({77, 7, 13});
|
||||||
m_ClientState = m_Client->ChangeState<client::GameState>(clientWorld, STEP_TIME);
|
// m_Camera.UpdatePerspective(m_StateMachine.GetAspectRatio());
|
||||||
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_PlaySpeed);
|
||||||
m_Client->Update(a_Delta);
|
m_Client->Update(a_Delta * m_PlaySpeed);
|
||||||
// TODO: m_ClientState might be invalid !
|
UpdateCamera(&m_Camera, CAMERA_FREE);
|
||||||
m_Renderer.Render(m_ClientState->GetCurrentLerp());
|
|
||||||
|
if (m_ClientState) {
|
||||||
|
float lerp = m_ClientState->GetCurrentLerp();
|
||||||
|
BeginMode3D(m_Camera);
|
||||||
|
m_Renderer.Render(lerp);
|
||||||
|
EndMode3D();
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr int SECONDS = 10;
|
||||||
|
if (IsKeyPressed(KEY_Q)) {
|
||||||
|
m_Client->SendPacket(td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsKeyPressed(KEY_Z))
|
||||||
|
m_Client->SendPacket(td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13)));
|
||||||
|
|
||||||
|
if (IsKeyPressed(KEY_F)) {
|
||||||
|
m_Server->Update(SECONDS);
|
||||||
|
m_Client->Update(SECONDS);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (IsKeyPressed(KEY_P))
|
||||||
|
m_PlaySpeed = 1 - m_PlaySpeed;
|
||||||
}
|
}
|
||||||
|
|
||||||
void DebugWorldState::OnAspectRatioChange(float a_Ratio) {
|
void DebugWorldState::OnAspectRatioChange(float a_Ratio) {
|
||||||
m_Camera.UpdatePerspective(a_Ratio);
|
// m_Camera.UpdatePerspective(a_Ratio);
|
||||||
}
|
}
|
||||||
|
|
||||||
void DebugWorldState::OnKeyDown(SDL_Keycode a_Key) {
|
void DebugWorldState::OnKeyDown(int a_Key) {
|
||||||
// temporary tests
|
// temporary tests
|
||||||
if (a_Key == SDLK_A) {
|
|
||||||
m_Client->SendPacket(td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1));
|
|
||||||
} else if (a_Key == SDLK_Z) {
|
|
||||||
m_Client->SendPacket(td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
DebugWorldState::~DebugWorldState() {}
|
DebugWorldState::~DebugWorldState() {}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#include <td/display/state/MainMenuState.h>
|
#include <td/display/state/MainMenuState.h>
|
||||||
|
|
||||||
#include <imgui.h>
|
#include <td/render/RayGui.h>
|
||||||
#include <td/display/menu/MainMenu.h>
|
#include <td/display/menu/MainMenu.h>
|
||||||
#include <td/display/state/DebugWorldState.h>
|
#include <td/display/state/DebugWorldState.h>
|
||||||
|
|
||||||
@@ -34,8 +34,8 @@ void MainMenuState::RenderBackButton() {
|
|||||||
PopState();
|
PopState();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MainMenuState::OnKeyDown(SDL_Keycode a_Key) {
|
void MainMenuState::OnKeyDown(int a_Key) {
|
||||||
if (a_Key == SDLK_ESCAPE)
|
if (a_Key == KEY_ESCAPE)
|
||||||
PopState();
|
PopState();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
|
#include <cassert>
|
||||||
|
#include <td/Maths.h>
|
||||||
#include <td/game/World.h>
|
#include <td/game/World.h>
|
||||||
|
#include <td/game/WorldTypes.h>
|
||||||
|
|
||||||
|
#include <td/protocol/packet/PacketSerialize.h>
|
||||||
#include <td/simulation/WorldTicker.h>
|
#include <td/simulation/WorldTicker.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
@@ -14,7 +18,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 +66,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;
|
||||||
@@ -78,5 +92,14 @@ void World::ResetSnapshots(std::shared_ptr<sim::WorldSnapshot>& a_Current, std::
|
|||||||
m_NextState = a_Next;
|
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 game
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -1,28 +0,0 @@
|
|||||||
#include <td/render/Camera.h>
|
|
||||||
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
namespace td {
|
|
||||||
namespace render {
|
|
||||||
|
|
||||||
void Camera::UpdatePerspective(float a_AspectRatio) {
|
|
||||||
m_ProjectionMatrix = maths::Perspective(80.0f / 180.0f * PI, a_AspectRatio, 0.1f, 160.0f);
|
|
||||||
m_InvProjectionMatrix = maths::Inverse(m_ProjectionMatrix);
|
|
||||||
OnPerspectiveChange();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Camera::SetCamPos(const Vec3f& a_NewPos) {
|
|
||||||
Vec3f front = {
|
|
||||||
std::cos(m_Yaw) * std::cos(m_Pitch),
|
|
||||||
std::sin(m_Pitch),
|
|
||||||
std::sin(m_Yaw) * std::cos(m_Pitch)
|
|
||||||
};
|
|
||||||
|
|
||||||
m_CamPos = a_NewPos;
|
|
||||||
m_ViewMatrix = maths::Look(m_CamPos, front, { 0, 1, 0 });
|
|
||||||
m_InvViewMatrix = maths::Transpose(maths::Inverse(m_ViewMatrix)); // why transpose ? I don't know
|
|
||||||
OnViewChange();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace render
|
|
||||||
} // namespace td
|
|
||||||
@@ -1,25 +1,23 @@
|
|||||||
#include <td/render/Renderer.h>
|
#include <td/render/Renderer.h>
|
||||||
|
|
||||||
#include <td/render/OpenGL.h>
|
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace render {
|
namespace render {
|
||||||
|
|
||||||
void BasicRenderer::Render(const GL::VertexArray& a_Vao) {
|
void BasicRenderer::Render(const GL::VertexArray& a_Vao) {
|
||||||
a_Vao.Bind();
|
a_Vao.Bind();
|
||||||
glDrawArrays(GL_TRIANGLES, 0, a_Vao.GetVertexCount());
|
// rlDrawArrays(RL_TRIANGLES, 0, a_Vao.GetVertexCount());
|
||||||
// glDrawElements(GL_TRIANGLES, a_Vao.GetVertexCount(), GL_UNSIGNED_INT, nullptr);
|
// rlDrawElements(RL_TRIANrlES, a_Vao.GetVertexCount(), RL_UNSIGNED_INT, nullptr);
|
||||||
a_Vao.Unbind();
|
a_Vao.Unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
RenderPipeline::RenderPipeline() {
|
RenderPipeline::RenderPipeline() {
|
||||||
glEnable(GL_TEXTURE_2D);
|
// rlEnable(RL_TEXTURE_2D);
|
||||||
glEnable(GL_BLEND);
|
// rlEnable(RL_BLEND);
|
||||||
glEnable(GL_DEPTH_TEST);
|
// rlEnable(RL_DEPTH_TEST);
|
||||||
glEnable(GL_CULL_FACE);
|
// rlEnable(RL_CULL_FACE);
|
||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
// rlBlendFunc(RL_SRC_ALPHA, RL_ONE_MINUS_SRC_ALPHA);
|
||||||
glDepthFunc(GL_LESS);
|
// rlDepthFunc(RL_LESS);
|
||||||
glFrontFace(GL_CCW);
|
// rlFrontFace(RL_CCW);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace render
|
} // namespace render
|
||||||
|
|||||||
@@ -1,28 +1,26 @@
|
|||||||
#include <td/render/loader/GLLoader.h>
|
#include <td/render/loader/GLLoader.h>
|
||||||
|
|
||||||
#include <td/render/OpenGL.h>
|
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace GL {
|
namespace GL {
|
||||||
|
|
||||||
VertexArray::~VertexArray() {
|
VertexArray::~VertexArray() {
|
||||||
if (m_ID != 0)
|
// if (m_ID != 0)
|
||||||
glDeleteVertexArrays(1, &m_ID);
|
// glDeleteVertexArrays(1, &m_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexArray::VertexArray(ElementBuffer&& indicies) : m_ElementBuffer(std::move(indicies)) {
|
VertexArray::VertexArray(ElementBuffer&& indicies) : m_ElementBuffer(std::move(indicies)) {
|
||||||
glGenVertexArrays(1, &m_ID);
|
// glGenVertexArrays(1, &m_ID);
|
||||||
Bind();
|
Bind();
|
||||||
BindElementArrayBuffer();
|
BindElementArrayBuffer();
|
||||||
// Unbind();
|
// Unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VertexArray::Bind() const {
|
void VertexArray::Bind() const {
|
||||||
glBindVertexArray(m_ID);
|
// glBindVertexArray(m_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VertexArray::Unbind() const {
|
void VertexArray::Unbind() const {
|
||||||
glBindVertexArray(0);
|
// glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VertexArray::BindVertexBuffer(VertexBuffer&& VertexBuffer) {
|
void VertexArray::BindVertexBuffer(VertexBuffer&& VertexBuffer) {
|
||||||
@@ -36,24 +34,24 @@ void VertexArray::BindElementArrayBuffer() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
VertexBuffer::~VertexBuffer() {
|
VertexBuffer::~VertexBuffer() {
|
||||||
if (m_ID != 0)
|
// if (m_ID != 0)
|
||||||
glDeleteBuffers(1, &m_ID);
|
// glDeleteBuffers(1, &m_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
VertexBuffer::VertexBuffer(const std::vector<float>& data, unsigned int stride) : m_DataStride(stride) {
|
VertexBuffer::VertexBuffer(const std::vector<float>& data, unsigned int stride) : m_DataStride(stride) {
|
||||||
glGenBuffers(1, &m_ID);
|
// glGenBuffers(1, &m_ID);
|
||||||
Bind();
|
Bind();
|
||||||
glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(data.size() * sizeof(float)), nullptr, GL_STATIC_DRAW);
|
// glBufferData(GL_ARRAY_BUFFER, static_cast<GLsizeiptr>(data.size() * sizeof(float)), nullptr, GL_STATIC_DRAW);
|
||||||
glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast<GLsizeiptr>(data.size() * sizeof(float)), data.data());
|
// glBufferSubData(GL_ARRAY_BUFFER, 0, static_cast<GLsizeiptr>(data.size() * sizeof(float)), data.data());
|
||||||
Unbind();
|
Unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
void VertexBuffer::Bind() const {
|
void VertexBuffer::Bind() const {
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, m_ID);
|
// glBindBuffer(GL_ARRAY_BUFFER, m_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VertexBuffer::Unbind() const {
|
void VertexBuffer::Unbind() const {
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
// glBindBuffer(GL_ARRAY_BUFFER, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void VertexBuffer::AddVertexAttribPointer(unsigned int index, unsigned int coordinateSize, unsigned int offset) {
|
void VertexBuffer::AddVertexAttribPointer(unsigned int index, unsigned int coordinateSize, unsigned int offset) {
|
||||||
@@ -67,32 +65,32 @@ void VertexBuffer::AddVertexAttribPointer(unsigned int index, unsigned int coord
|
|||||||
|
|
||||||
void VertexBuffer::BindVertexAttribs() const {
|
void VertexBuffer::BindVertexAttribs() const {
|
||||||
for (const VertexAttribPointer& pointer : m_VertexAttribs) {
|
for (const VertexAttribPointer& pointer : m_VertexAttribs) {
|
||||||
glVertexAttribPointer(pointer.m_Index, static_cast<GLint>(pointer.m_Size), GL_FLOAT, false, m_DataStride * sizeof(float),
|
// glVertexAttribPointer(pointer.m_Index, static_cast<GLint>(pointer.m_Size), GL_FLOAT, false, m_DataStride * sizeof(float),
|
||||||
reinterpret_cast<GLvoid*>(static_cast<std::size_t>(pointer.m_Offset)));
|
// reinterpret_cast<GLvoid*>(static_cast<std::size_t>(pointer.m_Offset)));
|
||||||
glEnableVertexAttribArray(pointer.m_Index);
|
// glEnableVertexAttribArray(pointer.m_Index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ElementBuffer::ElementBuffer(const std::vector<unsigned int>& indicies) {
|
ElementBuffer::ElementBuffer(const std::vector<unsigned int>& indicies) {
|
||||||
m_TriangleCount = indicies.size();
|
m_TriangleCount = indicies.size();
|
||||||
glGenBuffers(1, &m_ID);
|
// glGenBuffers(1, &m_ID);
|
||||||
Bind();
|
Bind();
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, static_cast<GLsizeiptr>(indicies.size() * sizeof(unsigned int)), nullptr, GL_STATIC_DRAW);
|
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, static_cast<GLsizeiptr>(indicies.size() * sizeof(unsigned int)), nullptr, GL_STATIC_DRAW);
|
||||||
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, static_cast<GLsizeiptr>(indicies.size() * sizeof(unsigned int)), indicies.data());
|
// glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, static_cast<GLsizeiptr>(indicies.size() * sizeof(unsigned int)), indicies.data());
|
||||||
Unbind();
|
Unbind();
|
||||||
}
|
}
|
||||||
|
|
||||||
ElementBuffer::~ElementBuffer() {
|
ElementBuffer::~ElementBuffer() {
|
||||||
if (m_ID != 0)
|
// if (m_ID != 0)
|
||||||
glDeleteBuffers(1, &m_ID);
|
// glDeleteBuffers(1, &m_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ElementBuffer::Bind() const {
|
void ElementBuffer::Bind() const {
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ID);
|
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ElementBuffer::Unbind() const {
|
void ElementBuffer::Unbind() const {
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace GL
|
} // namespace GL
|
||||||
|
|||||||
@@ -1,8 +1,6 @@
|
|||||||
#include <td/render/loader/WorldLoader.h>
|
#include <td/render/loader/WorldLoader.h>
|
||||||
|
|
||||||
#include <iostream>
|
#include <cstring>
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
#include <td/game/World.h>
|
#include <td/game/World.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
@@ -11,11 +9,12 @@ namespace render {
|
|||||||
namespace WorldLoader {
|
namespace WorldLoader {
|
||||||
|
|
||||||
const static int POSITION_VERTEX_SIZE = 3;
|
const static int POSITION_VERTEX_SIZE = 3;
|
||||||
// const static int TEXTURE_VERTEX_SIZE = 2;
|
const static int COLOR_VERTEX_SIZE = 4;
|
||||||
|
|
||||||
GL::VertexArray LoadWorldModel(const td::game::World* world) {
|
Mesh LoadWorldModel(const td::game::World* world) {
|
||||||
|
Mesh mesh = {0};
|
||||||
std::vector<float> positions;
|
std::vector<float> positions;
|
||||||
std::vector<float> colors;
|
std::vector<unsigned char> colors;
|
||||||
|
|
||||||
for (const auto& [coords, chunk] : world->GetChunks()) {
|
for (const auto& [coords, chunk] : world->GetChunks()) {
|
||||||
std::int32_t chunkX = coords.x * td::game::Chunk::ChunkWidth;
|
std::int32_t chunkX = coords.x * td::game::Chunk::ChunkWidth;
|
||||||
@@ -42,15 +41,10 @@ GL::VertexArray LoadWorldModel(const td::game::World* world) {
|
|||||||
const td::Color* tileColor = world->GetTileColor(tile);
|
const td::Color* tileColor = world->GetTileColor(tile);
|
||||||
|
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
int color = 255;
|
colors.push_back(tileColor->r);
|
||||||
color |= tileColor->r << 24;
|
colors.push_back(tileColor->g);
|
||||||
color |= tileColor->g << 16;
|
colors.push_back(tileColor->b);
|
||||||
color |= tileColor->b << 8;
|
colors.push_back(255);
|
||||||
|
|
||||||
int newColorIndex = colors.size();
|
|
||||||
colors.push_back(0);
|
|
||||||
|
|
||||||
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,15 +58,10 @@ GL::VertexArray LoadWorldModel(const td::game::World* world) {
|
|||||||
positions.insert(positions.end(), {fromX, 0, fromY, fromX, 0, toY, toX, 0, fromY, fromX, 0, toY, toX, 0, toY, toX, 0, fromY});
|
positions.insert(positions.end(), {fromX, 0, fromY, fromX, 0, toY, toX, 0, fromY, fromX, 0, toY, toX, 0, toY, toX, 0, fromY});
|
||||||
|
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
int color = 255;
|
colors.push_back(world->GetSpawnColor(TeamColor(spawnColor)).r);
|
||||||
color |= world->GetSpawnColor(TeamColor(spawnColor)).r << 24;
|
colors.push_back(world->GetSpawnColor(TeamColor(spawnColor)).g);
|
||||||
color |= world->GetSpawnColor(TeamColor(spawnColor)).g << 16;
|
colors.push_back(world->GetSpawnColor(TeamColor(spawnColor)).b);
|
||||||
color |= world->GetSpawnColor(TeamColor(spawnColor)).b << 8;
|
colors.push_back(255);
|
||||||
|
|
||||||
int newColorIndex = colors.size();
|
|
||||||
colors.push_back(0);
|
|
||||||
|
|
||||||
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,162 +73,31 @@ GL::VertexArray LoadWorldModel(const td::game::World* world) {
|
|||||||
positions.insert(positions.end(), {fromX, 0, fromY, fromX, 0, toY, toX, 0, fromY, fromX, 0, toY, toX, 0, toY, toX, 0, fromY});
|
positions.insert(positions.end(), {fromX, 0, fromY, fromX, 0, toY, toX, 0, fromY, fromX, 0, toY, toX, 0, toY, toX, 0, fromY});
|
||||||
|
|
||||||
for (int i = 0; i < 6; i++) {
|
for (int i = 0; i < 6; i++) {
|
||||||
int color = 255;
|
colors.push_back(world->GetSpawnColor(TeamColor(castleColor)).r);
|
||||||
color |= world->GetSpawnColor(TeamColor(castleColor)).r << 24;
|
colors.push_back(world->GetSpawnColor(TeamColor(castleColor)).g);
|
||||||
color |= world->GetSpawnColor(TeamColor(castleColor)).g << 16;
|
colors.push_back(world->GetSpawnColor(TeamColor(castleColor)).b);
|
||||||
color |= world->GetSpawnColor(TeamColor(castleColor)).b << 8;
|
colors.push_back(255);
|
||||||
|
|
||||||
int newColorIndex = colors.size();
|
|
||||||
colors.push_back(0);
|
|
||||||
|
|
||||||
memcpy(colors.data() + newColorIndex, &color, 1 * sizeof(int));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GL::VertexBuffer positionVBO(positions, POSITION_VERTEX_SIZE);
|
mesh.vertexCount = positions.size() / 3;
|
||||||
positionVBO.AddVertexAttribPointer(0, POSITION_VERTEX_SIZE, 0);
|
mesh.triangleCount = mesh.vertexCount / 3;
|
||||||
GL::VertexBuffer colorVBO(colors, 1);
|
|
||||||
colorVBO.AddVertexAttribPointer(1, 1, 0);
|
|
||||||
|
|
||||||
std::vector<unsigned int> indexes(positions.size() / 3, 0);
|
const std::size_t verteciesSize = mesh.vertexCount * POSITION_VERTEX_SIZE * sizeof(float);
|
||||||
for (size_t i = 0; i < indexes.size(); i++) {
|
const std::size_t colorsSize = mesh.vertexCount * COLOR_VERTEX_SIZE * sizeof(unsigned char);
|
||||||
indexes[i] = i + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
GL::ElementBuffer indexVBO(indexes);
|
mesh.vertices = (float*)MemAlloc(verteciesSize);
|
||||||
|
mesh.colors = (unsigned char*)MemAlloc(colorsSize);
|
||||||
|
|
||||||
GL::VertexArray worldVao(std::move(indexVBO)); // each pos = 3 vertecies
|
std::memcpy(reinterpret_cast<char*>(mesh.vertices), reinterpret_cast<const char*>(positions.data()), verteciesSize);
|
||||||
worldVao.Bind();
|
std::memcpy(reinterpret_cast<char*>(mesh.colors), reinterpret_cast<const char*>(colors.data()), colorsSize);
|
||||||
worldVao.BindVertexBuffer(std::move(positionVBO));
|
|
||||||
worldVao.BindVertexBuffer(std::move(colorVBO));
|
UploadMesh(&mesh, false);
|
||||||
worldVao.Unbind();
|
|
||||||
return worldVao;
|
return mesh;
|
||||||
}
|
}
|
||||||
|
|
||||||
GL::VertexArray LoadTileSelectModel() {
|
|
||||||
std::vector<float> positions = {
|
|
||||||
-0.5f,
|
|
||||||
-0.5f,
|
|
||||||
-1.0f,
|
|
||||||
|
|
||||||
0.5f,
|
|
||||||
-0.5f,
|
|
||||||
-1.0f,
|
|
||||||
|
|
||||||
0.0f,
|
|
||||||
0.5f,
|
|
||||||
-1.0f,
|
|
||||||
|
|
||||||
1,
|
|
||||||
.01,
|
|
||||||
1,
|
|
||||||
|
|
||||||
0,
|
|
||||||
.01,
|
|
||||||
1,
|
|
||||||
|
|
||||||
0,
|
|
||||||
1,
|
|
||||||
1,
|
|
||||||
};
|
|
||||||
|
|
||||||
int color = 255 << 24 | 255 << 16 | 255 << 8 | 150;
|
|
||||||
float colorFloat;
|
|
||||||
|
|
||||||
memcpy(reinterpret_cast<std::uint8_t*>(&colorFloat), &color, sizeof(float));
|
|
||||||
|
|
||||||
std::vector<float> colors(6, colorFloat);
|
|
||||||
|
|
||||||
GL::VertexBuffer positionVBO(positions, POSITION_VERTEX_SIZE);
|
|
||||||
positionVBO.AddVertexAttribPointer(0, POSITION_VERTEX_SIZE, 0);
|
|
||||||
GL::VertexBuffer colorVBO(colors, 1);
|
|
||||||
colorVBO.AddVertexAttribPointer(1, 1, 0);
|
|
||||||
|
|
||||||
std::vector<unsigned int> indexes(positions.size() / 3, 0);
|
|
||||||
// for (size_t i = 0; i < indexes.size(); i++) {
|
|
||||||
// indexes[i] = i + 1;
|
|
||||||
// }
|
|
||||||
GL::ElementBuffer indexVBO(indexes);
|
|
||||||
|
|
||||||
GL::VertexArray tileSelectVao(std::move(indexVBO));
|
|
||||||
tileSelectVao.Bind();
|
|
||||||
tileSelectVao.BindVertexBuffer(std::move(positionVBO));
|
|
||||||
tileSelectVao.BindVertexBuffer(std::move(colorVBO));
|
|
||||||
tileSelectVao.Unbind();
|
|
||||||
|
|
||||||
return tileSelectVao;
|
|
||||||
}
|
|
||||||
|
|
||||||
RenderData LoadTowerModel(const game::TowerPtr& tower) {
|
|
||||||
RenderData renderData;
|
|
||||||
|
|
||||||
float towerX, towerDX;
|
|
||||||
float towerY, towerDY;
|
|
||||||
|
|
||||||
if (tower->GetSize() == game::TowerSize::Little) {
|
|
||||||
towerX = tower->GetCenterX() - 1.5f;
|
|
||||||
towerDX = tower->GetCenterX() + 1.5f;
|
|
||||||
|
|
||||||
towerY = tower->GetCenterY() - 1.5f;
|
|
||||||
towerDY = tower->GetCenterY() + 1.5f;
|
|
||||||
} else {
|
|
||||||
towerX = tower->GetCenterX() - 2.5f;
|
|
||||||
towerDX = tower->GetCenterX() + 2.5f;
|
|
||||||
|
|
||||||
towerY = tower->GetCenterY() - 2.5f;
|
|
||||||
towerDY = tower->GetCenterY() + 2.5f;
|
|
||||||
}
|
|
||||||
std::vector<float> positions = {towerDX, 0.001, towerY, towerX, 0.001, towerY, towerX, 0.001, towerDY, towerDX, 0.001, towerY,
|
|
||||||
towerX, 0.001, towerDY, towerDX, 0.001, towerDY};
|
|
||||||
|
|
||||||
renderData.positions = positions;
|
|
||||||
|
|
||||||
std::uint8_t towerType = static_cast<std::uint8_t>(tower->GetType());
|
|
||||||
std::uint8_t r = 10 * towerType + 40, g = 5 * towerType + 30, b = 10 * towerType + 20;
|
|
||||||
|
|
||||||
float colorFloat;
|
|
||||||
int color = r << 24 | g << 16 | b << 8 | 255;
|
|
||||||
memcpy(&colorFloat, &color, sizeof(int));
|
|
||||||
|
|
||||||
std::vector<float> colors(6, colorFloat);
|
|
||||||
renderData.colors = colors;
|
|
||||||
|
|
||||||
return renderData;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
GL::VertexArray LoadMobModel() {
|
|
||||||
std::vector<float> positions = {
|
|
||||||
-0.5, 0, -0.5,
|
|
||||||
-0.5, 0, 0.5,
|
|
||||||
0.5, 0, -0.5,
|
|
||||||
|
|
||||||
0.5, 0, -0.5,
|
|
||||||
-0.5, 0, 0.5,
|
|
||||||
0.5, 0, 0.5
|
|
||||||
};
|
|
||||||
|
|
||||||
std::vector<unsigned int> indexes(positions.size() / 3, 0);
|
|
||||||
// for (size_t i = 0; i < indexes.size(); i++) {
|
|
||||||
// indexes[i] = i + 1;
|
|
||||||
// }
|
|
||||||
GL::ElementBuffer indexVBO(indexes);
|
|
||||||
|
|
||||||
GL::VertexBuffer positionVBO(positions, POSITION_VERTEX_SIZE);
|
|
||||||
positionVBO.AddVertexAttribPointer(0, POSITION_VERTEX_SIZE, 0);
|
|
||||||
|
|
||||||
GL::VertexArray mobVao(std::move(indexVBO)); // each pos = 1 color
|
|
||||||
mobVao.Bind();
|
|
||||||
mobVao.BindVertexBuffer(std::move(positionVBO));
|
|
||||||
mobVao.Unbind();
|
|
||||||
return mobVao;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} // namespace WorldLoader
|
} // namespace WorldLoader
|
||||||
|
|
||||||
|
|
||||||
} // namespace render
|
} // namespace render
|
||||||
} // namespace td
|
} // namespace td
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include <cassert>
|
||||||
#include <td/render/renderer/EntityRenderer.h>
|
#include <td/render/renderer/EntityRenderer.h>
|
||||||
|
|
||||||
#include <td/render/loader/WorldLoader.h>
|
#include <td/render/loader/WorldLoader.h>
|
||||||
@@ -5,25 +6,21 @@
|
|||||||
namespace td {
|
namespace td {
|
||||||
namespace render {
|
namespace render {
|
||||||
|
|
||||||
EntityRenderer::EntityRenderer(Camera& a_Camera, const game::WorldPtr& a_World) : Renderer(a_Camera), m_World(a_World) {
|
EntityRenderer::EntityRenderer(raylib::Camera& a_Camera, const game::WorldPtr& a_World) : Renderer(a_Camera), m_World(a_World), m_ZombieModel("assets/zombie.glb") {
|
||||||
m_EntityVao = std::make_unique<GL::VertexArray>(WorldLoader::LoadMobModel());
|
|
||||||
m_Shader->Start();
|
|
||||||
m_Shader->SetColorEffect({1, 0, 1});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
EntityRenderer::~EntityRenderer() {}
|
EntityRenderer::~EntityRenderer() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void EntityRenderer::Render(float a_Lerp) {
|
void EntityRenderer::Render(float a_Lerp) {
|
||||||
m_Shader->Start();
|
|
||||||
for (const auto& mob : m_World->GetMobList()) {
|
for (const auto& mob : m_World->GetMobList()) {
|
||||||
|
|
||||||
float x = Lerp<game::Mob>(*mob, a_Lerp, [](const game::Mob& a_Mob) { return static_cast<float>(a_Mob.m_Position.x); });
|
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); });
|
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});
|
m_ZombieModel.Draw({x, .001, z}, 1.0f, {255, 255, 255, 255});
|
||||||
Renderer::Render(*m_EntityVao);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
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 <td/render/RayGui.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 <td/render/RayGui.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
|
||||||
@@ -5,21 +5,18 @@
|
|||||||
namespace td {
|
namespace td {
|
||||||
namespace render {
|
namespace render {
|
||||||
|
|
||||||
TowerRenderer::TowerRenderer(Camera& a_Camera, const game::WorldPtr& a_World) : Renderer(a_Camera), m_World(a_World) {
|
TowerRenderer::TowerRenderer(raylib::Camera& a_Camera, const game::WorldPtr& a_World) : Renderer(a_Camera), m_World(a_World), m_TowerModel("assets/turret.obj"), m_TowerTexture("assets/turret_diffuse.png") {
|
||||||
m_EntityVao = std::make_unique<GL::VertexArray>(WorldLoader::LoadMobModel());
|
m_TowerModel.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = m_TowerTexture;
|
||||||
m_Shader->Start();
|
|
||||||
m_Shader->SetColorEffect({0, 0, 1});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TowerRenderer::~TowerRenderer() {}
|
TowerRenderer::~TowerRenderer() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void TowerRenderer::Render(float a_Lerp) {
|
void TowerRenderer::Render(float a_Lerp) {
|
||||||
m_Shader->Start();
|
|
||||||
for (const auto& tower : m_World->GetTowers()) {
|
for (const auto& tower : m_World->GetTowers()) {
|
||||||
m_Shader->SetModelPos({tower->GetCenterX(), 1, tower->GetCenterY()});
|
m_TowerModel.Draw({tower->GetCenterX(), .001, tower->GetCenterY()}, 0.3f, {255, 255, 255, 255});
|
||||||
Renderer::Render(*m_EntityVao);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +1,33 @@
|
|||||||
#include <td/render/renderer/WorldRenderer.h>
|
#include <td/render/renderer/WorldRenderer.h>
|
||||||
|
|
||||||
|
#include <td/Maths.h>
|
||||||
#include <td/render/loader/WorldLoader.h>
|
#include <td/render/loader/WorldLoader.h>
|
||||||
|
|
||||||
#include <imgui.h>
|
#include <td/render/RayGui.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace render {
|
namespace render {
|
||||||
|
|
||||||
WorldRenderer::WorldRenderer(Camera& a_Camera, const game::WorldPtr& a_World) : Renderer(a_Camera) {
|
WorldRenderer::WorldRenderer(raylib::Camera& a_Camera, const game::WorldPtr& a_World) : Renderer(a_Camera) {
|
||||||
m_WorldVao = std::make_unique<GL::VertexArray>(WorldLoader::LoadWorldModel(a_World.get()));
|
Mesh mesh = WorldLoader::LoadWorldModel(a_World.get());
|
||||||
|
m_WorldModel = std::make_unique<raylib::Model>(mesh);
|
||||||
}
|
}
|
||||||
|
|
||||||
WorldRenderer::~WorldRenderer() {}
|
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.position.x -= mouseDelta.x * delta * sensitivity;
|
||||||
|
m_Camera.position.z -= mouseDelta.y * delta * sensitivity;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void WorldRenderer::Render(float a_Lerp) {
|
void WorldRenderer::Render(float a_Lerp) {
|
||||||
m_Shader->Start();
|
m_WorldModel->Draw({}, 1.0f, {255, 255, 255, 255});
|
||||||
Renderer::Render(*m_WorldVao);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace render
|
} // namespace render
|
||||||
|
|||||||
@@ -4,16 +4,16 @@ namespace td {
|
|||||||
namespace shader {
|
namespace shader {
|
||||||
|
|
||||||
void CameraShaderProgram::SetProjectionMatrix(const Mat4f& proj) const {
|
void CameraShaderProgram::SetProjectionMatrix(const Mat4f& proj) const {
|
||||||
LoadMat4(m_LocationProjection, proj);
|
// LoadMat4(m_LocationProjection, proj);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CameraShaderProgram::SetViewMatrix(const Mat4f& view) const {
|
void CameraShaderProgram::SetViewMatrix(const Mat4f& view) const {
|
||||||
LoadMat4(m_LocationView, view);
|
// LoadMat4(m_LocationView, view);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CameraShaderProgram::GetAllUniformLocation() {
|
void CameraShaderProgram::GetAllUniformLocation() {
|
||||||
m_LocationProjection = static_cast<unsigned int>(GetUniformLocation("projectionMatrix"));
|
// m_LocationProjection = static_cast<unsigned int>(GetUniformLocation("projectionMatrix"));
|
||||||
m_LocationView = static_cast<unsigned int>(GetUniformLocation("viewMatrix"));
|
// m_LocationView = static_cast<unsigned int>(GetUniformLocation("viewMatrix"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -84,22 +84,22 @@ void main(void){
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
EntityShader::EntityShader() : CameraShaderProgram() {
|
EntityShader::EntityShader() : CameraShaderProgram() {
|
||||||
ShaderProgram::LoadProgram(vertexSource, fragmentSource);
|
// ShaderProgram::LoadProgram(vertexSource, fragmentSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void EntityShader::GetAllUniformLocation() {
|
void EntityShader::GetAllUniformLocation() {
|
||||||
CameraShaderProgram::GetAllUniformLocation();
|
// CameraShaderProgram::GetAllUniformLocation();
|
||||||
m_LocationColorEffect = static_cast<unsigned int>(GetUniformLocation("ColorEffect"));
|
// m_LocationColorEffect = static_cast<unsigned int>(GetUniformLocation("ColorEffect"));
|
||||||
m_LocationPosition = static_cast<unsigned int>(GetUniformLocation("modelPosition"));
|
// m_LocationPosition = static_cast<unsigned int>(GetUniformLocation("modelPosition"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void EntityShader::SetColorEffect(const Vec3f& color) {
|
void EntityShader::SetColorEffect(const Vec3f& color) {
|
||||||
LoadVector(m_LocationColorEffect, color);
|
// LoadVector(m_LocationColorEffect, color);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EntityShader::SetModelPos(const Vec3f& pos) const {
|
void EntityShader::SetModelPos(const Vec3f& pos) const {
|
||||||
LoadVector(m_LocationPosition, pos);
|
// LoadVector(m_LocationPosition, pos);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace shader
|
} // namespace shader
|
||||||
|
|||||||
@@ -1,145 +0,0 @@
|
|||||||
/*
|
|
||||||
* ShaderProgram.cpp
|
|
||||||
*
|
|
||||||
* Created on: 31 janv. 2020
|
|
||||||
* Author: simon
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include <td/render/shader/ShaderProgram.h>
|
|
||||||
#include <td/misc/Log.h>
|
|
||||||
#include <td/misc/Format.h>
|
|
||||||
|
|
||||||
#include <fstream>
|
|
||||||
#include <iostream>
|
|
||||||
#include <sstream>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
namespace td {
|
|
||||||
namespace shader {
|
|
||||||
|
|
||||||
ShaderProgram::ShaderProgram() :
|
|
||||||
m_ProgramID(0), m_VertexShaderID(0), m_FragmentShaderID(0) {
|
|
||||||
}
|
|
||||||
|
|
||||||
ShaderProgram::~ShaderProgram() {
|
|
||||||
CleanUp();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShaderProgram::Start() const {
|
|
||||||
glUseProgram(m_ProgramID);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShaderProgram::Stop() const {
|
|
||||||
glUseProgram(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int ShaderProgram::GetUniformLocation(const std::string& uniformName) const {
|
|
||||||
const int location = glGetUniformLocation(m_ProgramID, uniformName.c_str());
|
|
||||||
if (location == -1) {
|
|
||||||
utils::LOGD(utils::Format("Warning ! Uniform variable %s not found !", uniformName.c_str()));
|
|
||||||
}
|
|
||||||
return location;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShaderProgram::LoadFloat(unsigned int location, float value) const {
|
|
||||||
glUniform1f(static_cast<GLint>(location), value);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShaderProgram::LoadInt(unsigned int location, int value) const {
|
|
||||||
glUniform1i(static_cast<GLint>(location), value);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShaderProgram::LoadVector(unsigned int location,
|
|
||||||
const Vec2f& vector) const {
|
|
||||||
glUniform2f(static_cast<GLint>(location), vector.x, vector.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShaderProgram::LoadVector(unsigned int location,
|
|
||||||
const Vec3f& vector) const {
|
|
||||||
glUniform3f(static_cast<GLint>(location), vector.x, vector.y, vector.z);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShaderProgram::LoadBoolean(unsigned int location, bool value) const {
|
|
||||||
glUniform1i(static_cast<GLint>(location), value);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShaderProgram::LoadMat4(unsigned int location, const Mat4f& mat) const {
|
|
||||||
glUniformMatrix4fv(static_cast<GLint>(location), 1, false, reinterpret_cast<const float*>(&mat));
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShaderProgram::CleanUp() const {
|
|
||||||
Stop();
|
|
||||||
glDetachShader(m_ProgramID, m_VertexShaderID);
|
|
||||||
glDetachShader(m_ProgramID, m_FragmentShaderID);
|
|
||||||
glDeleteShader(m_VertexShaderID);
|
|
||||||
glDeleteShader(m_FragmentShaderID);
|
|
||||||
glDeleteProgram(m_ProgramID);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShaderProgram::LoadProgramFile(const std::string& vertexFile,
|
|
||||||
const std::string& fragmentFile) {
|
|
||||||
m_VertexShaderID = static_cast<unsigned int>(LoadShaderFromFile(vertexFile, GL_VERTEX_SHADER));
|
|
||||||
m_FragmentShaderID = static_cast<unsigned int>(LoadShaderFromFile(fragmentFile, GL_FRAGMENT_SHADER));
|
|
||||||
m_ProgramID = glCreateProgram();
|
|
||||||
glAttachShader(m_ProgramID, m_VertexShaderID);
|
|
||||||
glAttachShader(m_ProgramID, m_FragmentShaderID);
|
|
||||||
glLinkProgram(m_ProgramID);
|
|
||||||
glValidateProgram(m_ProgramID);
|
|
||||||
GetAllUniformLocation();
|
|
||||||
}
|
|
||||||
|
|
||||||
void ShaderProgram::LoadProgram(const std::string& vertexSource,
|
|
||||||
const std::string& fragmentSource) {
|
|
||||||
m_VertexShaderID = static_cast<unsigned int>(LoadShader(vertexSource, GL_VERTEX_SHADER));
|
|
||||||
m_FragmentShaderID = static_cast<unsigned int>(LoadShader(fragmentSource, GL_FRAGMENT_SHADER));
|
|
||||||
m_ProgramID = glCreateProgram();
|
|
||||||
glAttachShader(m_ProgramID, m_VertexShaderID);
|
|
||||||
glAttachShader(m_ProgramID, m_FragmentShaderID);
|
|
||||||
glLinkProgram(m_ProgramID);
|
|
||||||
glValidateProgram(m_ProgramID);
|
|
||||||
GetAllUniformLocation();
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int ShaderProgram::LoadShader(const std::string& source, GLenum type) {
|
|
||||||
unsigned int shaderID = glCreateShader(type);
|
|
||||||
|
|
||||||
const char* c_str = source.c_str();
|
|
||||||
int* null = 0;
|
|
||||||
glShaderSource(shaderID, 1, &c_str, null); // @suppress("Function cannot be resolved")
|
|
||||||
glCompileShader(shaderID);
|
|
||||||
GLint compilesuccessful;
|
|
||||||
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compilesuccessful);
|
|
||||||
if (!compilesuccessful) {
|
|
||||||
GLsizei size;
|
|
||||||
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &size);
|
|
||||||
std::vector<char> shaderError(static_cast<std::size_t>(size));
|
|
||||||
glGetShaderInfoLog(shaderID, size, &size, shaderError.data());
|
|
||||||
|
|
||||||
utils::LOGE("Could not compile shader !");
|
|
||||||
|
|
||||||
utils::LOGE(shaderError.data());
|
|
||||||
|
|
||||||
utils::LOGD(utils::Format("\nShader source : \n"
|
|
||||||
"------------------------------------------------------------------------------------------------------------------------------------\n"
|
|
||||||
"%s\n"
|
|
||||||
"------------------------------------------------------------------------------------------------------------------------------------\n"
|
|
||||||
, source.c_str()));
|
|
||||||
}
|
|
||||||
return shaderID;
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int ShaderProgram::LoadShaderFromFile(const std::string& file, GLenum type) {
|
|
||||||
std::stringstream stream;
|
|
||||||
std::ifstream fileStream(file);
|
|
||||||
|
|
||||||
if (fileStream) {
|
|
||||||
stream << fileStream.rdbuf();
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
return LoadShader(stream.str(), type);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace shader
|
|
||||||
} // namespace td
|
|
||||||
@@ -82,7 +82,7 @@ void main(void){
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
WorldShader::WorldShader() : CameraShaderProgram() {
|
WorldShader::WorldShader() : CameraShaderProgram() {
|
||||||
ShaderProgram::LoadProgram(vertexSource, fragmentSource);
|
// ShaderProgram::LoadProgram(vertexSource, fragmentSource);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace shader
|
} // namespace shader
|
||||||
|
|||||||
@@ -30,21 +30,29 @@ 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)
|
||||||
static const float stepTimeSecond = static_cast<float>(m_StepTime) / 1000.0f;
|
static const float stepTimeSecond = static_cast<float>(m_StepTime) / 1000.0f;
|
||||||
m_CurrentTime += a_Delta;
|
m_CurrentTime += a_Delta;
|
||||||
if (m_CurrentTime > stepTimeSecond) {
|
while (m_CurrentTime > stepTimeSecond) {
|
||||||
m_CurrentTime = std::fmod(m_CurrentTime, stepTimeSecond);
|
m_CurrentTime -= stepTimeSecond;
|
||||||
Step();
|
Step();
|
||||||
}
|
}
|
||||||
return (float)m_CurrentTime / stepTimeSecond;
|
return (float)m_CurrentTime / stepTimeSecond;
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#include <td/Types.h>
|
||||||
#include <td/simulation/CommandApply.h>
|
#include <td/simulation/CommandApply.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
@@ -6,7 +7,7 @@ namespace sim {
|
|||||||
CommandApply::CommandApply(const game::World& a_World, WorldSnapshot& a_Snapshot) : m_World(a_World), m_Snapshot(a_Snapshot) {}
|
CommandApply::CommandApply(const game::World& a_World, WorldSnapshot& a_Snapshot) : m_World(a_World), m_Snapshot(a_Snapshot) {}
|
||||||
|
|
||||||
void CommandApply::Handle(const protocol::commands::EndCommand& a_End) {
|
void CommandApply::Handle(const protocol::commands::EndCommand& a_End) {
|
||||||
(void) m_World;
|
(void)m_World;
|
||||||
}
|
}
|
||||||
|
|
||||||
void CommandApply::Handle(const protocol::commands::PlaceTowerCommand& a_PlaceTower) {
|
void CommandApply::Handle(const protocol::commands::PlaceTowerCommand& a_PlaceTower) {
|
||||||
@@ -24,6 +25,10 @@ void CommandApply::Handle(const protocol::commands::PlayerJoinCommand& a_PlayerJ
|
|||||||
void CommandApply::Handle(const protocol::commands::SpawnTroopCommand& a_SpawnTroop) {
|
void CommandApply::Handle(const protocol::commands::SpawnTroopCommand& a_SpawnTroop) {
|
||||||
auto zombie = std::make_shared<game::Zombie>();
|
auto zombie = std::make_shared<game::Zombie>();
|
||||||
zombie->m_Position = a_SpawnTroop->m_Position;
|
zombie->m_Position = a_SpawnTroop->m_Position;
|
||||||
|
|
||||||
|
// TODO: make it spawn dependant
|
||||||
|
zombie->m_Direction = Direction::PositiveY;
|
||||||
|
|
||||||
m_Snapshot.m_Mobs.push_back(zombie);
|
m_Snapshot.m_Mobs.push_back(zombie);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -1,11 +1,66 @@
|
|||||||
|
#include <td/Maths.h>
|
||||||
|
#include <td/Types.h>
|
||||||
|
#include <td/game/WorldTypes.h>
|
||||||
#include <td/simulation/system/EntityMove.h>
|
#include <td/simulation/system/EntityMove.h>
|
||||||
|
|
||||||
|
#include <td/game/World.h>
|
||||||
|
|
||||||
namespace td {
|
namespace td {
|
||||||
namespace sim {
|
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) {
|
void EntityMove::Tick(const game::World& a_World, WorldSnapshot& a_State, FpFloat a_Delta) {
|
||||||
for (auto& mob : a_State.m_Mobs) {
|
for (auto& mob : a_State.m_Mobs) {
|
||||||
mob->m_Position.x += a_Delta;
|
if (mob->m_HasReachedCastle)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
13
xmake.lua
13
xmake.lua
@@ -2,25 +2,26 @@ 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("rlimgui", "raylib-cpp", "splib 2.3.2", "zlib", "fpm", "enet6")
|
||||||
add_requires("libsdl3 3.2.16", "splib 2.3.1", "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
|
||||||
|
|
||||||
target("Tower-Defense2")
|
target("Tower-Defense2")
|
||||||
add_includedirs("include", {public = true})
|
add_includedirs("include", {public = true})
|
||||||
set_kind("binary")
|
set_kind("binary")
|
||||||
add_files("src/**.cpp")
|
add_files("src/**.cpp")
|
||||||
add_packages("libsdl3", "imgui", "glew", "splib", "zlib", "fpm", "enet6", {public = true})
|
add_packages("rlimgui", "raylib-cpp", "splib", "zlib", "fpm", "enet6", {public = true})
|
||||||
set_rundir(".")
|
set_rundir(".")
|
||||||
add_defines("TD_GL_LOADER_GLEW")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- Tests
|
-- Tests
|
||||||
|
|||||||
Reference in New Issue
Block a user