-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrafficController.pde
More file actions
215 lines (167 loc) · 6.28 KB
/
TrafficController.pde
File metadata and controls
215 lines (167 loc) · 6.28 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import java.util.function.Supplier;
import java.util.Stack;
class TrafficController implements GameElement {
// ----- Fields
private final float timeToStart = 2.35f;
private final float vehicleCullInterval = 2.5f;
private final float hVehicleMinSpeed = 3, hVehicleMaxSpeed = 5;
private final float vVehicleMinSpeed = 3, vVehicleMaxSpeed = 5;
private final Scene gameScene;
private final IntLabel score;
private final Stack<Vehicle> inactiveVVehicles = new Stack<Vehicle>();
private final Stack<Vehicle> inactiveHVehicles = new Stack<Vehicle>();
private final Stack<Vehicle> activeVVehicles = new Stack<Vehicle>();
private final Stack<Vehicle> activeHVehicles = new Stack<Vehicle>();
private final MultiCollection<Vehicle> activeVehicles = new MultiCollection<Vehicle>(activeVVehicles, activeHVehicles);
private final RoadLane[] lanes = new RoadLane[] {
new RoadLane(new Vec2Int(-100, 400), Vec2Int.UP, vVehicleMinSpeed, vVehicleMaxSpeed, this::takeVVehicle),
new RoadLane(new Vec2Int(0, 400), Vec2Int.UP, vVehicleMinSpeed, vVehicleMaxSpeed, this::takeVVehicle),
new RoadLane(new Vec2Int(100, 400), Vec2Int.UP, vVehicleMinSpeed, vVehicleMaxSpeed, this::takeVVehicle),
new RoadLane(new Vec2Int(-900, -100), Vec2Int.RIGHT, hVehicleMinSpeed, hVehicleMaxSpeed, this::takeHVehicle),
new RoadLane(new Vec2Int(-900, 0), Vec2Int.RIGHT, hVehicleMinSpeed, hVehicleMaxSpeed, this::takeHVehicle),
new RoadLane(new Vec2Int(-900, 100), Vec2Int.RIGHT, hVehicleMinSpeed, hVehicleMaxSpeed, this::takeHVehicle)
};
private TrafficLight trafficLight;
private float startTimer;
private boolean timerOut;
private boolean gameStarted;
private float vehicleCullTimer = vehicleCullInterval;
// ----- Constructors
public TrafficController(Scene gameScene) {
this.gameScene = gameScene;
//<>//
score = (IntLabel)gameScene.firstElement(e -> e instanceof IntLabel);
}
// ----- Methods
@Override
public void draw() {
float deltaTime = 1f / frameRate;
if (!timerOut) {
startTimer += deltaTime;
timerOut = startTimer >= timeToStart;
return;
}
if (!gameStarted) {
onGameStart();
gameStarted = true;
return;
}
for (RoadLane lane : lanes) {
lane.draw();
}
vehicleCullTimer -= deltaTime;
if (vehicleCullTimer <= 0f) {
cullVehicles();
vehicleCullTimer = vehicleCullInterval;
}
for (Vehicle v : activeVVehicles) {
if (v.position.y <= -375) {
disableVehicle(v);
score.increaseValue(1);
break;
}
}
}
private void onGameStart() {
trafficLight = (TrafficLight)gameScene.firstElement(e -> e instanceof TrafficLight);
trafficLight.setState(LightColor.GREEN);
TweenEngineHelpers.tween(score, JuicyIntLabelTweenAccessor.ALPHA_ID, .5f)
.target(255)
.delay(.45f)
.start(tweenManager);
}
private void cullVehicles() {
final int margin = 200;
final int rightLimit = width / 2 + margin, topLimit = -height / 2 - margin;
var toDisable = new ArrayList<Vehicle>();
for (Vehicle v : activeHVehicles) {
if (v.position.x >= rightLimit)
toDisable.add(v);
}
for (Vehicle v : toDisable) {
disableVehicle(v);
}
}
private Vehicle takeVehicle(Stack<Vehicle> inactiveSource, Stack<Vehicle> activeSource, Supplier<Vehicle> factory) {
if (inactiveSource.isEmpty()) {
var newVehicle = factory.get();
gameScene.add(newVehicle);
inactiveSource.push(newVehicle);
}
Vehicle val = inactiveSource.pop();
activeSource.push(val);
return val;
}
private Vehicle takeVVehicle() {
return takeVehicle(inactiveVVehicles, activeVVehicles, () -> new ControlledVehicle(trafficLight, this::onVehicleDestroyed));
}
private Vehicle takeHVehicle() {
return takeVehicle(inactiveHVehicles, activeHVehicles, () -> new Vehicle(this::onVehicleDestroyed));
}
private void onVehicleDestroyed(Vehicle subject) {
disableVehicle(subject);
}
private void disableVehicle(Vehicle val) {
val.disable();
if (val instanceof ControlledVehicle) {
activeVVehicles.remove(val);
inactiveVVehicles.push((ControlledVehicle)val);
} else {
activeHVehicles.remove(val);
inactiveHVehicles.push(val);
}
}
// ----- Nested Types
private class RoadLane implements GameElement {
// ----- Fields
private static final int numOfLanes = 3;
private final Vec2Int vehicleSpawnPos;
private final Vec2Int direction;
private final float minSpeed, maxSpeed;
private final Supplier<Vehicle> takeVehicle;
private float vehicleSpawnTimer;
private float[] laneDelays = new float[numOfLanes];
// ----- Constructors
public RoadLane(Vec2Int vehicleSpawnPos, Vec2Int dir, float minSpeed, float maxSpeed, Supplier<Vehicle> takeVehicle) {
this.vehicleSpawnPos = vehicleSpawnPos;
direction = dir;
this.minSpeed = minSpeed;
this.maxSpeed = maxSpeed;
this.takeVehicle = takeVehicle;
vehicleSpawnTimer = random(10f);
}
// ----- Methods
@Override
public void draw() {
vehicleSpawnTimer -= 1f / frameRate;
if (vehicleSpawnTimer <= 0f) {
spawnVehicle();
scheduleNextVehicleSpawn();
}
for (var vehicle : activeVVehicles) {
RectCollider current = vehicle.getCollider();
if (current == null) continue;
for (var otherVehicle : activeVehicles) {
if (otherVehicle == vehicle) continue;
RectCollider other = otherVehicle.getCollider();
if (other == null) continue;
if (current.intersects(other)) {
vehicle.destroy(true);
otherVehicle.destroy(true);
score.increaseValue(-5);
}
}
}
}
private void spawnVehicle() {
int laneIndex = (int)random(numOfLanes);
Vehicle spawned = takeVehicle.get();
spawned.position = vehicleSpawnPos;
spawned.initAndEnable(direction, minSpeed, maxSpeed); //<>//
}
private void scheduleNextVehicleSpawn() {
final float minTime = 5f, maxTime = 10f;
vehicleSpawnTimer = random(minTime, maxTime);
}
}
}