Game Development Reference
In-Depth Information
1. It needs two new fields. The first is an
AIAppState
called
aiManager
. It also
needs to keep track of its state in an
AIAppState
called
currentState
.
2. In the
setSpatial
method, we add the two gathering states to our control, and
make sure they're disabled, as shown in the following code:
this.spatial.addControl(new GatherFoodState());
this.spatial.addControl(new GatherWoodState());
this.spatial.getControl(GatherFoodState.class).setEnabled(false);
this.spatial.getControl(GatherWoodState.class).setEnabled(false);
3. We also add a method to set the state,
setCurrentState
. Sidestepping con-
ventions, it should not set an instance of a state, but enable an existing state the
AI control class has, while disabling the previous state (if any), as shown in the
following code:
public void setCurrentState(Class<? extends
AIStateRTS> newState) {
if(this.currentState != null &&
this.currentState.getClass() != newState){
this.currentState.setEnabled(false);
}
this.currentState = state;
this.currentState.setEnabled(true);
}
Now we have to write a class that manages the units. It will be based on the
AppState
pattern, and consists of the following steps:
1. We begin by creating a new class called
AIAppState
that extends
Ab-
stractAppState
.
2. It needs a
List<AIControl>
of the units it controls, called
aiList
. We also
add
Map<Class<? extends AIStateRTS>
,
Spatial>
called
re-
sources
that contains the resources in the world that can be gathered.
3. It then needs to keep track of its stock of
wood
and
food
. There are also fields
for the current
foodConsumption
value per second,
minimumFoodStor-
age
it would like to keep, and a
timer
for how long before it wants to reevalu-
ate its decisions.