I'm trying to implement FSM according to Programming Game AI by Example, it is a pretty standard and straightforward FSM that goes like similarly to this (some stuff omitted and translated from C++ to C#, hope it's fine):
class StateMachine<Entity>
{
Entity owner;
State<Entity> state;
void StateMachine<Entity>(Entity entity)
{
owner = entity;
}
void Update()
{
state.Execute(owner);
}
}
class State<Entity>
{
Entity owner;
void Execute(Entity owner)
{
if (owner.Foo)
owner.Bar();
}
}
class Entity()
{
public type Foo;
private StateMachine<Entity> stateMachine;
public void OnStart()
{
stateMachine = new StateMachine<Entity>(this);
}
public void Bar(){}
}
My problem with this state machine is that the because the state machine is not part of the class anymore (as opposed to, let's say, the naive "switch" / "if-else" implementation), it is necessary to expose internal logic of the Entity (e.g. variable Foo, method Boo()). Is this the right way to do it? What if the the Entity class grows, how does it scale. Is it really necessary to keep exposing more and more variables / creating getters/setters?
How are these FSMs implemented in larger projects?