-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfspace.lua
More file actions
1067 lines (913 loc) · 35.7 KB
/
fspace.lua
File metadata and controls
1067 lines (913 loc) · 35.7 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
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
local Fspace = {}
Fspace.__index = Fspace
local Ship = require("ship")
local ship
local Thrust = require("thrust")
local thrust
local Bullet = require("bullet")
local bullets = {}
local Rock = require("rock")
local rocks = {}
local Mine = require("mine")
local mines = {}
local Box = require("box")
local boxes = {}
local Alienship = require("alienship")
local alienship
local Alien = require("alien")
local alien
Light = require("light")
local light
local Digit = require("digit")
local score_display = {}
local Char = require("char")
local fspace_text = {}
local gameover_text = {}
local Colors = require("colors")
local colors = Colors:new()
local prideColors = colors:get_pride_colors()
-- Physics constants
local thrust_delta = 3 -- Amount of thrust acceleration when engaged
local rotation_delta = 10 -- Amount of rotation change per frame when steering
-- Update categories for load-balanced updating
local UPDATE_CATEGORIES = {
ANIMATION = 1,
PHYSICS = 2,
RENDERING = 3,
COLLISION = 4,
SPAWNING = 5
}
local current_update_category = UPDATE_CATEGORIES.ANIMATION
-- Game states
local GAME_STATES = {
PLAY = 1,
PAUSE = 2,
GAMEOVER = 3,
}
local game_state = GAME_STATES.PLAY
function Fspace:new()
local self = setmetatable({}, Fspace)
return self
end
local Notes = {
-- Octave 4 (middle)
C4 = 261.63,
Cs4 = 277.18, Db4 = 277.18,
D4 = 293.66,
Ds4 = 311.13, Eb4 = 311.13,
E4 = 329.63,
F4 = 349.23,
Fs4 = 369.99, Gb4 = 369.99,
G4 = 392.00,
Gs4 = 415.30, Ab4 = 415.30,
A4 = 440.00,
As4 = 466.16, Bb4 = 466.16,
B4 = 493.88,
-- Octave 3 (lower)
C3 = 130.81,
Cs3 = 138.59, Db3 = 138.59,
D3 = 146.83,
Ds3 = 155.56, Eb3 = 155.56,
E3 = 164.81,
F3 = 174.61,
Fs3 = 185.00, Gb3 = 185.00,
G3 = 196.00,
Gs3 = 207.65, Ab3 = 207.65,
A3 = 220.00,
As3 = 233.08, Bb3 = 233.08,
B3 = 246.94,
-- Octave 5 (higher)
C5 = 523.25,
Cs5 = 554.37, Db5 = 554.37,
D5 = 587.33,
Ds5 = 622.25, Eb5 = 622.25,
E5 = 659.25,
F5 = 698.46,
Fs5 = 739.99, Gb5 = 739.99,
G5 = 783.99,
Gs5 = 830.61, Ab5 = 830.61,
A5 = 880.00,
As5 = 932.33, Bb5 = 932.33,
B5 = 987.77,
}
-- Generates a sine‑based explosion sound
-- startFreq: starting frequency in Hz
-- endFreq: ending frequency in Hz
-- decayRate: exponential amplitude decay constant
-- duration: optional, defaults to 0.2 seconds
function Fspace:generateExplosion(startFreq, endFreq, decayRate, duration)
duration = duration or 0.2
print("endFreq: " .. endFreq)
local sampleRate = 44100
local sampleCount = duration * sampleRate
local soundData = love.sound.newSoundData(sampleCount, sampleRate, 16, 1)
for i = 0, sampleCount - 1 do
local t = i / sampleRate
-- amplitude envelope
local amp = math.exp(-t * decayRate)
-- frequency sweep
local freq = startFreq + (endFreq - startFreq) * (t / duration)
-- sine wave sample
local sample = amp * math.sin(2 * math.pi * freq * t)
soundData:setSample(i, sample)
end
return soundData
end
function Fspace:load()
local sd0 = Fspace:generateExplosion(Notes.B3,Notes.F3, 15, .05)
explosion_sound = love.audio.newSource(sd0)
local sd1 = Fspace:generateExplosion(Notes.E4,Notes.E4, 5, .02)
pew_sound = love.audio.newSource(sd1)
local snd2 = Fspace:generateExplosion(Notes.A3, Notes.B3 , 4, 0.4)
thrust_sound = love.audio.newSource(snd2)
game_state = GAME_STATES.PLAY
local x, y = 101, 101
ship = Ship.new(x, y, 0)
thrust = Thrust.new(x, y, 0)
-- Spawn a rock above the screen
local rock_x = x
local rock_y = -40
local rock = Rock.new(rock_x, rock_y, 0)
rock.dy = 2
rock.dr = 2
rock.scale = 3.0
table.insert(rocks, rock)
-- Create a global light
light = Light.new(x + 100, y + 100, 0)
light.current_frame = 1
-- Create an alienship NPC at screen center (stationary for now)
do
local screen_w, screen_h = love.graphics.getWidth(), love.graphics.getHeight()
alienship = Alienship.new(screen_w / 2, screen_h / 2, 0)
-- Also create the new `Alien` sprite and place it at the same position as the alienship
alien = Alien.new(alienship.x, alienship.y, alienship.angle)
end
score_display = {Digit.new(60, 50, 0), Digit.new(40, 50, 0), Digit.new(20, 50, 0)}
-- Create `fspace` title using helper
fspace_text = Fspace:chartext("fspace", 10, 10, 0, 20)
-- Create `game over` text, centered on screen
do
local screen_w, screen_h = love.graphics.getWidth(), love.graphics.getHeight()
local msg = "GAME OVER"
local spacing = 20
local start_x = (screen_w - #msg * spacing) / 2
gameover_text = Fspace:chartext(msg, start_x, screen_h / 2 - 20, 0, spacing)
end
end
function Fspace:fire_bullet()
pew_sound:stop()
pew_sound:play()
local distance_from_origin = 30
if ship.weapon_level == 0 then
-- Find tip of ship in world coordinates
local rad = math.rad(ship.angle + 90)
local tip_x = ship.x + math.cos(rad) * distance_from_origin * ship.scale
local tip_y = ship.y + math.sin(rad) * distance_from_origin * ship.scale
local bullet = Bullet.new(tip_x, tip_y, ship.angle)
local speed = 10
bullet.dx = math.cos(rad) * speed
bullet.dy = math.sin(rad) * speed
table.insert(bullets, bullet)
elseif ship.weapon_level == 1 then
local rad = math.rad(ship.angle + 90)
local forward_x = math.cos(rad)
local forward_y = math.sin(rad)
local perp_x = -math.sin(rad)
local perp_y = math.cos(rad)
local offset = 2
local tip_x = ship.x + forward_x * distance_from_origin * ship.scale + perp_x * offset
local tip_y = ship.y + forward_y * distance_from_origin * ship.scale + perp_y * offset
local tip_x2 = ship.x + forward_x * distance_from_origin * ship.scale - perp_x * offset
local tip_y2 = ship.y + forward_y * distance_from_origin * ship.scale - perp_y * offset
local bullet = Bullet.new(tip_x, tip_y, ship.angle)
local bullet2 = Bullet.new(tip_x2, tip_y2, ship.angle)
local speed = 10
bullet.color = {1, 0, 0} -- red bullet for level 1
bullet.dx = forward_x * speed
bullet.dy = forward_y * speed
bullet2.color = {1, 0, 0} -- red bullet for level 1
bullet2.dx = forward_x * speed
bullet2.dy = forward_y * speed
table.insert(bullets, bullet)
table.insert(bullets, bullet2)
elseif ship.weapon_level == 2 then
local rad = math.rad(ship.angle + 90)
local forward_x = math.cos(rad)
local forward_y = math.sin(rad)
local perp_x = -math.sin(rad)
local perp_y = math.cos(rad)
local offset = 3
local tip_x = ship.x + forward_x * distance_from_origin * ship.scale - perp_x * offset
local tip_y = ship.y + forward_y * distance_from_origin * ship.scale - perp_y * offset
local tip_x2 = ship.x + forward_x * distance_from_origin * ship.scale
local tip_y2 = ship.y + forward_y * distance_from_origin * ship.scale
local tip_x3 = ship.x + forward_x * distance_from_origin * ship.scale + perp_x * offset
local tip_y3 = ship.y + forward_y * distance_from_origin * ship.scale + perp_y * offset
local bullet = Bullet.new(tip_x, tip_y, ship.angle)
local bullet2 = Bullet.new(tip_x2, tip_y2, ship.angle)
local bullet3 = Bullet.new(tip_x3, tip_y3, ship.angle)
local speed = 10
bullet.color = {0.5, 0, 1.0} -- purple
bullet.dx = forward_x * speed
bullet.dy = forward_y * speed
bullet2.color = {0.5, 0, 1.0} -- purple
bullet2.dx = forward_x * speed
bullet2.dy = forward_y * speed
bullet3.color = {0.5, 0, 1.0} -- purple
bullet3.dx = forward_x * speed
bullet3.dy = forward_y * speed
table.insert(bullets, bullet)
table.insert(bullets, bullet2)
table.insert(bullets, bullet3)
elseif ship.weapon_level == 3 then
local rad = math.rad(ship.angle + 90)
local forward_x = math.cos(rad)
local forward_y = math.sin(rad)
local perp_x = -math.sin(rad)
local perp_y = math.cos(rad)
local offset = 3
local tip_x = ship.x + forward_x * distance_from_origin * ship.scale - perp_x * offset
local tip_y = ship.y + forward_y * distance_from_origin * ship.scale - perp_y * offset
local tip_x2 = ship.x + forward_x * distance_from_origin * ship.scale
local tip_y2 = ship.y + forward_y * distance_from_origin * ship.scale
local tip_x3 = ship.x + forward_x * distance_from_origin * ship.scale + perp_x * offset
local tip_y3 = ship.y + forward_y * distance_from_origin * ship.scale + perp_y * offset
local bullet = Bullet.new(tip_x, tip_y, ship.angle)
local bullet2 = Bullet.new(tip_x2, tip_y2, ship.angle)
local bullet3 = Bullet.new(tip_x3, tip_y3, ship.angle)
local bullet4 = Bullet.new(tip_x2, tip_y3, ship.angle + 15)
local bullet5 = Bullet.new(tip_x2, tip_y3, ship.angle - 15)
local speed = 10
bullet.color = {0.5, 0, 1.0} -- purple
bullet.dx = forward_x * speed
bullet.dy = forward_y * speed
bullet2.color = {0.5, 0, 1.0} -- purple
bullet2.dx = forward_x * speed
bullet2.dy = forward_y * speed
bullet3.color = {0.5, 0, 1.0} -- purple
bullet3.dx = forward_x * speed
bullet3.dy = forward_y * speed
bullet4.color = {1.0, 0.5, 0} -- orange
bullet4.dx = math.cos(math.rad(ship.angle + 90 + 15)) * speed
bullet4.dy = math.sin(math.rad(ship.angle + 90 + 15)) * speed
bullet5.color = {1.0, 0.5, 0} -- orange
bullet5.dx = math.cos(math.rad(ship.angle + 90 - 15)) * speed
bullet5.dy = math.sin(math.rad(ship.angle + 90 - 15)) * speed
table.insert(bullets, bullet)
table.insert(bullets, bullet2)
table.insert(bullets, bullet3)
table.insert(bullets, bullet4)
table.insert(bullets, bullet5)
end
end
-- Apply a linear and rotational nudge to a rock. Uses internal constants
-- (LINEAR_SCALE, ROTATION_SCALE, LEVER_ARM_DEFAULT) that can be
-- parameterized later.
function Fspace:nudge_rock(rock, fx, fy, contact_x, contact_y)
local LINEAR_SCALE = 1.0
local ROTATION_SCALE = 0.05
local LEVER_ARM_DEFAULT = 5
-- Apply linear impulse
rock.dx = (rock.dx or 0) + fx * LINEAR_SCALE
rock.dy = (rock.dy or 0) + fy * LINEAR_SCALE
-- Compute lever arm from rock center to contact point (if provided)
local rx, ry
if contact_x and contact_y then
rx = contact_x - rock.x
ry = contact_y - rock.y
else
local mag = math.sqrt((fx or 0)^2 + (fy or 0)^2)
if mag > 0 then
rx = -fy / mag * LEVER_ARM_DEFAULT
ry = fx / mag * LEVER_ARM_DEFAULT
else
rx = LEVER_ARM_DEFAULT
ry = 0
end
end
-- Torque = r x F (scalar in 2D)
local torque = rx * fy - ry * fx
rock.dr = (rock.dr or 0) + torque * ROTATION_SCALE
-- Mark collision time for visual feedback
rock.collision_time = love.timer.getTime()
end
function Fspace:update_color_animations(now)
-- check the time_current_frame of fspace_text[1] and if more than 1 second
-- rotate the colors of the prideColors table and assign to fspace_text
if #fspace_text > 0 and fspace_text[1].time_current_frame + 1 < now then
local first_color = table.remove(prideColors, 1)
table.insert(prideColors, first_color)
for i, char in ipairs(fspace_text) do
char.color = prideColors[(i - 1) % #prideColors + 1]
end
fspace_text[1].time_current_frame = now
end
end
function Fspace:update_light_animation(now)
-- Animate light frames
local duration = light.frame_durations[light.current_frame or 1] or 1
if light.time_current_frame + duration < now then
light.time_current_frame = now
light.current_frame = (light.current_frame or 1) + 1
if light.current_frame > #light.points then
light.current_frame = 1
end
end
end
function Fspace:update_score_display(now)
formated_score = string.format("%03d", ship.life)
-- get the 1's 10's and 100's digits of ship life
local len = #formated_score
for i = 1, 3 do
local digit_char = "0"
if len - i + 1 > 0 then
digit_char = formated_score:sub(len - i + 1, len - i + 1)
end
local digit_value = tonumber(digit_char)
score_display[i].current_frame = digit_value + 1
score_display[i].last_update = now
end
end
function Fspace:chartext(str, start_x, y, angle, spacing)
-- Build an array of `Char` objects for `str` starting at `start_x`,`y`.
start_x = start_x or 10
y = y or 10
angle = angle or 0
spacing = spacing or 20
local out = {}
local x = start_x
for i = 1, #str do
local c = str:sub(i,i)
if c == ' ' then
x = x + spacing
else
table.insert(out, Char.new(x, y, angle, c:lower()))
x = x + spacing
end
end
return out
end
function Fspace:update_check_GAMEOVER()
-- If player's score (ship.life) drops below zero, set game state to GAMEOVER
if ship and ship.life and ship.life <= 0 then
game_state = GAME_STATES.GAMEOVER
end
end
function Fspace:update_rocks()
-- Update rocks
for _, rock in ipairs(rocks) do
rock.y = rock.y + (rock.dy or 0)
rock.x = rock.x + (rock.dx or 0)
rock.angle = (rock.angle + (rock.dr or 0)) % 360
end
end
function Fspace:update_ship_rotation(dt)
if love.keyboard.isDown("left") then
ship.dr = ship.dr - rotation_delta
elseif love.keyboard.isDown("right") then
ship.dr = ship.dr + rotation_delta
else
ship.dr = ship.dr * 0.95
end
thrust.dr = ship.dr
ship.angle = (ship.angle + ship.dr * dt) % 360
thrust.angle = ship.angle
end
function Fspace:update_ship_thrust(dt)
-- Thrust logic: add thrust vector opposite to ship's angle when space is pressed
if love.keyboard.isDown("space") or love.mouse.isDown(1) then
thrust_sound:play()
-- Thrust should be directly opposite to ship's facing direction
local thrust_angle = (ship.angle + 90) % 360
local rad = math.rad(thrust_angle)
local tx = math.cos(rad) * thrust_delta
local ty = math.sin(rad) * thrust_delta
ship.dx = ship.dx + tx * dt
ship.dy = ship.dy + ty * dt
end
end
function Fspace:update_ship_physics(dt)
-- Gravity: accelerate ship downward
local gravity = 0.2
local max_vel_due_to_gravity = 20
local velocity_mag = (ship.dx or 0)^2 + (ship.dy or 0)^2
if velocity_mag > max_vel_due_to_gravity then
gravity = 0
end
ship.dy = ship.dy + gravity * dt
-- Move ship by velocity
ship.x = ship.x + ship.dx
ship.y = ship.y + ship.dy
end
function Fspace:wrap_objects()
-- Get screen size
local screen_w, screen_h = love.graphics.getWidth(), love.graphics.getHeight()
-- Wrap ship coordinates
if ship.x < 0 then ship.x = ship.x + screen_w end
if ship.x > screen_w then ship.x = ship.x - screen_w end
if ship.y < 0 then ship.y = ship.y + screen_h end
if ship.y > screen_h then ship.y = ship.y - screen_h end
-- Wrap rock coordinates
for _, rock in ipairs(rocks) do
if rock.x < 0 then rock.x = rock.x + screen_w end
if rock.x > screen_w then rock.x = rock.x - screen_w end
if rock.y < 0 then rock.y = rock.y + screen_h end
if rock.y > screen_h then rock.y = rock.y - screen_h end
end
end
function Fspace:sync_visual_sprites()
-- Move thrust sprite with ship
thrust.x = ship.x
thrust.y = ship.y
thrust.angle = ship.angle
-- Move light sprite with ship
light.x = ship.x
light.y = ship.y
light.angle = ship.angle
-- Keep alien aligned with the alienship (if both exist)
if alienship and alien then
alien.x = alienship.x
alien.y = alienship.y
alien.angle = alienship.angle
end
end
function Fspace:update_bullet_firing()
-- Fire bullet on right mouse, ctrl, alt, or f
if love.mouse.isDown(2) or love.keyboard.isDown("lctrl") or love.keyboard.isDown("rctrl") or love.keyboard.isDown("lalt") or love.keyboard.isDown("ralt") or love.keyboard.isDown("f") then
Fspace:fire_bullet()
bullet_fired = true
else
bullet_fired = false
end
end
function Fspace:update_bullets()
-- Update bullets
for i = #bullets, 1, -1 do
local b = bullets[i]
b.x = b.x + b.dx
b.y = b.y + b.dy
-- Remove bullet if out of bounds
if b.x < 0 or b.x > love.graphics.getWidth() or b.y < 0 or b.y > love.graphics.getHeight() then
table.remove(bullets, i)
end
end
end
function Fspace:collide_ship_with_rocks()
local rock_number_collision_threshold = 50
if #rocks > rock_number_collision_threshold then
return
end
-- collision detection ship and rocks (AABB)
local ship_min_x, ship_min_y, ship_max_x, ship_max_y = ship:get_bound_rect()
ship_min_x = ship_min_x + ship.x
ship_min_y = ship_min_y + ship.y
ship_max_x = ship_max_x + ship.x
ship_max_y = ship_max_y + ship.y
for _, rock in ipairs(rocks) do
-- Skip collision detection for 1.5 second after spawn
if rock.spawn_time and love.timer.getTime() - rock.spawn_time < 1.5 then
rock.color = {0.8, 0.8, 0.8} -- light gray to indicate recent spawn
else
if rock.collision_time > 0 and love.timer.getTime() - rock.collision_time < 1 then
rock.color = {1, 0, 0} -- red to indicate recent collision
else
rock.color = {0.5, 0.5, 0.5} -- normal gray color
local rock_min_x, rock_min_y, rock_max_x, rock_max_y = rock:get_bound_rect()
rock_min_x = rock_min_x + rock.x
rock_min_y = rock_min_y + rock.y
rock_max_x = rock_max_x + rock.x
rock_max_y = rock_max_y + rock.y
if not (ship_max_x < rock_min_x or ship_min_x > rock_max_x or ship_max_y < rock_min_y or ship_min_y > rock_max_y) then
rock.collision_time = love.timer.getTime()
explosion_sound:stop()
explosion_sound:play()
-- decrease ship life on collision
ship.life = ship.life - 5
if ship.life < 0 then ship.life = 0 end
-- adjust rock and ship velocities to simulate bounce
local normal_x = (rock.x - ship.x)
local normal_y = (rock.y - ship.y)
local length = math.sqrt(normal_x^2 + normal_y^2)
if length > 0 then
normal_x = normal_x / length
normal_y = normal_y / length
local relative_velocity_x = (rock.dx or 0) - (ship.dx or 0)
local relative_velocity_y = (rock.dy or 0) - (ship.dy or 0)
local velocity_along_normal = relative_velocity_x * normal_x + relative_velocity_y * normal_y
if velocity_along_normal < 0 then
local restitution = 0.8 -- bounciness factor
local impulse = -(1 + restitution) * velocity_along_normal
impulse = impulse / 2 -- divide by 2 for equal mass
local impulse_x = impulse * normal_x
local impulse_y = impulse * normal_y
ship.dx = (ship.dx or 0) - impulse_x
ship.dy = (ship.dy or 0) - impulse_y
-- Apply the same impulse to the rock using nudge_rock so the
-- translational and rotational effects are encapsulated.
Fspace:nudge_rock(rock, impulse_x, impulse_y, ship.x, ship.y)
end
end
end
end
end
end
end
function Fspace:collide_ship_with_mines()
if #mines > 20 then
return
end
if ship.mine_collision_time and love.timer.getTime() - ship.mine_collision_time < 0.5 then
ship.color = {1, 0.5, 0.5} -- flash ship light red after mine collision
else
ship.color = {0, 1, 1} -- normal cyan color
end
-- collision detection ship and mines (AABB)
local ship_min_x, ship_min_y, ship_max_x, ship_max_y = ship:get_bound_rect()
ship_min_x = ship_min_x + ship.x
ship_min_y = ship_min_y + ship.y
ship_max_x = ship_max_x + ship.x
ship_max_y = ship_max_y + ship.y
for i, m in ipairs(mines or {}) do
-- Skip collision detection for 1.5 second after spawn
if m.spawn_time and love.timer.getTime() - m.spawn_time < 1.5 then
m.color = {0.7, 1.0, 0.7} -- light green to indicate recent spawn
else
if m.collision_time > 0 and love.timer.getTime() - m.collision_time < 1 then
m.color = {1, 0, 0} -- red to indicate recent collision
else
m.color = {0.2, 1, 0.2} -- normal green color
local mine_min_x, mine_min_y, mine_max_x, mine_max_y = m:get_bound_rect()
mine_min_x = mine_min_x + m.x
mine_min_y = mine_min_y + m.y
mine_max_x = mine_max_x + m.x
mine_max_y = mine_max_y + m.y
if not (ship_max_x < mine_min_x or ship_min_x > mine_max_x or ship_max_y < mine_min_y or ship_min_y > mine_max_y) then
m.collision_time = love.timer.getTime()
explosion_sound:stop()
explosion_sound:play()
-- destroy mine on collision and decrease ship life
table.remove(mines, i)
ship.mine_collision_time = love.timer.getTime()
ship.life = ship.life - 10
if ship.life < 0 then ship.life = 0 end
end
end
end
end
end
function Fspace:collide_bullets_with_rocks()
local rock_number_collision_threshold = 50
-- collision detection bullets and rocks (AABB)
for _, rock in ipairs(rocks) do
for bi = #bullets, 1, -1 do
local bullet = bullets[bi]
local bx, by = bullet.x, bullet.y
local rock_min_x, rock_min_y, rock_max_x, rock_max_y = rock:get_bound_rect()
rock_min_x = rock_min_x + rock.x
rock_min_y = rock_min_y + rock.y
rock_max_x = rock_max_x + rock.x
rock_max_y = rock_max_y + rock.y
if bx >= rock_min_x and bx <= rock_max_x and by >= rock_min_y and by <= rock_max_y then
explosion_sound:stop()
explosion_sound:play()
-- Bullet hit rock: remove bullet, split rock
table.remove(bullets, bi)
rock.collision_time = love.timer.getTime()
-- Optionally spawn a mine
if #mines < 20 and math.random() < 0.05 then
local mine = Mine.new(rock.x, rock.y, rock.angle)
table.insert(mines, mine)
end
-- Optionally spawn a box (same frequency as mines)
if #boxes < 20 and math.random() < 0.05 then
local box = Box.new(rock.x, rock.y, rock.angle)
table.insert(boxes, box)
end
-- Optionally spawn smaller rocks
if rock.scale > 0.2 and #rocks < rock_number_collision_threshold then
for i = 1, 2 do
local angle = math.random() * 2 * math.pi
local newrock = Rock.new(rock.x, rock.y, math.deg(angle))
newrock.scale = rock.scale * 0.7
newrock.dx = math.cos(angle) * 2
newrock.dy = math.sin(angle) * 2
newrock.dr = (math.random() - 0.5) * 4
table.insert(rocks, newrock)
end
end
table.remove(rocks, _)
break
end
end
end
end
function Fspace:collide_bullets_with_mines()
-- collision detection bullets and mines (AABB)
for mi = #mines, 1, -1 do
local mine = mines[mi]
for bi = #bullets, 1, -1 do
local bullet = bullets[bi]
local bx, by = bullet.x, bullet.y
local mine_min_x, mine_min_y, mine_max_x, mine_max_y = mine:get_bound_rect()
mine_min_x = mine_min_x + mine.x
mine_min_y = mine_min_y + mine.y
mine_max_x = mine_max_x + mine.x
mine_max_y = mine_max_y + mine.y
if bx >= mine_min_x and bx <= mine_max_x and by >= mine_min_y and by <= mine_max_y then
explosion_sound:stop()
explosion_sound:play()
-- Bullet hit mine: remove both
table.remove(bullets, bi)
table.remove(mines, mi)
break
end
end
end
end
function Fspace:collide_ship_with_boxes()
if #boxes > 20 then
return
end
if ship.box_collision_time and love.timer.getTime() - ship.box_collision_time < 0.5 then
ship.color = {1, 0.5, 0.5} -- flash ship light red after box collision
else
ship.color = {0, 1, 1} -- normal cyan color
end
-- collision detection ship and boxes (AABB)
local ship_min_x, ship_min_y, ship_max_x, ship_max_y = ship:get_bound_rect()
ship_min_x = ship_min_x + ship.x
ship_min_y = ship_min_y + ship.y
ship_max_x = ship_max_x + ship.x
ship_max_y = ship_max_y + ship.y
for i, b in ipairs(boxes or {}) do
-- Skip collision detection for 1.5 second after spawn
if b.spawn_time and love.timer.getTime() - b.spawn_time < 1.5 then
b.color = {0.7, 0.7, 1.0} -- light blue to indicate recent spawn
else
if b.collision_time > 0 and love.timer.getTime() - b.collision_time < 1 then
b.color = {1, 0, 0} -- red to indicate recent collision
else
b.color = {0.2, 0.2, 1.0} -- normal blue color
local box_min_x, box_min_y, box_max_x, box_max_y = b:get_bound_rect()
box_min_x = box_min_x + b.x
box_min_y = box_min_y + b.y
box_max_x = box_max_x + b.x
box_max_y = box_max_y + b.y
if not (ship_max_x < box_min_x or ship_min_x > box_max_x or ship_max_y < box_min_y or ship_min_y > box_max_y) then
b.collision_time = love.timer.getTime()
-- destroy box on collision and decrease ship life
explosion_sound:stop()
explosion_sound:play()
table.remove(boxes, i)
ship.box_collision_time = love.timer.getTime()
ship.weapon_level = ( ship.weapon_level + 1 ) % ( ship.max_weapon_level + 1 )
print("Weapon level increased to ", ship.weapon_level)
end
end
end
end
end
function Fspace:collide_bullets_with_boxes()
-- collision detection bullets and boxes (AABB)
for bi = #boxes, 1, -1 do
local box = boxes[bi]
for bii = #bullets, 1, -1 do
local bullet = bullets[bii]
local bx, by = bullet.x, bullet.y
local box_min_x, box_min_y, box_max_x, box_max_y = box:get_bound_rect()
box_min_x = box_min_x + box.x
box_min_y = box_min_y + box.y
box_max_x = box_max_x + box.x
box_max_y = box_max_y + box.y
if bx >= box_min_x and bx <= box_max_x and by >= box_min_y and by <= box_max_y then
explosion_sound:stop()
explosion_sound:play()
-- Bullet hit box: remove both
table.remove(bullets, bii)
table.remove(boxes, bi)
break
end
end
end
end
function Fspace:spawn_rocks()
if #rocks < 2 then
local screen_w, screen_h = love.graphics.getWidth(), love.graphics.getHeight()
local center_x = screen_w / 2
local center_y = screen_h / 2
local angle = math.random() * math.pi + math.pi
local edge_x = center_x + math.cos(angle) * (math.max(screen_w, screen_h) / 2)
local edge_y = center_y + math.sin(angle) * (math.max(screen_w, screen_h) / 2)
local rock_angle_deg = math.deg(angle)
local newrock = Rock.new(edge_x, edge_y, rock_angle_deg)
-- Set velocity toward center
local to_center_x = center_x - edge_x
local to_center_y = center_y - edge_y
local length = math.sqrt(to_center_x^2 + to_center_y^2)
if length > 0 then
newrock.dx = to_center_x / length * 2
newrock.dy = to_center_y / length * 2
else
newrock.dx = 0
newrock.dy = 2
end
newrock.dr = (math.random() - 0.5) * 4
newrock.spawn_time = love.timer.getTime()
table.insert(rocks, newrock)
end
end
function Fspace:update(dt)
local now = love.timer.getTime()
-- Ship updates happen every frame (not scheduled)
self:update_ship_rotation(dt)
self:update_ship_thrust(dt)
self:update_ship_physics(dt)
self:sync_visual_sprites()
-- Spin-schedule: execute one category per update call
-- SPIN_SCHEDULE: ANIMATION
if current_update_category == UPDATE_CATEGORIES.ANIMATION then
self:update_color_animations(now)
self:update_light_animation(now)
self:update_score_display(now)
-- SPIN_SCHEDULE: PHYSICS
elseif current_update_category == UPDATE_CATEGORIES.PHYSICS then
self:update_rocks()
self:wrap_objects()
-- SPIN_SCHEDULE: RENDERING
elseif current_update_category == UPDATE_CATEGORIES.RENDERING then
self:update_bullet_firing()
self:update_bullets()
-- SPIN_SCHEDULE: COLLISION
elseif current_update_category == UPDATE_CATEGORIES.COLLISION then
self:collide_ship_with_rocks()
self:collide_ship_with_mines()
self:collide_ship_with_boxes()
self:collide_bullets_with_rocks()
self:collide_bullets_with_mines()
self:collide_bullets_with_boxes()
-- SPIN_SCHEDULE: SPAWNING
elseif current_update_category == UPDATE_CATEGORIES.SPAWNING then
self:spawn_rocks()
end
-- Advance to next category in spin schedule
current_update_category = current_update_category % 5 + 1
-- Check for game over condition each update
self:update_check_GAMEOVER()
end
function Fspace:draw()
love.graphics.setColor(1, 1, 1)
local screen_w, screen_h = love.graphics.getWidth(), love.graphics.getHeight()
-- Draw mines
for _, m in ipairs(mines or {}) do
m:draw()
end
-- Draw boxes
for _, b in ipairs(boxes or {}) do
b:draw()
end
-- Draw bullets
for _, b in ipairs(bullets) do
b:draw()
end
-- Draw rocks
for _, rock in ipairs(rocks) do
rock:draw()
end
-- Draw ship and thrust at main position
ship:draw()
-- Draw stationary alienship NPC at screen center
if alienship then
alienship:draw()
end
-- Draw the new `Alien` sprite (stationary)
if alien then
alien:draw()
end
if love.keyboard.isDown("space") then
thrust:draw()
end
if love.mouse.isDown(1) then
thrust:draw()
end
light:draw(light.current_frame)
for _, digit in ipairs(score_display) do
digit:draw(digit.current_frame)
end
for _, char in ipairs(fspace_text) do
char:draw(char.current_frame)
end
if game_state == GAME_STATES.GAMEOVER then
for _, char in ipairs(gameover_text) do
char:draw(char.current_frame)
end
end
-- Check mines for overlapping edges and draw at wrapped positions
for _, mine in ipairs(mines) do
local overlap = mine:is_overlapping(screen_w, screen_h)
if overlap.left then
mine.x = mine.x + screen_w
mine:draw()
mine.x = mine.x - screen_w
end
if overlap.right then
mine.x = mine.x - screen_w
mine:draw()
mine.x = mine.x + screen_w
end
if overlap.top then
mine.y = mine.y + screen_h
mine:draw()
mine.y = mine.y - screen_h
end
if overlap.bottom then
mine.y = mine.y - screen_h
mine:draw()
mine.y = mine.y + screen_h
end
end
-- Check boxes for overlapping edges and draw at wrapped positions
for _, box in ipairs(boxes) do
local overlap = box:is_overlapping(screen_w, screen_h)
if overlap.left then
box.x = box.x + screen_w
box:draw()
box.x = box.x - screen_w
end
if overlap.right then
box.x = box.x - screen_w
box:draw()
box.x = box.x + screen_w
end
if overlap.top then
box.y = box.y + screen_h
box:draw()
box.y = box.y - screen_h
end
if overlap.bottom then
box.y = box.y - screen_h
box:draw()
box.y = box.y + screen_h
end
end
-- Check overlapping edges and draw at wrapped positions