Game Development Reference
In-Depth Information
clip = 5;
ammo = 10;
3. In the state's controlUpdate method, we do a number of checks. First we
check whether clip is 0 . If this is true, we check whether ammo is also 0 . If this
is also true, the AI must flee! We disable this state and enable RetreatState
instead using the following code:
if(clip == 0){
if(ammo == 0){
this.setEnabled(false);
this.spatial.getControl(RetreatState.class).setEnabled(true);
}
4. If the state still has ammo, it should refill the clip. We also set a longer time until
it can fire again, as shown in the following code:
else {
clip += 5;
ammo -= 5;
fireCooldown = 5f;
}
5. In the main if statement, if the state has lost the target, it should disable the state
and switch to PatrolState , as shown in the following code:
else if(aiControl.getTarget() == null){
this.setEnabled(false);
this.spatial.getControl(PatrolState.class).setEnabled(true);
}
6. If it still has a target and is in a position to fire, it should fire, as shown in the fol-
lowing code:
else if(fireCooldown <= 0f &&
aiControl.getSpatial().getWorldTranslation().distance(aiControl.getTarget().getWorldTranslation())
< fireDistance){
clip--;
Search WWH ::




Custom Search