From 42874b90d93e245230a3d911ee19af920dc04296 Mon Sep 17 00:00:00 2001 From: Lynix Date: Sat, 19 Mar 2016 12:21:07 +0100 Subject: [PATCH] SDK: Add StateMachine (FSM) Former-commit-id: 8029f7cce71809ff1f661982d39750c48c86431f --- SDK/include/NDK/State.hpp | 28 +++++++++++++++++++++++ SDK/include/NDK/StateMachine.hpp | 39 ++++++++++++++++++++++++++++++++ SDK/include/NDK/StateMachine.inl | 26 +++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 SDK/include/NDK/State.hpp create mode 100644 SDK/include/NDK/StateMachine.hpp create mode 100644 SDK/include/NDK/StateMachine.inl diff --git a/SDK/include/NDK/State.hpp b/SDK/include/NDK/State.hpp new file mode 100644 index 000000000..0a113def3 --- /dev/null +++ b/SDK/include/NDK/State.hpp @@ -0,0 +1,28 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#pragma once + +#ifndef NDK_STATE_HPP +#define NDK_STATE_HPP + +#include + +namespace Ndk +{ + class StateMachine; + + class State + { + public: + State(); + ~State(); + + virtual void Enter(StateMachine& fsm) = 0; + virtual void Leave(StateMachine& fsm) = 0; + virtual bool Update(StateMachine& fsm, float elapsedTime) = 0; + }; +} + +#endif // NDK_STATE_HPP \ No newline at end of file diff --git a/SDK/include/NDK/StateMachine.hpp b/SDK/include/NDK/StateMachine.hpp new file mode 100644 index 000000000..8d05f8335 --- /dev/null +++ b/SDK/include/NDK/StateMachine.hpp @@ -0,0 +1,39 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#pragma once + +#ifndef NDK_STATEMACHINE_HPP +#define NDK_STATEMACHINE_HPP + +#include +#include +#include + +namespace Ndk +{ + class StateMachine + { + public: + inline StateMachine(); + StateMachine(const StateMachine&) = delete; + inline StateMachine(StateMachine&& fsm) = default; + inline ~StateMachine(); + + inline void ChangeState(std::shared_ptr state); + + inline bool Update(float elapsedTime); + + inline StateMachine& operator=(StateMachine&& fsm) = default; + StateMachine& operator=(const StateMachine&) = delete; + + private: + std::shared_ptr m_currentState; + std::shared_ptr m_nextState; + }; +} + +#include + +#endif // NDK_STATEMACHINE_HPP \ No newline at end of file diff --git a/SDK/include/NDK/StateMachine.inl b/SDK/include/NDK/StateMachine.inl new file mode 100644 index 000000000..0a1e195c4 --- /dev/null +++ b/SDK/include/NDK/StateMachine.inl @@ -0,0 +1,26 @@ +// Copyright (C) 2015 Jérôme Leclercq +// This file is part of the "Nazara Development Kit" +// For conditions of distribution and use, see copyright notice in Prerequesites.hpp + +#include +#include + +namespace Ndk +{ + inline void StateMachine::ChangeState(std::shared_ptr state) + { + m_nextState = std::move(state); + } + + inline bool StateMachine::Update(float elapsedTime) + { + if (m_nextState) + { + m_currentState->Leave(*this); + m_currentState = std::move(m_nextState); + m_currentState->Enter(*this); + } + + return m_currentState->Update(*this, elapsedTime); + } +}