Game Development Reference
In-Depth Information
'No transition was valid, no state to change to
Return ""
End Function
BasicState is now complete.
Just as finishing the base class used for transitions allowed us to finish the base
class for states, finishing the base class for states will allow us to write the state
machine object. We only have one kind of state machine, so it does not need
inheritance. Open the File menu and choose Save All, and then add a new class to
the project. Name it FSM.vb and add the following lines to the class:
'We need a place to store the states
Dim States As New Collection
'We need to remember what state is the current one
Dim currentStateName As String
You may have noticed that some variables are declared with New , and others are
not. Visual Basic treats certain types of variables differently than others. Basic data
types include integers and strings. In VB, there is always storage created for them,
and they are initialized automatically. Strings start with the empty string "" ,and
integers start with 0 . Collections are not a basic data type; they are objects. The New
keyword tells Visual Basic to actually create the object. Variables that deal in objects
start with the special value of Nothing until they are assigned to an actual object.
Our monster will want to load its FSMwith states. We let the monster control the
loading so that different monsters can use the same FSM class but load it with
monster-specific states. Add the following code to the class:
Public Sub LoadState(ByVal state As BasicState)
Dim stateName As String
'Get the short name of this state's class
stateName = state.GetType.Name
'The first state we get is the start state
If States.Count = 0 Then
currentStateName = stateName
End If
'Never add the same state twice
If Not States.Contains(stateName) Then
'Add the state, keyed by its name
States.Add(state, stateName)
End If
End Sub
Search Nedrilad ::

Custom Search