add StateMachine
This commit is contained in:
@@ -1,29 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/IClientSocket.h>
|
||||
#include <client/IClientState.h>
|
||||
#include <chrono>
|
||||
#include <td/common/StateMachine.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class Client {
|
||||
class ClientState;
|
||||
|
||||
class Client : public StateMachine<Client, void, float> {
|
||||
private:
|
||||
std::shared_ptr<IClientSocket> m_Socket;
|
||||
std::shared_ptr<IClientState> m_State;
|
||||
std::chrono::time_point<std::chrono::system_clock> m_LastTime;
|
||||
|
||||
public:
|
||||
Client(const std::shared_ptr<IClientSocket>& a_Socket) : m_Socket(a_Socket), m_LastTime(std::chrono::system_clock::now()) {}
|
||||
Client(const std::shared_ptr<IClientSocket>& a_Socket) : m_Socket(a_Socket) {}
|
||||
|
||||
void Update();
|
||||
|
||||
void UpdateState(const std::shared_ptr<IClientState>& a_State);
|
||||
|
||||
private:
|
||||
void Update(float a_Delta);
|
||||
|
||||
friend class IClientState;
|
||||
friend class ClientState;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
|
||||
20
include/client/ClientState.h
Normal file
20
include/client/ClientState.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/Client.h>
|
||||
#include <td/misc/SlotGuard.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class ClientState : public Client::State, private utils::SlotGuard {
|
||||
public:
|
||||
ClientState(Client& a_Client);
|
||||
virtual ~ClientState() {}
|
||||
|
||||
protected:
|
||||
void SendPacket(const protocol::PacketBase& a_Packet);
|
||||
virtual void HandlePacket(const protocol::PacketBase& a_Packet) {}
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
@@ -1,33 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/IClientSocket.h>
|
||||
#include <td/misc/SlotGuard.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class Client;
|
||||
|
||||
class IClientState : private utils::SlotGuard {
|
||||
public:
|
||||
virtual void HandlePacket(const protocol::PacketBase& a_Packet) = 0;
|
||||
virtual void Update(float a_Delta) = 0;
|
||||
|
||||
IClientState();
|
||||
virtual ~IClientState();
|
||||
|
||||
protected:
|
||||
void SendPacket(const protocol::PacketBase& a_Packet);
|
||||
void SetNewState(const std::shared_ptr<IClientState>& a_NewState);
|
||||
virtual void Init() {}
|
||||
|
||||
private:
|
||||
Client* m_Client;
|
||||
|
||||
void SetClient(Client* a_Client);
|
||||
|
||||
friend class Client;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
} // namespace td
|
||||
@@ -1,25 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <client/IClientState.h>
|
||||
#include <client/ClientState.h>
|
||||
#include <td/game/World.h>
|
||||
#include <td/simulation/ServerSimulation.h>
|
||||
|
||||
namespace td {
|
||||
namespace client {
|
||||
|
||||
class GameState : public IClientState {
|
||||
class GameState : public ClientState {
|
||||
private:
|
||||
std::shared_ptr<game::World> m_World;
|
||||
|
||||
public:
|
||||
GameState(const std::shared_ptr<game::World>& a_World);
|
||||
GameState(Client& a_Client, const std::shared_ptr<game::World>& a_World);
|
||||
~GameState() {}
|
||||
|
||||
virtual void HandlePacket(const protocol::PacketBase& a_Packet) override;
|
||||
virtual void Update(float a_Delta) override;
|
||||
|
||||
protected:
|
||||
virtual void Init() override;
|
||||
virtual void HandlePacket(const protocol::PacketBase& a_Packet) override;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
|
||||
Reference in New Issue
Block a user