-
Notifications
You must be signed in to change notification settings - Fork 1
/
Stage.java
103 lines (87 loc) · 3.06 KB
/
Stage.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
package ast;
import enums.TriggerFlavour;
import libs.Renderable;
import libs.RenderableObject;
import service.FireballSchedulerService;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* A stage can either be a level or substage. Each stage has an id, speed, list of ids of walls and fireballs.
*/
public abstract class Stage extends Node implements Renderable {
private final Integer id;
private final Speed speed;
private final List<Integer> wallIDs;
private final List<Integer> fireballIDs;
private final FireballSchedulerService fireballSchedulerService = new FireballSchedulerService();
protected final List<RenderableObject> renderableObjects = new ArrayList<>();
protected Stage(Integer id, Speed speed, List<Integer> wallIDs, List<Integer> fireballIDs) {
this.id = id;
this.speed = speed;
this.wallIDs = wallIDs;
this.fireballIDs = fireballIDs;
}
public Integer getId() {
return id;
}
public Speed getSpeed() {
return speed;
}
public List<Integer> getWallIDs() {
return wallIDs;
}
public List<Integer> getFireballIDs() {
return fireballIDs;
}
public void populateObjects(Program program) {
this.renderableObjects.clear();
populateWalls(program.getWalls());
populateFireballs(program.getFireballs());
populateSubstages(program);
populateGoal(program.getGame());
}
private void populateWalls(Map<Integer, Wall> walls) {
for (Integer wallId : this.wallIDs) {
Wall wall = walls.get(wallId);
if (wall != null) {
renderableObjects.add(wall.copy());
}
}
}
private void populateFireballs(Map<Integer, Fireball> fireballs) {
for (Integer fireballId : this.fireballIDs) {
Fireball fireball = fireballs.get(fireballId);
if (fireball == null) {
continue;
}
if (fireball.getTrigger().getTriggerFlavour() == TriggerFlavour.Loop) {
fireballSchedulerService.addFireballToSchedule(fireball.copy());
} else {
renderableObjects.add(fireball.copy());
}
}
renderableObjects.addAll(fireballSchedulerService.fireballSchedule);
}
// Overridable by subclasses
public void populateSubstages(Program program) {
}
public abstract void populateGoal(Game game);
@Override
public void update(final Integer s) {
renderableObjects.forEach(r -> r.update(s));
fireballSchedulerService.updateFireballSchedules(renderableObjects);
}
@Override
public void render(Graphics g) {
// using traditional for loop results in concurrent modification exception
//noinspection ForLoopReplaceableByForEach
for (int i = 0; i < renderableObjects.size(); i++) {
renderableObjects.get(i).render(g);
}
}
public List<RenderableObject> getRenderableObjects() {
return renderableObjects;
}
}