add client
This commit is contained in:
24
src/client/Client.cpp
Normal file
24
src/client/Client.cpp
Normal file
@@ -0,0 +1,24 @@
|
||||
#include <client/Client.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
void Client::Update() {
|
||||
auto timeElapsed = std::chrono::system_clock::now() - m_LastTime;
|
||||
float timeSeconds = std::chrono::duration<float, std::chrono::seconds::period>(timeElapsed).count();
|
||||
Update(timeSeconds);
|
||||
m_LastTime = std::chrono::system_clock::now();
|
||||
}
|
||||
|
||||
void Client::UpdateState(const std::shared_ptr<IClientState>& a_State) {
|
||||
m_State = a_State;
|
||||
m_State->SetClient(this);
|
||||
}
|
||||
|
||||
void Client::Update(float a_Delta) {
|
||||
assert(m_State);
|
||||
m_State->Update(a_Delta);
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
27
src/client/IClientState.cpp
Normal file
27
src/client/IClientState.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include <client/IClientState.h>
|
||||
#include <client/Client.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
void IClientState::SetClient(Client* a_Client) {
|
||||
assert(a_Client);
|
||||
m_Client = a_Client;
|
||||
Connect(m_Client->m_Socket->OnReceive, std::bind(&IClientState::HandlePacket, this, std::placeholders::_1));
|
||||
Init();
|
||||
}
|
||||
|
||||
IClientState::IClientState() : m_Client(nullptr) {}
|
||||
|
||||
IClientState::~IClientState() {}
|
||||
|
||||
void IClientState::SendPacket(const protocol::PacketBase& a_Packet) {
|
||||
m_Client->m_Socket->Send(a_Packet);
|
||||
}
|
||||
|
||||
void IClientState::SetNewState(const std::shared_ptr<IClientState>& a_NewState) {
|
||||
m_Client->UpdateState(a_NewState);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
19
src/client/socket/FakeSocket.cpp
Normal file
19
src/client/socket/FakeSocket.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <client/socket/FakeSocket.h>
|
||||
#include <server/socket/FakeSocket.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
void FakeSocket::Send(const protocol::PacketBase& a_Packet) {
|
||||
m_Server->OnReceivePeer(m_PeerId, a_Packet);
|
||||
}
|
||||
|
||||
std::shared_ptr<FakeSocket> FakeSocket::Connect(const std::shared_ptr<server::FakeSocket>& a_Server) {
|
||||
auto socket = std::make_shared<FakeSocket>(Private());
|
||||
socket->m_Server = a_Server;
|
||||
socket->m_PeerId = a_Server->ConnectFakePeer(socket);
|
||||
return socket;
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
12
src/client/state/GameState.cpp
Normal file
12
src/client/state/GameState.cpp
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <client/state/GameState.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
GameState::GameState(const std::shared_ptr<game::World>& a_World) : m_World(a_World) {}
|
||||
void GameState::HandlePacket(const protocol::PacketBase& a_Packet) {}
|
||||
void GameState::Update(float a_Delta) {}
|
||||
void GameState::Init() {}
|
||||
|
||||
} // namespace client
|
||||
} // namespace td
|
||||
47
src/main.cpp
47
src/main.cpp
@@ -19,6 +19,10 @@
|
||||
#include <server/socket/FakeSocket.h>
|
||||
#include <server/state/GameState.h>
|
||||
|
||||
#include <client/Client.h>
|
||||
#include <client/socket/FakeSocket.h>
|
||||
#include <client/state/GameState.h>
|
||||
|
||||
class WorldApply : public td::protocol::PacketHandler {
|
||||
private:
|
||||
td::game::World& m_World;
|
||||
@@ -102,8 +106,12 @@ int main(int argc, char** argv) {
|
||||
td::game::WorldPtr serverWorld = GetWorld();
|
||||
|
||||
// server
|
||||
auto fakeSocket = std::make_shared<td::server::FakeSocket>();
|
||||
td::server::Server server(fakeSocket);
|
||||
auto serverFakeSocket = std::make_shared<td::server::FakeSocket>();
|
||||
td::server::Server server(serverFakeSocket);
|
||||
|
||||
// client
|
||||
auto clientFakeSocket = td::client::FakeSocket::Connect(serverFakeSocket);
|
||||
td::client::Client client(clientFakeSocket);
|
||||
|
||||
// init GL context
|
||||
td::Display display(1920, 1080, "Tower-Defense 2");
|
||||
@@ -124,33 +132,32 @@ int main(int argc, char** argv) {
|
||||
|
||||
td::sim::ClientSimulation simulation(*clientWorld, td::STEP_TIME);
|
||||
ClientHandler clientHandler(simulation);
|
||||
|
||||
simulation.OnMissingLockSteps.Connect([&fakeSocket](const std::vector<td::StepTime>& a_MissingSteps){
|
||||
fakeSocket->ReceiveFromFakePeer(0, td::protocol::packets::LockStepRequestPacket(a_MissingSteps));
|
||||
});
|
||||
|
||||
// temporary tests
|
||||
display.OnKeyDown.Connect([&fakeSocket](SDL_Keycode key) {
|
||||
if (key == SDLK_A) {
|
||||
fakeSocket->ReceiveFromFakePeer(0, td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1));
|
||||
} else if (key == SDLK_Z) {
|
||||
fakeSocket->ReceiveFromFakePeer(0, td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13)));
|
||||
}
|
||||
});
|
||||
|
||||
// make a fake player join
|
||||
fakeSocket->ConnectFakePeer(0);
|
||||
|
||||
|
||||
// packets from the server to the client
|
||||
fakeSocket->OnSendToFakePeer.Connect([&clientHandler](td::PeerID a_Peer, const td::protocol::PacketBase& a_Packet) {
|
||||
clientFakeSocket->OnReceive.Connect([&clientHandler](const td::protocol::PacketBase& a_Packet) {
|
||||
a_Packet.Dispatch(clientHandler);
|
||||
});
|
||||
|
||||
simulation.OnMissingLockSteps.Connect([&clientFakeSocket](const std::vector<td::StepTime>& a_MissingSteps) {
|
||||
clientFakeSocket->Send(td::protocol::packets::LockStepRequestPacket(a_MissingSteps));
|
||||
});
|
||||
|
||||
// temporary tests
|
||||
display.OnKeyDown.Connect([&clientFakeSocket](SDL_Keycode key) {
|
||||
if (key == SDLK_A) {
|
||||
clientFakeSocket->Send(td::protocol::packets::SpawnTroopPacket(td::EntityType::Zombie, 1));
|
||||
} else if (key == SDLK_Z) {
|
||||
clientFakeSocket->Send(td::protocol::packets::PlaceTowerPacket(td::TowerType::Archer, td::TowerCoords(77, 13)));
|
||||
}
|
||||
});
|
||||
|
||||
server.UpdateState(std::make_shared<td::server::GameState>(serverWorld));
|
||||
client.UpdateState(std::make_shared<td::client::GameState>(clientWorld));
|
||||
|
||||
while (!display.IsCloseRequested()) {
|
||||
display.PollEvents();
|
||||
server.Update();
|
||||
client.Update();
|
||||
float lerp = simulation.Update();
|
||||
renderer.Render(lerp);
|
||||
display.Update();
|
||||
|
||||
@@ -10,5 +10,15 @@ void Server::Update() {
|
||||
m_LastTime = std::chrono::system_clock::now();
|
||||
}
|
||||
|
||||
void Server::UpdateState(const std::shared_ptr<IServerState>& a_State) {
|
||||
m_State = a_State;
|
||||
m_State->SetServer(this);
|
||||
}
|
||||
|
||||
void Server::Update(float a_Delta) {
|
||||
assert(m_State);
|
||||
m_State->Update(a_Delta);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
|
||||
@@ -4,20 +4,43 @@ namespace td {
|
||||
namespace server {
|
||||
|
||||
void FakeSocket::SendPeer(PeerID a_Peer, const protocol::PacketBase& a_Packet) {
|
||||
OnSendToFakePeer(a_Peer, a_Packet);
|
||||
auto socket = m_Clients.at(a_Peer);
|
||||
assert(socket.has_value());
|
||||
socket.value()->OnReceive(a_Packet);
|
||||
}
|
||||
|
||||
void FakeSocket::ReceiveFromFakePeer(PeerID a_Peer, const protocol::PacketBase& a_Packet) {
|
||||
OnReceivePeer(a_Peer, a_Packet);
|
||||
}
|
||||
|
||||
void FakeSocket::ConnectFakePeer(PeerID a_Peer) {
|
||||
OnConnectPeer(a_Peer);
|
||||
PeerID FakeSocket::ConnectFakePeer(const std::shared_ptr<client::FakeSocket>& a_Socket) {
|
||||
int peerId = GetNextFreeId();
|
||||
if (peerId == -1) {
|
||||
peerId = m_Clients.size();
|
||||
m_Clients.push_back(a_Socket);
|
||||
} else {
|
||||
m_Clients.emplace(m_Clients.begin() + peerId, a_Socket);
|
||||
}
|
||||
a_Socket->OnConnect();
|
||||
OnConnectPeer(peerId);
|
||||
return peerId;
|
||||
}
|
||||
|
||||
void FakeSocket::DisconnectFakePeer(PeerID a_Peer) {
|
||||
auto socket = m_Clients.at(a_Peer);
|
||||
assert(socket.has_value());
|
||||
socket.value()->OnDisconnect();
|
||||
OnDisconnectPeer(a_Peer);
|
||||
}
|
||||
|
||||
int FakeSocket::GetNextFreeId() {
|
||||
auto it = std::find_if(m_Clients.begin(), m_Clients.end(), [](const auto& a_Value) { return !a_Value.has_value(); });
|
||||
|
||||
if (it == m_Clients.end())
|
||||
return -1;
|
||||
|
||||
return std::distance(m_Clients.begin(), it);
|
||||
}
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
|
||||
Reference in New Issue
Block a user