-
Notifications
You must be signed in to change notification settings - Fork 0
/
crate.p8
1372 lines (1148 loc) · 52.2 KB
/
crate.p8
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
pico-8 cartridge // http://www.pico-8.com
version 32
__lua__
-- PICO-Crate
-- by Harmony Honey
-- HarmonyHunnie.com
--
-- Music created by Gruber
-- Pico-8 Tunes Volume 1
-- https://www.lexaloffle.com/bbs/?tid=29008
-- https://gruber99.bandcamp.com/album/pico-8-tunes-vol-1
--
-- Maps created in Tiled
-- converted for tiled_room using tiled.py
-- global vars
ent_table = {}
classes = {}
flag_solid = 0
--gravity = 0.2
room = 1
room_text = ""
room_change = 0
load_timer = 0
debug = false
death_count = 0
death_length = 4
start_time = 0
time_elapsed = 0
time_min = "00"
time_sec = "00"
is_win = false
win_time = 3600
win_record = "0|00:00"
win_length = 0
tiled_room = {
"0000000000000000000000000000000000000000000000000000000000009000000000000000a000000000000000a0000065555555555600005e000cc0e00500005010e00004e50000655555555556000000a0000000000000009000000000000000000000000000000000000000000000000000000000000000000000000000,walk (with arrow keys)",
"0000000000000000000000000000000000000000000000000000000000000000000b0b000b0b0000006555555555560000500000e00005000050e09000040500005100a000e0050000655563365556000055555555555500000000000000a0000000000000009000000000000000000000000000000000000000000000000000,jump (with z key)",
"000b990000000000000999000000000000099c00000000000000a00000bb0000006555555555560000500c0c00000500005e00000e0405000050000000000500005000e0b99005000050000099c0e500005e00000a00050000501002ba0e0500006555555555560000000a000000a00000000a000000c00000000c0000000000,push me",
"000000b99000000000000099c0000000000bb00a00bb0000000655555555600000050a000040500000050900e0005000000500000000500000050000000e50000005000e0000500000050000000b500000050e000655600000051002b555500000065555565550000000ccc0000a00000000000000b99000000000000099c000,pickup (with x key)",
"0000b99000000000000099c00000000000000a0000000000000065555556000000066c04ccc6600000050000000c500000050000e000500000060e00000050000005000000e060000005000e000050000005b200102b500000065555555560000000000000a00000000000000b99000000000000099c00000000000000000000,stack two",
"00000000000000000000000000000000000000000000000000000000000000000065555555555600005000e0000e0500005e00000000450000500000e00005000050e0000000e5000052010e00000500006555633333350000555555555556000000000000000000000000000000000000000000000000000000000000000000,toss n cross",
"000b9900000000000009990000000000000999000000000000099c00000b00000000a00b000a0bb000655555655555600050e0055500e050005000e656004050005221000e000e50006555533355556000a000655560ac0000c0000cc000a000000000000000a0000000000000b99990000000000099999000000000009999c0,push bridge",
"0000000000b99000000000000099c00000000000000a00000065555555555600005ccc040ccc050000500000000005000050e000000005000050000000e005000050000e00000500005000000000050000500e00000025000050000010e2250000655555555556000000a0000000000000009000000000000000000000000000,stack three",
"000000000000000000000000000000000000000000000000655555555555555650ccc000000ccc0550000000000000e55000000b9900000550e000099c00000550000e00a000e006500003333333000552e006555556040552010555555500b5655556555556555600a00000000000000b99000000000000099c000000000000,spike hill",
"000000000000000000000655555600000000050000450000000005e000050000000005000005000000000500e035000000000500006500000000050e000500000000053000e500000000056000050000000005000e05000000000500003500000000052e00650000000005221005000000000655555600000000000000000000,up up up",
"000000000000000000000000000000000065555555555600005c0cc400cc05000050000000000500005000000e0005000050e000000005000050000000e005000050000000000500005000e0000025000050900000022500005ba00010e2b5000065555555555600000000000000a000000000000000a0000000000000009000,stack four",
"00b99000000000000099c00000000000000a00000000000000ba0000000000006555555555555556500000000ccc00055000e00000000e05540000e0000e0005500000000000002550e0000000e01025533333333333655665555555555565550000ccc000000a00000000000000b990000000000000999000000000000099c0,stepping stones",
"00000000000000000000000000000000655555555555555650000e00000000055040000000e000055e0000000000000550000000e00000055000e0000000000555600000000e0005555000e0000000255550000000000e25555000000e0010255553333333336556556555555555655500000000000000000000000000000000,cross n stack",
"000000000000000000000000000000000065555555555600005000e04000050000500000000e050000500000000005000050e000000005000050000000e005000050000000000500005000e00000050000500000000025000050000000e225000050e00010022500006555555555560000000000000000000000000000000000,stairway to heaven",
"00000000000000000000000000000000000000000000000065555555555555565000e00000000005500000000000e0055000000e0000000550e000000000040550000000000000055000000030e0000552100e0050000e0565563333533333355555555565555556000000000000000000000000000000000000000000000000,spike long jump",
"000000000000000000006555555600000000500040e5000000005e000005000000005000000500000000500000050000000050000e050000000050e000050000000050000065000000005000e055000000005e000055000000005000656500000000500e55550000000052105555000000006555655500000000000000000000,throw n dodge",
"000000000000000000000000000000000655555555555560050000000000e050050400000000005005000000e00000500500e00000000050050000000000015005000000e000025005e00000000e025005000000000022500500000e000222500533333333365560065555555556555000000000000000000000000000000000,spike stack",
"000000000000000000000000000000006555555555555556500000e0000000055000000000e00405500e00000000000550000000000000e55000000e0000000550e0000000e0333550000e0000006556520003333333555552e00655555565555201055555555555655556555555555500000000000000000000000000000000,spike stairs",
"00000b9b0000000000006555555600000000500c9c05000000005040000500000b90500000050000099d500e00b5000009c050000065000000005e0000550000000050000055000000005000e055000000005000005500000000500006550000000052e005550b9900005221b555d999000065555655099c0000000000000000,two solutions",
"655555555556000050cc00ccc0e5000050e000000005d900500000e0000500005000e000020500005e100000e2b50000655600e055560000005000005cc00b990050b9905000099c0050999050bb00a00050999065555556005099c0000000e500500a000e0004059d5e0a00000e000500533333333333350065555555555556,down the boot",
"65555555555555555ccc00cc00a0065550100000e0c00065522220000000e0055555563333000005555555555600b99555600a0e0000999556000900000099c5500e000000e00a0550000033336555555b99006555555555599c0000cc00ccc550a000e0000004055ba00000000e000555555633333333355555555555555556,the big two",
"6555555555555556500cc00a00ccc00550000e0900000005500000000e00000550e0333333300e05500065555550000550405cc0000000b553335000b9900056655560e0999000055cc0c00099c0e005500000000a003335500e00000a0065555220033333335555522006555555555552210555555555556555555555555555,the final challenge",
}
-- core loop
function _init()
tiled_load()
music(6)
end
function _update60()
-- update
for e in all(ent_table) do
if e.update != nil then
e.update(e)
end
end
-- move
for e in all(ent_table) do
if e.is_moving and (e.speed_x != 0 or e.speed_y != 0) then
move(e)
end
if e.is_using_gravity then
e.speed_y += e.gravity
end
end
if load_timer > 0 then
load_timer -= 1
if load_timer == 0 then
tiled_load()
end
end
-- enable debug
if not debug and btn(4, 1) and btn(4) and btn(5) then
debug_enable()
end
-- debug change room
-- if holding shift
if debug and btn(4, 1) then
if btn_press(0) then
room_change = -1
tiled_load()
elseif btn_press(1) then
room_change = 1
tiled_load()
end
end
-- needed for btn_press()
btn_last_collect()
-- timer
time_elapsed = time() - start_time
time_min = tostr(flr(time_elapsed / 60))
time_sec = tostr(flr(time_elapsed % 60))
if #time_min == 1 then
time_min = "0"..time_min
end
if #time_sec == 1 then
time_sec = "0"..time_sec
end
end
function _draw()
cls()
-- map
map(0, 0, 0, 0, 128, 64)
-- draw ents
for e in all(ent_table) do
e.draw(e)
end
-- death count
rectfill(0,0, 11 + death_length, 8, 0)
spr(9, 1, 0)
print(death_count, 11, 2, 7)
-- timer
rectfill(97, 0, 128, 8, 0)
print(time_min..":"..time_sec, 99, 2, 7)
spr(11, 120, 1)
-- winner
if is_win then
rectfill(62 - win_length / 2, 0, 64 + win_length / 2, 8, 0)
print(win_record, 64 - win_length / 2, 2, 11)
end
-- room 1
if room == 1 then
print("pico-crate", 33, 16, 9)
print("by harmony honey", 21, 29, 10)
end
-- room text
-- l = text_length, px = print_x, py = print_y
local l = #room_text * 4
local px = 64
local py = 122
rectfill(px - (l / 2) - 1, py - 1, px + (l / 2) - 1, py + 5, 0)
color(7)
print(room_text, px - (l / 2), py)
-- debug text
if debug then
cursor(0, 8, 8)
print("stat: "..stat(1))
print("ent: "..#ent_table)
print("room: "..room)
end
end
-- functions
-- easier than typing flr(arg + 0.5)
function round(arg)
return flr(arg + 0.5)
end
-- convert bool to number
function numbool(arg)
if arg then
return 1
else
return 0
end
end
btn_last = {}
function btn_last_collect()
for i = 0, 5 do
btn_last[i] = btn(i)
end
end
function btn_press(arg)
return btn(arg) and not btn_last[arg]
end
-- custom map loader using data copied from tiled
function tiled_load()
-- clear entities
ent_table = {}
-- reload map data from cartridge (clear to blank for now)
reload(0x2000, 0x2000, 0x1000)
-- advance room
if room_change != 0 then
room = room + sgn(room_change)
-- you win
if room == #tiled_room + 1 then
is_win = true
if time_elapsed < win_time then
win_time = time_elapsed
win_record = tostr(death_count).."-"..time_min..":"..time_sec
win_length = #win_record * 4
end
room = 1
start_time = time()
death_count = 0
end
room = mid(1, room, #tiled_room)
room_change = 0
end
local map = tiled_room[room]
room_text = sub(map, 258)
-- create tiles and entities
for i = 0, 255 do
local t = sub(map, i + 1, i + 1)
local y = flr(i / 16)
local x = i - (y * 16)
if t == "5" then
mset(x, y, 16)
elseif t == "6" then
mset(x, y, 17)
elseif t == "9" then
mset(x, y, 32)
elseif t == "a" then
mset(x, y, 48)
elseif t == "b" then
mset(x, y, 49)
elseif t == "c" then
mset(x, y, 33)
elseif t == "d" then
mset(x, y, 50)
elseif t == "e" then
mset(x, y, 34)
end
-- look for classes
foreach(classes,
function(class)
if tostr(class.tile) == t then
-- create entity
local ent = entity_create(class, x * 8, y * 8)
-- adjust position based on sprite offset
ent.x += ent.sprite_x
ent.y += ent.sprite_y
end
end)
end
-- if room == #tiled_room - 1 then load_win() end
end
-- copy and paste sprite on sheet
function sprite_copy(copy, paste)
local source = sprite_first_byte(copy)
local dest = sprite_first_byte(paste)
for i = 0, 7 do
memcpy(tostr(dest + (i * 64), true), tostr(source + (i * 64), true), 4)
end
end
-- returns first top left byte of sprite as decimal
function sprite_first_byte(sprite)
local y = flr(sprite / 16)
local x = sprite - (y * 16)
local byte = y * (64 * 8)
byte += x * 4
return byte
end
-- axis aligned bounding box
-- description from: https://love2d.org/wiki/BoundingBox.lua
-- Collision detection function;
-- Returns true if two boxes overlap, false if they don't;
-- x1,y1 are the top-left coords of the first box, while w1,h1 are its width and height;
-- x2,y2,w2 & h2 are the same, but for the second box.
function aabb(x1,y1,w1,h1, x2,y2,w2,h2)
return x1 < x2+w2 and
x2 < x1+w1 and
y1 < y2+h2 and
y2 < y1+h1
end
-- check tilemap for flag_solid
function check_solid_tile_x(this, distance)
local check_up = false
local check_down = false
local y1 = flr(this.y / 8)
local y2 = flr((this.y + this.hitbox_y - 1) / 8)
-- left
if distance == -1 then
local x = flr((this.x - 1) / 8)
check_up = fget(mget(x, y1), flag_solid)
check_down = fget(mget(x, y2), flag_solid)
-- right
elseif distance == 1 then
local x = flr((this.x + this.hitbox_x) / 8)
check_up = fget(mget(x, y1), flag_solid)
check_down = fget(mget(x, y2), flag_solid)
end
return check_up or check_down
end
-- check tilemap for flag_solid
function check_solid_tile_y(this, distance)
local check_left = false
local check_right = false
local x1 = flr(this.x / 8)
local x2 = flr((this.x + this.hitbox_x - 1) / 8)
-- up
if distance == -1 then
local y = flr((this.y - 1) / 8)
check_left = fget(mget(x1, y), flag_solid)
check_right = fget(mget(x2, y), flag_solid)
-- down
elseif distance == 1 then
local y = flr((this.y + this.hitbox_y) / 8)
check_left = fget(mget(x1, y), flag_solid)
check_right = fget(mget(x2, y), flag_solid)
end
return check_left or check_right
end
-- aabb but for solid tiles
function check_tile_solid(this)
-- check 1, 2, 3, 4
local c1 = false
local c2 = false
local c3 = false
local c4 = false
local x1 = flr(this.x / 8)
local y1 = flr(this.y / 8)
local x2 = flr((this.x + this.hitbox_x - 1) / 8)
local y2 = flr((this.y + this.hitbox_y - 1) / 8)
c1 = fget(mget(x1, y1), flag_solid)
c2 = fget(mget(x2, y1), flag_solid)
c3 = fget(mget(x1, y2), flag_solid)
c4 = fget(mget(x2, y2), flag_solid)
return c1 or c2 or c3 or c4
end
-- check tilemap, and then check entities
function check_solid_x(this, distance)
local hit = false
hit = check_solid_tile_x(this, distance)
-- fix for holding crate and not checking enough pixels
if not hit and this.is_holding_crate then
local test = {
x = this.x,
y = this.y + 4,
hitbox_x = this.hitbox_x,
hitbox_y = 8,
}
hit = check_solid_tile_x(test, distance)
end
if not hit then
hit = entity_check_solid(this, distance, 0) != nil
end
return hit
end
-- check tilemap, and then check entities
function check_solid_y(this, distance)
local hit = false
hit = check_solid_tile_y(this, distance)
if not hit then
hit = entity_check_solid(this, 0, distance) != nil
end
return hit
end
-- move entity
function move(this)
-- clear bools
this.has_moved_x = false
this.has_moved_y = false
this.has_hit_up = false
this.has_hit_down = false
this.has_hit_left = false
this.has_hit_right = false
local dy -- distance y
this.remainder_y += this.speed_y
dy = round(this.remainder_y)
this.remainder_y -= dy
if dy != 0 then
move_y(this, dy)
end
local dx -- distance x
this.remainder_x += this.speed_x
dx = round(this.remainder_x)
this.remainder_x -= dx
if dx != 0 then
move_x(this, dx)
end
end
-- return distance of upcoming move
function move_get_distance(this)
return {
x = round(this.remainder_x + this.speed_x),
y = round(this.remainder_y + this.speed_y),
}
end
-- move x axis
function move_x(this, dist)
local hit = false
this.has_moved_x = true
if this.is_colliding then
local step = sgn(dist)
for i=1, abs(dist) do
if not check_solid_x(this, step) then
this.x += step
else
this.speed_x = 0
this.remainder_x = 0
if step == -1 then
this.has_hit_left = true
else
this.has_hit_right = true
end
hit = true
break
end
end
else
this.x += dist
end
return hit
end
-- move y axis
function move_y(this, dist)
local hit = false
this.has_moved_y = true
this.is_on_floor_3 = this.is_on_floor_2
this.is_on_floor_2 = this.is_on_floor
this.is_on_floor = false
if this.is_colliding then
local step = sgn(dist)
for i=1, abs(dist) do
if not check_solid_y(this, step) then
this.y += step
else
this.speed_y = 0
this.remainder_y = 0
if step == -1 then
this.has_hit_up = true
else
this.has_hit_down = true
this.is_on_floor = true
end
hit = true
break
end
end
else
this.x += dist
end
return hit
end
-- used to instantiate a new entity
function entity_create(class, x, y)
local ent = {}
-- copy keys from base class
for k, v in pairs(class_base) do ent[k] = v end
-- override keys from inherited class
if class.inherit_from != nil then
for k, v in pairs(class.inherit_from) do ent[k] = v end
end
-- override keys from actual class
for k, v in pairs(class) do ent[k] = v end
-- set position
ent.x = x
ent.y = y
-- initialize
if ent.init != nil then
ent.init(ent)
end
add(ent_table, ent)
return ent
end
-- remove entity instance from table
function entity_remove(ent)
del(ent_table, ent)
end
function entity_find(class, ignore)
for e in all(ent_table) do
if e.name == class.name and e != ignore then
return e
end
end
end
function entity_check(this, x, y, class, ignore)
local ent = nil
for e in all(ent_table) do
if e != this and e.name == class.name and e != ignore then
if aabb(x, y, this.hitbox_x, this.hitbox_y, e.x, e.y, e.hitbox_x, e.hitbox_y) then
ent = e
break
end
end
end
return ent
end
function entity_check_all(this, x, y, class, ignore)
local ent = {}
local i = 1
for e in all(ent_table) do
if e != this and e.name == class.name and e != ignore then
if aabb(x, y, this.hitbox_x, this.hitbox_y, e.x, e.y, e.hitbox_x, e.hitbox_y) then
ent[i] = e
i += 1
end
end
end
return ent
end
-- only checks for solid entities
-- dx, dy = distance x and y
function entity_check_solid(this, dx, dy, ignore)
local ent = nil
for e in all(ent_table) do
if e != this and e.is_solid and e != ignore then
if aabb(this.x + dx, this.y + dy, this.hitbox_x, this.hitbox_y, e.x, e.y, e.hitbox_x, e.hitbox_y) then
ent = e
break
end
end
end
return ent
end
function entity_check_moving(this, x, y, ignore)
local ent = nil
for e in all(ent_table) do
if e != this and e.is_moving and e != ignore then
if aabb(x, y, this.hitbox_x, this.hitbox_y, e.x, e.y, e.hitbox_x, e.hitbox_y) then
ent = e
break
end
end
end
return ent
end
-- debug funcs
function debug_enable()
sfx(13)
debug = true
menuitem(4, "disable debug", debug_disable)
end
function debug_disable()
debug = false
menuitem(4)
end
-- classes
-- all classes inherit from class_base before inherit_from
class_base = {
tile = nil,
inherit_from = nil,
name = "base",
x = 0,
y = 0,
speed_x = 0,
speed_y = 0,
gravity = 0.2,
remainder_x = 0,
remainder_y = 0,
sprite = -1,
sprite_x = 0,
sprite_y = 0,
draw_offset_x = 0,
draw_offset_y = 0,
flip_x = false,
flip_y = false,
hitbox_x = 8,
hitbox_y = 8,
is_moving = false,
is_solid = false,
is_colliding = false,
is_using_gravity = false,
is_remove_tile = true,
has_moved_x = false,
has_moved_y = false,
has_hit_up = false,
has_hit_down = false,
has_hit_left = false,
has_hit_right = false,
is_on_floor = false,
is_on_floor_2 = false,
is_on_floor_3 = false,
init = nil,
update = nil,
draw = function(this)
spr(this.sprite, this.x - this.sprite_x + this.draw_offset_x, this.y - this.sprite_y + this.draw_offset_y,
1, 1, this.flip_x, this.flip_y)
end
}
add(classes, class_base)
class_player = {
name = "player",
tile = 1,
sprite = 2,
sprite_idle1 = 2,
sprite_idle2 = 3,
sprite_run1 = 4,
sprite_run2 = 5,
sprite_jump = 6,
is_moving = true,
is_solid = true,
is_colliding = true,
is_using_gravity = true,
move_speed = 1,
move_slow = 0.8,
move_accel = 0.1,
move_last = 0,
push_speed = 0.3,
gravity = 0.25,
jump_speed = 2,
jump_frames = 10,
jump_count = 0,
is_jump = false,
anim_frame = 0,
anim_step_run = 0.2,
anim_step_idle = 0.04,
is_holding_crate = false,
speed_drop_x = 1,
speed_drop_y = -1,
speed_throw_x = 1.0,
speed_throw_y = -2.5,
speed_throw_fast_x = 1.7,
speed_throw_fast_y = -2.5,
speed_throw_high_x = 0.7,
speed_throw_high_y = -3.5,
update = function(this)
-- check for spikes
if this.speed_y > 0 and entity_check(this, this.x, this.y, class_spike) != nil then
this.death(this)
death_count += 1
death_length = #tostr(death_count) * 4
return 0
end
-- check for exit portal
local portal = entity_check(this, this.x, this.y, class_exit)
if portal != nil then
local go = false
-- make sure player is touching portal, not just held crate
if this.is_holding_crate then
go = this.y < portal.y - 4
else
go = true
end
if go then
this.death(this)
room_change = 1
return 0
end
end
-- animation
if this.is_on_floor then
-- idle
if abs(this.speed_x) < 0.2 then
this.anim_frame += this.anim_step_idle
if this.anim_frame >= 2 then
this.anim_frame = mid(0, this.anim_frame - 2, 1.99)
end
if flr(this.anim_frame) == 0 then
this.sprite = this.sprite_idle1
elseif flr(this.anim_frame) == 1 then
this.sprite = this.sprite_idle2
-- this frame is half as long
this.anim_frame += this.anim_step_idle
end
this.draw_offset_y = 0
-- run
else
this.anim_frame += this.anim_step_run
if this.anim_frame >= 4 then
this.anim_frame = mid(0, this.anim_frame - 4, 3.99)
end
if flr(this.anim_frame) == 0 then
this.sprite = this.sprite_idle1
this.draw_offset_y = 0
elseif flr(this.anim_frame) == 1 then
this.sprite = this.sprite_run1
this.draw_offset_y = -1
elseif flr(this.anim_frame) == 2 then
this.sprite = this.sprite_idle1
this.draw_offset_y = 0
elseif flr(this.anim_frame) == 3 then
this.sprite = this.sprite_run2
this.draw_offset_y = -1
end
end
end
-- walking
if btn(0) and this.speed_x then
this.speed_x -= this.move_accel
this.speed_x = max(this.speed_x, -this.move_speed)
this.flip_x = true
elseif btn(1) and this.speed_x then
this.speed_x += this.move_accel
this.speed_x = min(this.speed_x, this.move_speed)
this.flip_x = false
else
this.speed_x *= this.move_slow
end
this.move_last = this.speed_x
-- start jump
if btn_press(4) and this.check_jump(this) then
--this.speed_y = -this.jump_speed
this.sprite = this.sprite_jump
this.draw_offset_y = 0
sfx(6)
this.is_jump = true
this.jump_count = 0
end
-- jump height
if this.is_jump then
if btn(4) then
this.speed_y = -this.jump_speed
this.jump_count += 1
if this.jump_count > this.jump_frames then
this.is_jump = false
end
else
this.is_jump = false
end
end
-- crate pickup / throw
if this.is_holding_crate then
if btn_press(5) then
this.crate_throw(this)
--[[
if btn(0) or btn(1) then
this.crate_throw_fast(this)
else
this.crate_throw(this)
end
--]]
end
elseif btn_press(5) then
local dir = sgn(numbool(not this.flip_x) - 1)
local e = entity_check(this, this.x, this.y + 1, class_crate)
if e != nil then
local offset = this.crate_find_space(this, e)
if offset then
this.crate_pickup(this)
this.x += offset
del(ent_table, e)
end
end
end
-- in on floor
-- crate push
if this.is_on_floor and move_get_distance(this).x != 0 then
local dir = this.get_dir(this)
local e = entity_check(this, this.x + dir, this.y, class_crate)
if e != nil then
e.push(e, dir)
-- slow movement when pushing
if abs(this.speed_x) > this.push_speed then
this.speed_x = this.push_speed * sgn(this.speed_x)
end
end
end
-- debug spawn crate
if debug and btn(4, 1) and btn_press(5) then
this.crate_pickup(this)
end
--treadmill_move(this)
end,
draw = function(this)
if not this.is_holding_crate then
class_base.draw(this)
-- draw debug collider
if debug then rect(this.x, this.y - 8, this.x + 7, this.y + 7, 11) end
else
-- offset y for crate
local oy = numbool(this.sprite == this.sprite_idle2)
-- draw body
spr(this.sprite, this.x, this.y + 8 + this.draw_offset_y, 1, 1, this.flip_x, this.flip_y)
-- draw crate
spr(class_crate.sprite, this.x, this.y + oy + this.draw_offset_y)
-- draw debug collider
if debug then rect(this.x, this.y, this.x + this.hitbox_x - 1, this.y + this.hitbox_y - 1, 11) end
end
end,
get_dir = function(this)
return sgn(numbool(not this.flip_x) - 1)
end,
crate_pickup = function(this)
this.is_holding_crate = true
this.y -= 8
this.hitbox_y = 16
sfx(2)
end,
crate_release = function(this)
this.is_holding_crate = false
this.hitbox_y = 8
this.y += 8
return entity_create(class_crate, this.x, this.y - 8)
end,
crate_drop = function(this)
local c = this.crate_release(this)
local dir = this.get_dir(this)
c.speed_x = this.speed_drop_x * dir
c.speed_y = this.speed_drop_y
sfx(4)
end,
crate_throw = function(this)
local c = this.crate_release(this)
local dir = this.get_dir(this)
c.speed_x = this.speed_throw_x * dir
c.speed_y = this.speed_throw_y
sfx(3)
end,
crate_throw_fast = function(this)
local c = this.crate_release(this)
local dir = this.get_dir(this)
c.speed_x = this.speed_throw_fast_x * dir
c.speed_y = this.speed_throw_fast_y
sfx(5)
end,
crate_throw_high = function(this)
local c = this.crate_release(this)
local dir = this.get_dir(this)
c.speed_x = this.speed_throw_high_x * dir
c.speed_y = this.speed_throw_high_y
sfx(5)
end,
crate_check_space = function(finder, ignore)
local check = false
check = check_tile_solid(finder)
if not check then
local e = entity_check(finder, finder.x, finder.y, class_crate, ignore)
if e != nil then
check = true
end
end
return check
end,
crate_find_space = function(this, ignore)
local offset = nil
local finder = {}
finder.x = this.x
finder.y = this.y - 8
finder.hitbox_x = 8