Game Development Reference
In-Depth Information
'Add it to our list of transitions
MyTransitions.Add(Txn)
End Sub
We see in Figure 3.2 that the Hiding state has only one outgoing transition. At
this point, we have completed the Hiding state. If we add this state to the FSM, we
could test it to see if it works. We have written a great deal of code at this point
and tested none of it, so perhaps some testing is in order. We should expect
problems because we have not completed everything, but what we have should
run. Before we can do any serious testing, however, we need to give the monster
an FSM, and we need to load our single completed state into it.
Navigate to the code for Monster.vb. You can do this via a right-click on
Monster.vb in the Solution Explorer window or by clicking the tab above the
Editing pane if it is present. If a skeleton for the Load event handler is not present,
click the top-left drop-down and select (Monster Events). Then click Load in the
right drop-down to create the skeleton. Then add the following two lines above
the Load event handler:
'We need an FSM
Dim Brains As New FSM
To the Load event handler itself, add the following lines:
'The first state loaded is the start state
Brains.LoadState(New HidingState)
Working from in the parentheses out, this asks VB to create a new HidingState
object and pass it to the LoadState method of the FSM object we call Brains. All
that remains is the ability to ask the FSM to think. Switch to the Design view of
Monster.vb and double-click the Think button. VB will switch back to the Code
view and create the Click event handler for ThinkButton . Add the following line
to that handler:
Brains.RunAI(Me)
The Me keyword is how the monster form can refer to itself. Brains is an FSM
object, and its RunAI member expects to be passed an object of type Monster. We
have not loaded every state into the Brains FSM object, but one state is enough
for testing. From the Debug menu, select Start Debugging. VB saves all the files
and compiles them before running the program. Click the Think button and
manipulate the user interface. When finished, close the form or select Stop
Debugging from the Debug menu. You should see something like Figure 3.5.
 
Search Nedrilad ::




Custom Search