// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. #pragma once #include "Engine/Core/Collections/Array.h" class StateMachine; /// /// State machine state /// class State { friend StateMachine; protected: StateMachine* _parent; protected: /// /// Init /// /// Parent state machine State(StateMachine* parent); /// /// Destructor /// ~State(); public: /// /// State's activity /// /// True if state is active, otherwise false bool IsActive() const; /// /// Checks if can enter to that state /// /// True if can enter to that state, otherwise false virtual bool CanEnter() const { return true; } /// /// Checks if can exit from that state /// /// Next state to enter after exit from the current state /// True if can exit from that state, otherwise false virtual bool CanExit(State* nextState) const { return true; } protected: virtual void enterState() { } virtual void exitState() { } }; /// /// State machine logic pattern /// class StateMachine { friend State; protected: State* _currentState; Array _states; public: /// /// Init /// StateMachine(); /// /// Destructor /// virtual ~StateMachine(); public: /// /// Gets current state /// /// Current state virtual State* CurrentState() { return _currentState; } /// /// Gets readonly states array /// /// Array with states const Array* GetStates() const { return &_states; } public: /// /// Go to state /// /// Target state index void GoToState(int32 stateIndex) { GoToState(_states[stateIndex]); } /// /// Go to state /// /// Target state virtual void GoToState(State* state); protected: virtual void switchState(State* nextState); };