add StateMachine
This commit is contained in:
39
include/td/common/StateMachine.h
Normal file
39
include/td/common/StateMachine.h
Normal file
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace td {
|
||||
|
||||
template <typename TDerived, typename TReturn, typename... TArgs>
|
||||
class StateMachine {
|
||||
public:
|
||||
class State {
|
||||
public:
|
||||
State(TDerived& a_StateMachine) : m_StateMachine(a_StateMachine) {}
|
||||
virtual ~State() {}
|
||||
|
||||
virtual TReturn Update(TArgs... args) = 0;
|
||||
|
||||
protected:
|
||||
TDerived& m_StateMachine;
|
||||
};
|
||||
|
||||
StateMachine() {}
|
||||
virtual ~StateMachine() {}
|
||||
|
||||
TReturn Update(TArgs... args) {
|
||||
assert(m_State);
|
||||
return m_State->Update(args...);
|
||||
}
|
||||
|
||||
template <typename T, typename... Args>
|
||||
void ChangeState(Args&&... args) {
|
||||
m_State = std::make_unique<T>(static_cast<TDerived&>(*this), args...);
|
||||
}
|
||||
|
||||
private:
|
||||
std::unique_ptr<State> m_State;
|
||||
};
|
||||
|
||||
|
||||
} // namespace td
|
||||
@@ -16,8 +16,7 @@ class ClientSimulation : public protocol::PacketHandler {
|
||||
std::uint64_t m_StepTime;
|
||||
game::World& m_World;
|
||||
GameBuffer m_History;
|
||||
std::uint64_t m_CurrentTime;
|
||||
std::uint64_t m_LastTime;
|
||||
float m_CurrentTime;
|
||||
StepTime m_CurrentStep;
|
||||
|
||||
std::shared_ptr<WorldSnapshot> m_LastSnapshot;
|
||||
@@ -45,7 +44,7 @@ class ClientSimulation : public protocol::PacketHandler {
|
||||
/**
|
||||
* \return the progress [0-1] between two steps
|
||||
*/
|
||||
float Update();
|
||||
float Update(float a_Delta);
|
||||
|
||||
virtual void Handle(const protocol::packets::LockStepsPacket& a_LockSteps) override;
|
||||
virtual void Handle(const protocol::packets::LockStepResponsePacket& a_LockSteps) override;
|
||||
|
||||
Reference in New Issue
Block a user