Game Development Reference
In-Depth Information
individual transitions. This will work fine for our simple FSM. Be aware that
coding this way can easily lead to an explosion of classes, however.
There is a different way to implement transitions and states that does not involve a child class for
every unique transition or state. Experienced programmers should look up the Delegate keyword
and the AddressOf function. Armed with these two powerful tools, we could store functions the
way we store data. Then we could use a single class for all transitions and a single class for all
states. This yields less code at the cost of more complicated concepts.
Right-click MonsterAI in the Solution Explorer window and add another class.
Name this class BasicTransition.vb and add it. Then add the MustInherit key-
word to the very first line.
Public MustInherit Class BasicTransition
There will be only two parts to our transitions. One part is code that will evaluate
the current world conditions and decide whether or not to take the transition.
That code is unique for every transition, so BasicTransition will force child
objects to implement it. Add the following code to the class:
'If a transition is valid, return the name of the next state as a string
The other part of a transition is the state to transition to. We are going to store the
name of the next state in a string. It needs be accessible by child classes derived
from BasicTransition ,sowemarkit Protected . The Transition object will give
that string to the state that called it. The state machine will use that string to find
the next state in its collection of states. Add the following code to the class:
'Store the name of the state to transition to.
Protected NextState As String
So how are the states to be named? We could write code that maps each state,
which is an object, to a string. Instead, there is a way to do this automatically,
which means less code to maintain if we add a new state.
We will exploit the fact that each state in the FSM is an object of a different class
than any other state. The FSM will only keep a single copy of the Flee state. The
class that implements the Flee state will be a different class than the classes that
implements the Hiding and Attack states. So instead of dealing with the hard
question, ''Who are you?,'' we will deal with the easier question, ''What are you?''
in regard to naming the states.
Search Nedrilad ::

Custom Search