Game Development Reference
In-Depth Information
In the .NET languages such as VB, code can ask any object what type of object it
is. Every different class of object has a unique type name. Since our FSMwill only
store one of any given type of state, asking a state what type it is will provide us
with a unique string identifier for that state. Our code will be asking the states,
but it will be telling the answer to the transitions. So the transitions needed to
know what type of data to store.
Since all transitions will need to store something in the NextState string variable,
we will take care of it in the parent class. Add the following code to the class:
'All child objects should initialize their next state
Public Sub Initialize(ByVal someStateName As String)
NextState = someStateName
End Sub
All code that creates transitions will also need to initialize them. We will do this
in the states. While we have not created any specific transitions, we have com-
pleted the parent class for all transitions. When we create a Transition object, we
care a great deal which transition class we are using. Other than that we rely on
the fact that all of them can be treated as a BasicTransition . We have defined
transitions sufficiently well that we can finish off the BasicState class.
Now would be a good time to go to the File menu and choose Save All. Double-
click BasicState.vb in the Solution Explorer or click the tab for it in the Editing
pane. Since all states will use the same method for checking their transitions, we
will put it in the parent class. Add the following code to the class:
'All states use the same method for checking transitions
Public Function TransitionCheck(ByVal World As Monster) As String
'We can hold any transition in a BasicTransition
Dim Txn As BasicTransition
'We need to store the name of any state returned
Dim nextState As String
'Loop through the collection in regular order
For Each Txn In MyTransitions
'Store off the state name if any
nextState = Txn.ShouldTransition(World)
If nextState <> "" Then
'The first valid transition is the one
Return nextState
End If
Next
 
Search Nedrilad ::




Custom Search