-
Notifications
You must be signed in to change notification settings - Fork 12
/
base.p8
1498 lines (1348 loc) · 61.3 KB
/
base.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 41
__lua__
--newleste.p8 base cart
--original game by:
--maddy thorson + noel berry
-- based on evercore v2.0.2
--with major project contributions by
--taco360, meep, gonengazit, and akliant
-- [data structures]
function vector(x,y)
return {x=x,y=y}
end
function rectangle(x,y,w,h)
return {x=x,y=y,w=w,h=h}
end
-- [globals]
objects,got_fruit, --tables
freeze,delay_restart,sfx_timer,ui_timer, --timers
cam_x,cam_y,cam_spdx,cam_spdy,cam_gain,cam_offx,cam_offy, --camera values <camtrigger>
_pal, --for outlining
shake,screenshake
=
{},{},
0,0,0,-99,
0,0,0,0,0.1,0,0,
pal,
0,false
local _g=_ENV --for writing to global vars
-- [entry point]
function _init()
max_djump,deaths,frames,seconds,minutes,time_ticking,berry_count=1,0,0,0,0,true,0
music(0,0,7)
load_level(1)
end
-- [effects]
clouds={}
for i=0,16 do
add(clouds,{
x=rnd"128",
y=rnd"128",
spd=1+rnd"4",
w=32+rnd"32"
})
end
particles={}
for i=0,24 do
add(particles,{
x=rnd"128",
y=rnd"128",
s=flr(rnd"1.25"),
spd=0.25+rnd"5",
off=rnd(),
c=6+rnd"2",
})
end
dead_particles={}
-- [player entity]
player={
init=function(_ENV)
djump, hitbox, collides,layer = max_djump, rectangle(1,3,6,5), true,2
--<fruitrain>--
-- ^ refers to setting berry_timer and berry_count to 0
foreach(split"grace,jbuffer,dash_time,dash_effect_time,dash_target_x,dash_target_y,dash_accel_x,dash_accel_y,spr_off,berry_timer,berry_count", function(var)
_ENV[var]=0
end)
create_hair(_ENV)
end,
update=function(_ENV)
if pause_player then
return
end
-- horizontal input
local h_input=split"0,-1,1,1"[btn()%4+1]
-- spike collision / bottom death
if is_flag(0,0,-1) or
y>lvl_ph and not exit_bottom then
kill_player(_ENV)
end
-- on ground checks
local on_ground=is_solid(0,1)
-- <fruitrain> --
if is_solid(0,1,true) then
berry_timer+=1
else
berry_timer, berry_count=0, 0
end
for i,f in inext,fruitrain do
if f.type==fruit and not f.golden and berry_timer>5 then
-- to be implemented:
-- save berry
-- save golden
berry_count+=1
_g.berry_count+=1
berry_timer, got_fruit[f.fruit_id]=-5, true
init_object(lifeup, f.x, f.y,berry_count)
del(fruitrain, f)
destroy_object(f);
(fruitrain[i] or {}).target=f.target
end
end
-- </fruitrain> --
-- landing smoke
if on_ground and not was_on_ground then
init_smoke(0,4)
end
-- jump and dash input
local jump,dash=btn(🅾️) and not p_jump,btn(❎) and not p_dash
p_jump,p_dash=btn(🅾️),btn(❎)
-- jump buffer
if jump then
jbuffer=5
end
jbuffer=max(jbuffer-1)
-- grace frames and dash restoration
if on_ground then
grace=7
if djump<max_djump then
psfx"22"
djump=max_djump
end
end
grace=max(grace-1)
-- dash effect timer (for dash-triggered events, e.g., berry blocks)
dash_effect_time-=1
-- dash startup period, accel toward dash target speed
if dash_time>0 then
init_smoke()
dash_time-=1
spd=vector(
appr(spd.x,dash_target_x,dash_accel_x),
appr(spd.y,dash_target_y,dash_accel_y)
)
else
-- x movement
local accel=on_ground and 0.6 or 0.4
-- set x speed
spd.x=abs(spd.x)<=1 and
appr(spd.x,h_input,accel) or
appr(spd.x,sign(spd.x),0.15)
-- facing direction
if spd.x~=0 then
flip.x=spd.x<0
end
-- y movement
local maxfall=2
-- wall slide
if is_solid(h_input,0) then
maxfall=0.4
-- wall slide smoke
if rnd()<0.2 then
init_smoke(h_input*6)
end
end
-- apply gravity
if not on_ground then
spd.y=appr(spd.y,maxfall,abs(spd.y)>0.15 and 0.21 or 0.105)
end
-- jump
if jbuffer>0 then
if grace>0 then
-- normal jump
psfx"18"
jbuffer,grace,spd.y=0,0,-2
init_smoke(0,4)
else
-- wall jump
local wall_dir=is_solid(-3,0) and -1 or is_solid(3,0) and 1
if wall_dir then
psfx"19"
jbuffer,spd=0,vector(wall_dir*-2,-2)
-- wall jump smoke
init_smoke(wall_dir*6)
end
end
end
-- dash
if dash then
if djump>0 then
init_smoke()
djump-=1
dash_time,_g.has_dashed,dash_effect_time=4, true, 10
-- vertical input
local v_input=btn(⬆️) and -1 or btn(⬇️) and 1 or 0
-- calculate dash speeds
local dspd=h_input&v_input==0 and 5 or 3.5355339059
spd=vector(h_input~=0 and h_input*dspd or
v_input~=0 and 0 or flip.x and -1 or 1,
v_input*dspd)
-- effects
psfx"20"
_g.freeze,_g.shake=2,5
-- dash target speeds and accels
dash_target_x,dash_target_y,dash_accel_x,dash_accel_y=
2*sign(spd.x), split"-1.5,0,2"[v_input+2],
v_input==0 and 1.5 or 1.06066017177 , spd.x==0 and 1.5 or 1.06066017177 -- 1.5 * sqrt()
-- emulate soft dashes
if ph_input==-h_input and oob(ph_input,0) then
spd.x=0
end
else
-- failed dash smoke
psfx"21"
init_smoke()
end
end
end
-- animation
spr_off+=0.25
sprite = on_ground and (
btn(⬇️) and 6 or -- crouch
btn(⬆️) and 7 or -- look up
spd.x*h_input~=0 and 1+spr_off%4 or 1) -- walk or stand
or is_solid(h_input,0) and 5 or 3 -- wall slide or mid air
update_hair(_ENV)
-- exit level (except summit)
if (exit_right and left()>=lvl_pw or
exit_top and y<-4 or
exit_left and right()<0 or
exit_bottom and top()>=lvl_ph) and levels[lvl_id+1] then
next_level()
end
-- was on the ground, previous horizontal input (for soft dashes)
was_on_ground,ph_input=on_ground, h_input
end,
draw=function(_ENV)
-- draw player hair and sprite
pal(8,djump==1 and 8 or 12)
draw_hair(_ENV)
draw_obj_sprite(_ENV)
pal()
end
}
function create_hair(_ENV)
hair={}
for i=1,5 do
add(hair,vector(x,y))
end
end
function update_hair(_ENV)
local last=vector(x+(flip.x and 6 or 1),y+(btn(⬇️) and 4 or 2.9))
foreach(hair, function(h)
h.x+=(last.x-h.x)/1.5
h.y+=(last.y+0.5-h.y)/1.5
last=h
end)
end
function draw_hair(_ENV)
for i,h in inext,hair do
circfill(round(h.x),round(h.y),split"2,2,1,1,1"[i],8)
end
end
-- [other entities]
player_spawn={
init=function(_ENV)
layer=2
sfx"15"
sprite=3
target=y
y=min(y+48,lvl_ph)
_g.cam_x,_g.cam_y=mid(x,64,lvl_pw-64),mid(y,64,lvl_ph-64)
spd.y=-4
state=0
delay=0
create_hair(_ENV)
djump=max_djump
--- <fruitrain> ---
foreach(fruitrain, function(f)
--this gets called many times but saves tokens for checking if fruitrain is empty
fruitrain[1].target=_ENV
add(objects,f)
f.x,f.y=x,y
fruit.init(f)
end)
--- </fruitrain> ---
end,
update=function(_ENV)
-- jumping up
if state==0 and y<target+16 then
state,delay=1, 3
-- falling
elseif state==1 then
spd.y+=0.5
if spd.y>0 then
if delay>0 then
-- stall at peak
spd.y=0
delay-=1
elseif y>target then
-- clamp at target y
y,spd,state,delay,_g.shake=target,vector(0,0),2,5,4
init_smoke(0,4)
sfx"16"
end
end
-- landing and spawning player object
elseif state==2 then
delay-=1
sprite=6
if delay<0 then
destroy_object(_ENV)
local p=init_object(player,x,y);
--- <fruitrain> ---
(fruitrain[1] or {}).target=p
--- </fruitrain> ---
end
end
update_hair(_ENV)
end,
draw=player.draw
-- draw=function(this)
-- set_hair_color(max_djump)
-- draw_hair(this,1)
-- draw_obj_sprite(this)
-- unset_hair_color()
-- end
}
--<camtrigger>--
camera_trigger={
update=function(_ENV)
if timer and timer>0 then
timer-=1
if timer==0 then
_g.cam_offx,_g.cam_offy=offx,offy
else
_g.cam_offx+=cam_gain*(offx-cam_offx)
_g.cam_offy+=cam_gain*(offy-cam_offy)
end
elseif player_here() then
timer=5
end
end
}
--</camtrigger>--
spring={
init=function(_ENV)
delta,dir=0,sprite==9 and 0 or is_solid(-1,0) and 1 or -1
end,
update=function(_ENV)
delta*=0.75
--can save tokens by setting hit as _ENV
--but i'm not desperate enough yet
local hit=player_here()
if hit then
if dir==0 then
hit.move(0,y-hit.y-4,1)
hit.spd.x*=0.2
hit.spd.y=-3
else
hit.move(x+dir*4-hit.x,0,1)
hit.spd=vector(dir*3,-1.5)
end
hit.dash_time,hit.dash_effect_time,delta,hit.djump=0,0,4,max_djump
end
end,
draw=function(_ENV)
local delta=flr(delta)
if dir==0 then
sspr(72,0,8,8-delta,x,y+delta)
else
spr(8,dir==-1 and x+delta or x,y,1-delta/8,1,dir==1)
end
end
}
refill={
init=function(_ENV)
offset,timer,hitbox=rnd(),0,rectangle(-1,-1,10,10)
end,
update=function(_ENV)
if timer>0 then
timer-=1
if timer==0 then
psfx"12"
init_smoke()
end
else
offset+=0.02
local hit=player_here()
if hit and hit.djump<max_djump then
psfx"11"
init_smoke()
hit.djump,timer=max_djump,60
end
end
end,
draw=function(_ENV)
if timer==0 then
spr(15,x,y+sin(offset)+0.5)
else
-- color"7"
-- line(x,y+4,x+3,y+7)
-- line(x+4,y+7,x+7,y+4)
-- line(x+7,y+3,x+4,y)
-- line(x+3,y,x,y+3)
foreach(split(
[[0,4,3,7
4,7,7,4
7,3,4,0
3,0,0,3]],"\n"),function(t)
local o1,o2,o3,o4=unpack(split(t))
line(x+o1,y+o2,x+o3,y+o4,7)
end
)
end
end
}
fall_floor={
init=function(_ENV)
solid_obj,state,unsafe_ground,delay=true,0,true,0
end,
update=function(_ENV)
--it looks like weird stuff goes on here with the decimal constants (mostly to ensure rounding correctly), but it should be equivalent to vanilla
--(and if i made an error, probably no one cares)
-- idling
if delay>0 then
delay-=0.2
elseif state==0 then
for i=-1,1 do
if check(player,i,abs(i)-1) then
psfx"13"
state,delay=1,2.79
init_smoke()
break
end
end
-- shaking
elseif state==1 then
state,delay,collideable=2,11.79--,false
-- invisible, waiting to reset
else
if not player_here() then
psfx"12"
state,collideable=0,true
init_smoke()
end
end
--if sprite 0 is not empty, need to fixup this
sprite=state==1 and 25.8-delay or state==0 and 23
end
}
smoke={
init=function(_ENV)
layer,spd,flip=3,vector(0.3+rnd"0.2",-0.1),vector(rnd()<0.5,rnd()<0.5)
x+=-1+rnd"2"
y+=-1+rnd"2"
end,
update=function(_ENV)
sprite+=0.2
if sprite>=29 then
destroy_object(_ENV)
end
end
}
--- <fruitrain> ---
fruitrain={}
fruit={
check_fruit=true,
init=function(_ENV)
y_,off,tx,ty,golden=y,0,x,y,sprite==11
if golden and deaths>0 then
destroy_object(_ENV)
end
end,
update=function(_ENV)
if target then
tx+=0.2*(target.x-tx)
ty+=0.2*(target.y-ty)
local dtx,dty=x-tx,y_-ty
local a,k=atan2(dtx,dty),dtx^2+dty^2 > r^2 and 0.2 or 0.1
x+=k*(r*cos(a)-dtx)
y_+=k*(r*sin(a)-dty)
else
local hit=player_here()
if hit then
hit.berry_timer,target,r=
0,fruitrain[#fruitrain] or hit,fruitrain[1] and 8 or 12
add(fruitrain,_ENV)
end
end
off+=0.025
y=y_+sin(off)*2.5
end
}
--- </fruitrain> ---
fly_fruit={
check_fruit=true,
init=function(_ENV)
start,step,sfx_delay=y,0.5,8
end,
update=function(_ENV)
--fly away
if has_dashed then
sfx_delay-=1
if sfx_delay==0 then
_g.sfx_timer=20
sfx"10"
end
spd.y=appr(spd.y,-3.5,0.25)
if y<-16 then
destroy_object(_ENV)
end
-- wait
else
step+=0.05
spd.y=sin(step)*0.5
end
-- collect
if player_here() then
--- <fruitrain> ---
init_smoke(-6)
init_smoke(6)
local f=init_object(fruit,x,y,10) --if this happens to be in the exact location of a different fruit that has already been collected, this'll cause a crash
--TODO: fix this if needed
f.fruit_id=fruit_id
fruit.update(f)
--- </fruitrain> ---
destroy_object(_ENV)
end
end,
draw=function(_ENV)
spr(10,x,y)
for ox=-6,6,12 do
spr((has_dashed or sin(step)>=0) and 12 or y>start and 14 or 13,x+ox,y-2,1,1,ox==-6)
end
end
}
lifeup={
init=function(_ENV)
spd.y,duration,flash,_g.sfx_timer,outline=-0.25,30,0,20--,false
sfx"9"
end,
update=function(_ENV)
duration-=1
if duration<=0 then
destroy_object(_ENV)
end
flash+=0.5
end,
draw=function(_ENV)
--<fruitrain>--
?split"1000,2000,3000,4000,5000,1up"[min(sprite,6)],x-4,y-4,7+flash%2
--<fruitrain>--
end
}
psfx=function(num)
if sfx_timer<=0 then
sfx(num)
end
end
-- [tile dict]
tiles={}
foreach(split([[
1,player_spawn
8,spring
9,spring
10,fruit
11,fruit
12,fly_fruit
15,refill
23,fall_floor
]],"\n"),function(t)
local tile,obj=unpack(split(t))
tiles[tile]=_ENV[obj]
end)
-- [object functions]
function init_object(_type,sx,sy,tile)
--generate and check berry id
local id=sx..","..sy..","..lvl_id
if _type.check_fruit and got_fruit[id] then
return
end
--local _g=_g
local _ENV=setmetatable({},{__index=_g})
type, collideable, sprite, flip, x, y, hitbox, spd, rem, fruit_id, outline, draw_seed=
_type, true, tile, vector(), sx, sy, rectangle(0,0,8,8), vector(0,0), vector(0,0), id, true, rnd()
function left() return x+hitbox.x end
function right() return left()+hitbox.w-1 end
function top() return y+hitbox.y end
function bottom() return top()+hitbox.h-1 end
function is_solid(ox,oy,require_safe_ground)
for o in all(objects) do
if o!=_ENV and (o.solid_obj or o.semisolid_obj and not objcollide(o,ox,0) and oy>0) and objcollide(o,ox,oy) and not (require_safe_ground and o.unsafe_ground) then
return true
end
end
return oy>0 and not is_flag(ox,0,3) and is_flag(ox,oy,3) or -- one way platform or
is_flag(ox,oy,0) -- solid terrain
end
function oob(ox,oy)
return not exit_left and left()+ox<0 or not exit_right and right()+ox>=lvl_pw or top()+oy<=-8
end
function is_flag(ox,oy,flag)
for i=mid(0,lvl_w-1,(left()+ox)\8),mid(0,lvl_w-1,(right()+ox)/8) do
for j=mid(0,lvl_h-1,(top()+oy)\8),mid(0,lvl_h-1,(bottom()+oy)/8) do
local tile=tile_at(i,j)
if flag>=0 then
if fget(tile,flag) and (flag~=3 or j*8>bottom()) then
return true
end
elseif ({spd.y>=0 and bottom()%8>=6,
spd.y<=0 and top()%8<=2,
spd.x<=0 and left()%8<=2,
spd.x>=0 and right()%8>=6})[tile-15] then
return true
end
end
end
end
function objcollide(other,ox,oy)
return other.collideable and
other.right()>=left()+ox and
other.bottom()>=top()+oy and
other.left()<=right()+ox and
other.top()<=bottom()+oy
end
function check(type,ox,oy)
for other in all(objects) do
if other.type==type and other~=_ENV and objcollide(other,ox,oy) then
return other
end
end
end
function player_here()
return check(player,0,0)
end
function move(ox,oy,start)
for axis in all{"x","y"} do
rem[axis]+=vector(ox,oy)[axis]
local amt=round(rem[axis])
rem[axis]-=amt
local upmoving=axis=="y" and amt<0
local riding,movamt=not player_here() and check(player,0,upmoving and amt or -1)--,nil
if collides then
local step,p=sign(amt),_ENV[axis]
local d=axis=="x" and step or 0
for i=start,abs(amt) do
if is_solid(d,step-d) or oob(d,step-d) then
spd[axis],rem[axis]=0,0
break
else
_ENV[axis]+=step
end
end
movamt=_ENV[axis]-p --save how many px moved to use later for solids
else
movamt=amt
if (solid_obj or semisolid_obj) and upmoving and riding then
movamt+=top()-bottom()-1
local hamt=round(riding.spd.y+riding.rem.y)
hamt+=sign(hamt)
if movamt<hamt then
riding.spd.y=max(riding.spd.y)--,0)
else
movamt=0
end
end
_ENV[axis]+=amt
end
if (solid_obj or semisolid_obj) and collideable then
collideable=false
local hit=player_here()
if hit and solid_obj then
hit.move(axis~="x" and 0 or amt>0 and right()+1-hit.left() or amt<0 and left()-hit.right()-1,
axis~="y" and 0 or amt>0 and bottom()+1-hit.top() or amt<0 and top()-hit.bottom()-1,
1)
if player_here() then
kill_player(hit)
end
elseif riding then
riding.move(vector(movamt,0)[axis],vector(0,movamt)[axis],1)
end
collideable=true
end
end
end
function init_smoke(ox,oy)
init_object(smoke,x+(ox or 0),y+(oy or 0),26)
end
add(objects,_ENV);
(type.init or time)(_ENV)
return _ENV
end
function destroy_object(obj)
del(objects,obj)
end
function kill_player(obj)
sfx_timer,shake=12,9
sfx"17"
deaths+=1
destroy_object(obj)
--dead_particles={}
for dir=0,0.875,0.125 do
add(dead_particles,{
x=obj.x+4,
y=obj.y+4,
t=2,
dx=sin(dir)*3,
dy=cos(dir)*3
})
end
-- <fruitrain> ---
foreach(fruitrain,function(f)
full_restart = full_restart or f.golden
end)
fruitrain={}
--- </fruitrain> ---
delay_restart=15
-- <transition>
tstate=0
-- </transition>
end
-- [room functions]
function next_level()
load_level(lvl_id+1)
end
function load_level(id)
--remove existing objects
foreach(objects,destroy_object)
--reset camera speed, drawing timer setup
ui_timer,cam_spdx,cam_spdy,has_dashed=5,0,0--,false
local diff_level=lvl_id~=id
--set level index
lvl_id=id
--set level globals
local tbl=split(levels[lvl_id])
for i=1,4 do
_ENV[split"lvl_x,lvl_y,lvl_w,lvl_h"[i]]=tbl[i]*16
end
lvl_pw,lvl_ph=lvl_w*8,lvl_h*8
local exits=tonum(tbl[5]) or 0b0001
-- exit_top,exit_right,exit_bottom,exit_left=exits&1!=0,exits&2!=0,exits&4!=0, exits&8!=0
for i,v in inext,split"exit_top,exit_right,exit_bottom,exit_left" do
_ENV[v]=exits&(0.5<<i)~=0
end
--reload map
if diff_level then
reload()
end
--chcek for mapdata strings
if mapdata[lvl_id] then
--hex loaded levels go at (0,0), despite what the levels table says (to make everhorn nicer)
lvl_x,lvl_y=0,0
if diff_level then
--replace mapdata with base256
--encoding is offset by 1, to avoid shenanigans with null chars
for i=0,#mapdata[lvl_id]-1 do
mset(i%lvl_w,i\lvl_w,ord(mapdata[lvl_id][i+1])-1)
end
end
end
-- entities
for tx=0,lvl_w-1 do
for ty=0,lvl_h-1 do
local tile=tile_at(tx,ty)
if tiles[tile] then
init_object(tiles[tile],tx*8,ty*8,tile)
end
end
end
foreach(objects,function(_ENV)
(type.end_init or time)(_ENV)
end)
--<camtrigger>--
--generate camera triggers
cam_offx,cam_offy=0,0
for s in all(camera_offsets[lvl_id]) do
local tx,ty,tw,th,offx_,offy_=unpack(split(s))
local _ENV=init_object(camera_trigger,tx*8,ty*8)
hitbox.w,hitbox.h,offx,offy=tw*8,th*8,offx_,offy_
end
--</camtrigger>--
end
-- [main update loop]
function _update()
frames+=1
if time_ticking then
seconds+=frames\30
minutes+=seconds\60
seconds%=60
end
frames%=30
sfx_timer=max(sfx_timer-1)
-- cancel if freeze
if freeze>0 then
freeze-=1
return
end
-- screenshake toggle
if btnp(⬆️,1) then
screenshake=not screenshake
end
-- restart (soon)
if delay_restart>0 then
cam_spdx,cam_spdy=0,0
delay_restart-=1
if delay_restart==0 then
-- <fruitrain> --
if full_restart then
full_restart=false
_init()
-- </fruitrain> --
else
load_level(lvl_id)
end
end
end
-- update each object
foreach(objects,function(_ENV)
move(spd.x,spd.y,type==player and 0 or 1);
(type.update or time)(_ENV)
draw_seed=rnd()
end)
--move camera to player
foreach(objects,function(_ENV)
if type==player or type==player_spawn then
move_camera(_ENV)
end
end)
end
-- [drawing functions]
function _draw()
if freeze>0 then
return
end
-- reset all palette values
pal()
--set cam draw position
draw_x,draw_y=round(cam_x)-64,round(cam_y)-64
if shake>0 then
shake-=1
if screenshake then
draw_x+=-2+rnd"5"
draw_y+=-2+rnd"5"
end
end
camera(draw_x,draw_y)
-- draw bg color
cls()
-- bg clouds effect
foreach(clouds,function(_ENV)
x+=spd-_g.cam_spdx
_g.rectfill(x+_g.draw_x,y+_g.draw_y,x+w+_g.draw_x,y+16-w*0.1875+_g.draw_y,1)
if x>128 then
x,y=-w,_g.rnd"120"
end
end)
-- draw bg terrain
map(lvl_x,lvl_y,0,0,lvl_w,lvl_h,4)
-- draw outlines
pal(split"1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1")
pal=time
foreach(objects,function(_ENV)
if outline then
for i=1,4 do
camera(draw_x+split"-1,0,0,1"[i],draw_y+split"0,-1,1,0"[i]) draw_object(_ENV)
end
end
end)
pal=_pal
camera(draw_x,draw_y)
pal()
--set draw layering
--0: background layer
--1: default layer
--2: player layer
--3: foreground layer
local layers={{},{},{}}
foreach(objects,function(_ENV)
if layer==0 then
draw_object(_ENV) --draw below terrain
else
add(layers[layer or 1],_ENV) --add object to layer, default draw below player
end
end)
-- draw terrain
map(lvl_x,lvl_y,0,0,lvl_w,lvl_h,2)
-- draw objects
foreach(layers,function(l)
foreach(l,draw_object)
end)
-- draw platforms
map(lvl_x,lvl_y,0,0,lvl_w,lvl_h,8)