Game Development Reference
In-Depth Information
else
return PatrolState;
end
end
This function subtracts the player ' s position from the teapot ' s position. If the length
of that vector is less than 20, the player is considered
The teapot then checks
its hit points. If it only has one hit point, it runs away; otherwise, it attacks. If the
player is not close, the teapot patrols.
We can take this a step further by making the transitional logic generic as well. Let ' s
say we have a land mine that sits idle until the player gets close and then explodes.
We can define these states, as shown in Figure 18.2.
close.
Figure 18.2
Mine
'
s state machine.
Notice how the logical condition to switch states is the same here; both the patrol
state of the guard and this idle state check to see if the player is close. The definition
of
is likely different in each case, but the logic is the same.
Each of these pieces of transitional logic can be encapsulated into generic functions,
and each state can have a list of one or more of these functions paired with a target
state. Each tick, the state iterates through the list of transitions, and if any transition
returns true , the state it is paired with becomes the new state. Each transition can be
parameterized with whatever is appropriate for that transition. For example, the dis-
tance check for the mine
close
'
s idle and the guard
'
s patrol states can each be set to sepa-
rate distances. You can even create
transitions that are parameterized
with two other transitions, allowing you to set up rather complex logical chains. This
is exactly what I built for Drawn to Life: The Next Chapter.
The basic concept of state machines is rather simple, but they can grow to be
extremely complex. The typical monster in Drawn to Life had around 15
and
and
or
-
20 states,
each with 2
3 transitions. Most of these states were shared with other enemies, with
one or perhaps two unique states that helped define that particular creature. The iter-
ation time on the enemies was very quick, and most states had fewer than 100 lines
of code. Once the core system was in place, I could crank out the initial implementa-
tion of an enemy in about a day.
While the states in a state machine are meant to implement specific behaviors, the
bulk of decision making tends to come from the transitional logic between states.
-
Search WWH ::




Custom Search