-
Notifications
You must be signed in to change notification settings - Fork 19
/
AISystem.java
111 lines (95 loc) · 3.5 KB
/
AISystem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package net.zomis.cardshifter.ecs.ai;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import net.zomis.cardshifter.ecs.actions.ActionPerformEvent;
import net.zomis.cardshifter.ecs.actions.ECSAction;
import net.zomis.cardshifter.ecs.base.ComponentRetriever;
import net.zomis.cardshifter.ecs.base.ECSGame;
import net.zomis.cardshifter.ecs.base.ECSGameState;
import net.zomis.cardshifter.ecs.base.ECSSystem;
import net.zomis.cardshifter.ecs.base.Entity;
import net.zomis.cardshifter.ecs.base.Retriever;
import net.zomis.cardshifter.ecs.events.StartGameEvent;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
public class AISystem implements ECSSystem {
private static final Logger logger = LogManager.getLogger(AISystem.class);
@Retriever
private ComponentRetriever<AIComponent> ai;
private final ScheduledExecutorService executor;
public AISystem(ScheduledExecutorService executor) {
this.executor = executor;
}
public static void setup(ECSGame game, ScheduledExecutorService executor) {
AISystem system = new AISystem(executor);
game.addSystem(system);
if (game.getGameState() != ECSGameState.NOT_STARTED) {
logger.warn("Game has already been started when adding AISystem. Performing AI Check directly");
system.aiPerform(game);
}
}
private void aiPerform(ECSGame game) {
Set<Entity> ais = game.getEntitiesWithComponent(AIComponent.class);
logger.info("AI entities " + ais);
for (Entity entity : ais) {
AIComponent aiComp = ai.get(entity);
long delay = aiComp.getDelay();
ECSAction action = aiComp.getAI().getAction(entity);
if (action != null && !game.isGameOver()) {
logger.info(entity + " will perform " + action + " in " + delay + " milliseconds");
Runnable runnable = () -> this.perform(entity, action);
if (delay <= 0) {
runnable.run();
}
else {
executor.schedule(runnable, delay, TimeUnit.MILLISECONDS);
}
return;
}
else {
logger.info(entity + ": No actions available");
}
}
}
private void perform(Entity entity, ECSAction action) {
try {
logger.info(entity + " performs " + action);
boolean performed = action.perform(entity);
if (!performed) {
logger.error(entity + " AI cannot perform action " + action);
aiPerform(entity.getGame());
}
}
catch (Throwable ex) {
// We're on a different thread, so we need information in case anything goes wrong
logger.error(entity + " Error performing " + action, ex);
}
}
@Override
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");
}
}
}
}