From 1640a4fb4bb0c831dd3f2a28bf99a49f3f41e829 Mon Sep 17 00:00:00 2001 From: ThomasAvans Date: Mon, 22 Apr 2024 10:40:59 +0200 Subject: Processed review --- shared/FSM.cpp | 3 ++ shared/FSM.h | 50 ++++++++++++++++++++++++++++++++ shared/FSM.hpp | 68 ++++++++++++++++++++++++++++++++++++++++++++ shared/IBehaviouralState.hpp | 25 ++++++++++++++++ 4 files changed, 146 insertions(+) create mode 100644 shared/FSM.cpp create mode 100644 shared/FSM.h create mode 100644 shared/FSM.hpp create mode 100644 shared/IBehaviouralState.hpp (limited to 'shared') diff --git a/shared/FSM.cpp b/shared/FSM.cpp new file mode 100644 index 0000000..fad7fb3 --- /dev/null +++ b/shared/FSM.cpp @@ -0,0 +1,3 @@ +#include "FSM.h" + +#include "IBehaviouralState.hpp" \ No newline at end of file diff --git a/shared/FSM.h b/shared/FSM.h new file mode 100644 index 0000000..792a44f --- /dev/null +++ b/shared/FSM.h @@ -0,0 +1,50 @@ +#ifndef _FSM_H_ +#define _FSM_H_ + +#include +#include + +#include "IBehaviouralState.hpp" + +/// +/// +/// +/// IBehaviouralState is the only accepted +/// class. +template class FSM { +public: + template FSM(TPState &...args) { + int i = 0; + + ((void)_states.emplace(i++, args), ...); + } + + /// + /// Implement with FSM::act() + /// + void act(); + + /// + /// Used to check current state. + /// + /// Current state. + std::shared_ptr &get_state() { return _currentState; } + + /// + /// Used to get all states. + /// + /// Current states. + std::map> get_states(); + + /// + /// Sets current state, calls appropiate functions. + /// + /// State to transition into. + void set_state(std::shared_ptr); + +private: + std::map> _states; + std::shared_ptr _currentState; +}; + +#endif // _FSM_H_ \ No newline at end of file diff --git a/shared/FSM.hpp b/shared/FSM.hpp new file mode 100644 index 0000000..c86ed80 --- /dev/null +++ b/shared/FSM.hpp @@ -0,0 +1,68 @@ +#ifndef _FSM_HPP_ +#define _FSM_HPP_ + +#include "FSM.h" + +#include + +/// +/// This is a generic update method, unknown classes are not supported. +/// +/// IBehaviouralState is the only accepted +/// class. +template void FSM::act() { + // No implementation for unknown class. + std::cout << "No implementation for unknown class" << std::endl; +} +// +///// +///// Calls the IBehaviouralState act method. +///// +///// IBehaviouralState is the only accepted +/// class. +template <> inline void FSM::act() { _currentState->act(); } + +/// +/// This is a generic return of the type. +/// +/// Any class type works with this return. +/// The current IBehaviouralState map. +template +std::map> FSM::get_states() { + return _states; +} + +///// +///// State transitioning from current state to newState. +///// Calls Exit on the current state and Enter on the new state. +///// +///// IBehaviouralState is the only accepted +/// class. +///// New state to transition into. +template <> +inline void +FSM::set_state(std::shared_ptr newState) { + // We can guarantee all statemachines are based on IBehaviouralState + if (_currentState != nullptr) + _currentState->exit(); + + _currentState = nullptr; + _currentState = newState; + _currentState->enter(); +} + +/// +/// State transitioning from current state to newState. +/// Calls Exit on the current state and Enter on the new state. +/// +/// IBehaviouralState is the only accepted +/// class. New state to transition +/// into. +template +void FSM::set_state(std::shared_ptr newState) { + // We can guarantee all statemachines are based on IBehaviouralState + // No implementation for an unknown state change class. + std::cout << "No implementation for unknown state change class" << std::endl; +} + +#endif // _FSM_HPP_ \ No newline at end of file diff --git a/shared/IBehaviouralState.hpp b/shared/IBehaviouralState.hpp new file mode 100644 index 0000000..c41fef3 --- /dev/null +++ b/shared/IBehaviouralState.hpp @@ -0,0 +1,25 @@ +#ifndef _FSM_IBEHAVIOURALSTATE_HPP_ +#define _FSM_IBEHAVIOURALSTATE_HPP_ + +/// +/// Sub class used to define methods implemented by behavioural specific states. +/// +class IBehaviouralState { +public: + /// + /// Enters the current state. Used for setup. + /// + virtual void enter() = 0; + + /// + /// Updates the current state, used for physics, etc. + /// + virtual void act() = 0; + + /// + /// Exits the state, used for cleanup. + /// + virtual void exit() = 0; +}; + +#endif // _FSM_IBEHAVIOURALSTATE_HPP_ \ No newline at end of file -- cgit v1.2.3