Skip to content

Implementing a Finite State Machine #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ add_subdirectory(AdversarialSearch/Minimax)
add_subdirectory(Miscellaneous)
add_subdirectory(NeuralNetwork/Perceptron)
add_subdirectory(NeuralNetwork/MLP)
add_subdirectory(FSM)
5 changes: 5 additions & 0 deletions FSM/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
set(FILES
FSM.cpp
)
add_compile_definitions(ENABLE_FSM_DEBUG)
add_executable(FSM ${FILES})
28 changes: 28 additions & 0 deletions FSM/FSM.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include "StateMachine.h"
#include "MoveTo.h"
#include "Idle.h"

using namespace std;

int main()
{
FSM::StateMachine fsm;

fsm.TransitionTo(new FSM::Idle(), true);
fsm.TransitionTo(new FSM::MoveTo());
fsm.Tick();
fsm.TransitionTo(nullptr);
cout << endl;

fsm.TransitionTo(nullptr);
fsm.TransitionTo(new FSM::Idle(), true);
fsm.TransitionTo(nullptr);
cout << endl;

fsm.TransitionTo(nullptr);
fsm.TransitionTo(new FSM::MoveTo());
fsm.Tick();
fsm.TransitionTo(nullptr);

return 0;
}
30 changes: 30 additions & 0 deletions FSM/Idle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <iostream>
#include "State.h"

namespace FSM
{
class Idle : public State
{
~Idle()
{
//
}

void OnEnter()
{
//
}

void OnUpdate()
{
//
}

void OnExit()
{
//
}
};
}
30 changes: 30 additions & 0 deletions FSM/MoveTo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <iostream>
#include "State.h"

namespace FSM
{
class MoveTo : public State
{
~MoveTo()
{
//
}

void OnEnter()
{
//
}

void OnUpdate()
{
//
}

void OnExit()
{
//
}
};
}
24 changes: 24 additions & 0 deletions FSM/State.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once

#include <typeinfo>

namespace FSM
{
class State
{
public:
virtual ~State() = 0;
const char* name() const
{
return typeid(*this).name();
}
virtual void OnEnter() = 0;
virtual void OnUpdate() = 0;
virtual void OnExit() = 0;
};

State::~State()
{
//
}
}
80 changes: 80 additions & 0 deletions FSM/StateMachine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#pragma once

#include <memory>
#include <assert.h>
#include <iostream>
#include "State.h"

namespace FSM
{
class StateMachine
{
private:
State* _deferredNextState;
std::unique_ptr<State> _currentState;

void inline ApplyStateTransition()
{
if (!_deferredNextState)
{
return;
}
if (_currentState)
{
#ifdef ENABLE_FSM_DEBUG
std::cout << _currentState->name() << ": OnExit()" << std::endl;
#endif
_currentState->OnExit();
}
_currentState.reset(_deferredNextState);
#ifdef ENABLE_FSM_DEBUG
std::cout << _currentState->name() << ": OnEnter()" << std::endl;
#endif
if (_currentState)
{
_currentState->OnEnter();
}
_deferredNextState = nullptr;
}

public:
~StateMachine()
{
if (_deferredNextState)
{
delete _deferredNextState;
}
}

void Tick()
{
ApplyStateTransition();
if (_currentState)
{
#ifdef ENABLE_FSM_DEBUG
std::cout << _currentState->name() << ": OnUpdate()" << std::endl;
#endif
_currentState->OnUpdate();
}
}

void TransitionTo(State* const pNewState, bool immediate = false)
{
if (pNewState == _currentState.get())
{
#ifdef ENABLE_FSM_DEBUG
std::cout << "Trying transitioning to same state! " << pNewState->name() << std::endl;
#endif
return;
}
#ifdef ENABLE_FSM_DEBUG
std::cout << "FSM - Transitioning to '" << (pNewState == nullptr ? "NULL" : pNewState->name()) << "' / immediate = " << std::boolalpha << immediate << std::endl;
#endif
_deferredNextState = pNewState;
if (immediate)
{
Tick();
}
}
};
}