-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.pde
302 lines (248 loc) · 7.13 KB
/
action.pde
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* action.pde
*
* A base class for a game action
*
* Created on: January 15, 2020
* Author: Sean LaPlante
*/
enum GameAction {
PREPARE_SHOT,
EXECUTING_SHOT,
CHANGE_LEVEL
}
enum GameActionState {
ACTION_START,
ACTION_ACTIVE,
ACTION_COMPLETE
}
Action getAction(Action currentAction) {
if (currentAction == null) {
return new PrepareShot();
}
if (currentAction.state != GameActionState.ACTION_COMPLETE) {
return currentAction;
}
switch(currentAction.action) {
case PREPARE_SHOT:
return new ExecuteShot();
case EXECUTING_SHOT:
return new ChangeLevel();
case CHANGE_LEVEL:
return new PrepareShot();
}
return new PrepareShot();
}
abstract class Action {
/*
* Generic base class for a game action
*/
public final GameAction action;
protected GameActionState state;
protected boolean isTouching;
protected MainGame engine;
Action(GameAction a) {
action = a;
state = GameActionState.ACTION_START;
isTouching = false;
}
public abstract void actionStart();
public abstract void actionActive();
void display() {
switch(state) {
case ACTION_START:
actionStart();
break;
case ACTION_ACTIVE:
actionActive();
break;
case ACTION_COMPLETE:
break;
}
}
void nextState() {
/*
* Bump up to the next state of the action
*/
switch(state) {
case ACTION_START:
state = GameActionState.ACTION_ACTIVE;
break;
case ACTION_ACTIVE:
state = GameActionState.ACTION_COMPLETE;
break;
case ACTION_COMPLETE:
// Do nothing, last state.
break;
}
}
void resetAction() {
state = GameActionState.ACTION_START;
}
void handleInput(InputType input) {
switch(input) {
case TOUCH_START:
isTouching = true;
break;
case TOUCH_END:
isTouching = false;
break;
case TOUCH_MOVE:
break;
}
}
}
class PrepareShot extends Action {
/*
* Action to prepare a shot. Draws shot lines, etc...
*/
private boolean validAngle;
private ShotLines shotLines;
PrepareShot() {
super(GameAction.PREPARE_SHOT);
validAngle = false;
// Initialize our handler for shot lines
if (DEBUG) {
shotLines = new ShotLines(100);
} else {
shotLines = new ShotLines(3);
}
}
void actionStart() {}
void actionActive() {
// Handle aiming
if (mouseY < (ENGINE.screen.launchY - BALL_RADIUS)) {
shotLines.display();
validAngle = true;
} else {
validAngle = false;
}
}
void handleInput(InputType input) {
super.handleInput(input);
if (isTouching) {
nextState();
}
if (!isTouching && state == GameActionState.ACTION_ACTIVE) {
// Release
if (!validAngle) {
resetAction();
return;
}
ENGINE.launchLine = shotLines.launchLine;
nextState();
}
}
}
class ExecuteShot extends Action {
/*
* Action to execute a shot.
*/
private float reqDist = BALL_RADIUS * 5;
private PVector velocity;
private boolean launchPosUpdated;
private int doneCount;
private float velocityMag;
private Ball prevBall;
public int launchCount;
ExecuteShot() {
super(GameAction.EXECUTING_SHOT);
velocity = null;
prevBall = null;
doneCount = 0;
launchCount = 1;
launchPosUpdated = false;
velocityMag = SHOT_SPEED;
}
void actionStart() {
velocity = ENGINE.launchLine.getHeading();
velocity.setMag(SHOT_SPEED);
for (int i = 0; i < ENGINE.hud.numBalls; i++) {
Ball ball = new Ball(ENGINE.screen.launchPoint.x, ENGINE.screen.launchPoint.y);
ball.setVelocity(velocity);
ENGINE.balls.add(ball);
}
nextState();
}
void actionActive() {
int deleteCount = 0;
if (ENGINE.balls.size() == 0) {
println("shot complete");
// Commit all the collected balls
ENGINE.hud.numBalls += ENGINE.hud.ballsCollectedThisTurn;
ENGINE.hud.ballsCollectedThisTurn = 0;
nextState();
return;
}
if (prevBall != null && prevBall.distTraveled >= reqDist) {
prevBall = null;
launchCount++;
}
if (launchCount == 1) {
ENGINE.launchPointBall.isVisible = false;
}
int i = 0;
for (Ball ball : ENGINE.balls) {
if (!ball.fired) {
ball.fire();
}
ball.move();
if (ball.isDone) {
doneCount++;
}
if (doneCount == 1 && !launchPosUpdated) {
// The first ball to land updates
// the new launch 'x' position.
println("HERE");
ENGINE.screen.launchPoint.x = ball.location.x;
launchPosUpdated = true;
ball.isDelete = true;
ENGINE.launchPointBall.isVisible = true;
}
if (ball.isDelete) {
ENGINE.deleteBall(ball);
deleteCount++;
}
i++;
if (i >= launchCount) {
prevBall = ball;
break;
}
}
launchCount -= deleteCount;
}
void handleInput(InputType input) {
super.handleInput(input);
if (input == InputType.TOUCH_END && state == GameActionState.ACTION_ACTIVE) {
// SPEED UP!
velocityMag += SHOT_SPEED;
for (Ball ball : ENGINE.balls) {
ball.setVelocityMag(velocityMag);
}
}
}
}
class ChangeLevel extends Action {
/*
* Action to change to the next level.
*/
ChangeLevel() {
super(GameAction.CHANGE_LEVEL);
}
void actionStart() {
/*
* Increment the level
*/
ENGINE.hud.level++;
println("Moving to level: ", ENGINE.hud.level);
ENGINE.world.generateNewRow();
ENGINE.hud.saveGame(false);
nextState();
}
void actionActive() {
/*
* Execute the level change animation and generate blocks
*/
nextState();
}
}