Game Development Reference
code is a function instead of a sub. Since it returns a value, we have to specify the
type of value returned—in this case a Boolean. Dealing with the monster's health
requires similar code:
'We will assume that health is either good or bad
Public Function GoodHealth() As Boolean
'We will use thirty percent as our threshold
If CurrentHealth.Value >= 0.3 * CurrentHealth.Maximum Then
The core of this function could be compressed to a single line at a modest cost in
readability. What remains is to give our monster a place to speak its mind. We
will do this by creating a routine to write to the ThoughtsTextBox control:
'The output side of the interface:
Public Sub Say(ByVal someThought As String)
'Everything we thought before, a new thought, and a newline
ThoughtsTextBox.AppendText(someThought & vbCrLf)
The & character is used for character string concatenation. Our code takes
everything that was already in the text box and adds another line to it.
This completes everything in the user interface except the Think button. Before
we can make the Think button do anything, we will have to implement our FSM.
We start with the states.
Our states need common parts shared by all states and unique parts for each
specific state. We will use classes and inheritance to accomplish this. We will
create a class called BasicState to hold all the commonalities. The three classes
we will actually use in our FSM will inherit from BasicState . We will never
attempt to put a BasicState object into our FSM; our monster only understands
Hiding, Attack, and Flee. It does not understand BasicState .
Right-click MonsterAI in the Solution Explorer window and select
Add?Class. The Add New Item dialog box appears; name the new class
BasicState.vb and click Add. VB will create the file and display it in the Editing
pane. The first thing we will do to our new class is make sure that we cannot