-
Notifications
You must be signed in to change notification settings - Fork 0
/
celeste-typed.lua
2203 lines (1982 loc) · 38.1 KB
/
celeste-typed.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
-- ~celeste~
-- matt thorson + noel berry
-- globals --
-------------
struct Vec {
x: number,
y: number
}
struct Rect {
x: number,
y: number,
w: number,
h: number
}
struct Room {
x: Num
y: Num
}
struct Cloud {
x: Num
y: Num
spd: Num
w: Num
}
struct Particle {
x: Num
y: Num
s: Num
spd: Num
off: Num
c: Num
}
struct DeadParticle {
x: Num
y: Num
t: Num
spd: Vec
}
struct Flip {
x: Bool
y: Bool
}
struct BaseObject {
collidable: Bool
solids: Bool
spr: ?
flip: Flip
x: Num
y: Num
hitbox: Rect
spd: Vec
rem: Vec
is_solid: Function<"is_solid">
is_ice: Function<"is_ice">
collide: Function<"collide">
check: Function<"check">
move: Function<"move">
move_x: Function<"move_x">
move_y: Function<"move_y">
}
struct Hair {
x: Num
y: Num
size: Num
}
struct PlayerType {
init: Function<["player.init"]>
update: Function<["player.update"]>
draw: Function<["player.draw"]>
}
struct Player extends BaseObject {
type: PlayerType
p_jump: Bool
p_dash: Bool
grace: Num
jbuffer: Num
djump: Num
dash_time: Num
dash_effect_time: Num
dash_target: Vec
dash_accel: Vec
hitbox: Rect
spr_off: Num
was_on_ground: Bool
hair: Array<Hair>
}
struct PlayerSpawnType {
tile: Num
init: Function<["player_spawn.init"]>
update: Function<["player_spawn.update"]>
draw: Function<["player_spawn.draw"]>
}
struct PlayerSpawn extends BaseObject {
type: PlayerSpawnType
spr: Num
target: Vec
state: Num
delay: Num
}
struct SpringType {
tile: Num
init: Function<["spring.init"]>
update: Function<["spring.update"]>
}
struct Spring extends BaseObject {
type: SpringType
hide_in: Num
hide_for: Num
delay: Num
}
struct BalloonType {
tile: Num
init: Function<["balloon.init"]>
update: Function<["balloon.update"]>
draw: Function<["balloon.draw"]>
}
struct Balloon extends BaseObject {
type: BalloonType
offset: Num
start: Num
timer: Num
}
struct FallFloorType {
tile: Num
init: Function<["fall_floor.init"]>
update: Function<["fall_floor.update"]>
draw: Function<["fall_floor.draw"]>
}
struct FallFloor extends BaseObject {
type: FallFloorType
state: Num
delay: Num
}
struct SmokeType {
init: Function<["smoke.init"]>
update: Function<["smoke.update"]>
}
struct Smoke extends BaseObject {
type: SmokeType
}
struct FruitType {
tile: Num
if_not_fruit: Bool
init: Function<["fruit.init"]>
update: Function<["fruit.update"]>
}
struct Fruit extends BaseObject {
type: FruitType
start: Num
off: Num
}
struct FlyFruitType {
tile: Num
if_not_fruit: Bool
init: Function<["fly_fruit.init"]>
update: Function<["fly_fruit.update"]>
draw: Function<["fly_fruit.draw"]>
}
struct FlyFruit extends BaseObject {
type: FlyFruitType
start: Num
fly: Bool
step: Num
sfx_delay: Num
}
struct LifeupType {
init: Function<["lifeup.init"]>
update: Function<["lifeup.update"]>
draw: Function<["lifeup.draw"]>
}
struct Lifeup {
type: LifeupType
duration: Num
flash: Num
}
struct FakeWallType {
tile: Num
if_not_fruit: Bool
init: Function<["fake_wall.init"]>
update: Function<["fake_wall.update"]>
}
struct FakeWall extends BaseObject {
type: FakeWallType
}
struct KeyType {
tile: Num
if_not_fruit: Bool
update: Function<["key.update"]>
}
struct Key extends BaseObject {
type: KeyType
}
struct ChestType {
tile: Num
if_not_fruit: Bool
init: Function<["chest.init"]>
update: Function<["chest.update"]>
}
struct Chest extends BaseObject {
type: ChestType
start: Num
timer: Num
}
struct PlatformType {
init: Function<["platform.init"]>
update: Function<["platform.update"]>
draw: Function<["platform.draw"]>
}
struct Platform extends BaseObject {
type: PlatformType
last: Num
}
struct MessageType {
tile: Num
last: Num -- This is never used. Message.last is used instead.
draw: Function<["message.draw"]>
}
struct Message extends BaseObject {
type: MessageType
last: Num
}
struct BigChestType {
tile: Num
init: Function<["big_chest.init"]>
draw: Function<["big_chest.draw"]>
}
struct BigChestParticle {
x: Num
y: Num
h: Num
spd: Num
}
struct BigChest extends BaseObject {
type: BigChestType
state: Num
timer: Num
particles: Array<BigChestParticle>
}
struct OrbType {
init: Function<["orb.init"]>
draw: Function<["orb.draw"]>
}
struct OrbParticle {
}
struct Orb extends BaseObject {
type: OrbType
particles: Array<OrbParticle> -- never used
}
struct FlagType {
tile: Num
init: Function<["flag.init"]>
draw: Function<["flag.draw"]>
}
struct Flag extends BaseObject {
type: FlagType
score: Num
show: Bool
}
struct RoomTitleType {
init: Function<["room_title.init"]>
draw: Function<["room_title.draw"]>
}
struct RoomTitle extends BaseObject {
type: RoomTitleType
delay: Num
}
union AnyObjectType {
PlayerType
PlayerSpawnType
SpringType
BalloonType
FallFloorType
SmokeType
FruitType
FlyFruitType
LifeupType
FakeWallType
KeyType
ChestType
PlatformType
MessageType
BigChestType
OrbType
FlagType
RoomTitleType
}
union AnyObject {
Player
PlayerSpawn
Spring
Balloon
FallFloor
Smoke
Fruit
FlyFruit
Lifeup
FakeWall
Key
Chest
Platform
Message
BigChest
Orb
Flag
RoomTitle
}
globals {
-- initialized at start of file
room: Room
objects: Array<AnyObject>
types: Array<AnyObjectType>
freeze: Num
shake: Num
will_restart: Bool
delay_restart: Num
got_fruit: Array<Bool>
has_dashed: Bool
sfx_timer: Num
has_key: Bool
pause_player: Bool
flash_bg: Bool
music_timer: Num
-- constants
k_left: Num
k_right: Num
k_up: Num
k_down: Num
k_jump: Num
k_dash: Num
-- initialized in title_screen
frames: Num
deaths: Num
max_djump: Num
start_game: Bool
start_game_flash: Num
-- initialized in begin_game
seconds: Num
minutes: Num
-- initialized later at top level of file
clouds: Array<Cloud>
particles: Array<Particle>
dead_particles: Array<DeadParticle>
-- global functions
_init: Function<["_init"]>
title_screen: Function<["title_screen"]>
begin_game: Function<["begin_game"]>
level_index: Function<["level_index"]>
is_title: Function<["is_title"]>
psfx: Function<["psfx"]>
create_hair: Function<["create_hair"]>
set_hair_color: Function<["set_hair_color"]>
draw_hair: Function<["draw_hair"]>
unset_hair_color: Function<["unset_hair_color"]>
break_spring: Function<["break_spring"]>
break_fall_floor: Function<["break_fall_floor"]>
init_object: Function<["init_object"]>
destroy_object: Function<["destroy_object"]>
kill_player: Function<["kill_player"]>''
restart_room: Function<["restart_room"]>
next_room: Function<["next_room"]>
load_room: Function<["load_room"]>
_update: Function<["_update"]>
_draw: Function<["_draw"]>
draw_object: Function<["draw_object"]>
draw_time: Function<["draw_time"]>
clamp: Function<["clamp"]>
appr: Function<["appr"]>
sign: Function<["sign"]>
maybe: Function<["maybe"]>
solid_at: Function<["solid_at"]>
ice_at: Function<["ice_at"]>
tile_flag_at: Function<["tile_flag_at"]>
tile_at: Function<["tile_at"]>
-- object types
player: PlayerType
player_spawn: PlayerSpawnType
spring: SpringType
balloon: BalloonType
fall_floor: FallFloorType
smoke: SmokeType
fruit: FruitType
fly_fruit: FlyFruitType
lifeup: LifeupType
fake_wall: FakeWallType
key: KeyType
chest: ChestType
platform: PlatformType
message: MessageType
big_chest: BigChestType
flag: FlagType
}
room = { x=0, y=0 }
objects = {}
types = {}
freeze = 0
shake = 0
will_restart = false
delay_restart = 0
got_fruit = {}
has_dashed = false
sfx_timer = 0
has_key = false
pause_player = false
flash_bg = false
music_timer = 0
k_left = 0
k_right = 1
k_up = 2
k_down = 3
k_jump = 4
k_dash = 5
-- entry point --
-----------------
function _init()
unique_name "_init"
body
title_screen()
end
function title_screen()
unique_name "title_screen"
body
got_fruit = {}
for i=0,29 do
add(got_fruit,false) end
frames=0
deaths=0
max_djump=1
start_game=false
start_game_flash=0
music(40,0,7)
load_room(7,3)
end
function begin_game()
unique_name "begin_game"
body
frames=0
seconds=0
minutes=0
music_timer=0
start_game=false
music(0,0,7)
load_room(0,0)
end
function level_index()
unique_name "level_index"
return Num
body
return room.x%8+room.y*8
end
function is_title()
unique_name "is_title"
return Bool
body
return level_index()==31
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=0+flr(rnd(5)/4),
spd=0.25+rnd(5),
off=rnd(1),
c=6+flr(0.5+rnd(1))
})
end
dead_particles = {}
-- player entity --
-------------------
player =
{
init=function(this: Player)
unique_name "player.init"
body
this.p_jump=false
this.p_dash=false
this.grace=0
this.jbuffer=0
this.djump=max_djump
this.dash_time=0
this.dash_effect_time=0
this.dash_target={x=0,y=0}
this.dash_accel={x=0,y=0}
this.hitbox = {x=1,y=3,w=6,h=5}
this.spr_off=0
this.was_on_ground=false
create_hair(this)
end,
update=function(this: Player)
unique_name "player.update"
locals {
input: Num
on_group: Bool
on_ice: Bool
jump: Bool
dash: Bool
maxrun: Num
accel: Num
deccel: Num
maxfall: Num
gravity: Num
wall_dir: Num
d_full: Num
d_half: Num
v_input: Num
}
body
if (pause_player) return
local input = btn(k_right) and 1 or (btn(k_left) and -1 or 0)
-- spikes collide
if spikes_at(this.x+this.hitbox.x,this.y+this.hitbox.y,this.hitbox.w,this.hitbox.h,this.spd.x,this.spd.y) then
kill_player(this) end
-- bottom death
if this.y>128 then
kill_player(this) end
local on_ground=this.is_solid(0,1)
local on_ice=this.is_ice(0,1)
-- smoke particles
if on_ground and not this.was_on_ground then
init_object(smoke,this.x,this.y+4)
end
local jump = btn(k_jump) and not this.p_jump
this.p_jump = btn(k_jump)
if (jump) then
this.jbuffer=4
elseif this.jbuffer>0 then
this.jbuffer-=1
end
local dash = btn(k_dash) and not this.p_dash
this.p_dash = btn(k_dash)
if on_ground then
this.grace=6
if this.djump<max_djump then
psfx(54)
this.djump=max_djump
end
elseif this.grace > 0 then
this.grace-=1
end
this.dash_effect_time -=1
if this.dash_time > 0 then
init_object(smoke,this.x,this.y)
this.dash_time-=1
this.spd.x=appr(this.spd.x,this.dash_target.x,this.dash_accel.x)
this.spd.y=appr(this.spd.y,this.dash_target.y,this.dash_accel.y)
else
-- move
local maxrun=1
local accel=0.6
local deccel=0.15
if not on_ground then
accel=0.4
elseif on_ice then
accel=0.05
if input==(this.flip.x and -1 or 1) then
accel=0.05
end
end
if abs(this.spd.x) > maxrun then
this.spd.x=appr(this.spd.x,sign(this.spd.x)*maxrun,deccel)
else
this.spd.x=appr(this.spd.x,input*maxrun,accel)
end
--facing
if this.spd.x!=0 then
this.flip.x=(this.spd.x<0)
end
-- gravity
local maxfall=2
local gravity=0.21
if abs(this.spd.y) <= 0.15 then
gravity*=0.5
end
-- wall slide
if input!=0 and this.is_solid(input,0) and not this.is_ice(input,0) then
maxfall=0.4
if rnd(10)<2 then
init_object(smoke,this.x+input*6,this.y)
end
end
if not on_ground then
this.spd.y=appr(this.spd.y,maxfall,gravity)
end
-- jump
if this.jbuffer>0 then
if this.grace>0 then
-- normal jump
psfx(1)
this.jbuffer=0
this.grace=0
this.spd.y=-2
init_object(smoke,this.x,this.y+4)
else
-- wall jump
local wall_dir=(this.is_solid(-3,0) and -1 or this.is_solid(3,0) and 1 or 0)
if wall_dir!=0 then
psfx(2)
this.jbuffer=0
this.spd.y=-2
this.spd.x=-wall_dir*(maxrun+1)
if not this.is_ice(wall_dir*3,0) then
init_object(smoke,this.x+wall_dir*6,this.y)
end
end
end
end
-- dash
local d_full=5
local d_half=d_full*0.70710678118
if this.djump>0 and dash then
init_object(smoke,this.x,this.y)
this.djump-=1
this.dash_time=4
has_dashed=true
this.dash_effect_time=10
local v_input=(btn(k_up) and -1 or (btn(k_down) and 1 or 0))
if input!=0 then
if v_input!=0 then
this.spd.x=input*d_half
this.spd.y=v_input*d_half
else
this.spd.x=input*d_full
this.spd.y=0
end
elseif v_input!=0 then
this.spd.x=0
this.spd.y=v_input*d_full
else
this.spd.x=(this.flip.x and -1 or 1)
this.spd.y=0
end
psfx(3)
freeze=2
shake=6
this.dash_target.x=2*sign(this.spd.x)
this.dash_target.y=2*sign(this.spd.y)
this.dash_accel.x=1.5
this.dash_accel.y=1.5
if this.spd.y<0 then
this.dash_target.y*=.75
end
if this.spd.y!=0 then
this.dash_accel.x*=0.70710678118
end
if this.spd.x!=0 then
this.dash_accel.y*=0.70710678118
end
elseif dash and this.djump<=0 then
psfx(9)
init_object(smoke,this.x,this.y)
end
end
-- animation
this.spr_off+=0.25
if not on_ground then
if this.is_solid(input,0) then
this.spr=5
else
this.spr=3
end
elseif btn(k_down) then
this.spr=6
elseif btn(k_up) then
this.spr=7
elseif (this.spd.x==0) or (not btn(k_left) and not btn(k_right)) then
this.spr=1
else
this.spr=1+this.spr_off%4
end
-- next level
if this.y<-4 and level_index()<30 then next_room() end
-- was on the ground
this.was_on_ground=on_ground
end, --<end update loop
draw=function(this: Player)
unique_name "player.draw"
body
-- clamp in screen
if this.x<-1 or this.x>121 then
this.x=clamp(this.x,-1,121)
this.spd.x=0
end
set_hair_color(this.djump)
draw_hair(this,this.flip.x and -1 or 1)
spr(this.spr,this.x,this.y,1,1,this.flip.x,this.flip.y)
unset_hair_color()
end
}
psfx=function(num: Num)
unique_name "psfx"
body
if sfx_timer<=0 then
sfx(num)
end
end
create_hair=function(obj: ?)
unique_name "create_hair"
body
obj.hair={}
for i=0,4 do
add(obj.hair,{x=obj.x,y=obj.y,size=max(1,min(2,3-i))})
end
end
set_hair_color=function(djump: Num)
unique_name "set_hair_color"
body
pal(8,(djump==1 and 8 or djump==2 and (7+flr((frames/3)%2)*4) or 12))
end
draw_hair=function(obj: Player, facing: Num)
unique_name "draw_hair"
locals {
last: Vec
}
body
local last={x=obj.x+4-facing*2,y=obj.y+(btn(k_down) and 4 or 3)}
foreach(obj.hair,function(h)
unique_name "draw_hair.foreach"
capture {
last: Vec
}
body
h.x+=(last.x-h.x)/1.5
h.y+=(last.y+0.5-h.y)/1.5
circfill(h.x,h.y,h.size,8)
last=h
end)
end
unset_hair_color=function()
unique_name "unset_hair_color"
body
pal(8,8)
end
player_spawn = {
tile=1,
init=function(this: PlayerSpawn)
unique_name "player_spawn.init"
body
sfx(4)
this.spr=3
this.target= {x=this.x,y=this.y}
this.y=128
this.spd.y=-4
this.state=0
this.delay=0
this.solids=false
create_hair(this)
end,
update=function(this: PlayerSpawn)
unique_name "player_spawn.update"
body
-- jumping up
if this.state==0 then
if this.y < this.target.y+16 then
this.state=1
this.delay=3
end
-- falling
elseif this.state==1 then
this.spd.y+=0.5
if this.spd.y>0 and this.delay>0 then
this.spd.y=0
this.delay-=1
end
if this.spd.y>0 and this.y > this.target.y then
this.y=this.target.y
this.spd = {x=0,y=0}
this.state=2
this.delay=5
shake=5
init_object(smoke,this.x,this.y+4)
sfx(5)
end
-- landing
elseif this.state==2 then
this.delay-=1
this.spr=6
if this.delay<0 then
destroy_object(this)
init_object(player,this.x,this.y)
end
end
end,
draw=function(this: PlayerSpawn)
unique_name "player_spawn.draw"
body
set_hair_color(max_djump)
draw_hair(this,1)
spr(this.spr,this.x,this.y,1,1,this.flip.x,this.flip.y)
unset_hair_color()
end
}
add(types,player_spawn)
spring = {
tile=18,
init=function(this: Spring)
unique_name "spring.init"
body
this.hide_in=0
this.hide_for=0
end,
update=function(this: Spring)
unique_name "spring.update"
locals {
hit: ?
below: ?
}
body
if this.hide_for>0 then
this.hide_for-=1
if this.hide_for<=0 then
this.spr=18
this.delay=0
end
elseif this.spr==18 then
local hit = this.collide(player,0,0)
if hit ~=nil and hit.spd.y>=0 then
this.spr=19
hit.y=this.y-4
hit.spd.x*=0.2
hit.spd.y=-3
hit.djump=max_djump
this.delay=10
init_object(smoke,this.x,this.y)
-- breakable below us
local below=this.collide(fall_floor,0,1)
if below~=nil then
break_fall_floor(below)
end
psfx(8)
end
elseif this.delay>0 then
this.delay-=1
if this.delay<=0 then
this.spr=18
end
end
-- begin hiding
if this.hide_in>0 then
this.hide_in-=1
if this.hide_in<=0 then
this.hide_for=60
this.spr=0
end
end
end
}
add(types,spring)
function break_spring(obj: Spring)
unique_name "break_spring"
body
obj.hide_in=15
end
balloon = {
tile=22,
init=function(this: Balloon)
unique_name "balloon.init"