-
Notifications
You must be signed in to change notification settings - Fork 1
/
finalproj.pde
executable file
·1227 lines (1031 loc) · 26.7 KB
/
finalproj.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//vim: sw=3:tabstop=3:sts=3:expandtab
/* Quick shim for processing.JS */
class Rectangle2D {
float x; float y; float width; float height;
Rectangle2D(float x, float y, float w, float h) {
this.x = x;
this.y = y;
this.width = w;
this.height = h;
}
boolean contains(float x2, float y2) {
if (x2 < x || x2 > x+width) {
return false;
}
if (y2 < y || y2 > y+height) {
return false;
}
return true;
}
}
PImage planeIcon;
final float MUSIC_LENGTH = 201.5;
final float FULL_FUEL = 2.5;
final float WALL_THICKNESS = 40;
final float DISTANCE_ALARM_DIST = 80;
final float CRASH_DIST = 25;
final float FUEL_ALARM_SECS = 15;
boolean proximityAlarmActive = false;
boolean fuelAlarmActive = false;
MainScreen screen_m;
InstructionScreen screen_i;
Game g;
color BT_MO_COLOR = #0036A0; /* button mouseover color */
float PIXELS_PER_METER = 40;
PVector GLOBAL_G = new PVector(0.0, 9.8*PIXELS_PER_METER);
final int SCREEN_MAIN = 0;
final int SCREEN_INST = 1;
final int SCREEN_PLAY = 2;
int difficulty = 1; /* Difficulty selection. Copied into the Game object when it is constructed. */
void setup() {
size(900,900);
background(0);
frameRate(50);
resetDrawState();
fill(0xFF);
textSize(10);
pushMatrix();
scale(7);
textAlign(CENTER, CENTER);
text("Loading...", (width/2)/7, (height/2)/7);
popMatrix();
planeIcon = loadImage("data/airplane_mode_on.png");
screen_m = new MainScreen();
screen_i = new InstructionScreen();
}
float t = 0;
float gameStartTime = 0;
void draw() {
if(wantsExit && currentScreen != SCREEN_MAIN) {
currentScreen = SCREEN_MAIN;
screenDidChange();
g = null;
stopSound(0);
stopSound(3);
stopSound(2);
stopSound(4);
fuelAlarmActive = false;
proximityAlarmActive = false;
wantsExit = false;
}
resetDrawState();
background(0);
float old_t = t;
float delta_t = 1/frameRate;
t += delta_t;
if (currentScreen == SCREEN_MAIN) {
screen_m.tick(t, delta_t);
}
else if (currentScreen == SCREEN_INST) {
screen_i.tick(t, delta_t);
}
else if (currentScreen == SCREEN_PLAY) {
g.tick(t-gameStartTime, delta_t);
if (g.over) {
t = old_t;
}
}
}
void mouseMoved() {
if (currentScreen == SCREEN_MAIN) {
screen_m.mouse(mouseX, mouseY);
}
else if (currentScreen == SCREEN_INST) {
screen_i.mouse(mouseX, mouseY);
}
else if (currentScreen == SCREEN_PLAY) {
g.moved(mouseX, mouseY);
}
}
void mouseDragged() {
mouseMoved();
}
void keyPressed() {
if (currentScreen == SCREEN_MAIN) {
if (keyCode == 37) {
difficulty = max(0, difficulty-1);
}
if (keyCode == 39) {
difficulty = min(8, difficulty+1);
}
}
}
void mouseClicked() {
if (currentScreen == SCREEN_MAIN) {
int res = screen_m.click(mouseX, mouseY);
if (res == 1) {
currentScreen = SCREEN_INST; // Go to instructions
screenDidChange();
}
if (res == 2) {
gameStartTime = t;
g = new Game(difficulty);
playSound(0, 12.500);
currentScreen = SCREEN_PLAY; // Go to difficulty selection
screenDidChange();
}
}
else if (currentScreen == SCREEN_INST) {
int res = screen_i.click(mouseX, mouseY);
if (res == 1) {
currentScreen = SCREEN_MAIN; // Go to instructions
screenDidChange();
}
}
}
void mousePressed() {
if (currentScreen == SCREEN_PLAY) {
g.pressed(mouseX, mouseY);
}
}
void mouseReleased() {
if (currentScreen == SCREEN_PLAY) {
g.released(mouseX, mouseY);
}
}
void resetDrawState() {
textAlign(LEFT, TOP);
strokeWeight(1);
stroke(0);
fill(255);
rectMode(CORNERS);
imageMode(CENTER);
}
/*
Airplane class. Represents a single airplane across its life.
*/
class Airplane {
float theta;
float targetTheta;
PVector r;
final float SPEED = 20.0;
float fuel;
float maxFuel;
int dest;
int source;
boolean haveNotReachedDestination = true;
/*
Constructor for Airplane.
x = initial x-position
y = initial y-position
theta = initial angle (CCW from right)
source = region number for the source (origin) of the plane
dest = region number for the destination of the plane
safety = the factor of safety (should be above 1.0) for the plane's fuel.
*/
public Airplane(float x, float y, float theta, int source, int dest, float safety) {
maxFuel = max(width, height)*.5*sqrt(2)/SPEED;
fuel = maxFuel*safety;
maxFuel *= FULL_FUEL;
this.source = source;
this.dest = dest;
this.theta = theta;
targetTheta = theta;
r = new PVector(x, y);
}
/*
Return true if (x,y) is a valid click point for the plane.
*/
boolean overlap(float x, float y) {
float a = x-r.x;
float b = y-r.y;
return sqrt(a*a+b*b) < 20;
}
/*
Return a color for the fuel bar.
*/
color getFuelColor() {
float pct = fuel/maxFuel;
if (pct < .5) {
return #FF0000;
}
else if (pct > .75) {
return #00FF00;
}
return #FFFF00;
}
/*
Draws the plane and updates its state.
- Draws with flashing if low on fuel or turning.
- Draws fuel gauge
- Draw destination
- Draw drag widget for this plane if necessary
- Update theta to point toward targetTheta
- Warp plane to starting region if necessary
- Check if fuel is out, trigger endgame if so.
*/
void tick(float t, float delta_t) {
resetDrawState();
pushMatrix();
translate(round(r.x), round(r.y));
pushMatrix();
scale(.2);
rotate(-theta);
if (fuel/maxFuel < .3) {
// Flash plane quickly if low on fuel
tint(lerpColor(#FF0000, #00FFFF, sin(t*30)));
}
else {
if (abs(targetTheta - theta) > .2) {
// Plane flashes if turning
tint(lerpColor(#FFFFFF, #C5BCC6, sin(t*10)));
}
else {
tint(#FFFFFF);
}
}
image(planeIcon, 0,0);
tint(#FFFFFF);
popMatrix();
/* Draw fuel gauge */
translate(0, 30);
stroke(0x88);
strokeWeight(1);
fill(0);
rectMode(CORNERS);
rect(-20,-3,20,3);
noStroke();
fill(getFuelColor());
rect(-20,-3,lerp(-20, 20, fuel/maxFuel),3);
textAlign(LEFT, CENTER);
pushMatrix();
scale(.9);
text(floor(fuel)+"s", 300/9, 0);
popMatrix();
/* Draw destination */
textx("Dest: "+regionToString(dest), .9, -20, 10);
popMatrix();
if (g.over) {
return;
}
/* Draw drag widget if necessary */
if (g.isPlaneSelected(this)) {
strokeWeight(3);
stroke(0xFF);
line(g.m.x, g.m.y, r.x, r.y);
}
float TURN_SPEED = .4;
float deltaTheta = Angle.sub(targetTheta, theta);
if (deltaTheta > 0) {
theta = theta + delta_t*TURN_SPEED;
}
else {
theta = theta - delta_t*TURN_SPEED;
}
theta = theta + 2*PI;
theta = theta % (2*PI);
if (abs(Angle.sub(targetTheta, theta)) < delta_t*TURN_SPEED) {
theta = targetTheta;
}
r.add(new PVector(SPEED*cos(theta)*delta_t, -SPEED*sin(theta)*delta_t));
int region = getRegionFromPoint(r);
boolean warpToStart = false;
if (region != -1) {
if (region == dest) {
// This plane is no longer "alive" (has succeeded in its journey)
this.haveNotReachedDestination = false;
playSound(1);
return;
}
else if (region == source) {
// If region is the source, ignore unless the plane is off the screen
if (r.x < 0 || r.x > width || r.y < 0 || r.y > height) {
warpToStart = true;
}
}
else {
// If the plane is in a strange region, re-spawn from source,
// directing toward destination
warpToStart = true;
}
}
if (warpToStart) {
r = randomPosition(source);
PVector difference = PVector.sub(randomPosition(dest), r);
difference.y = -difference.y;
theta = thetaFromVector(difference);
targetTheta = theta;
}
fuel = max(fuel-delta_t, 0);
if (fuel <= 0) {
g.over = true;
g.planeThatRanOut = this;
stopSound(3);
stopSound(0);
}
}
}
/*
Produces human-readable name for difficulty level.
*/
String diffToString(int diff) {
if (diff == 0) {
return "Easy";
}
if (diff == 1) {
return "Medium";
}
if (diff == 2) {
return "Hard";
}
if (diff == 6) {
return "Are you crazy?";
}
if (diff == 7) {
return "Insane!";
}
if (diff == 8) {
return "There's no way you're beating this.";
}
String r = "Hard";
for (int i = 0; i < (diff-2); i++) {
r = "Very " + r;
}
return r;
}
class MainScreen {
Rectangle2D bt1;
Rectangle2D bt2;
/*
Constructor for MainScreen.
*/
MainScreen() {
float W = 150;
float H = 50;
bt1 = new Rectangle2D(width*(.5-.2)-W/2, height*.9-H/2, W, H);
bt2 = new Rectangle2D(width*(.5+.2)-W/2, height*.9-H/2, W, H);
}
boolean mo1 = false;
boolean mo2 = false;
/*
Handles mouseover for instructions/play buttons
*/
void mouse(float x, float y) {
mo1 = mo2 = false;
if (bt1.contains(x,y)) {
mo1 = true;
}
if (bt2.contains(x,y)) {
mo2 = true;
}
}
/*
Handles click detection for the instructions and play
buttons
*/
int click(float x, float y) {
if (bt1.contains(x,y)) {
return 1;
}
if (bt2.contains(x,y)) {
return 2;
}
return -1;
}
/*
Displays:
- blinking "turn up the sound"
- selected difficulty
- credits
- title
- instructions/play buttons
*/
void tick(float t, float delta_t) {
resetDrawState();
color(0xFF);
noStroke();
textAlign(CENTER, CENTER);
pushMatrix();
translate(width/2, height*.2);
scale(7);
text("ATC-Sim", 0, 0);
popMatrix();
pushMatrix();
translate(width/2, height*.3);
scale(1.3);
text("By Philip Peterson", 0, 0);
popMatrix();
textAlign(RIGHT, CENTER);
pushMatrix();
translate(width/2, height*.7);
scale(2.5);
text("Difficulty: ", 0, 0);
textAlign(LEFT, CENTER);
text(diffToString(difficulty), 0, 0);
popMatrix();
pushMatrix();
textAlign(CENTER, CENTER);
translate(width/2, height*.73);
scale(1.3);
text("(Use left/right arrow keys to change)", 0, 0);
popMatrix();
pushMatrix();
translate(width/2, height*.8);
scale(1.5);
if (t % 1 < .5) {
fill(#FFFF00);
}
text("Turn up the sound!", 0, 0);
popMatrix();
fill(0xFF);
pushMatrix();
translate(width/2, height/2);
scale(1.0);
text(
"The following are CC-by-SA (https://creativecommons.org/licenses/by/3.0/us/)\n"
+"Airplane icon by VisualPharm. Colors were inverted. Converted to Ogg/MP3 format.\n"
+"Industrial Alarm sound by Mike Koenig. Converted to Ogg/MP3 format.\n"
+"School Fire Alarm sound by Cullen Card. Converted to Ogg/MP3 format.\n"
+"Explosion Ultra Bass sound by Mark DiAngelo. Converted to Ogg/MP3 format.\n"
+"\n"
+"The following is CC-0:\n"
+"Good! sound by syseQ. Converted to Ogg/MP3 format.\n"
+"\n"
, 0,0);
translate(0, 100);
scale(1.5);
text("Performance of Chopin's \"Winter Wind\" by Antonio Pompa-Baldi\nUsed with permission.\n(Soundcloud: AntonioPompaBaldi1)", 0, 0);
popMatrix();
stroke(0xFF);
strokeWeight(2.0);
fill(0x44);
if (mo1) {
fill(BT_MO_COLOR);
}
rect(bt1.x, bt1.y, bt1.x+bt1.width, bt1.y+bt1.height, 20);
fill(0x44);
if (mo2) {
fill(BT_MO_COLOR);
}
rect(bt2.x, bt2.y, bt2.x+bt2.width, bt2.y+bt2.height, 20);
fill(0xFF);
pushMatrix();
translate(bt1.x+bt1.width/2, bt1.y+bt1.height/2-2);
scale(2.0);
text("Instructions", 0, 0);
popMatrix();
pushMatrix();
translate(bt2.x+bt2.width/2, bt2.y+bt2.height/2-2);
scale(2.0);
text("Play", 0, 0);
popMatrix();
}
}
class InstructionScreen {
Rectangle2D.Float bt1;
/* Constructor for InstructionScreen */
InstructionScreen() {
float W = 150;
float H = 50;
bt1 = new Rectangle2D(width*(.5-.2)-W/2, height*.9-H/2, W, H);
}
boolean mo1 = false;
/* Checks if the mouse is over the button */
void mouse(float x, float y) {
mo1 = false;
if (bt1.contains(x,y)) {
mo1 = true;
}
}
/* Checks if the mouse has clicked the button */
int click(float x, float y) {
if (bt1.contains(x,y)) {
return 1;
}
return -1;
}
/* Draws instructions and button to screen. */
void tick(float t, float delta_t) {
resetDrawState();
color(0xFF);
noStroke();
textAlign(LEFT, TOP);
textx(
"Each airplane has to make it to its destination before it runs out of fuel.\nThe goal is to get all the airplanes to their destination, or to have the time run out\n"
+"without crashing.\n\n\n"
+"Click and drag an airplane to change its heading.\nNote that this takes time because of inertia.\nWhen an airplane is turning, it will pulsate in brightness to indicate this.\n\n\n"
+"If an airplane goes off the radar in the wrong direction, it will get warped back\nto the start, costing you fuel.\n\n\n\n"
+"You can stop the game by pressing (Esc).\n\n\n\n"
+"All airplanes are at the same altitude. Don't let them crash!", 2.4, 20.0, 20.0);
/* Draw buttons */
stroke(0xFF);
strokeWeight(2.0);
fill(0x44);
if (mo1) {
fill(BT_MO_COLOR);
}
rect(bt1.x, bt1.y, bt1.x+bt1.width, bt1.y+bt1.height, 20);
/* Draw button text */
textAlign(CENTER, CENTER);
fill(0xFF);
pushMatrix();
translate(bt1.x+bt1.width/2, bt1.y+bt1.height/2-2);
scale(2.0);
text("Back to Menu", 0, 0);
popMatrix();
}
}
/*
Represents a game session.
*/
class Game {
int diff;
int crash1 = -1;
int crash2 = -1;
Airplane planeThatRanOut = null;
Airplane planes[];
float times[];
boolean over = false;
int indexOfLastPlane = 0;
/*
Construct a new Game with difficulty in range [0, infinity)
Initializes the list of planes, with staggered start times.
*/
Game(int difficulty) {
this.diff = diff;
int numPlanes = (diff+1)*20;
planes = new Airplane[numPlanes];
times = new float[numPlanes];
float avgTime = MUSIC_LENGTH/numPlanes;
times[0] = 0;
for (int i = 0; i < numPlanes; i++) {
PVector start = randomPosition((i % 8));
int endRegion = getRandomEndRegion(i%8);;
PVector end = randomPosition(jitterRegion(endRegion));
PVector difference = PVector.sub(end, start);
difference.y = -difference.y;
float theta = thetaFromVector(difference);
// From levels 0 to 5, gradually decrease safety margin.
float safety = lerp(2.5, 1.8, min(difficulty/3.0, 1.0));
planes[i] = new Airplane(start.x, start.y, theta, i%8, endRegion, safety);
if (i > 0) {
float timeSafety = lerp(1,.5,max(difficulty/6.0, 0));
float timeDiff = avgTime+timeSafety;
if (difficulty > 8) {
timeDiff = timeSafety+2;
}
times[i] = times[i-1]+random(timeSafety, timeDiff);
}
}
}
int ctr;
static final int TICKS_BETWEEN_ALARM_CHECKS = 10;
/*
Draws essentially everything related to the HUD.
Also updates most of the logic.
- Checks if the game has ended, displays message if so.
- Displays time remaining
- Updates alarms every few ticks.
- Draws border pieces.
- Tells planes that are still alive/relevant to tick.
*/
void tick(float t, float delta_t) {
resetDrawState();
if (MUSIC_LENGTH - t <= 0) {
over = true; // Won!
}
fill(0x33);
textAlign(CENTER, CENTER);
pushMatrix();
translate( width/2, height/2);
scale(4.0);
text("Time remaining: " + secsToTime(round(MUSIC_LENGTH - t)), 0, 0);
translate(0, 20);
fill(#FF0000);
if (proximityAlarmActive && (t % .5) < .25) {
text("PROXIMITY ALARM", 0, 0);
}
translate(0, -40);
fill(#FF8800);
if (fuelAlarmActive && ((t+.25) % 1) < .5) {
text("FUEL ALARM", 0, 0);
}
popMatrix();
if (!over) {
ctr++;
if (ctr == TICKS_BETWEEN_ALARM_CHECKS) {
ctr = 0;
checkAlarms();
}
}
// Draw corner zones
fill(color(0x33, 0x33, 0x33, 0x88));
stroke(0xFF);
strokeWeight(1.0);
beginShape();
/* NW */
vertex(0,0);
vertex(width/4, 0);
vertex(width/4, WALL_THICKNESS);
vertex(WALL_THICKNESS, height/4);
vertex(0, height/4);
endShape(CLOSE);
/* SW */
beginShape();
vertex(0, height);
vertex(width/4, height);
vertex(width/4, height-WALL_THICKNESS);
vertex(WALL_THICKNESS, height-height/4);
vertex(0, height-height/4);
endShape(CLOSE);
/* SE */
beginShape();
vertex(width, height);
vertex(width, height-height/4);
vertex(width-WALL_THICKNESS, height-height/4);
vertex(width-width/4, height-WALL_THICKNESS);
vertex(width-width/4, height);
endShape(CLOSE);
/* NE */
beginShape();
vertex(width, 0);
vertex(width-width/4, 0);
vertex(width-width/4, WALL_THICKNESS);
vertex(width-WALL_THICKNESS, height/4);
vertex(width, height/4);
endShape(CLOSE);
/* Draw sides */
rectMode(CORNERS);
rect(0, height/4, WALL_THICKNESS, height-height/4); // W
rect(width-WALL_THICKNESS, height/4, width, height-height/4); // E
rect(width/4, height-WALL_THICKNESS, width-width/4, height-1); // S
rect(width/4, 0, width-width/4, WALL_THICKNESS); // N
/* Draw labels */
fill(0xFF);
textAlign(CENTER, CENTER);
textx("N", 1.3, width/2, WALL_THICKNESS/2);
textx("S", 1.3, width/2, height-WALL_THICKNESS/2);
textx("W", 1.3, WALL_THICKNESS/2, height/2);
textx("E", 1.3, width-WALL_THICKNESS/2, height/2);
textx("NW", 1.3, width*.1, height*.1);
textx("NE", 1.3, width-width*.1, height*.1);
textx("SW", 1.3, width*.1, height-height*.1);
textx("SE", 1.3, width-width*.1, height-height*.1);
// Draw planes
for (int i = 0; i < planes.length; i++ ) {
if (!over) {
// Update indexOfLastPlane if necessary
if (i > indexOfLastPlane) {
if (times[i] <= t) {
indexOfLastPlane = i;
}
}
}
if (times[i] <= t && planes[i].haveNotReachedDestination) {
planes[i].tick(t, delta_t);
}
}
if (over) {
textAlign(CENTER);
color(0xFF);
textx("GAME OVER", 2.0, width*.5, height*.4);
ellipseMode(CENTER);
strokeWeight(5);
stroke(#FFFF00);
noFill();
// Display failure explanation
if (planeThatRanOut != null) {
textx("RAN OUT OF FUEL", 2.0, width*.5, height*.5);
ellipse(planeThatRanOut.r.x, planeThatRanOut.r.y, 100, 100);
}
else if (crash1 != -1 && crash2 != -1) {
textx("MID-AIR COLLISION", 2.0, width*.5, height*.5);
ellipse(planes[crash1].r.x, planes[crash1].r.y, 80, 80);
}
else {
textx("YOU WON!", 2.0, width*.5, height*.5);
}
textx("Press (Esc) to return to menu.", 2.0, width*.5, height*.6);
}
}
/*
Mouse Modes:
0 = normal, unclicked
1 = left clicked
*/
int mouseMode = 0;
PVector m = new PVector(0,0);
int selectedPlane = -1;
/*
When mouse is released: tell plane to move to face the direction indicated by player.
*/
void released(float x, float y) {
if (selectedPlane != -1) {
Airplane p = planes[selectedPlane];
float a, b;
a = x - p.r.x;
b = -(y - p.r.y);
float theta = thetaFromVector(new PVector(a,b));
//theta = theta % (2*PI);
p.targetTheta = theta;
}
mouseMode = 0;
selectedPlane = -1;
}
/*
*/
void pressed(float x, float y) {
for (int i = 0; i < planes.length; i++ ){
if (planes[i].overlap(x,y)) {
mouseMode = 1;
selectedPlane = i;
return;
}
}
selectedPlane = -1;
mouseMode = 0;
}
void moved(float x, float y) {
m.x = x;
m.y = y;
}
boolean isPlaneSelected(Airplane p) {
if (selectedPlane == -1)
return false;
return planes[selectedPlane] == p;
}
void checkAlarms() {
boolean isDistanceAlarm = false;
boolean isCrashAlarm = false;
boolean isFuelAlarm = false;
int crash1 = -1;
int crash2 = -1;
boolean atLeastOneAlive = false;
for (int i = 0; i <= indexOfLastPlane; i++) {
if (planes[i].haveNotReachedDestination) {
atLeastOneAlive = true;
if (planes[i].fuel < FUEL_ALARM_SECS) {
isFuelAlarm = true;
}
}
for (int j = 0; j <= indexOfLastPlane; j++) {
if (i == j) {
continue;
}
if (!planes[i].haveNotReachedDestination || !planes[j].haveNotReachedDestination) {
continue;
}
float a = planes[i].r.x - planes[j].r.x;
float b = planes[i].r.y - planes[j].r.y;
float r = sqrt(a*a+b*b);
if (r <= DISTANCE_ALARM_DIST) {
isDistanceAlarm = true;
if (r <= CRASH_DIST) {
isCrashAlarm = true;
crash1 = i;
crash2 = j;
}
}
}
if (isCrashAlarm) {
break;
}
}
if(isFuelAlarm) {
loopSound(4);
fuelAlarmActive = true;
}
else {
stopSound(4);
fuelAlarmActive = false;
}
if(isCrashAlarm) {
playSound(2);
stopSound(0);
stopSound(3);
g.over = true;
g.crash1 = crash1;
g.crash2 = crash2;
}
else if(isDistanceAlarm) {
loopSound(3);
proximityAlarmActive = true;
}
else {
stopSound(3);
proximityAlarmActive = false;
if(!atLeastOneAlive) {
over = true;
}
}
}
}
final int REGION_NW = 0;
final int REGION_N = 1;
final int REGION_NE = 2;
final int REGION_E = 3;
final int REGION_SE = 4;
final int REGION_S = 5;
final int REGION_SW = 6;
final int REGION_W = 7;
float[] getRegionBounds(int region) {
float ax, ay, bx, by;