Skip to content

Commit

Permalink
Added a method to call AIs in a game directly
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Oct 5, 2014
1 parent 213f7dd commit e9d3f46
Showing 1 changed file with 23 additions and 0 deletions.
Expand Up @@ -84,5 +84,28 @@ public void startGame(ECSGame game) {
game.getEvents().registerHandlerAfter(this, ActionPerformEvent.class, event -> this.aiPerform(event.getEntity().getGame()));
game.getEvents().registerHandlerAfter(this, StartGameEvent.class, event -> this.aiPerform(event.getGame()));
}

/**
* Call all AIs in the game directly. Useful for when a new AI has been initialized while the game is running
* @param game Game to call AIs in
*/
public static void call(ECSGame game) {
Set<Entity> ais = game.getEntitiesWithComponent(AIComponent.class);
ComponentRetriever<AIComponent> ai = game.componentRetreiver(AIComponent.class);

logger.info("AI entities " + ais);
for (Entity entity : ais) {
AIComponent aiComp = ai.get(entity);
ECSAction action = aiComp.getAI().getAction(entity);
if (action != null && !game.isGameOver()) {
logger.info(entity + " performs " + action);
action.perform(entity);
return;
}
else {
logger.info(entity + ": No actions available");
}
}
}

}

0 comments on commit e9d3f46

Please sign in to comment.