-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
1504 lines (1194 loc) · 35.7 KB
/
main.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
-- [SublimeLinter luacheck-globals:term,colours,textutils,keys,fs]
-- Zombease - a top-down zombie survival shooter by viluon
--TODO: Melee/ranged/knockback resistance
-- Passed from launcher (menu.lua)
local args = { ... }
local settings = args[ 1 ]
local terminal = args[ 2 ]
local terminal_scale = args[ 3 ]
local root = args[ 4 ]
if not require or type( settings ) ~= "table" then
print( "wiLL kIll you But RUn mEnU.LUa FiRst" )
error( " -- nOT thEmm zOMbiez", 0 )
end
-- Imports
local bump = require "utils.bump"
local buffer = require "desktox.buffer"
local round = require( "desktox.utils" ).round
-- Localisation
local textutils = textutils
local coroutine = coroutine
local colours = colours
local term = term
local keys = keys
local math = math
local fs = fs
local os = os
local yield = coroutine.yield
local queue = os.queueEvent
local random = math.random
local clock = os.clock
local remove = table.remove
-- Functions
local redraw, spawn_zombie, update, fire, get_coord_speeds,
move_player, place_static, equip_weapon, reload_weapon,
place_pickup, bake_weapon, collision_filter,
save_state, has_weapon
-- Display setup
local main_window = terminal or ( settings.multishell_support and term.current() or term.native() )
if main_window.setTextScale and terminal_scale then
main_window.setTextScale ( terminal_scale )
end
local w, h = main_window.getSize()
local main_buf = buffer.new( 0, 0, w, h )
local overlay_buf = buffer.new( 0, 0, w, h, main_buf, -1, -2, "\0" )
-- Constants
local WAVE_PREPARATION_TIME = 10
local MAX_ZOMBIES = 30
--- Under what amount of bullets left in the clip should the counter change colour
--TODO: This could be based on the weapon's RPS
local BULLETS_LEFT_WARNING = 2
local framerate_cap = settings.limit_FPS
-- Data
local now = clock()
local end_time
local start_time = now
--local stuff = "interestring"
local running = true
local HUD_enabled = true
local auto_reload = true
local selected_weapon_index
--- Stats
local damage_dealt = 0
local zombies_spawned = 0
local pickup_taken_count = 0
local pickup_dropped_count = 0
local kills = 0
---- Imagine Dragons
local shots = 0
local melee_swings = 0
local hits = 0
local melee_hits = 0
local wave_count = 0
---- Technical
local time_spent_rendering = 0
local time_spent_updating = 0
local time_spent_waiting = 0
local report_performance = settings.report_performance
------ Rendering details
local time_spent_clearing = 0
local time_spent_drawing_overlay = 0
local time_spent_drawing_borders = 0
local time_spent_drawing_drawables = 0
local time_spent_drawing_background = 0
local time_spent_rendering_main_buffer = 0
local time_spent_rendering_overlay_buffer = 0
local next_wave_countdown = -math.huge
--- Held keys and buttons
local held = { mouse = {} }
local world = bump.newWorld( 8 )
local world_size = {
x = 50;
y = 50;
}
local camera_offset = {
x = 0;
y = 0;
}
local weapon_kinds = {}
local bullet_kinds = {}
--- Load assets
--TODO: Generalize this code
local weapons = {}
local weapon_dir = root .. "assets/weapons/"
for _, name in ipairs( fs.list( weapon_dir ) ) do
local f = io.open( weapon_dir .. name, "r" )
name = name:gsub( "%.tbl", "" )
if f then
local contents = f:read( "*a" )
f:close()
local fn = loadstring( contents, name )
if fn then
setfenv( fn, { colours = colours; [ "colors" ] = colours } )
local ok, result = pcall( fn )
if ok then
weapon_kinds[ name ] = result
weapons[ name ] = {
kind = result;
}
else
error( "Loading asset " .. weapon_dir .. name .. " failed:\n\t" .. result )
end
end
end
end
local player = {
type = "player";
text = "^";
bg = colours.brown;
fg = colours.black;
health = 100;
last_moved = -1;
movement_speed = 0.1;
reload_time_multiplier = 1;
-- New save inventory (what you begin with)
inventory = {
ammunition = {
generic = 4;
};
weapons = {
weapons.fists,
};
attachments = {
};
};
}
if settings.difficulty == 1 then
player.reload_time_multiplier = 2 / 3
elseif settings.difficulty == 2 then
player.reload_time_multiplier = 1
elseif settings.difficulty == 3 then
player.reload_time_multiplier = 1.5
end
--- Load bullet kinds
local bullet_dir = root .. "assets/bullets/"
for _, name in ipairs( fs.list( bullet_dir ) ) do
local f = io.open( bullet_dir .. name, "r" )
name = name:gsub( "%.tbl", "" )
if f then
local contents = f:read( "*a" )
f:close()
local fn = loadstring( contents, name )
if fn then
setfenv( fn, { colours = colours; [ "colors" ] = colours } )
local ok, result = pcall( fn )
if ok then
bullet_kinds[ name ] = result
player.inventory.ammunition[ name ] = player.inventory.ammunition[ name ] or 0
else
error( "Loading asset " .. bullet_dir .. name .. " failed:\n\t" .. result )
end
end
end
end
local attachment_kinds = {}
--- Load attachment kinds
local attachments_dir = root .. "assets/attachments/"
for _, name in ipairs( fs.list( attachments_dir ) ) do
local f = io.open( attachments_dir .. name, "r" )
name = name:gsub( "%.tbl", "" )
if f then
local contents = f:read( "*a" )
f:close()
local fn = loadstring( contents, name )
if fn then
setfenv( fn, { colours = colours; [ "colors" ] = colours } )
local ok, result = pcall( fn )
if ok then
attachment_kinds[ name ] = result
else
error( "Loading asset " .. attachments_dir .. name .. " failed:\n\t" .. result )
end
end
end
end
player.x = 10
player.y = 6
local zombie_damage_multiplier
if settings.difficulty == 1 then
zombie_damage_multiplier = 2 / 3
elseif settings.difficulty == 2 then
zombie_damage_multiplier = 1
elseif settings.difficulty == 3 then
zombie_damage_multiplier = 1.5
end
local zombie_kinds = {
[ 1 ] = {
name = "generic";
text = "&";
bg = colours.black;
fg = colours.green;
-- How much time (in seconds) until the zombie can move
movement_speed = 0.6;
damage = 8 * zombie_damage_multiplier;
health = 20;
-- Since which wave can the kind spawn
difficulty = 1;
drop = {
-- Pistol ammo
{ probability = 0.45; item = 1; };
-- Pistol weapon
{ probability = 0.33; item = 4; };
};
};
[ 2 ] = {
name = "runner";
text = "$";
bg = colours.black;
fg = colours.lime;
movement_speed = 0.25;
damage = 4 * zombie_damage_multiplier;
health = 12;
difficulty = 2;
drop = {
{ probability = 0.05; item = 1; };
{ probability = 0.2; item = 3; };
};
};
[ 3 ] = {
name = "heavyduty";
text = "@";
bg = colours.grey;
fg = colours.lime;
movement_speed = settings.difficulty == 3 and 0.6 or 0.95;
damage = 26 * zombie_damage_multiplier;
health = 55;
difficulty = 3;
drop = {
{ probability = 0.4; item = 2; };
{ probability = 0.15; item = 5; };
};
};
[ 4 ] = {
name = "mercenary";
text = "\1";
bg = colours.lime;
fg = colours.black;
movement_speed = 0.5;
damage = 10 * zombie_damage_multiplier;
health = 40;
difficulty = 5;
drop = {
{ probability = 0.4; item = 1; };
{ probability = 0.2; item = 2; };
{ probability = 0.05; item = 6; };
}
}
}
local zombies = {
target = main_buf;
}
local bullets = {
target = overlay_buf;
}
local statics = {
target = main_buf;
}
local pickup_kinds = {
[ 1 ] = {
name = "pistol_ammo";
text = "=";
bg = colours.black;
fg = colours.lightGrey;
life = 5;
on_pickup = function()
player.inventory.ammunition.generic = player.inventory.ammunition.generic + random( 1, 5 )
end;
};
[ 2 ] = {
name = "assault_ammo";
text = "\19"; -- two exclamation marks
bg = colours.black;
fg = colours.lightGrey;
life = 8;
on_pickup = function()
player.inventory.ammunition.assault = player.inventory.ammunition.assault + random( 10, 50 )
end;
};
[ 3 ] = {
name = "small_health";
text = "\3";
bg = colours.black;
fg = colours.pink;
life = 10;
on_pickup = function()
player.health = player.health + 10
end;
};
[ 4 ] = {
name = "pistol";
weapon = weapons.pistol;
text = weapons.pistol.kind.text;
bg = colours.black;
fg = colours.white;
life = 15;
on_pickup = function()
if not has_weapon( weapons.pistol ) then
player.inventory.weapons[ #player.inventory.weapons + 1 ] = weapons.pistol
end
end;
};
[ 5 ] = {
name = "assault_rifle";
weapon = weapons.assault_rifle;
text = weapons.assault_rifle.kind.text;
bg = colours.black;
fg = colours.white;
life = 15;
on_pickup = function()
if not has_weapon( weapons.assault_rifle ) then
player.inventory.weapons[ #player.inventory.weapons + 1 ] = weapons.assault_rifle
end
end;
};
[ 6 ] = {
name = "katana";
weapon = weapons.katana;
text = weapons.katana.kind.text;
bg = colours.black;
fg = colours.white;
life = 15;
on_pickup = function()
if not has_weapon( weapons.katana ) then
player.inventory.weapons[ #player.inventory.weapons + 1 ] = weapons.katana
end
end;
};
}
local pickups = {
target = main_buf;
}
-- Drawable stuffs, in order of rendering
local drawables = { statics, pickups, zombies, bullets }
-- Stuffs that can perish over time
local ephemerals = { statics, pickups, bullets }
-- Stuffs that collide
local physicals = { zombies, bullets }
--- Props
local blood = {
text = "\215";
bg = colours.black;
fg = colours.red;
-- This blood has a life! (In seconds)
life = 10;
}
-- FPS counter
local frames = 0
local last_fps = 0
local last_fps_reset = 0
local FPS_RESET_INTERVAL = 0.5
--- Calculate the x and y speeds of a bullet from a mouse click.
function get_coord_speeds( x, y, max )
local scale = math.sqrt( x * x + y * y ) / max
return x / scale, y / scale
end
--- The universal collision filter describing interactions in the game world. For use with world:move().
-- @param item description
-- @param other description
-- @return The type of collision for bump.lua to use
function collision_filter( item, other )
if other.type == "pickup" or item.type == "pickup" then
return "cross"
end
return "slide"
end
--- Redraw the view.
-- @return nil
function redraw()
local time
local local_now
if report_performance then
local_now = clock()
time = local_now
end
overlay_buf:clear( -1, -2, "\0" )
main_buf:clear( colours.black )
if report_performance then
local_now = clock()
time_spent_clearing = time_spent_clearing + local_now - time
time = local_now
end
--stuff = next_wave_countdown
-- Draw the background
local line_number = math.floor( camera_offset.y / 6 )
for y = camera_offset.y % 6, h - 1, 6 do
for x = camera_offset.x % 10 - 10, w - 1, 10 do
main_buf:write( x + line_number % 2 * 4, y, "\127", colours.black, colours.grey )
end
line_number = line_number + 1
end
line_number = math.floor( camera_offset.y / 6 )
for y = camera_offset.y % 6 - 3, h - 1, 6 do
for x = camera_offset.x % 10 - 5, w - 1, 10 do
main_buf:write( x + line_number % 2 * 4, y, ".", colours.black, colours.grey )
end
line_number = line_number + 1
end
if report_performance then
local_now = clock()
time_spent_drawing_background = time_spent_drawing_background + local_now - time
time = local_now
end
-- Draw the world borders
main_buf
:clear_column(
camera_offset.x, colours.black,
colours.grey, "*",
camera_offset.y,
camera_offset.y + world_size.y
)
:clear_column(
camera_offset.x + world_size.x, colours.black,
colours.grey, "*",
camera_offset.y,
camera_offset.y + world_size.y
)
:clear_line (
camera_offset.y, colours.black,
colours.grey, "*",
camera_offset.x,
camera_offset.x + world_size.x
)
:clear_line (
camera_offset.y + world_size.y, colours.black,
colours.grey, "*",
camera_offset.x,
camera_offset.x + world_size.x
)
if report_performance then
local_now = clock()
time_spent_drawing_borders = time_spent_drawing_borders + local_now - time
time = local_now
end
-- Draw all objects in the world
for index = 1, #drawables do
local collection = drawables[ index ]
local target = collection.target
for i = 1, #collection do
local object = collection[ i ]
local pos_y = round( object.y + camera_offset.y )
local pos_x = round( object.x + camera_offset.x )
if pos_y >= 0 and pos_y < h then
if pos_x >= 0 and pos_x < w then
-- Object is visible, draw it
local kind = object.kind
target:write( pos_x, pos_y, object.text, kind.bg, object.fg or kind.fg )
end
end
end
end
if report_performance then
local_now = clock()
time_spent_drawing_drawables = time_spent_drawing_drawables + local_now - time
time = local_now
end
-- Draw the player
main_buf:write( player.x + camera_offset.x, player.y + camera_offset.y, player.text, player.bg, player.fg )
-- Draw the overlay
--- Weapon info
local weapon_info, weapon_info_colour
if not player.weapon.melee then
local reloading = player.weapon.reload_start > now - player.weapon.reload_delay * player.reload_time_multiplier
local clip
if not reloading then
clip = player.weapon.current_clip_size
weapon_info = clip .. "/" .. player.inventory.ammunition[ player.weapon.bullet_kind ]
else
weapon_info = "reloading"
end
weapon_info_colour = reloading and colours.orange or ( clip < BULLETS_LEFT_WARNING and colours.red or colours.lightGrey )
else
if player.weapon.current_cooldown > 0 then
local percentage = round( 100 * ( 1 - player.weapon.current_cooldown / player.weapon.cooldown ) )
weapon_info = "(" .. percentage .. "%)"
weapon_info_colour = colours.orange
else
weapon_info = "melee"
weapon_info_colour = colours.lightGrey
end
end
overlay_buf
:write( 0, 0, ""
.. " Health: " .. player.health
.. " Zombies: " .. #zombies
.. " Bullets: " .. #bullets
.. " FPS: " .. last_fps,
-2, colours.lightGrey )
:write( w - #weapon_info, h - 1, weapon_info, -2, weapon_info_colour )
--- Weapon selection bar
for index, weapon in ipairs( player.inventory.weapons ) do
if weapon == player.weapon then
overlay_buf:write( ( index - 1 ) * 3, h - 1, "[ ]", -2, colours.white )
local text, bg, fg = weapon.text, -1, colours.white
if weapon.melee then
local frame = weapon.anim[ round( ( #weapon.anim - 1 ) * weapon.current_cooldown / weapon.cooldown ) + 1 ]
text, bg, fg = frame.text, frame.bg, frame.fg
end
overlay_buf:write( player.x + camera_offset.x, player.y + camera_offset.y, text, bg, fg )
end
overlay_buf:write( ( index - 1 ) * 3 + 1, h - 1, weapon.text, -2, colours.lightGrey )
end
-- Wave countdown
if next_wave_countdown > -math.huge then
local text = ""
.. " WAVE #"
.. wave_count
.. " IN "
.. ( next_wave_countdown < 5 and round( next_wave_countdown, 1 ) or math.floor( next_wave_countdown ) )
.. " "
overlay_buf:write( round( w / 2 - #text / 2 ), 5, text, -2, colours.white )
end
if report_performance then
local_now = clock()
time_spent_drawing_overlay = time_spent_drawing_overlay + local_now - time
time = local_now
end
if player.health <= 0 then
end_time = end_time or clock()
local text = " YOU ARE DEAD "
local seconds = end_time - start_time
local time_str = math.floor( seconds / 60 ) .. "m " .. math.floor( seconds % 60 ) .. "s"
overlay_buf:write( round( w / 2 - #text / 2 ), round( h / 2 ) - 4, text, colours.black, colours.white )
local stats = ""
.. " Difficulty: " .. settings.difficulty .. " \n"
.. " Zombies killed: " .. kills .. " (" .. round( kills / ( zombies_spawned + 0.000001 ) * 100 ) .. "%) \n"
.. " Time alive: " .. time_str .. " \n"
.. " Waves survived: " .. wave_count - 1 .. " \n"
.. " Shots fired: " .. shots .. " (" .. round( hits / ( shots + 0.000001 ) * 100 ) .. "% hit) \n"
.. " Melee swings: " .. melee_swings .. " (" .. round( melee_hits / ( melee_swings + 0.000001 ) * 100 ) .. "% hit) \n"
.. " Damage dealt: " .. damage_dealt .. " \n"
.. ( random() > 0.1 and " Pickups taken: " or " Picks uped: " )
.. pickup_taken_count .. " (" .. round( pickup_taken_count / ( pickup_dropped_count + 0.000001 ) * 100 ) .. "%) \n"
local i = -2
for line in stats:gmatch( "([^\n]*)\n" ) do
overlay_buf:write( round( w / 2 - 12 ), round( h / 2 ) + i, line, colours.black, colours.white )
i = i + 1
end
end
if HUD_enabled then
overlay_buf:render()
--overlay_buf:render_to_window( main_window, nil, nil, true )
if report_performance then
local_now = clock()
time_spent_rendering_overlay_buffer = time_spent_rendering_overlay_buffer + local_now - time
time = local_now
end
end
main_buf:render_to_window( main_window, nil, nil, true )
if report_performance then
local_now = clock()
time_spent_rendering_main_buffer = time_spent_rendering_main_buffer + local_now - time
end
end
--- Update the world.
-- @param dt Time since last update
-- @return nil
function update( dt )
--TODO: This needs fixing!
if player.health <= 0 then
running = false
end
if not running then
return
end
-- Enemy wave mechanics
next_wave_countdown = next_wave_countdown - dt
if #zombies == 0 and next_wave_countdown == -math.huge then
-- Countdown to next wave
wave_count = wave_count + 1
next_wave_countdown = WAVE_PREPARATION_TIME
elseif #zombies == 0 and next_wave_countdown <= 0 then
-- Send the next wave!
-- Spawn zombies
for _ = 1, math.min( wave_count * 5, MAX_ZOMBIES ) do
spawn_zombie()
end
next_wave_countdown = -math.huge
end
--TODO: What about using a single table? Would that break anything?
-- Update physics-enabled objects
for index = 1, #physicals do
local to_destroy = {}
local collection = physicals[ index ]
for i = 1, #collection do
local object = collection[ i ]
if object.type == "zombie" then
if object.health <= 0 then
local drop = object.kind.drop
if drop then
for ii = 1, #drop do
local kind = pickup_kinds[ drop[ ii ].item ]
if random() < drop[ ii ].probability
and ( not kind.weapon or not has_weapon( kind.weapon ) ) then
place_pickup( object.x, object.y, kind )
end
end
end
to_destroy[ #to_destroy + 1 ] = object
kills = kills + 1
end
if now - object.last_moved >= object.movement_speed then
-- Move the zombie toward the player
local new_x, new_y, collisions, len = world:move(
object,
object.x + ( object.x > player.x and -1 or 1 ),
object.y + ( object.y > player.y and -1 or 1 ),
collision_filter
)
object.x, object.y = new_x, new_y
for ii = 1, len do
if collisions[ ii ].other == player then
player.health = player.health - object.damage
break
end
end
object.last_moved = now
end
elseif object.type == "bullet" then
local new_x, new_y, collisions, len = world:move(
object,
object.x + object.speed_x * dt,
object.y + object.speed_y * dt,
collision_filter
)
local should_die = false
if new_x < 0 or new_x > world_size.x or new_y < 0 or new_y > world_size.y then
should_die = true
else
if len > 0 then
for ii = 1, len do
local other = collisions[ ii ].other
if other.type ~= "pickup" then
should_die = true
end
if other.health and other ~= player then
other.health = other.health - object.damage
other.x = other.x + ( object.x < other.x and object.knockback or -object.knockback )
other.y = other.y + ( object.y < other.y and object.knockback or -object.knockback )
if object.kind and object.kind.melee then
melee_hits = melee_hits + 1
else
hits = hits + 1
end
damage_dealt = damage_dealt + object.damage
end
end
end
end
if should_die then
to_destroy[ #to_destroy + 1 ] = object
else
object.x, object.y = new_x, new_y
end
end
end
for i = 1, #to_destroy do
local corpse = to_destroy[ i ]
world:remove( corpse )
for ii = 1, #collection do
if corpse == collection[ ii ] then
if corpse.type == "zombie" then
place_static( corpse.x, corpse.y, blood )
end
remove( collection, ii )
break
end
end
end
end
--[[
local bullets_to_destroy = {}
-- Move the bullets!
for i = 1, #bullets do
local bullet = bullets[ i ]
local new_x, new_y, collisions, len = world:move(
bullet,
bullet.x + bullet.speed_x * dt,
bullet.y + bullet.speed_y * dt
)
bullet.x, bullet.y = new_x, new_y
if new_x < 0 or new_x > world_size.x or new_y < 0 or new_y > world_size.y or len > 0 then
bullets_to_destroy[ #bullets_to_destroy + 1 ] = bullet
for index = 1, len do
local object = collisions[ index ].other
if object.health and object ~= player then
object.health = object.health - bullet.damage
hits = hits + 1
end
end
end
end
-- Remove the *truly dead* zombies from the world
for _, v in ipairs( zombies_to_destroy ) do
world:remove( v )
for i = 1, #zombies do
if zombies[ i ] == v then
place_static( v.x, v.y, blood )
remove( zombies, i )
break
end
end
end
-- Remove the collided bullets from the world
for _, v in ipairs( bullets_to_destroy ) do
world:remove( v )
for i = 1, #bullets do
if bullets[ i ] == v then
remove( bullets, i )
break
end
end
end
--]]
-- Loop through things that can perish
for i = 1, #ephemerals do
local collection = ephemerals[ i ]
local to_destroy = {}
for index = 1, #collection do
local object = collection[ index ]
if object.created + object.kind.life < now then
to_destroy[ #to_destroy + 1 ] = object
end
end
for _, v in ipairs( to_destroy ) do
if v.collides then
world:remove( v )
end
for index = 1, #collection do
if collection[ index ] == v then
remove( collection, index )
break
end
end
end
end
-- Update the camera position to centre on the player character
camera_offset.x = round( -player.x + w / 2 )
camera_offset.y = round( -player.y + h / 2 )
end
--- Fire a bullet.
-- @param x description
-- @param y description
-- @param speed_x description
-- @param speed_y description
-- @param kind description
-- @return nil
function fire( x, y, dir_x, dir_y, kind, damage_multiplier, accuracy )
x = x or 0
y = y or 0
kind = kind and bullet_kinds[ kind ] or bullet_kinds.generic
accuracy = accuracy or 1
-- We take the natural number from the knockback (2 out of 2.1) and the decimal part as the chance (0.1 out of 2.1)
local knockback = math.floor( kind.knockback ) * ( random() < kind.knockback % 1 and 1 or 0 )
local accuracy_modifier = ( 1 + random() * ( 1 - accuracy ) )
if random() > 0.5 then
dir_x = dir_x * accuracy_modifier + ( random() - 0.5 ) * ( 1 - accuracy )
dir_y = dir_y / accuracy_modifier + ( random() - 0.5 ) * ( 1 - accuracy )
else
dir_x = dir_x / accuracy_modifier + ( random() - 0.5 ) * ( 1 - accuracy )
dir_y = dir_y * accuracy_modifier + ( random() - 0.5 ) * ( 1 - accuracy )
end
local speed_x, speed_y = get_coord_speeds( dir_x, dir_y, kind.speed )
local ratio = math.abs( speed_x / speed_y )
-- Perform the first step, so that we don't hit the place we're firing *from*
x = x + ( round( speed_x, 1 ) == 0 and 0 or ( speed_x > 0 and 1 or -1 ) )
y = y + ( round( speed_y, 1 ) == 0 and 0 or ( speed_y > 0 and 1 or -1 ) )
local bullet = {
type = "bullet";
fg = knockback == 0 and kind.fg or kind.knockback_highlight or colours.cyan;
x = x;
y = y;
speed_x = speed_x;
speed_y = speed_y;
text = ( ratio >= 1.333 and kind.text_horizontal or kind.text_vertical ) or kind.text;
kind = kind;