I have implemented the menu skeleton as I mentioned in the check list. You can download the source code of revision 13 here.
Finite State Machine is a sort of design pattern that can be very useful. I learned it from a book about AI called “Programming Game AI by Example”. There is no one way to implement a Finite State Machine(FSM) and the one in the book is in C++, but I will show you how I implemented it in BJBK with C#.
What is a FSM? FSM is a directed graph, in which only one state(or vertex of the graph) is active and the active state can change to other states to which it is connected to with edges. You can think of it as a world map with cities and roads. The active state is the city in which your party or characters are currently at. The party can travel only on the roads connecting the cities in order to travel to another city(changing the active state). The main difference is that in a FSM, some roads can be one way roads.
Let’s see the code in FiniteStateMachine.cs:
interface State<T>
{
void Enter(T e);
void Execute(T e);
void Exit(T e);
}
class FiniteStateMachine<T>
{
public FiniteStateMachine(T e)
{
Entity = e;
}
public void Update()
{
if (CurrentState == null)
return;
CurrentState.Execute(Entity);
}
public void ChangeState(State<T> s)
{
if (CurrentState != null)
CurrentState.Exit(Entity);
if (s!=null)
{
CurrentState = s;
CurrentState.Enter (Entity);
}
}
private State<T> CurrentState;
private T Entity;
}
Let us observe the interface State first. This interface is generic, that means it gets a class type as a parameter. It is also an interface, which mean it lacks implementation. As the name imply, this interface represent the actual state or vertices of the graph. In order to create different states, we only need to implement this interface.
When the characters goes to a new city or the FSM change the active state, the new active state’s method Enter will be called once. With some class as a parameter. From this point, the Execute method will be called once every frame until the active state will change again. When the active state will change again, the current active state’s method Exit will be called.
What is the class passed as parameter? The parameter is a class of a generic type, this will allow the states have a common class to use. This class will usually create all the related states in the first place. In BJBK the generic class is Menu and it’s states are the different menus available. These can be MainMenu, Options, Video, Sound and etc. However, the same FSM could be used for AI or other things.
The class FiniteStateMachine simply implements what I have said about the states. It is generic and points to the generic class that will be passed to every state when their methods are called. Update should be called each frame, or each time you want the FSM to execute the curent active state. ChangeState will change to a new active state.
That’s it, that is the FSM implementation. However, you might feel something is missing. There is no class or interface to represent the edges of the graph?
The reason is that by calling ChangeState you virtually create the edge. The edges are not “precalculated”, but they are rather implied by the calls to ChangeState on run time. The same goes with the graph structure. There is no explicit graph structure. You can create new vertexes at run time and change the graph structure during run time. This is all done programmatically. Although, in the case of the BJBK menu, the graph is static and does not change during run time.
