This repository has been archived by the owner on Jun 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.lua
2062 lines (1940 loc) · 129 KB
/
game.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
-- title: Wolf-80
-- author: Wojciech Graj
-- desc: First-Person Shooter Game
-- script: lua
-- input: gamepad
function g_math_sign(x)
return x>0 and 1 or x<0 and -1 or 0
end
function g_entity_move(self, speed)
local math_floor = math.floor
local math_sign = g_math_sign
local math_min = math.min
local math_max = math.max
local sign_speed = math_sign(speed)
if mget(
math_max(math_min(math_floor(self.pos_x + self.dir_x * speed + sign_speed * math_sign(self.dir_x) * 0.25), g_LEVEL_WIDTH - 1), 0),
math_floor(self.pos_y)) == 0 then
self.pos_x = self.pos_x + self.dir_x * speed
end
if mget(
math_floor(self.pos_x),
math_max(math_min(math_floor(self.pos_y + self.dir_y * speed + sign_speed * math_sign(self.dir_y) * 0.25), g_LEVEL_HEIGHT - 1), 0)) == 0 then
self.pos_y = self.pos_y + self.dir_y * speed
end
end
Weapon = {
ui_tex_idx = 0,
ammo = 0,
weapon_texture_y = 0,
textures_x = {},
raise_time = 0,
shoot_time = 0,
reload_time = 0,
double_spread_angle = 0,
min_dmg = 0,
max_dmg = 0,
n_pellets = 0,
range_sqr = 0,
}
Weapon.__index = Weapon
-- Weapon States:
-- 0: raise
-- 1: lower
-- 2: ready
-- 3: shoot
-- 4: reload
function Weapon.new(ui_tex_idx, ammo, texture_y, textures_x, raise_time, shoot_time, reload_time, spread_angle, min_dmg, max_dmg, n_pellets, range)
local self = setmetatable({}, Weapon)
self.ui_tex_idx = ui_tex_idx
self.ammo = ammo
self.texture_y = texture_y
self.textures_x = textures_x
self.raise_time = raise_time
self.shoot_time = shoot_time
self.reload_time = reload_time
self.double_spread_angle = spread_angle * 2
self.min_dmg = min_dmg
self.max_dmg = max_dmg
self.n_pellets = n_pellets
self.range_sqr = range * range
return self
end
function g_ray_entity_collides(rel_pos_x, rel_pos_y, dir_x, dir_y, hitbox_rad)
if (rel_pos_x * dir_x + rel_pos_y * dir_y) / math.sqrt(rel_pos_x * rel_pos_x + rel_pos_y * rel_pos_y) > 0 then --in front
local dist_perp = math.abs(dir_x * rel_pos_y - dir_y * rel_pos_x)
if dist_perp < hitbox_rad then
return true
end
end
return false
end
-- Target Types
-- 0: Player
-- 1: Enemies
function Weapon:shoot(entity, target_type)
local math_cos = math.cos
local math_sin = math.sin
local math_random = math.random
local math_floor = math.floor
local math_abs = math.abs
local ray_entity_collides = g_ray_entity_collides
sfx(0)
self.ammo = self.ammo - 1
for _=1,self.n_pellets do
local angle = (math_random() - 0.5) * self.double_spread_angle
local dir_x = entity.dir_x * math_cos(angle) - entity.dir_y * math_sin(angle)
local dir_y = entity.dir_x * math_sin(angle) + entity.dir_y * math_cos(angle)
local damage = math_floor(math_random(self.min_dmg, self.max_dmg))
local map_x = math_floor(entity.pos_x)
local map_y = math_floor(entity.pos_y)
local delta_dist_x = math_abs(1 / dir_x)
local delta_dist_y = math_abs(1 / dir_y)
local step_x
local side_dist_x
if dir_x < 0 then
step_x = -1
side_dist_x = (entity.pos_x - map_x) * delta_dist_x
else
step_x = 1
side_dist_x = (map_x + 1.0 - entity.pos_x) * delta_dist_x
end
local step_y
local side_dist_y
if dir_y < 0 then
step_y = -1
side_dist_y = (entity.pos_y - map_y) * delta_dist_y
else
step_y = 1
side_dist_y = (map_y + 1.0 - entity.pos_y) * delta_dist_y
end
-- DDA
local side
while true do
if side_dist_x < side_dist_y then
side_dist_x = side_dist_x + delta_dist_x
map_x = map_x + step_x
side = 0
else
side_dist_y = side_dist_y + delta_dist_y
map_y = map_y + step_y
side = 1
end
tile_data = mget(map_x, map_y)
if tile_data > 0
and tile_data // 16 < 8 then --if more than half-height
break
end
end
local wall_dist
if side == 0 then
wall_dist = side_dist_x - delta_dist_x
else --side == 1
wall_dist = side_dist_y - delta_dist_y
end
local wall_dist_x = dir_x * wall_dist
local wall_dist_y = dir_y * wall_dist
local wall_dist_sqr = wall_dist * wall_dist
local max_dist_sqr = math.min(wall_dist_sqr, self.range_sqr)
if target_type == 0 then
local player = g_player
local rel_pos_x = player.pos_x - entity.pos_x
local rel_pos_y = player.pos_y - entity.pos_y
local dist_sqr = rel_pos_x * rel_pos_x + rel_pos_y * rel_pos_y
if dist_sqr < max_dist_sqr
and ray_entity_collides(rel_pos_x, rel_pos_y, dir_x, dir_y, player.hitbox_rad) then
player:damage(damage * g_ENEMY_WEAPON_SCALING[g_settings.difficulty])
end
else --target_type == 1
local enemies = g_enemies
local closest_enemy
local closest_enemy_dist_sqr = 9999
for _,enemy in pairs(enemies) do
if enemy ~= nil then
local rel_pos_x = enemy.pos_x - entity.pos_x
local rel_pos_y = enemy.pos_y - entity.pos_y
local dist_sqr = rel_pos_x * rel_pos_x + rel_pos_y * rel_pos_y
if closest_enemy_dist_sqr > dist_sqr
and ray_entity_collides(rel_pos_x, rel_pos_y, dir_x, dir_y, enemy.hitbox_rad) then
closest_enemy = enemy
closest_enemy_dist_sqr = dist_sqr
end
end
end
if closest_enemy_dist_sqr < max_dist_sqr then
closest_enemy:damage(damage * g_WEAPON_SCALING[g_settings.difficulty])
local dist = math.sqrt(closest_enemy_dist_sqr) - 0.1
hitmarker_set(
entity.pos_x + dir_x * dist,
entity.pos_y + dir_y * dist,
1
)
elseif wall_dist_sqr == max_dist_sqr then
if side == 0 then
local dx = step_x * -0.1
hitmarker_set(entity.pos_x + wall_dist_x + dx, entity.pos_y + wall_dist_y + dx * dir_y / dir_x, 0)
else --side == 1
local dy = step_y * -0.1
hitmarker_set(entity.pos_x + wall_dist_x + dy * dir_x / dir_y, entity.pos_y + wall_dist_y + dy, 0)
end
end
end
end
end
Item = {
id = 0,
pos_x = 0,
pos_y = 0,
type = 0,
value = 0,
sprite = nil,
}
Item.__index = Item
-- Item types
-- 0: Health
-- 1: Ammo pistol
-- 2: Ammo shotgun
-- 3: Key
-- Keys
-- 1: Gold
-- 2: Silver
function Item.new(id, pos_x, pos_y, type, value)
local self = setmetatable({}, Item)
self.id = id
self.pos_x = pos_x
self.pos_y = pos_y
self.type = type
self.value = value
if type == 3 then
self.sprite = Sprite.new(pos_x, pos_y, 69 + value, 8, 4, 0.75)
else
self.sprite = Sprite.new(pos_x, pos_y, 64 + 2 * type, 4, 4, 0.75)
end
return self
end
function Item:process(_delta)
local player = g_player
local rel_pos_x = player.pos_x - self.pos_x
local rel_pos_y = player.pos_y - self.pos_y
local dist_sqr = rel_pos_x * rel_pos_x + rel_pos_y * rel_pos_y
if dist_sqr < 0.04 then
if self.type == 0 then
player.health = player.health + self.value
elseif self.type == 1 then
local weapon = g_WEAPONS[2]
weapon.ammo = weapon.ammo + self.value
elseif self.type == 2 then
local weapon = g_WEAPONS[3]
weapon.ammo = weapon.ammo + self.value
else --self.type == 3
player.keys[self.value] = 1
end
g_items[self.id] = nil
end
end
Hitmarker = {
visible = false,
timer = 0,
sprite = nil,
}
Hitmarker.__index = Hitmarker
function Hitmarker.new()
local self = setmetatable({}, Hitmarker)
self.sprite = Sprite.new(0, 0, 0, 6, 6, 0)
return self
end
-- Hitmarker types
-- 0: wall
-- 1: enemy
function hitmarker_set(pos_x, pos_y, type)
for _,hitmarker in pairs(g_hitmarkers) do
if not hitmarker.visible then
hitmarker.timer = 0
hitmarker.visible = true
hitmarker.sprite.pos_x = pos_x
hitmarker.sprite.pos_y = pos_y
hitmarker.sprite.tex_id = 59 + type
break
end
end
end
function Hitmarker:process(delta)
if self.visible then
self.timer = self.timer + delta
if self.timer > 200 then
self.visible = false
end
end
end
Player = {
pos_x = 0,
pos_y = 0,
dir_x = -1,
dir_y = 0,
plane_x = 0,
plane_y = 0.8,
speed_move = 0.003,
weapon_idx = 1,
weapon_state = 0,
weapon_timer = 0,
health = 100,
hitbox_rad = 0.4,
move = g_entity_move,
keys = {
[1] = 0,
[2] = 0,
}
}
Player.__index = Player
function Player.new(pos_x, pos_y)
local self = setmetatable({}, Player)
self.pos_x = pos_x
self.pos_y = pos_y
return self
end
function Player:rotate(delta)
local speed = g_settings.turn_speed * delta
local old_dir_x = self.dir_x
local math_cos = math.cos
local math_sin = math.sin
self.dir_x = self.dir_x * math_cos(speed) - self.dir_y * math_sin(speed)
self.dir_y = old_dir_x * math_sin(speed) + self.dir_y * math_cos(speed)
local old_plane_x = self.plane_x
self.plane_x = self.plane_x * math_cos(speed) - self.plane_y * math_sin(speed)
self.plane_y = old_plane_x * math_sin(speed) + self.plane_y * math_cos(speed)
end
function Player:damage(value)
if self.health > 0 then
self.health = self.health - value
if self.health <= 0 then
g_game_state = 2
g_timer = 0
end
end
end
function Player:process(delta)
self.weapon_timer = self.weapon_timer + delta
local weapon = g_WEAPONS[self.weapon_idx]
if btn(2) then
self:rotate(delta)
elseif btn(3) then
self:rotate(-delta)
end
if btn(0) then
self:move(self.speed_move * delta)
elseif btn(1) then
self:move(-self.speed_move * delta)
end
if btnp(4) then
if self.weapon_state == 2 then
self.weapon_state = 1
self.weapon_timer = 0
end
elseif btnp(5) and self.weapon_state == 2 and weapon.ammo > 0 then
self.weapon_state = 3
self.weapon_timer = 0
weapon:shoot(self, 1)
end
if self.weapon_state == 0 then
if self.weapon_timer >= weapon.raise_time then
self.weapon_state = 2
end
elseif self.weapon_state == 1 then
if self.weapon_timer >= weapon.raise_time then
self.weapon_state = 0
self.weapon_timer = 0
self.weapon_idx = self.weapon_idx % 3 + 1
end
elseif self.weapon_state == 3 then
if self.weapon_timer >= weapon.shoot_time then
if weapon.reload_time == 0 then
self.weapon_state = 2
else
self.weapon_state = 4
self.weapon_timer = 0
end
end
elseif self.weapon_state == 4 then
if self.weapon_timer >= weapon.reload_time then
self.weapon_state = 2
end
end
end
Sprite = {
pos_x = 0,
pos_y = 0,
tex_id = 0,
scl_horiz = 1,
scl_vert = 1,
offset_vert = 0, --from 0.5 (floor) to -0.5 (ceiling)
screen_offset_vert = 0,
screen_width = 0,
screen_height = 0,
dist = 0, --Distance to player (negative if not in viewing triangle)
screen_x = 0,
draw_start_y = 0,
draw_end_y = 0,
draw_start_x = 0,
draw_end_x = 0,
}
Sprite.__index = Sprite
function Sprite.new(pos_x, pos_y, tex_id, scl_horiz, scl_vert, offset_vert)
local self = setmetatable({}, Sprite)
self.pos_x = pos_x
self.pos_y = pos_y
self.tex_id = tex_id
self.scl_horiz = scl_horiz
self.scl_vert = scl_vert
self.offset_vert = offset_vert
return self
end
function Sprite:process(inv_det)
local SCREEN_WIDTH = g_SCREEN_WIDTH
local SCREEN_HEIGHT = g_SCREEN_HEIGHT
local SCREEN_HALF_HEIGHT = SCREEN_HEIGHT / 2
local player = g_player
local math_abs = math.abs
local rel_x = self.pos_x - player.pos_x
local rel_y = self.pos_y - player.pos_y
self.dist = math.sqrt(rel_x * rel_x + rel_y * rel_y)
local trans_y = inv_det * (player.plane_x * rel_y - player.plane_y * rel_x)
if trans_y <= 0 then
self.dist = -self.dist
return
end
local trans_x = inv_det * (player.dir_y * rel_x - player.dir_x * rel_y)
self.screen_x = math.floor((SCREEN_WIDTH * 0.5) * (1 + trans_x / trans_y))
self.screen_width = math_abs(SCREEN_HEIGHT / trans_y) / self.scl_horiz
self.draw_start_x = self.screen_x - self.screen_width // 2
if self.draw_start_x < 0 then
self.draw_start_x = 0
elseif self.draw_start_x >= SCREEN_WIDTH then
self.dist = -self.dist
return
end
self.draw_end_x = self.screen_x + self.screen_width // 2
if self.draw_end_x >= SCREEN_WIDTH then
self.draw_end_x = SCREEN_WIDTH - 1
elseif self.draw_end_x < 0 then
self.dist = -self.dist
return
end
self.screen_height = math_abs(SCREEN_HEIGHT / trans_y) / self.scl_vert
self.screen_offset_vert = SCREEN_HALF_HEIGHT * self.offset_vert // trans_y
self.draw_start_y = SCREEN_HALF_HEIGHT - self.screen_height // 2 + self.screen_offset_vert
if self.draw_start_y < 0 then
self.draw_start_y = 0
end
self.draw_end_y = SCREEN_HALF_HEIGHT + self.screen_height // 2 + self.screen_offset_vert
if self.draw_end_y >= SCREEN_HEIGHT then
self.draw_end_y = SCREEN_HEIGHT - 1
end
end
function Sprite:set_enemy_tex(tex_id)
self.tex_id = tex_id
self.scl_horiz = 48 / g_SPRITE_SIZES[tex_id][1]
end
Enemy = {
id = 0,
pos_x = 0,
pos_y = 0,
dir_x = -1,
dir_y = 0,
state = 0,
timer = 0,
tex_ids = {},
sprite = nil,
weapon = nil,
health = 0,
activate_dist_sqr = 0,
shoot_dist_sqr = 0,
die_time = 0,
speed_move = 0,
hitbox_rad = 0,
pain_time = 0,
pain_chance = 0,
}
Enemy.__index = Enemy
g_enemy_pistol_tex_ids = {192, 194, 195, 196, 198, 200, 202, 204, 96, 206, 76}
g_enemy_shotgun_tex_ids = {128, 130, 131, 132, 134, 136, 138, 140, 100, 142, 78}
-- Enemy types
-- 0: pistol
-- 1: shotgun
-- Enemy states
-- 0: neutral
-- 1: raise
-- 2: ready
-- 3: shoot
-- 4: reload
-- 5: die
-- 6: hurt
-- Enemy sprites
-- 01: forward
-- 02: left
-- 03: right
-- 04: back
-- 05: raise
-- 06: ready
-- 07: shoot
-- 08: die
-- 09: corpse
-- 10: walk
-- 11: pain
function Enemy.new(id, type, pos_x, pos_y, angle)
local self = setmetatable({}, Enemy)
self.id = id
self.pos_x = pos_x
self.pos_y = pos_y
self.activate_dist_sqr = 64
self.shoot_dist_sqr = 16
self.dir_x = math.cos(angle)
self.dir_y = math.sin(angle)
if type == 0 then
self.tex_ids = g_enemy_pistol_tex_ids
self.sprite = Sprite.new(pos_x, pos_y, 0, 1, 1.5, 0.25)
self.weapon = g_ENEMY_WEAPONS[1]
self.health = 30
self.die_time = 1000
self.speed_move = 0.002
self.hitbox_rad = 0.3
self.pain_time = 170
self.pain_chance = 0.7
elseif type == 1 then
self.tex_ids = g_enemy_shotgun_tex_ids
self.sprite = Sprite.new(pos_x, pos_y, 0, 1, 1.5, 0.25)
self.weapon = g_ENEMY_WEAPONS[2]
self.health = 60
self.die_time = 1000
self.speed_move = 0.0015
self.hitbox_rad = 0.3
self.pain_time = 210
self.pain_chance = 0.61
end
return self
end
function Enemy:move(speed)
g_entity_move(self, speed)
self.sprite.pos_x = self.pos_x
self.sprite.pos_y = self.pos_y
end
function Enemy:process(delta)
local player = g_player
local rel_pos_x = player.pos_x - self.pos_x
local rel_pos_y = player.pos_y - self.pos_y
self.dist_sqr = rel_pos_x * rel_pos_x + rel_pos_y * rel_pos_y
local angle = math.atan2(
self.dir_x * rel_pos_y - self.dir_y * rel_pos_x,
self.dir_x * rel_pos_x + self.dir_y * rel_pos_y)
local abs_angle = math.abs(angle)
self.timer = self.timer + delta
if self.state == 0 then
if self.dist_sqr <= 1
or (abs_angle <= 0.5235988 --30 deg
and self.activate_dist_sqr >= self.dist_sqr) then
self.state = 1
self.timer = 0
self.sprite:set_enemy_tex(self.tex_ids[5])
else
local tex_idx
if abs_angle <= 0.785398163397 then --pi / 4
tex_idx = 1
elseif abs_angle >= 2.35619449019 then --3 * pi / 4
tex_idx = 4
elseif angle > 0 then
tex_idx = 2
else
tex_idx = 3
end
self.sprite:set_enemy_tex(self.tex_ids[tex_idx])
end
elseif self.state == 1 then
if self.timer >= self.weapon.raise_time then
self.state = 2
self.sprite:set_enemy_tex(self.tex_ids[6])
end
elseif self.state == 2 then
local math_floor = math.floor
local math_abs = math.abs
local inv_mag = 1 / math.sqrt(self.dist_sqr)
self.dir_x = rel_pos_x * inv_mag
self.dir_y = rel_pos_y * inv_mag
local map_x = math_floor(self.pos_x)
local map_y = math_floor(self.pos_y)
local delta_dist_x = math_abs(1 / self.dir_x)
local delta_dist_y = math_abs(1 / self.dir_y)
local step_x
local side_dist_x
if self.dir_x < 0 then
step_x = -1
side_dist_x = (self.pos_x - map_x) * delta_dist_x
else
step_x = 1
side_dist_x = (map_x + 1.0 - self.pos_x) * delta_dist_x
end
local step_y
local side_dist_y
if self.dir_y < 0 then
step_y = -1
side_dist_y = (self.pos_y - map_y) * delta_dist_y
else
step_y = 1
side_dist_y = (map_y + 1.0 - self.pos_y) * delta_dist_y
end
-- DDA LOS check
local side
while true do
if side_dist_x < side_dist_y then
side_dist_x = side_dist_x + delta_dist_x
map_x = map_x + step_x
side = 0
else
side_dist_y = side_dist_y + delta_dist_y
map_y = map_y + step_y
side = 1
end
tile_data = mget(map_x, map_y)
if tile_data > 0
and tile_data // 16 < 8 then --if more than half-height
break
end
end
local wall_dist_sqr
if side == 0 then
wall_dist_sqr = side_dist_x - delta_dist_x
else --side == 1
wall_dist_sqr = side_dist_y - delta_dist_y
end
wall_dist_sqr = wall_dist_sqr * wall_dist_sqr
if self.shoot_dist_sqr < self.dist_sqr --player too far
or wall_dist_sqr < self.dist_sqr then --player not visible
self:move(self.speed_move * delta)
local tex_idx
if self.timer // 500 % 2 == 0 then --TODO:un-hardcode
tex_idx = 6
else
tex_idx = 10
end
self.sprite:set_enemy_tex(self.tex_ids[tex_idx])
else
self.state = 3
self.timer = 0
self.weapon:shoot(self, 0)
self.sprite:set_enemy_tex(self.tex_ids[7])
end
elseif self.state == 3 then
if self.timer >= self.weapon.shoot_time then
self.state = 4
self.timer = 0
self.sprite:set_enemy_tex(self.tex_ids[6])
end
elseif self.state == 4 then
if self.timer >= self.weapon.reload_time then
self.state = 2
end
elseif self.state == 5 then
if self.timer >= self.die_time then
self.sprite.tex_id = self.tex_ids[9]
self.sprite.scl_horiz = 1
self.sprite.scl_vert = 4
self.sprite.offset_vert = 0.75
g_enemies[self.id] = nil
table.insert(g_sprites, self.sprite)
end
elseif self.state == 6 then
if self.timer >= self.pain_time then
self.state = 2
self.sprite:set_enemy_tex(self.tex_ids[6])
end
end
end
function Enemy:damage(value)
if self.health > 0 then
self.health = self.health - value
if self.health <= 0 then
self.sprite:set_enemy_tex(self.tex_ids[8])
self.state = 5
self.timer = 0
else
if (not (self.state == 6))
and math.random() < self.pain_chance then
self.state = 6
self.timer = 0
self.sprite:set_enemy_tex(self.tex_ids[11])
elseif self.state == 0 then
self.state = 2
self.sprite:set_enemy_tex(self.tex_ids[6])
end
end
end
end
Door = {
map_x = 0,
map_y = 0,
opening = false,
closed = true,
type = 1,
tex_id = 0,
height = 0,
timer = 0,
}
Door.__index = Door
function Door.new(map_x, map_y, tex_id, type)
local self = setmetatable({}, Door)
self.map_x = map_x
self.map_y = map_y
self.tex_id = tex_id
self.type = type
return self
end
function Door:process(delta)
if self.closed then
if self.opening then
self.timer = self.timer + delta
if self.timer > 100 then
if self.height == 15 then
self.closed = false
mset(self.map_x, self.map_y, 0)
else
self.timer = 0
self.height = self.height + 1
mset(self.map_x, self.map_y, self.tex_id + 16 * self.height)
end
end
else
local player = g_player
if player.keys[self.type] == 1 then
local rel_pos_x = player.pos_x - self.map_x - 0.5
local rel_pos_y = player.pos_y - self.map_y - 0.5
local dist_sqr = rel_pos_x * rel_pos_x + rel_pos_y * rel_pos_y
if dist_sqr < 2.0 then
self.opening = true
end
end
end
end
end
function g_get_tex_pixel(offset, id, x, y)
return peek4(offset + 0x40 * (id + 16 * (y // 8) + x // 8) + 0x8 * (y % 8) + (x % 8))
end
function init()
g_SCREEN_WIDTH = 240
g_SCREEN_HEIGHT = 120
g_LEVEL_WIDTH = 113
g_LEVEL_HEIGHT = 32
g_SPRITE_SIZES = {
[0]= {16,16},
[2]= {16,16},
[4]= {16,16},
[6]= {16,16},
[8]= {16,16},
--Enemy pistol
[192]={16,32},
[194]={8 ,32},
[195]={8 ,32},
[196]={16,32},
[198]={16,32},
[200]={16,32},
[202]={16,32},
[204]={16,32},
[96]= {32,16},
[206]={16,32},
[76]= {16,32},
--Enemy shotgun
[128]={16,32},
[130]={8 ,32},
[131]={8 ,32},
[132]={16,32},
[134]={16,32},
[136]={16,32},
[138]={16,32},
[140]={16,32},
[100]={32,16},
[142]={16,32},
[78]= {16,32},
--Items
[64]= {16,16},
[66]= {16,16},
[68]= {16,16},
[70]= {8 ,16},
[71]= {8 ,16},
--Hitmarkers
[59]= {8, 8 },
[60]= {8, 8 },
}
g_TEX_MAP = {
[1]=1,
[2]=3,
[3]=5,
[4]=7,
[5]=9,
[6]=11,
[7]=13,
[8]=33,
[9]=35,
[10]=37,
[11]=39,
[12]=41,
[13]=43,
}
g_player = Player.new(10, 7)
g_prev_time = 0
g_sprites = {
Sprite.new(8.5, 5.5, 2, 2, 1, 0.125),
Sprite.new(45.5, 27.5, 6, 2, 2, -0.5),
Sprite.new(40, 14, 6, 2, 2, -0.5),
Sprite.new(38.5, 27.5, 0, 2, 2, 0.5),
Sprite.new(32.5, 18.5, 8, 1, 1, 0),
Sprite.new(32.5, 21.5, 8, 1, 1, 0),
Sprite.new(36.5, 15.5, 4, 2, 2, 0.5),
Sprite.new(62.5, 17.5, 6, 2, 2, -0.5),
Sprite.new(71.5, 17.5, 6, 2, 2, -0.5),
Sprite.new(80.5, 17.5, 6, 2, 2, -0.5),
Sprite.new(89.5, 17.5, 6, 2, 2, -0.5),
Sprite.new(98.5, 17.5, 6, 2, 2, -0.5),
Sprite.new(107.5, 17.5, 6, 2, 2, -0.5),
Sprite.new(62.5, 25.5, 6, 2, 2, -0.5),
Sprite.new(71.5, 25.5, 6, 2, 2, -0.5),
Sprite.new(80.5, 25.5, 6, 2, 2, -0.5),
Sprite.new(89.5, 25.5, 6, 2, 2, -0.5),
Sprite.new(98.5, 25.5, 6, 2, 2, -0.5),
Sprite.new(107.5, 25.5, 6, 2, 2, -0.5),
}
g_WEAPONS = {
Weapon.new(204, 1e9, 107, {234, 228}, 300, 250, 0 , 0.05, 2, 20, 1, 1 ),
Weapon.new(200, 0 , 113, {234, 228}, 350, 350, 0 , 0.05, 5, 15, 1, 32),
Weapon.new(196, 0 , 113, {222, 216, 210}, 400, 350, 350, 0.12, 5, 15, 7, 32),
}
g_ENEMY_WEAPONS = {
Weapon.new(0, 99999999, 0, nil, 0, 350, 0 , 0.4, 3, 15, 1, 32),
Weapon.new(0, 99999999, 0, nil, 0, 350, 350, 0.4, 3, 15, 3, 32),
}
g_ENEMY_WEAPON_SCALING = {
[1] = 0.65,
[2] = 1.0,
[3] = 1.35,
}
g_WEAPON_SCALING = {
[1] = 1.2,
[2] = 1.0,
[3] = 0.9,
}
g_enemies = {
[1]=Enemy.new(1, 0, 8.5, 10.5, 1.57),
[2]=Enemy.new(2, 0, 23.5, 16.5, 1.57),
[3]=Enemy.new(3, 0, 23.5, 23.5, -1.57),
[4]=Enemy.new(4, 0, 45.5, 26.5, 1.57),
[5]=Enemy.new(5, 0, 45.5, 28.5, -1.57),
[6]=Enemy.new(6, 1, 43.5, 14.5, 3.14),
[7]=Enemy.new(7, 0, 50.5, 13.5, 0),
[8]=Enemy.new(8, 0, 47.5, 13.5, 0),
[9]=Enemy.new(9, 0, 76, 15.5, 1.57),
[10]=Enemy.new(10, 1, 67, 15.5, 1.57),
[11]=Enemy.new(11, 0, 85, 15.5, 1.57),
[12]=Enemy.new(12, 1, 94, 15.5, 1.57),
[13]=Enemy.new(13, 0, 103, 15.5, 1.57),
[14]=Enemy.new(14, 0, 76, 27.5, -1.57),
[15]=Enemy.new(15, 1, 67, 27.5, -1.57),
[16]=Enemy.new(16, 0, 85, 27.5, -1.57),
[17]=Enemy.new(17, 1, 94, 27.5, -1.57),
[18]=Enemy.new(18, 0, 103, 27.5, -1.57),
[19]=Enemy.new(19, 1, 95.5, 21.5, 3.14),
[20]=Enemy.new(20, 0, 55.5, 19.5, 1.57),
}
g_items = {
[1]= Item.new(1, 45.5, 27.5, 3, 2),
[2]= Item.new(2, 59.5, 27.5, 3, 1),
[3]= Item.new(3, 10.5, 13.5, 0, 35),
[4]= Item.new(4, 6.5, 13.5, 1, 15),
[5]= Item.new(5, 36.5, 27.5, 2, 4),
[6]= Item.new(6, 45.5, 6.5, 0, 15),
[7]= Item.new(7, 47.5, 13.5, 2, 2),
[8]= Item.new(8, 56.5, 6.5, 2, 2),
[9]= Item.new(9, 55.5, 21.5, 0, 25),
[10]=Item.new(10, 40.5, 27.5, 1, 15),
[11]=Item.new(11, 37.5, 14.5, 1, 15),
[12]=Item.new(12, 60.5, 21.5, 0, 50),
[13]=Item.new(13, 65.5, 21.5, 1, 20),
[14]=Item.new(14, 70.5, 21.5, 2, 6),
}
g_doors = {
Door.new(110, 21, 6, 1),
Door.new(57, 21, 7, 2),
}
g_NUM_HITMARKERS = 7
g_hitmarkers = {}
for i=1,g_NUM_HITMARKERS do
g_hitmarkers[i] = Hitmarker.new()
end
g_timer = 0
g_settings = {
floor_ceil = false,
interlace = 2, --disabled=g_interlace>=2
difficulty = 2,
debug = false,
turn_speed = 0.003,
selected_setting = 0,
sprite = 0,
}
g_pause_text = {
[true]="ON",
[false]="OFF",
[1]="EASY",
[2]="NORM",
[3]="HARD",
}
-- Game States
-- 1: Alive
-- 2: Dead
-- 3: Paused
g_game_state = 1
math.randomseed(tstamp())
end
init()
function TIC()
local t = time()
local delta = t - g_prev_time --msec since last frame
g_prev_time = t
g_timer = g_timer + delta
local game_state = g_game_state
if btnp(7) then
if game_state == 1 then
g_game_state = 3
g_timer = 0
g_settings.selected_setting = 0
elseif game_state == 3 then
g_game_state = 1
end
end
if game_state == 1 then
game_process(delta)
elseif game_state == 2 then
death_process(delta)
elseif game_state == 3 then
pause_process(delta)
end
end
function death_process(delta)
if g_timer >= 3000 then
reset()
end
local dist = math.floor(g_timer * 0.0272)
for i=0,7 do
spr(328, i * 32, dist, 0, 1, 0, 0, 4, 1)
end
end