-
Notifications
You must be signed in to change notification settings - Fork 34
/
defenders.lua
1089 lines (999 loc) · 29.2 KB
/
defenders.lua
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
win = am.window{
title = "Defenders of the Weeping Quasar",
width = 640, height = 480,
}
-- constants
ship_start_pos = vec2(0, -100)
ship_min_position = vec2(-310, -230)
ship_max_position = -ship_min_position
ship_speed = 300
ship_radius = 5
laser_velocity = vec2(0, 600)
laser_interval = 0.1
-- sounds
laser_sound = 86572901
explode_sound_slug = 54429702
explode_sound_tank = 71980902
explode_sound_worm = 6816502--64989002
explode_sound_boss = 83275102
spawn_sound_slug = 90404206
spawn_sound_tank = 48219003
spawn_sound_worm = 56680800
spawn_sound_boss = 42319309
ship_explode_sound = 80020902
ship_explode_sound2 = 11085802
hit_sound = 9975402
boss_hit_sound = 5734004
boss_sound = 31642009
boss_shoot_sound = 90380302
start_sound = 24012402
title_sound = 35270309
-- global state
global_state = am.load_state("defenders_state") or {
high_score = 0
}
-- utility functions
function animated_sprite(frames, delay)
local frame = 1
local t = am.frame_time
return am.sprite(frames[1]):action(function(sprite)
if am.frame_time - t >= delay then
frame = frame % #frames + 1
sprite.source = frames[frame]
t = am.frame_time
end
end)
end
function shake_screen(state, amount, duration)
local last_shake_t = 0
local start_t = am.frame_time
state.camera:action("shake", function(scene)
if am.frame_time - start_t > duration then
scene.position2d = vec2(0)
return true
end
if am.frame_time - last_shake_t > 0.05 then
scene.position2d = math.randvec2() * amount
last_shake_t = am.frame_time
end
end)
end
-- explosions
explosion_img = [[
...
.W.
...]]
function spawn_explosion(state, position, size, color, color_var, sound)
local explosion_particles =
am.blend("add")
^ am.particles2d{
sprite_source = explosion_img,
source_pos = position,
source_pos_var = vec2(5, 5) * size,
max_particles = 200 * size,
start_particles = 200 * size,
emission_rate = 0,
life = 0.2,
life_var = 0.5,
angle_var = math.rad(180),
speed = 200 * size,
speed_var = 50 * size,
start_color = color,
start_color_var = color_var,
start_size = 30,
start_size_var = 10,
end_size = 10,
end_size_var = 5,
warmup_time = 0.05,
}
local radius = size * 20 + math.random() * 10
local circle1 = am.circle(position, radius, vec4(1))
local circle2 = am.circle(position - vec2(radius * 0.2, 0), radius * 0.8, vec4(0, 0, 0, 1))
local flash = am.rect(
win.left - 50, win.bottom - 50, win.right + 50, win.top + 50,
color * vec4(0.25, 0.25, 0.25, 1))
state.explosions_group:append(explosion_particles)
state.explosions_group:append(circle1)
state.scene:action(am.series{
am.delay(0.1),
function()
state.explosions_flash_group:append(flash)
circle1.color = color + vec4(0.5)
state.explosions_group:append(circle2)
return true
end,
am.delay(0.1),
function()
state.explosions_flash_group:remove(flash)
state.explosions_group:remove(circle1)
state.explosions_group:remove(circle2)
return true
end,
function()
if explosion_particles"particles2d".active_particles == 0 then
state.explosions_group:remove(explosion_particles)
return true
end
end
})
local pitch = math.random() * 0.5 + 0.75
main_scene:action(am.play(sound, false, pitch))
shake_screen(state, size * 8, size * 0.1)
end
-- slug enemy
slug_frame1 = [[
GGGGGGGG
GggggggG
GgGGGGgG
GgGMMGgG
GgGMMGgG
GgGGGGgG
GggggggG
GGGGGGGG
]]
slug_frame2 = [[
gggggggg
gGGGGGGg
gGggggGg
gGgmmgGg
gGgmmgGg
gGggggGg
gGGGGGGg
gggggggg
]]
slug_hit_frame = slug_frame1:gsub("[^%.%s]", "W")
slug_sprite = animated_sprite({slug_frame1, slug_frame2}, 0.5)
slug_hit_sprite = am.sprite(slug_hit_frame)
function slug_update(enemy, state)
local dt = am.delta_time
local pos = enemy.position
local t = am.frame_time - enemy.t
local vx = math.sin(t*3) * 150
local vy = -80
pos = pos + vec2(vx, vy) * dt
enemy.position = pos
enemy.node.position2d = pos
if am.frame_time > enemy.last_fire_time + 1.1 then
local vel = math.normalize(state.ship.position - pos) * 150
spawn_bullet(state, pos, vel)
enemy.last_fire_time = am.frame_time
end
end
function slug_hit(enemy)
local node = enemy.node
node"scale":remove_all()
node"scale":append(slug_hit_sprite)
node:action(am.play(hit_sound))
node:action(am.series{am.delay(0.1), function()
node"scale":remove_all()
node"scale":append(slug_sprite)
return true
end})
end
function slug(state, x)
local position = vec2(x, win.top + 25)
local node = am.translate(position)
^ am.scale(4)
^ slug_sprite
return {
node = node,
update = slug_update,
position = position,
last_fire_time = am.frame_time,
hp = 1,
points = 200,
t = am.frame_time,
radius = 20,
explosion_size = 1,
explosion_sound = explode_sound_slug,
spawn_sound = spawn_sound_slug,
explosion_color = vec4(0.1, 1, 0.1, 1),
explosion_color_var = vec4(0.2, 0.2, 0.2, 0),
hit = slug_hit,
}
end
-- tank enemy
tank_frame1 = [[
...r.r.r...
.rrrrrrrrr.
.rRrrrrrRr.
rrrrMMMrrrr
.rrMMmMMrr.
rrrMmmmMrrr
.rrMMmMMrr.
rrrrMMMrrrr
.rRrrrrrRr.
.rrrrrrrrr.
...r.r.r...
]]
tank_frame2 = [[
...r.r.r...
.rrrrrrrrr.
.rrrrRrrrr.
rrrrmmmrrrr
.rrmmMmmrr.
rrRmMMMmRrr
.rrmmMmmrr.
rrrrmmmrrrr
.rrrrRrrrr.
.rrrrrrrrr.
...r.r.r...
]]
tank_hit_frame = tank_frame1:gsub("[^%.%s]", "W")
tank_sprite = animated_sprite({tank_frame1, tank_frame2}, 0.5)
tank_hit_sprite = am.sprite(tank_hit_frame)
function tank_update(enemy, state)
local dt = am.delta_time
local pos = enemy.position
local t = am.frame_time - enemy.t
local phase_time = 0.5
local phase = math.floor(t / phase_time) % 8
local vx, vy = 0, 0
if phase == 0 or phase == 4 then
vx = 0
vy = -160
elseif phase == 2 then
vx = 200
vy = 0
elseif phase == 6 then
vx = -200
vy = 0
end
pos = pos + vec2(vx, vy) * dt
enemy.position = pos
enemy.node.position2d = pos
if am.frame_time > enemy.last_fire_time + phase_time * 2 then
local vel = math.normalize(state.ship.position - pos) * 200
spawn_bullet(state, pos, vel)
enemy.last_fire_time = am.frame_time
end
end
function tank_hit(enemy)
local node = enemy.node
node:replace("sprite", tank_hit_sprite)
node:action(am.play(hit_sound))
node:action(am.series{am.delay(0.1), function()
node:replace("sprite", tank_sprite)
return true
end})
enemy.position = enemy.position + vec2(0, 10)
end
function tank(state, x)
local position = vec2(x, win.top + 30)
local node = am.translate(position)
^ am.scale(4)
^ tank_sprite
return {
node = node,
update = tank_update,
position = position,
last_fire_time = am.frame_time,
hp = 6,
points = 500,
t = am.frame_time,
radius = 25,
explosion_size = 1.5,
explosion_sound = explode_sound_tank,
spawn_sound = spawn_sound_tank,
explosion_color = vec4(1, 0.1, 0.1, 1),
explosion_color_var = vec4(0.2, 0.2, 0.2, 0),
hit = tank_hit,
}
end
-- worm enemy
worm_frame1 = [[
...b...
..bBb..
.bBCBb.
bBCCCBb
.bBCBb.
..bBb..
...b...
]]
worm_frame2 = [[
...C...
..CBC..
.CBbBC.
CBbbbBC
.CBbBC.
..CBC..
...C...
]]
worm_hit_frame = worm_frame1:gsub("[^%.%s]", "W")
worm_sprite = animated_sprite({worm_frame1, worm_frame2}, 0.5)
worm_hit_sprite = am.sprite(worm_hit_frame)
function worm_update(enemy, state)
local dt = am.delta_time
local pos = enemy.position
local center = enemy.center
local dir = enemy.dir
local t = am.frame_time - enemy.t
local vx, vy = dir * 80, -80
center = center + vec2(vx, vy) * dt
local radius = 50
pos = center + vec2(math.cos(t*4*dir) * radius, math.sin(t*4*dir) * radius)
enemy.center = center
enemy.position = pos
enemy.node.position2d = pos
end
function worm_hit(enemy)
local node = enemy.node
node"scale":remove_all()
node"scale":append(worm_hit_sprite)
node:action(am.play(hit_sound))
node:action(am.series{am.delay(0.1), function()
node"scale":remove_all()
node"scale":append(worm_sprite)
return true
end})
end
function worm(state, x, dir)
local position = vec2(x, win.top+20)
local node = am.translate(position)
^ am.scale(4)
^ worm_sprite
return {
node = node,
dir = dir,
update = worm_update,
position = position,
center = position,
last_fire_time = am.frame_time,
hp = 1,
points = 100,
t = am.frame_time,
radius = 15,
explosion_size = 0.8,
explosion_sound = explode_sound_worm,
spawn_sound = spawn_sound_worm,
explosion_color = vec4(0.2, 1, 1, 1),
explosion_color_var = vec4(0.2, 0.2, 0.2, 0),
hit = worm_hit,
}
end
-- boss
boss_frame1 = [[
...............w...............
....kwkkkwkwwkwww...k...w......
....w.....k...kmkkkkwwwkk......
.wwwwkk...w...kMk...w...kkkw...
......kkwwkkwwwmwwkkww..w......
......w.......wMw...w......k...
...kwwkwww....kmk...w..wwwkkk..
......k.......kMk...w...k..k...
..wwwkwkwwwkwwwmwwkwwkkkk......
....w......k..wMw.......k......
....k......w..kmk...kkwwkwwww..
....k...wwkkkwkMk....w....k....
....k.....k...wmw....k....w....
....w.....w..wwMww...w....w....
.wkkwwkkwwkkwwmMmwwkkwwkkwwkkw.
wwmMmMmMmMmMmMMWMMmMmMmMmMmMmww
.wkkwwkkwwkkwwmMmwwkkwwkkwwkkw.
....w........wwMww..w....w.w...
....k..k......wmw...w....w.w...
..kwwwkwkkwwkkkMkwkkk..w.......
.......k......kmk...w.wkw...w..
.kkkkkkwkkwwkkwMw...w..k....k..
.......k......wmw.wkwwwkwwwkkw.
....kkwwwkwwwkkMk..w...k....k..
.......w..k...kmk..w..wkw...w..
...k...w..w...wMw..w...w.......
.wwkkwwk......wmw..k.......w...
...k...k......kMkwkkkwwwkwwkw..
.......wkwwwkwkmk..k....w..w...
.......k..k...www..w....k......
...............w...............
]]
boss_frame2 = [[
...............w...............
....kwkkkwkwwkwww...k...w......
....w.....k...kMkkkkwwwkk......
.wwwwkk...w...kmk...w...kkkw...
......kkwwkkwwwMwwkkww..w......
......w.......wmw...w......k...
...kwwkwww....kMk...w..wwwkkk..
......k.......kmk...w...k..k...
..wwwkwkwwwkwwwMwwkwwkkkk......
....w......k..wmw.......k......
....k......w..kMk...kkwwkwwww..
....k...wwkkkwkmk....w....k....
....k.....k...wMw....k....w....
....w.....w..wwmww...w....w....
.wkkwwkkwwkkwwMMMwwkkwwkkwwkkw.
wwMmMmMmMmMmMmMWMmMmMmMmMmMmMww
.wkkwwkkwwkkwwMMMwwkkwwkkwwkkw.
....w........wwmww..w....w.w...
....k..k......wMw...w....w.w...
..kwwwkwkkwwkkkmkwkkk..w.......
.......k......kMk...w.wkw...w..
.kkkkkkwkkwwkkwmw...w..k....k..
.......k......wMw.wkwwwkwwwkkw.
....kkwwwkwwwkkmk..w...k....k..
.......w..k...kMk..w..wkw...w..
...k...w..w...wmw..w...w.......
.wwkkwwk......wMw..k.......w...
...k...k......kmkwkkkwwwkwwkw..
.......wkwwwkwkMk..k....w..w...
.......k..k...www..w....k......
...............w...............
]]
boss_hit_frame = boss_frame1:gsub("[^%.%s]", "C")
boss_sprite = animated_sprite({boss_frame1, boss_frame2}, 0.5)
boss_hit_sprite = am.sprite(boss_hit_frame)
function boss_update(enemy, state)
local dt = am.delta_time
local pos = enemy.position
local t = am.frame_time - enemy.t
local vx, vy = 0, 0
if t < 4 then
return
else
if pos.y > 140 then
vy = -120
elseif pos.y > 0 then
vy = -30
end
local phase = math.floor((t - 3) / 1) % 8
if phase == 1 or phase == 7 then
vx = 200
elseif phase == 3 or phase == 5 then
vx = -200
end
end
pos = pos + vec2(vx, vy) * dt
enemy.position = pos
enemy.node"translate".position2d = pos
if am.frame_time > enemy.last_fire_time + 1 and t > 5 then
local n = 16
local speed = (100 * math.random() + 200)
for i = 0, n-1 do
local angle = i/n*math.pi*2
local vel = vec2(math.cos(angle), math.sin(angle)) * speed
spawn_bullet(state, pos, vel)
end
enemy.node:action(am.play(boss_shoot_sound, false, 0.5, 2))
enemy.last_fire_time = am.frame_time - math.random() * 0.5
end
end
function boss_hit(enemy, state)
local node = enemy.node
node"scale":remove_all()
node"scale":append(boss_hit_sprite)
node:action(am.play(boss_hit_sound))
node:action("flash", am.series{am.delay(0.05), function()
node"scale":remove_all()
node"scale":append(boss_sprite)
return true
end})
enemy.position = enemy.position + vec2(0, 10)
enemy.health_bar.x2 = enemy.hp / enemy.max_hp * 200 - 100
end
function boss_kill(enemy, state)
state.no_new_enemies = false
state.scene:action(am.series{
am.tween(enemy.bg_color"rect", 2, {color = vec4(0.3, 0, 0, 0)}),
function()
state.scene:remove(enemy.bg_color)
return true
end
})
bg_stars"particles2d".start_color = vec4(0, 0, 1, 1)
bg_stars"particles2d".speed = 600
end
function boss(state)
state.no_new_enemies = true
local bg_color = am.blend"alpha"
^ am.rect(win.left - 50, win.bottom - 50, win.right + 50, win.top + 50,
vec4(0.3, 0, 0, 0))
state.scene:prepend(bg_color)
state.scene:action(am.tween(bg_color"rect", 2, {color = vec4(0.3, 0, 0, 1)}))
bg_stars"particles2d".start_color = vec4(1, 0.5, 0.5, 1)
bg_stars"particles2d".speed = 1200
local position = vec2(0, win.top + 70)
local health_bar = am.rect(-100, 210, 100, 220, vec4(1, 0, 1, 1))
local health_bar_bg = am.rect(-102, 208, 102, 222, vec4(0.5, 0.5, 0.5, 1))
local node =
am.group{
am.translate(position)
^ am.scale(4)
^ boss_sprite
,
am.group{
health_bar_bg,
health_bar,
}
}
local max_hp = 100
node:action(am.play(boss_sound))
return {
node = node,
bg_color = bg_color,
update = boss_update,
position = position,
last_fire_time = am.frame_time,
hp = max_hp,
max_hp = max_hp,
points = 100000,
t = am.frame_time,
radius = 62,
explosion_size = 3,
explosion_sound = explode_sound_boss,
spawn_sound = spawn_sound_boss,
explosion_color = vec4(1, 1, 1, 1),
explosion_color_var = vec4(0.5, 0.5, 0.5, 0),
num_explosions = 10,
hit = boss_hit,
kill = boss_kill,
health_bar = health_bar,
}
end
-- enemy patterns
enemy_spawn_patterns = {
{
{enemy = slug, args = {-200}, delay = 0.6},
{enemy = slug, args = {-200}, delay = 0.4},
{enemy = slug, args = {-200}, delay = 0.4},
{enemy = slug, args = {-200}, delay = 0.4},
},
{
{enemy = slug, args = {200 }, delay = 0.4},
{enemy = slug, args = {200 }, delay = 0.4},
{enemy = slug, args = {200 }, delay = 0.4},
{enemy = slug, args = {200 }, delay = 0.4},
},
{
{enemy = slug, args = {0}, delay = 0.4},
{enemy = slug, args = {0}, delay = 0.4},
{enemy = slug, args = {0}, delay = 0.4},
{enemy = slug, args = {0}, delay = 0.4},
},
{
{enemy = tank, args = {50}, delay = 1},
{enemy = tank, args = {50}, delay = 1},
{enemy = tank, args = {50}, delay = 1},
{enemy = tank, args = {50}, delay = 1},
},
{
{enemy = tank, args = {-120}, delay = 1},
{enemy = tank, args = {-120}, delay = 1},
{enemy = tank, args = {-120}, delay = 1},
{enemy = tank, args = {-120}, delay = 1},
},
{
{enemy = worm, args = {-220, 1}, delay = 0.3},
{enemy = worm, args = {-220, 1}, delay = 0.1},
{enemy = worm, args = {-220, 1}, delay = 0.1},
{enemy = worm, args = {-220, 1}, delay = 0.1},
{enemy = worm, args = {-220, 1}, delay = 0.1},
{enemy = worm, args = {-220, 1}, delay = 0.1},
{enemy = worm, args = {-220, 1}, delay = 0.1},
},
{
{enemy = worm, args = {220, -1}, delay = 0.3},
{enemy = worm, args = {220, -1}, delay = 0.1},
{enemy = worm, args = {220, -1}, delay = 0.1},
{enemy = worm, args = {220, -1}, delay = 0.1},
{enemy = worm, args = {220, -1}, delay = 0.1},
{enemy = worm, args = {220, -1}, delay = 0.1},
{enemy = worm, args = {220, -1}, delay = 0.1},
},
{
{enemy = worm, args = {220, -1}, delay = 3},
},
{
{enemy = boss, args = {0}, delay = 3},
}
}
pattern_sets = {
{ 1, 2, 3, 1, 6, 7, 6, 7, },
{ 1, 2, 3, 3, 4, 5, 4, 5, 6, 7, },
{ 1, 2, 3, 1, 2, 6, 7, 6, 7, },
{ 9 },
{ 8 },
}
-- enemy bullets
bullet_sprite = am.sprite[[
.mmm.
mmMmm
mMWMm
mmMmm
.mmm.
]]
function spawn_bullet(state, position, velocity)
local node = am.translate(position)
^ am.scale(4)
^ bullet_sprite
local bullet = {
node = node,
position = position,
velocity = velocity,
radius = 12,
}
table.insert(state.bullets, bullet)
state.bullets_group:append(node)
end
function update_bullets(state)
local dt = am.delta_time
local bullets = state.bullets
-- update bullet positions
for i = #bullets, 1, -1 do
local bullet = bullets[i]
local pos = bullet.position
local vel = bullet.velocity
pos = pos + vel * dt
if pos.y > win.top + 20 or pos.y < win.bottom - 20 or
pos.x > win.right + 20 or pos.x < win.left - 20
then
state.bullets_group:remove(bullet.node)
table.remove(bullets, i)
else
bullet.position = pos
bullet.node.position2d = pos
end
end
end
-- general enemy logic
function update_enemies(state)
local dt = am.delta_time
local enemies = state.enemies
for i = #enemies, 1, -1 do
local enemy = enemies[i]
enemy:update(state)
local pos = enemy.position
if enemy.hp <= 0 then
local size = enemy.explosion_size
local color = enemy.explosion_color
local color_var = enemy.explosion_color_var
local sound = enemy.explosion_sound
spawn_explosion(state, pos, size, color, color_var, sound)
state.score = state.score + enemy.points
if enemy.kill then
enemy:kill(state)
end
if (enemy.num_explosions or 1) > 1 then
state.explosions_group:action(coroutine.create(function()
for i = 2, enemy.num_explosions do
local explosion_pos = pos +
vec2(math.random() * 2 - 1, math.random() * 2 - 1)
* enemy.radius
spawn_explosion(state, explosion_pos, size, color, color_var, sound)
am.wait(am.delay(math.random() * 0.2 + 0.05))
end
end))
end
end
if enemy.hp <= 0 or
pos.y < win.bottom - 100 or
pos.y > win.top + 100 or
pos.x < win.left - 100 or
pos.x > win.right + 100
then
state.enemies_group:remove(enemy.node)
table.remove(enemies, i)
else
enemy.position = pos
enemy.node.position2d = pos
end
end
end
function spawn_enemies(state)
if state.no_new_enemies or state.dead then
state.last_enemy_spawn_time = am.frame_time
return
end
local pat = enemy_spawn_patterns[state.current_pattern]
local phase = state.pattern_phase
local last_t = state.last_enemy_spawn_time
if am.frame_time > last_t + pat[phase].delay then
-- spawn next enemy
local enemy = pat[phase].enemy(state, unpack(pat[phase].args))
state.enemies_group:append(enemy.node)
table.insert(state.enemies, enemy)
state.enemies_group:action(am.play(enemy.spawn_sound, false, 2 ^ (math.random(8) / 12)))
state.last_enemy_spawn_time = am.frame_time
phase = phase + 1
if phase > #pat then
-- choose new pattern
local pool = state.pattern_pool
if #pool == 0 then
-- next patt
state.pattern_set = state.pattern_set % #pattern_sets + 1
table.append(pool, pattern_sets[state.pattern_set])
end
local r = math.random(#pool)
state.current_pattern = pool[r]
table.remove(pool, r)
phase = 1
end
state.pattern_phase = phase
end
end
-- player ship
ship_sprite = am.sprite[[
...W...
R.WWW.R
W.WBW.W
WWWBWWW
..WWW..
]]
function init_ship()
local node =
am.translate(vec2(0, -260))
^ am.scale(4)
^ ship_sprite
local ship = {
node = node,
position = ship_start_pos,
}
return ship
end
function update_ship(state)
if state.dead then
return
end
-- update ship position
local dt = am.delta_time
local pos = state.ship.position
local dir_x, dir_y = 0, 0
if win:key_down"left" then
dir_x = -1
elseif win:key_down"right" then
dir_x = 1
end
if win:key_down"down" then
dir_y = -1
elseif win:key_down"up" then
dir_y = 1
end
local vel
if dir_x ~= 0 or dir_y ~= 0 then
local vel = math.normalize(vec2(dir_x, dir_y)) * ship_speed
pos = pos + dt * vel
end
pos = math.clamp(pos, ship_min_position, ship_max_position)
state.ship.position = pos
state.ship.node.position2d = state.ship.position
-- check for collision with bullets
for _, bullet in ipairs(state.bullets) do
if math.distance(bullet.position, pos) < bullet.radius + ship_radius then
end_game(state)
return
end
end
-- check for collision with enemies
for _, enemy in ipairs(state.enemies) do
if math.distance(enemy.position, pos) < enemy.radius + ship_radius then
end_game(state)
return
end
end
end
laser_sprite = am.sprite[[
.YYYYY.
YOOOOOY
ORRRRRO
R.....R
.......
R.....R
]]
function update_lasers(state)
local dt = am.delta_time
local lasers = state.lasers
for i = #lasers, 1, -1 do
-- update laser pos
local laser = lasers[i]
local pos = laser.position
pos = pos + laser_velocity * dt
-- check for collision with enemies
local hit = false
for _, enemy in ipairs(state.enemies) do
if enemy.position.y - enemy.radius < win.top and
math.distance(enemy.position, pos) < enemy.radius + 5
then
enemy.hp = enemy.hp - 1
if enemy.hit then
enemy:hit(state)
end
hit = true
break
end
end
if hit or pos.y > win.top + 20 then
state.lasers_group:remove(laser.node)
table.remove(lasers, i)
else
laser.position = pos
laser.node.position2d = pos
end
end
-- add new laser
if
not state.dead and
win:key_down"x" and
am.frame_time - state.last_laser_time > laser_interval
then
local pos = state.ship.position
local laser = {
position = pos,
node = am.translate(pos) ^ am.scale(4) ^ laser_sprite
}
table.insert(lasers, laser)
state.lasers_group:append(laser.node)
state.last_laser_time = am.frame_time
state.lasers_group:action(am.play(laser_sound))
-- kickback
state.ship.position = state.ship.position + vec2(0, -4)
end
end
-- score
function update_score(state)
state.score_node"text".text = state.score
end
-- background stars
star_img = [[
...
.W.
...]]
bg_stars = am.blend("add")
^ am.particles2d{
sprite_source = star_img,
source_pos = vec2(0, 250),
source_pos_var = vec2(320, 0),
max_particles = 200,
emission_rate = 100,
life = 1.5,
angle = math.rad(270),
speed = 600,
speed_var = 200,
start_color = vec4(0, 0, 1, 1),
start_size = 10,
start_size_var = 4,
warmup_time = 1,
}
-- title screen
function create_title_scene()
local line1 = am.text("DEFENDERS", vec4(1, 0, 1, 1))
local line2 = am.text("OF THE", vec4(1, 0, 1, 1))
local line3 = am.text("WEEPING QUASAR", vec4(1, 0, 1, 1))
local line4 = am.text("by Ian MacLarty", vec4(0.5, 0, 1, 1))
local title = am.group{
am.translate(0, 150) ^ am.scale(5) ^ line1,
am.translate(0, 80) ^ am.scale(3) ^ line2,
am.translate(0, 20) ^ am.scale(4) ^ line3,
am.translate(0, -50) ^ am.scale(2) ^ line4,
}:action(coroutine.create(function()
local colors = {vec4(1, 0, 1, 1), vec4(0, 1, 1, 1), vec4(1, 1, 0, 1), vec4(0, 1, 0, 1)}
local i = 1
while true do
line1.color = colors[i]
line2.color = colors[i]
line3.color = colors[i]
am.wait(am.delay(0.1))
i = i % #colors + 1
end
end))
local score =
am.translate(0, -100)
^ am.text("HI SCORE: "..global_state.high_score, vec4(0, 1, 1, 1))
local instructions =
am.translate(0, -150)
^ am.text("ARROW KEYS: MOVE\nX: FIRE\nPRESS X TO START", vec4(0.75, 0.75, 0.75, 1))
return am.group{
bg_stars,
title,
score,
instructions
}:action(function()
if win:key_pressed"x" then
start_game()
end
end)
:action(coroutine.create(function()
while true do
am.wait(am.play(title_sound, false, 1, 1))
am.wait(am.play(title_sound, false, 2^(-2/12), 1))
end
end))
end
-- game start/end logic
function start_game()
local ship = init_ship()
local lasers_group = am.group()
local bullets_group = am.group()
local enemies_group = am.group()
local explosions_group = am.group()
local explosions_flash_group = am.group()
local score_node = am.translate(300, 230)
^ am.scale(2)
^ am.text("0", "right", "top")
local scene = am.group{
explosions_flash_group,
bg_stars,
lasers_group,
enemies_group,