Chapter 5, Artificial Intelligence, deals with several methods to control AI in games. As we learned in that chapter, control and predictability are very important. Even if we have the smartest AI in the world, as programmers, we want to be able to know that the AI will perform a certain action at a certain time. This is where triggers can be extremely useful. In fact, with a good trigger system there might not be need for much AI at all.
One example of trigger usage might be a warehouse where guards are in a patrolling state. Once the player reaches a certain area (maybe where they should not go), an alarm is triggered. At this point, we also want the guards to switch to a more aggressive state.
This recipe will link the trigger system we created previously in the chapter with the StateMachine-based AIControl
class from the Decision making – Finite State Machine recipe in Chapter 5, Artificial Intelligence. Even if you haven't followed the recipes in Chapter 5, Artificial Intelligence, but have a different class controlling AI, it should be quite easy to adapt this recipe to accommodate that class.
As with the previous examples, we begin by creating a new class that extends the ScriptObject
interface. We can call it AIScriptControl
.
AIControl
field called aiControl
and a Class<AIState>
field called targetState
.Spatial
called target
.enabled
.trigger
method, we should call onTrigger
if enabled
is true
.onTrigger
method, we apply targetState
to aiControl
:aiControl.setState(targetState);
target
is not null, we call aiControl.setTarget(target)
.StateMachine
for the AIControl
we created was a closed system and it didn't need any external input to change the states. Now, we need to be able to trigger it externally so let's add a setter method in the AIControl
. Create a new method called setState
, which takes a Class<AIState>
called state
as an input parameter.spatial
has the supplied state, and enable it if possible:if(spatial.getControl(state) != null){ spatial.getControl(state).setEnabled(true); }
This recipe follows the pattern we established in the Creating a trigger system recipe. In the onTrigger
method, we apply targetState
, which will change the behavior and actions of the AI. For example, it can change from PatrolState
to AttackState
. We only supply the class type and not a whole instance of the class since the AI should already have the state and it might be configured already. In this way, we tell the AI to simply change the state if it is available. We also have a target
field, in case the new state requires that.
It doesn't have to end with that. We can, for example, with some modification trigger the AI to start walking a path, turn in a certain direction, or take cover and other things. This functionality can either be built into this class or be made as separate classes.
To explore an example in detail, let's have a look at what will be needed to have the AI move to a specific location once AIScriptControl
is triggered.
AIState
, which handles moving to a set location. Chapter 5, Artificial Intellegence, explains this. The SeekCoverState
can easily be modified to only have a target
field rather than a list to choose from.CoverPoint
control from the same recipe can function as a waypoint too. It can also be extended so that using cover at WayPoint
is an option within the class.WayPoint
to the state. Since we're not supplying a whole class, we can't set it in AIState
itself. One way would be to pass it through the setTarget
method of AIControl
.3.147.77.208