Game Development Reference
In-Depth Information
4. The update method is pretty simple. It starts by subtracting foodConsump-
tion from the storage. Then, if timer has reached 0 , it will call the evalu-
ate method, as shown in the following code:
food -= foodConsumption * tpf;
food = Math.max(0, food);
timer-= tpf;
if(timer <= 0f){
evaluate();
timer = 5f;
}
5. In the evaluate method, we begin by establishing the food requirement, as
shown in the following code:
float foodRequirement = foodConsumption * 20f +
minimumFoodStorage;
6. Then we decide how urgent food gathering is, on a factor of 0.0 - 1.0, as shown in
the following code:
float factorFood = 1f - (Math.min(food,
foodRequirement)) / foodRequirement;
7. Now we decide how many workers should be assigned to food gathering by tak-
ing that factor and multiplying it by the total amount of workers, as shown in the
following code:
int numWorkers = aiList.size();
int requiredFoodGatherers = (int)
Math.round(numWorkers * factorFood);
int foodGatherers =
workersByState(GatherFoodState.class);
8. We create a helper method, called workersByState , that returns the number
of workers assigned to a given state, as shown in the following code:
private int workersByState(Class<? extends
AIStateRTS> state){
int amount = 0;
for(AIControl_RTS ai: aiList){
Search WWH ::




Custom Search