-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplay.agc
1543 lines (1324 loc) · 49.9 KB
/
play.agc
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
/*------------------------------------------------------------------------------*\
Galaga - Arcade game remake
Created by Stefan Wessels.
Copyright (c) 2017 Wessels Consulting Ltd. All rights reserved.
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely.
\*------------------------------------------------------------------------------*/
//------------------------------------------------------------------------------
function CleanupEffects()
done as integer[]
for i = 0 to gRunningEffects.length
dec gRunningEffects[i].ttl#, gDT#
if gRunningEffects[i].ttl# < 0
SetSpriteVisible(gRunningEffects[i].spr, 0)
done.insert(i)
endif
next i
done.sort()
for i = done.length to 0 step -1
gRunningEffects.remove(done[i])
next i
endfunction
//------------------------------------------------------------------------------
function RunBeamAction(enemy ref as Object)
sprite = gSprites[gSpriteOffsets[IMAGE_BEAM]]
select gMakeBeam
// 2 - Position/Show beam
case BEAM_POSITION:
SetSpritePositionByOffset(sprite, enemy.x#, enemy.y#+18.0)
gBeamSize#[0] = GetSpriteX(sprite)
gBeamSize#[1] = GetSpriteY(sprite)
gBeamSize#[2] = gBeamSize#[0] + GetSpriteWidth(sprite)
gBeamSize#[3] = gBeamSize#[1] + 0.1
gBeamSize#[4] = gBeamSize#[1] + GetSpriteHeight(sprite)
SetSpriteScissor(sprite, gBeamSize#[0], gBeamSize#[1], gBeamSize#[2], gBeamSize#[3])
SetSpriteVisible(sprite, 1)
inc gMakeBeam
endcase
// 3 - Open up the beam
case BEAM_OPENING:
inc gBeamSize#[3], cBeamOpenSpeed
if gBeamSize#[3] >= gBeamSize#[4]
inc gMakeBeam
gBeamSize#[3] = gBeamSize#[4]
enemy.timer# = cBeamHoldDuration
endif
SetSpriteScissor(sprite, gBeamSize#[0], gBeamSize#[1], gBeamSize#[2], gBeamSize#[3])
endcase
// 4 - Hold beam open
case BEAM_HOLD:
dec enemy.timer#, gDT#
if gCapture or enemy.timer# < 0.0 then inc gMakeBeam
endcase
// 5 - Close beam down
case BEAM_CLOSING:
dec gBeamSize#[3], cBeamOpenSpeed
if gBeamSize#[3] <= gBeamSize#[1] + 0.1
SetSpriteVisible(sprite, 0)
enemy.dy# = 103.0 - enemy.y#
//enemy.nextPlan = PLAN_GOTO_GRID
enemy.plan = PLAN_DIVE_ATTACK
SetupEnemyVelocityAndRotation(enemy)
gMakeBeam = 0
else
SetSpriteScissor(sprite, gBeamSize#[0], gBeamSize#[1], gBeamSize#[2], gBeamSize#[3])
endif
endcase
endselect
// Make the collision box the size of the beam at this stage
hc# = (gBeamSize#[3] - gBeamSize#[1])
SetSpriteShapeBox(sprite, -GetSpriteOffsetX(sprite), -GetSpriteOffsetY(sprite), GetSpriteWidth(sprite)/2.0, -GetSpriteOffsetY(sprite) + hc#, 0)
if gMakeBeam >= BEAM_OPENING and GetSpriteCollision(sprite, gPlayers[gCurrentPlayer].ships[0].spr)
gCapture = CAPTURE_PLAYER_TOUCHED
endif
endfunction
//------------------------------------------------------------------------------
function AddAttacker(index, position)
for i = index to gAttackers.length
if gAttackers[i] = -1
gAttackers[i] = position
//gEnemies[gCurrentPlayer, position].attackIndex = i
inc gNumAttackers
// exit j loop as a slot was found
exit
endif
next i
endfunction
//------------------------------------------------------------------------------
function HandleCapture()
preR# = gPlayers[gCurrentPlayer].ships[0].r#
if gCapture <= CAPTURE_PLAYER_SPIN
inc gPlayers[gCurrentPlayer].ships[0].r#, cCaptureSpinSpeed * gDT#
SetSpriteAngle(gPlayers[gCurrentPlayer].ships[0].spr, gPlayers[gCurrentPlayer].ships[0].r#+90)
// Normalize the angle
gPlayers[gCurrentPlayer].ships[0].r# = GetSpriteAngle(gPlayers[gCurrentPlayer].ships[0].spr)-90
endif
select gCapture
case CAPTURE_PLAYER_TOUCHED:
beamX# = GetSpriteXByOffset(gSprites[gSpriteOffsets[IMAGE_BEAM]])
offsetX# = GetSpriteXByOffset(gPlayers[gCurrentPlayer].ships[0].spr)
if offsetX# - 1.0 < beamX#
inc offsetX#, cPlayerCaptureSpeed * gDT#
elseif offsetX# + 1.0 > beamX#
dec offsetX#, cPlayerCaptureSpeed * gDT#
else
offsetX# = beamX#
endif
gPlayers[gCurrentPlayer].ships[0].x# = offsetX# - GetSpriteOffsetX(gPlayers[gCurrentPlayer].ships[0].spr)
if gPlayers[gCurrentPlayer].ships[0].y# > 70.0
dec gPlayers[gCurrentPlayer].ships[0].y#, cPlayerCaptureSpeed * gDT#
else
inc gCapture
endif
SetSpritePositionByOffset(gPlayers[gCurrentPlayer].ships[0].spr, offsetX#, gPlayers[gCurrentPlayer].ships[0].y#)
endcase
case CAPTURE_PLAYER_DISPLAY:
SetTextVisible(gText[TEXT_FIGHTER_CAPTURED], 1)
// TODO: Get the sound and add it to project and then this line
//PlaySound(gSounds[SOUND_CAPTURE_PLAYER))
gRotationCount# = 0.0
inc gCapture
endcase
case CAPTURE_PLAYER_SPIN:
inc gRotationCount#, cCaptureSpinSpeed * gDT#
if gRotationCount# >= 720.0 and (GetSpriteAngle(gPlayers[gCurrentPlayer].ships[0].spr)-90) - preR# < 0
gPlayers[gCurrentPlayer].ships[0].r# = 0.0
SetSpriteAngle(gPlayers[gCurrentPlayer].ships[0].spr, gPlayers[gCurrentPlayer].ships[0].r#)
gPlayers[gCurrentPlayer].ships[0].timer# = 1.0
inc gCapture
endif
endcase
case CAPTURE_PLAYER_HOLD:
dec gPlayers[gCurrentPlayer].ships[0].timer#, gDT#
if gPlayers[gCurrentPlayer].ships[0].timer# < 0.0
SetTextVisible(gText[TEXT_FIGHTER_CAPTURED], 0)
inc gCapture
endif
endcase
case CAPTURE_PLAYER_HOME:
// TODO: Fix the capture so it's a real capture
gPlayers[gCurrentPlayer].ships[0].y# = 91.0
SetSpritePosition(gPlayers[gCurrentPlayer].ships[0].spr, gPlayers[gCurrentPlayer].ships[0].x#, gPlayers[gCurrentPlayer].ships[0].y#)
// TODO: Hacked in but causes all kinds of grief - Fix this
// issues - stages don't tick over upon death; boss will still beam when there are 2 players.
gPlayers[gCurrentPlayer].ships[1].plan = PLAN_ALIVE
SetSpriteVisible(gPlayers[gCurrentPlayer].ships[1].spr,1)
gCapture = 0
endcase
endselect
endfunction
//------------------------------------------------------------------------------
function HandlePlayer()
// Flash the label for the player that's up
inc gFlashTimer#, gDT#
if gFlashTimer# >= cFlashTime
index = gText[gFlashIndex[gCurrentPlayer]]
SetTextVisible(index, 1-GetTextVisible(index))
gFlashTimer# = 0.0
endif
// if not a or not b then c will evaluate both a and b in AGK, even if a is false
if not gPlayers[gCurrentPlayer].ships[0].spr then exitFunction
if not GetSpriteVisible(gPlayers[gCurrentPlayer].ships[0].spr) then exitFunction
// if the player is being captured
if gCapture
HandleCapture()
else
// Handle direction input
if gDirection
if gPlayers[gCurrentPlayer].ships[0].plan && PLAN_ALIVE
newx# = gPlayers[gCurrentPlayer].ships[0].x#
if gPlayers[gCurrentPlayer].ships[1].plan && PLAN_ALIVE
width# = GetSpriteWidth(gPlayers[gCurrentPlayer].ships[1].spr)
else
width# = 0
endif
inc newx#, gDirection * cPlayerMovementSpeed * gDT#
offsetX# = GetSpriteOffsetX(gPlayers[gCurrentPlayer].ships[0].spr)
if newx# - offsetX# < 0
newx# = offsetX#
elseif newx# + offsetX# >= 100.0 - width#
newx# = 100.0 - width# - offsetX#
endif
gPlayers[gCurrentPlayer].ships[0].x# = newx#
SetSpritePositionByOffset(gPlayers[gCurrentPlayer].ships[0].spr, newx#, gPlayers[gCurrentPlayer].ships[0].y#)
if gPlayers[gCurrentPlayer].ships[1].plan = PLAN_ALIVE
gPlayers[gCurrentPlayer].ships[1].x# = newx# + width#
SetSpritePositionByOffset(gPlayers[gCurrentPlayer].ships[1].spr, gPlayers[gCurrentPlayer].ships[1].x#, gPlayers[gCurrentPlayer].ships[1].y#)
endif
endif
endif
endif
// don't allow firing before play starts
if gSubState < cPSPlay then exitFunction
// Fire if not 2 bullets/ship onscreen already
if gFire
if not gBullets[0].plan
bpointIndex = 0
elseif not gBullets[1].plan
bpointIndex = 1
else
bpointIndex = -1
endif
if bpointIndex >= 0
inc gPlayers[gCurrentPlayer].shotsFired
PlaySound(gSounds[SOUND_PLAYER_SHOOT])
sprite = gBullets[bpointIndex].spr
gBullets[bpointIndex].plan = PLAN_ALIVE
gBullets[bpointIndex].x# = gPlayers[gCurrentPlayer].ships[0].x# - (GetSpriteOffsetX(sprite)/2.0)
gBullets[bpointIndex].y# = gPlayers[gCurrentPlayer].ships[0].y#
SetSpritePositionByOffset(sprite, gBullets[bpointIndex].x#, gBullets[bpointIndex].y#)
SetSpriteVisible(sprite, 1)
if gPlayers[gCurrentPlayer].ships[1].plan = PLAN_ALIVE
inc gPlayers[gCurrentPlayer].shotsFired
inc bpointIndex, 2
sprite = gBullets[bpointIndex].spr
gBullets[bpointIndex].plan = PLAN_ALIVE
gBullets[bpointIndex].x# = gBullets[bpointIndex-2].x# + GetSpriteWidth(gPlayers[gCurrentPlayer].ships[0].spr)
gBullets[bpointIndex].y# = gBullets[bpointIndex-2].y#
SetSpritePositionByOffset(sprite, gBullets[bpointIndex].x#, gBullets[bpointIndex].y#)
SetSpriteVisible(sprite, 1)
endif
endif
endif
endfunction
//------------------------------------------------------------------------------
function EnemyFireAShot(enemy ref as Object)
if gBullets[gBulletIndex].plan = PLAN_DEAD
gBullets[gBulletIndex].plan = PLAN_ALIVE
gBullets[gBulletIndex].x# = enemy.x#
gBullets[gBulletIndex].y# = enemy.y#
dx# = enemy.x# - gPlayers[gCurrentPlayer].ships[0].x#
dy# = enemy.y# - gPlayers[gCurrentPlayer].ships[0].y#
// Limit the vertical "slide" of the bullets. This is really
// tan(angle)*y but tan(45) = 1 so it reduces nicely
//Clamp(dy#, -dy#, dx#)
if dx# > -dy# then dx# = -dy#
if dx# < dy# then dx# = dy#
gBullets[gBulletIndex].distance# = sqrt(dx#^2 + dy#^2)
r# = atan2(dy#, dx#)
// the velocity along the vectors
gBullets[gBulletIndex].vx# = cBulletSpeedEnemy * cos(r#)
gBullets[gBulletIndex].vy# = cBulletSpeedEnemy * sin(r#)
gBullets[gBulletIndex].dx# = dx#
gBullets[gBulletIndex].dy# = dy#
SetSpritePositionByOffset(gBullets[gBulletIndex].spr, gBullets[gBulletIndex].x#, gBullets[gBulletIndex].y#)
SetSpriteVisible(gBullets[gBulletIndex].spr, 1)
inc gBulletIndex
if gBulletIndex >= cMaxBullets then gBulletIndex = gSpriteNumbers[IMAGE_BLUE_BULLET]
endif
endfunction
//------------------------------------------------------------------------------
function MoveBullets()
for i = 0 to cMaxBullets
if gBullets[i].plan = PLAN_ALIVE
sprite = gBullets[i].spr
if i < gSpriteNumbers[IMAGE_BLUE_BULLET]
// TODO: Player bullets only travvel in astraight line but
// when the player is being beamed, the bullets can go at an
// angle - add support for that
dec gBullets[i].y#, cBulletSpeed * gDT#
if(gBullets[i].y# < (1/cOriginalYCellsF)*100.0)
gBullets[i].plan = PLAN_DEAD
SetSpriteVisible(sprite, 0)
endif
else
tx# = gDT# * gBullets[i].vx#
ty# = gDT# * gBullets[i].vy#
// Update the position
dec gBullets[i].x#, tx#
dec gBullets[i].y#, ty#
if(gBullets[i].y# > ((cOriginalYCellsF-2)/cOriginalYCellsF)*100.0)
gBullets[i].plan = PLAN_DEAD
SetSpriteVisible(sprite, 0)
else
if gPlayers[gCurrentPlayer].ships[0].plan = PLAN_ALIVE and GetSpriteCollision(sprite, gPlayers[gCurrentPlayer].ships[0].spr)
KillPlayer(gPlayers[gCurrentPlayer].ships[0])
gBullets[i].plan = PLAN_DEAD
SetSpriteVisible(sprite, 0)
continue
endif
if gPlayers[gCurrentPlayer].ships[1].plan = PLAN_ALIVE
if GetSpriteCollision(gBullets[i].spr, gPlayers[gCurrentPlayer].ships[1].spr)
KillPlayer(gPlayers[gCurrentPlayer].ships[1])
gBullets[i].plan = PLAN_DEAD
SetSpriteVisible(sprite, 0)
elseif gPlayers[gCurrentPlayer].ships[0].plan = PLAN_DEAD
// if player 1 is alive and 0 dead, assign 1 to 0 so 0 is always the 1 that's alive
gPlayers[gCurrentPlayer].ships[0] = gPlayers[gCurrentPlayer].ships[1]
gPlayers[gCurrentPlayer].ships[1].plan = PLAN_DEAD
endif
endif
endif
endif
SetSpritePositionByOffset(sprite, gBullets[i].x#, gBullets[i].y#)
endif
next i
endfunction
//------------------------------------------------------------------------------
function IncrementScore(byValue as integer)
oldScore = gPlayers[gCurrentPlayer].score
newScore = oldScore + byValue
// under 1M points, give life at 20k and every 70k
if newScore < 1000000
if oldScore < 20000 and newScore >= 20000 or (oldScore / 70000) < (newScore / 70000)
inc gPlayers[gCurrentPlayer].lives
ShowLivesIcons()
// TODO - Play the sound for extra life here
endif
endif
gPlayers[gCurrentPlayer].score = newScore
SetTextString(gText[gScoreIndex[gCurrentPlayer]], FormatValue(gPlayers[gCurrentPlayer].score))
if gPlayers[gCurrentPlayer].score > gHighScores[0].score
SetTextString(gText[TEXT_20000], FormatValue(gPlayers[gCurrentPlayer].score))
endif
endfunction
//------------------------------------------------------------------------------
function KillPlayer(player ref as Object)
player.plan = PLAN_DEAD
PlaySound(gSounds[SOUND_PLAYER_DIE])
// Hide the player ship
SetSpriteVisible(player.spr, 0)
// Stop the StarField
gStarSpeed# = 0.0
// Play an explosion
sprite = gSprites[gSpriteOffsets[IMAGE_PLAYER_EXPLOSION]]
SetSpritePositionByOffset(sprite, player.x#, player.y#)
SetSpriteVisible(sprite, 1)
PlaySprite(sprite, cPlayerExplosionFPS, 0)
// Add explosion to effects lidt for cleanup
effect as TypeStar
effect.spr = sprite
effect.ttl# = gSpriteAnimFrames[IMAGE_PLAYER_EXPLOSION] * (1.0/(cPlayerExplosionFPS-1))
gRunningEffects.Insert(effect)
SetSpriteVisible(player.spr, 0)
endfunction
//------------------------------------------------------------------------------
function KillEnemy(enemy ref as Object)
// Kill the enemy
SetSpriteVisible(enemy.spr, 0)
inc gEnemiesKilledThisStage
inc gPlayers[gCurrentPlayer].hits
dec gPlayers[gCurrentPlayer].enemiesAlive
kind = enemy.isa
// TODO: Make sure the beam can just disappear - this may need
// to be fixed so the beam retracts when the enemy dies
if enemy.plan = PLAN_BEAM_ACTION
SetSpriteVisible(gSprites[gSpriteOffsets[IMAGE_BEAM]], 0)
gMakebeam = BEAM_OFF
endif
enemy.plan = PLAN_DEAD
PlaySound(gSounds[gKillSound[kind]])
// Standing in grid gives one score, flying another
if enemy.plan = PLAN_GRID
IncrementScore(gScoreSheet[0, kind])
else
IncrementScore(gScoreSheet[1, kind])
endif
// If on an attack run, take it off
if enemy.attackIndex >= 0
gAttackers[enemy.attackIndex] = -1
dec gNumAttackers
endif
// Play an explosion
sprite = gSprites[gSpriteOffsets[IMAGE_EXPLOSION]+gNextExplosion]
inc gNextExplosion
if gNextExplosion = gSpriteNumbers[IMAGE_EXPLOSION] then gNextExplosion = 0
SetSpritePositionByOffset(sprite, enemy.x#, enemy.y#)
SetSpriteVisible(sprite, 1)
PlaySprite(sprite, cEnemyExplosionFPS, 0)
// Add explosion to effects list for cleanup
effect as TypeStar
effect.spr = sprite
effect.ttl# = gSpriteAnimFrames[IMAGE_EXPLOSION] * (1.0/cEnemyExplosionFPS)
gRunningEffects.Insert(effect)
endfunction
//------------------------------------------------------------------------------
function CheckEnemyDead(enemy ref as Object)
// Check if enemy was shot
for j = 0 to 3
if gBullets[j].plan = PLAN_ALIVE
if GetSpriteCollision(enemy.spr, gBullets[j].spr)
gBullets[j].plan = PLAN_DEAD
SetSpriteVisible(gBullets[j].spr, 0)
// Green Commanders become blue, they don't die yet
if enemy.isa = IMAGE_BOSS_GREEN
// Hide the green
SetSpriteVisible(enemy.spr, 0)
enemy.isa = IMAGE_BOSS_BLUE
// enable the blue
enemy.spr = gSprites[gSpriteOffsets[IMAGE_BOSS_BLUE]+gSpritesUsed[gCurrentPlayer, IMAGE_BOSS_BLUE]]
inc gSpritesUsed[gCurrentPlayer, IMAGE_BOSS_BLUE]
SetSpritePositionByOffset(enemy.spr, enemy.x#, enemy.y#)
SetSpriteAngle(enemy.spr, enemy.r#)
SetSpriteVisible(enemy.spr, 1)
PlaySound(gSounds[SOUND_HIT_BOSS_GREEN])
else
KillEnemy(enemy)
endif
// Don't check other bullets, this enemy is dead
exitFunction
endif
endif
next j
// Check for a collision with the player if not on a spawn path - on a spawn path doesn't collide with the player
if not (gPlayers[gCurrentPlayer].spawnActive or gCapture)
if gPlayers[gCurrentPlayer].ships[0].plan = PLAN_ALIVE and GetSpriteCollision(enemy.spr, gPlayers[gCurrentPlayer].ships[0].spr)
KillEnemy(enemy)
KillPlayer(gPlayers[gCurrentPlayer].ships[0])
endif
if gPlayers[gCurrentPlayer].ships[1].plan = PLAN_ALIVE and GetSpriteCollision(enemy.spr, gPlayers[gCurrentPlayer].ships[1].spr)
KillEnemy(enemy)
KillPlayer(gPlayers[gCurrentPlayer].ships[1])
endif
endif
endfunction
//------------------------------------------------------------------------------
function SetupEnemyVelocityAndRotation(enemy ref as Object)
// waves come in at the same velocity but attacks speed up
if not gBugsAttack
speed# = cBugTravelSpeed
else
speed# = gBugAttackSpeed#
endif
// larger circles require higer velocity to come out shoulder to shoulder with the smaller
// inner circles
if enemy.plan = PLAN_PATH
index = enemy.pathIndex >> 1
if index = aPath_Bottom_Double_Out
if enemy.pointIndex > 4 then speed# = speed# * 1.625 else speed# = speed# * 1.1
elseif index = aPath_Top_Double_Left
if enemy.pointIndex > 4 then speed# = speed# * 1.625
endif
endif
// How fat to travel and at what angle
enemy.distance# = sqrt(enemy.dx#^2 + enemy.dy#^2)
r# = atan2(enemy.dy#, enemy.dx#)
// set the enemy rotation only if it's not about to land in the grid and is not in the grid.
// That's because the grid causes left/right motion that leads to extreme rotation which
// this ignores
if enemy.plan < PLAN_ORIENT or enemy.plan > PLAN_GRID
// if the enemy is about to land in the grid, don't change its angle as that looks bad
if not (abs(enemy.dy#) < 3.0 and enemy.plan = PLAN_GOTO_GRID) then enemy.r# = 90 + r#
endif
// the velocity along the vectors
enemy.vx# = speed# * cos(r#)
enemy.vy# = speed# * sin(r#)
// point the sprite at the desired direction
SetSpriteAngle(enemy.spr, enemy.r#)
endfunction
//------------------------------------------------------------------------------
function GetGridCoordinate(enemy ref as Object)
col = gGridCols[enemy.positionIndex]
row = gGridRows[enemy.positionIndex]
// add an offset for breathing or left-right walking
if gPlayers[gCurrentPlayer].gridBreathing
breatheX# = 1.418 * gPlayers[gCurrentPlayer].gridTimer#
breatheY# = 0.675 * gPlayers[gCurrentPlayer].gridTimer#
// these "magic numbers" are calculated. For example:
// 10 bees wide is cols 0-9. 9 * 6.7 = 60.3
// 100% - 60.3% / 2 = 19.85 - that's the "left edge"
nx# = 19.85 + (col * 6.7) + (col-5)*BreatheX#
ny# = 15.25 + (row * 4.2) + (row+1)*BreatheY#
else
// 19.85 / 2 = 9.925
walkX# = 9.925 * gPlayers[gCurrentPlayer].gridTimer#
nx# = (col * 6.7) + walkX#
ny# = 15.25 + (row * 4.2)
endif
// The non-commanders are another 1.6% down from the commanders
if row then inc ny#, 1.6
inc ny#, gPlayers[gCurrentPlayer].gridYOffset#
enemy.dx# = nx# - enemy.x#
enemy.dy# = ny# - enemy.y#
SetupEnemyVelocityAndRotation(enemy)
endfunction
//------------------------------------------------------------------------------
function NextPathPoint(enemy ref as Object)
index = enemy.pathIndex >> 1
enemy.dx# = gPathDataX[index, enemy.pointIndex]
enemy.dy# = gPathDataY[index, enemy.pointIndex]
// Odd paths are mirrored
if enemy.pathIndex && 1 then enemy.dx# = -enemy.dx#
SetupEnemyVelocityAndRotation(enemy)
endfunction
//------------------------------------------------------------------------------
function SetupFlutterArc(enemy ref as Object)
// save the current facing angle
r# = enemy.r# - 90.0
sign# = gMirror[enemy.positionIndex]
// Get the angle to the player
dx# = gPlayers[gCurrentPlayer].ships[0].x# - enemy.x#
// Figure out the flutter pattern (zig/zag/zag)
if enemy.y# < 65.0
dy# = 20.0
if sign# = 0
dx# = clamp(-80.0, -15.0, dx#)
else
dx# = clamp(15.0, 80.0, dx#)
endif
else
dy# = 15.0
if sign# = 0
dx# = clamp(15.0, 25.0, dx#)
else
dx# = clamp(-25.0, -15.0, dx#)
endif
endif
// angle to the destination
enemy.dr# = atan2(dy#, dx#)
// rate of turn (increment angle)
enemy.ir# = (enemy.dr# - r#) * 3.0
// distance to travel (as a line, not on the curve)
enemy.dx# = dx#
enemy.dy# = dy#
SetupEnemyVelocityAndRotation(enemy)
// Restore the sprite angle that was changed in SetupEnemyVelocityAndRotation
inc r#, 90.0
enemy.r# = r#
SetSpriteAngle(enemy.spr, r#)
endfunction
//------------------------------------------------------------------------------
function DecisionOnPostPath(enemy ref as Object)
// if just launched, set up to shoot, if not a peel-away
if enemy.positionIndex < 40
if enemy.pathIndex >> 1 = aPath_Launch
enemy.timer# = 0.3
enemy.shotsToFire = 1
endif
endif
// Install the nextPlan as the plan
enemy.plan = enemy.nextPlan
// Do some setup for the plan that follows completing a path
select enemy.plan
// TODO: - The DIVE_AWAY guys also have PLAN_PATH as the nextPath - Introduce a new state
case PLAN_PATH:
// Set enemy on an arc to face the bottom of the screen
if enemy.isa = IMAGE_BEE
enemy.pathIndex = PATH_BEE_ATTACK + gMirror[enemy.positionIndex]
enemy.nextPlan = PLAN_DESCEND
else
enemy.pathIndex = PATH_BUTTERFLY_ATTACK + gMirror[enemy.positionIndex]
enemy.nextPlan = PLAN_FLUTTER
endif
NextPathPoint(enemy)
endcase
// After the sweep, get to a height where the circle back starts.
// Need a seperate state because the bees in rows start at difefrent heights so can't make a path
// that goes down includes the half-circle arc
case PLAN_DESCEND:
// Just barely get a wing out the bottom of the screen
enemy.dx# = 0
enemy.dy# = 81.0 - enemy.y#
SetupEnemyVelocityAndRotation(enemy)
endcase
// after bottom circle, bee must decide to go home or make a full cirlce
case PLAN_HOME_OR_FULL_CIRCLE:
if gPlayers[gCurrentPlayer].enemiesAlive < 6 and gPlayers[gCurrentPlayer].ships[0].plan = PLAN_ALIVE
enemy.plan = PLAN_PATH
enemy.pathIndex = PATH_BEE_TOP_CIRCLE + 1-gMirror[enemy.positionIndex]
NextPathPoint(enemy)
// if full circle, afterwards dive out the bottom
enemy.nextPlan = PLAN_DIVE_ATTACK
else
// Half cirlce was enough, go home
enemy.plan = PLAN_GOTO_GRID
endif
endcase
// Calculate the flutter arc
case PLAN_FLUTTER:
SetupFlutterArc(enemy)
endcase
// Start an attack dive that ends with the enemy disappearing off the bottom
case PLAN_DIVE_AWAY_LAUNCH:
enemy.plan = PLAN_PATH
enemy.nextPlan = PLAN_DIVE_AWAY
enemy.pathIndex = PATH_LAUNCH + gMirror[enemy.positionIndex-36]
NextPathPoint(enemy)
endcase
// enemy has launched into the dive away, now set the destination and carry on
// At end, the enemy will simply disapopear (die)
case PLAN_DIVE_AWAY:
// Pick a point to dive at - a 20% range around the player
left = gPlayers[gCurrentPlayer].ships[0].x# - 10.0
right = gPlayers[gCurrentPlayer].ships[0].x# + 10.0
Clamp(10, 90, left)
Clamp(10, 90, right)
enemy.dx# = Random(left,right) - enemy.x#
enemy.dy# = 103.0 - enemy.y#
SetupEnemyVelocityAndRotation(enemy)
endcase
case PLAN_DIVE_ATTACK:
// Send it out the screen
enemy.dy# = 103.0 - enemy.y#
enemy.nextPlan = PLAN_GOTO_GRID
SetupEnemyVelocityAndRotation(enemy)
endcase
case PLAN_GOTO_BEAM:
enemy.dx# = gPlayers[gCurrentPlayer].ships[0].x# - enemy.x#
enemy.dy# = 62.0 - GetSpriteYByOffset(enemy.spr)
SetupEnemyVelocityAndRotation(enemy)
endcase
// This is used by the Challange stages
case PLAN_DEAD:
dec gPlayers[gCurrentPlayer].enemiesAlive
SetSpriteVisible(enemy.spr, 0)
endcase
endselect
endfunction
//------------------------------------------------------------------------------
function DecisionTime(enemy ref as Object)
select enemy.plan
// Reached the grid
case PLAN_GOTO_GRID:
if gPlayers[gCurrentPlayer].ships[0].plan = PLAN_ALIVE and not gPlayers[gCurrentPlayer].spawnActive and gPlayers[gCurrentPlayer].enemiesAlive < 5
// keep attacking straight away
// TODO - I think this is actually right at the top of the screen, not here at the grid level
enemy.plan = PLAN_PATH
if enemy.isa = IMAGE_BEE
enemy.pathIndex = PATH_BEE_ATTACK + gMirror[enemy.positionIndex]
enemy.nextPlan = PLAN_DESCEND
else
enemy.pathIndex = PATH_BUTTERFLY_ATTACK + gMirror[enemy.positionIndex]
enemy.nextPlan = PLAN_FLUTTER
endif
NextPathPoint(enemy)
else
// rotate into place
enemy.plan = PLAN_ORIENT
endif
endcase
// keep rotating till zero facing, then just be in the grid
case PLAN_ORIENT:
// Take out of attack mode when arriving in the grid
if enemy.attackIndex >= 0
gAttackers[enemy.attackIndex] = -1
dec gNumAttackers
enemy.attackIndex = -1
endif
if enemy.r# < -cGridOrientAngle
inc enemy.r#, cGridOrientAngle
elseif enemy.r# > cGridOrientAngle
dec enemy.r#, cGridOrientAngle
else
enemy.r# = 0.0
enemy.plan = PLAN_GRID
endif
endcase
// end of a point in the path, maybe the end of the path
case PLAN_PATH:
inc enemy.pointIndex
// go to next path point unless this was the last, then
// go to the next plan in DecisionOnPostPath
if enemy.pointIndex <= gPathDataX[enemy.pathIndex>>1].length
NextPathPoint(enemy)
else
enemy.pointIndex = 0
// Activate nextPlan and set up paramaters to run that plan
DecisionOnPostPath(enemy)
endif
endcase
// After descend, start the bototm half circle path
case PLAN_DESCEND:
enemy.plan = PLAN_PATH
enemy.pathIndex = PATH_BEE_BOTTOM_CIRCLE + gMirror[enemy.positionIndex]
NextPathPoint(enemy)
// Set up for deciding what to do at the end of that bottom circle
enemy.nextPlan = PLAN_HOME_OR_FULL_CIRCLE
endcase
case PLAN_FLUTTER:
if enemy.y# < 100.0
SetupFlutterArc(enemy)
else
enemy.plan = PLAN_DIVE_ATTACK
// TODO - Figure out a better answer to the dx# on a butterfly leaving
enemy.dx# = 0
enemy.dy# = 3.0
enemy.nextPlan = PLAN_GOTO_GRID
SetupEnemyVelocityAndRotation(enemy)
endif
endcase
// Out the bottom so go away
case PLAN_DIVE_AWAY
enemy.plan = PLAN_DEAD
dec gPlayers[gCurrentPlayer].enemiesAlive
SetSpriteVisible(enemy.spr, 0)
endcase
case PLAN_DIVE_ATTACK
enemy.plan = PLAN_GOTO_GRID
enemy.y# = 0.0
enemy.x# = gStartPathX[aPath_Top_Double_Right]
if not gMirror[enemy.positionIndex] then enemy.x# = 100.0 - enemy.x#
endcase
case PLAN_GOTO_BEAM:
enemy.r# = 90
SetSpriteAngle(enemy.spr, enemy.r#+90)
enemy.plan = PLAN_BEAM_ACTION
endcase
endselect
endfunction
//------------------------------------------------------------------------------
function UpdateEnemy(enemy ref as Object)
// Anything that moves means it's not all quiet
if enemy.plan <> PLAN_GRID then gQuiescence = 0
if enemy.plan = PLAN_BEAM_ACTION
RunBeamAction(enemy)
// while in beam state there's no movement
exitFunction
endif
// TODO - Give GetGridCoordinate it's own update because running it through here is a waste
// for grid (or grid-bound) enemies, call every frame as the next position keeps changing
if enemy.plan >= PLAN_GOTO_GRID and enemy.plan <= PLAN_GRID
GetGridCoordinate(enemy)
endif
// Calculate how much to move this frame in each axis
tx# = gDT# * enemy.vx#
ty# = gDT# * enemy.vy#
// figure out what that is along the vector
dist# = sqrt(tx#^2+ty#^2)
// Butterflies flutter on an ARC
if enemy.plan = PLAN_FLUTTER
// get the current angle
r# = enemy.r# - 90
// save the angle
ro# = r#
// adjust for the increment
inc r#, enemy.ir# * gDT#
// see if desired angle reached and stop changing
if (r# - ro#) * (enemy.dr# - ro#) < 0.0 then enemy.ir# = 0.0
// get the x/y step sizes
tx# = gDT# * gBugAttackSpeed# * cos(r#)
ty# = gDT# * gBugAttackSpeed# * sin(r#)
// set the facing angle in the Object and the sprite
enemy.r# = r#+90
SetSpriteAngle(enemy.spr, enemy.r#)
else
// See how far along the path, and clip to not overshoot
if dist# > enemy.distance#
tx# = enemy.dx#
ty# = enemy.dy#
endif
endif
// Updat the position
inc enemy.x#, tx#
inc enemy.y#, ty#
// update the distance to travel components
dec enemy.dx#, tx#
dec enemy.dy#, ty#
dec enemy.distance#, dist#
// If at the end of travel figure out what the plan should be
if enemy.plan <> PLAN_GRID and enemy.distance# <= 0.0
DecisionTime(enemy)
endif
// Let the enemy shoot if it needs to
if enemy.timer# > 0.0
dec enemy.timer#, gDT#
if enemy.timer# <= 0.0
if enemy.shotsToFire
EnemyFireAShot(enemy)
dec enemy.shotsToFire
if enemy.shotsToFire then enemy.timer# = cEnemyGunReloadTime
endif
endif
endif
// Put the enemy where it wants to go
SetSpritePositionByOffset(enemy.spr, enemy.x#, enemy.y#)
endfunction
//------------------------------------------------------------------------------
function UpdateGridOffsets()
// Breathe in-out
oldTimer# = gPlayers[gCurrentPlayer].gridTimer#
inc gPlayers[gCurrentPlayer].gridTimer#, gDT# * gPlayers[gCurrentPlayer].gridDir#
if gPlayers[gCurrentPlayer].gridTimer# >= gGridTime[gPlayers[gCurrentPlayer].gridBreathing] or gPlayers[gCurrentPlayer].gridTimer# <= 0.0
// At extets, flip direction
gPlayers[gCurrentPlayer].gridDir# = -gPlayers[gCurrentPlayer].gridDir#
// but step back into range so there's no chance of getting
// stuck outside the range on a smaller gDT# next frame
inc gPlayers[gCurrentPlayer].gridTimer#, gDT# * gPlayers[gCurrentPlayer].gridDir#
endif
// After walking left/right, activate breathing when the grid is in the middle
if not gPlayers[gCurrentPlayer].gridBreathing
if not gPlayers[gCurrentPlayer].spawnActive
middle# = (gGridTime[0] / 2.0)
s1# = middle# - oldTimer#
s2# = middle# - gPlayers[gCurrentPlayer].gridTimer#
// if this is < 0 the signs are not the same meaning the gris crossed just over the middle
if s1# * s2# < 0
gPlayers[gCurrentPlayer].gridBreathing = 1
gPlayers[gCurrentPlayer].gridTimer# = 0.0
gPlayers[gCurrentPlayer].gridDir# = 1.0
endif
endif
endif
endfunction
//------------------------------------------------------------------------------
function UpdateEnemies()
// The grid keeps moving
UpdateGridOffsets()
// Start by assuming the enemies are all standing in the grid
gQuiescence = 1
// TODO: Rename this variable
// Assume there are no active attackers on bombing runs
for i = 0 to cMaxEnemies
// if not dead, the enemy must be updated
if gEnemies[gCurrentPlayer, i].plan
UpdateEnemy(gEnemies[gCurrentPlayer, i])
// collide enemy against player and its bullets
CheckEnemyDead(gEnemies[gCurrentPlayer, i])
// count the enemies on bombing runs
plan = gEnemies[gCurrentPlayer, i].plan
endif
next i
endfunction
//------------------------------------------------------------------------------
function AttackReady()
// Can't attack while still spawning
if gPlayers[gCurrentPlayer].spawnActive then exitFunction(0)
// also wait for the waves to all make it to the grid
if not gQuiescence then exitFunction(0)
// prime the attack structure with 2 attackers, not bosses, hence start looking from 4
gNumAttackers = 0
for i = 0 to gAttackOrder.length
if gNumAttackers < 2
position = gAttackOrder[i]
if gEnemies[gCurrentPlayer, position].plan = PLAN_GRID
gAttackers[gNumAttackers] = position
inc gNumAttackers
endif
endif
next i
// mark unsed slots as such
for i = gNumAttackers to gAttackers.length
gAttackers[i] = -1
next i
endfunction(1)
//------------------------------------------------------------------------------
function ChooseAttacker()