-
Notifications
You must be signed in to change notification settings - Fork 2
/
flowerhead.p8
3208 lines (2845 loc) · 103 KB
/
flowerhead.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 16
__lua__
-- f l o w e r h e a d
-- by charlie tran
game_mode={
intro={},
game={is_game=1},
lvl_complete={},
outro={animtimer=0},
lvl_select={},
--debug_menu={}
}
cartdata("pootify_flowerhead")
function _init()
-- persistent data
-- last level completed
last_completed=dget(0)
-- best time
best_time=dget(1)
-- did the player skip levels?
skipped=false
current_game_mode=game_mode.intro
printh("------\n")
gravity=.05
friction=.88
-- speed of our animation loops
runanimspeed=.3
wallrunanimspeed=.2
-- game counters
gametime=0
death_count=0
thrown_count=0
coroutines={}
deferred_draws={}
-- holds all entities
entities={player}
game_mode.intro:init()
clouds:init()
cam:init()
levels:init()
player:init()
end
function _update60()
run_coroutines()
if not freeze then
current_game_mode:update()
end
end
function run_coroutines()
for _,coroutine in pairs(coroutines) do
if costatus(coroutine) != 'dead' then
local ok,error=cowrap(coroutine)
else
del(coroutines,coroutine)
end
end
end
function _draw()
current_game_mode:draw()
for _,deferred in pairs(deferred_draws) do
deferred()
end
--if toggles.performance then draw_debug() end
end
function game_mode.game:update()
--game runs at 60fps, increase
--gametime by 1/60 sec
gametime+=.016666667
cam:update()
for _,e in pairs(entities) do
e:update()
end
particles:update()
explosions:update()
end
function game_mode.game:draw()
cls()
clouds:draw()
cam:draw()
levels:draw()
flowers:draw()
for _,e in pairs(entities) do
e:draw()
end
particles:draw()
explosions:draw()
banners:draw()
cam:fade()
end
clouds={
speed=.1,
padding=40,
}
clouds.list={}
function reset_level()
player:init()
cam.fadeout=1
lvl.door_open=lvl.start_opened
if not lvl.started then
lvl.started=true
banners:add(lvl)
end
-- reset all tiles
crumbling={}
flowers.map={}
flowers.tiles={}
for _,t in pairs(lvl.changed_tiles) do
mset(t[1],t[2],t[3])
end
-- clean up bees
bees.count=0
sfx(-1,3)
for index,v in pairs(lvl.bee_index) do
lvl.bee_index[index]=1
end
lvl.percent_complete=0
lvl.planted=0
lvl.bombs_disabled=true
--reset entities
entities={player}
if lvl.flowerheart_x then
flowerheart_class:new({
x=lvl.flowerheart_x,
y=lvl.flowerheart_y
})
end
truncate(flowers.map)
truncate(particles.list)
truncate(explosions.list)
truncate(deferred_draws)
--spawn bombs
for _,s in pairs(lvl.bomb_spawns) do
bomb_class:new({
x=s[1],y=s[2],vx=0,vy=0
})
end
end
function clouds:init()
for i=1,45 do
srand(i)
local size=i/3
self.list[i]={
x=size+rnd(128-size),
y=size+rnd(128-size),
size=size
}
end
--reset randomness seed
srand(bnot(time()))
end
function clouds:draw()
-- reset camera before draw
-- so clouds move independent
-- of map & entities
camera(0,0)
-- this time factor is used to
-- drift the clouds in the
-- x direction. multiplied by
-- 10 to get the right cloud
-- drifting speed
-- draw the clouds as circles
-- drifting in the x direction
-- over time and with parallax
local tm=time()
-- cloud checker pattern
-- 0 = filled, 1 = empty
-- 0110
-- 1111
-- 1011
-- 1111
fillp(0b0110111110111111)
for _,cloud in pairs(self.list) do
local cloudx,cloudy
-- the y offset is the
-- product of our current cam
-- offset and the cloud size
-- so that larger (closer)
-- clouds appear to move more
-- multiplied the magic .01
-- to get the right parallax
-- feeling
cloudy=cloud.y-
(cam.y*cloud.size*.01)%
192
-- our x offset is the same,
-- with our time factor added
-- so that the clouds appear
-- to drift to the left
local xdrift=.1*cam.x+tm
local xoffset=cloud.x-
(xdrift*
cloud.size*
clouds.speed)
local xrange=128+
clouds.padding+
2*cloud.size
cloudx=xoffset%xrange- cloud.size
-- draw our cloud circle
circfill(cloudx,cloudy,cloud.size,1)
end
fillp()
end
level_class={
cx1=nil,cy1=nil,cx2=nil,cy2=nil,
index=nil,
timer_enabled=false,
num_bees=0,
changed_tiles={},
bombs_disabled=true,
desc=""
}
function level_class.new(o)
local level=setmetatable(
o or {},
{__index=level_class}
)
level:setup()
return level
end
function level_class:setup()
local cx1,cy1=self.cx1,self.cy1
--initialize the level object
--with initial coordinates
--from the top-left block
--x/y = pixel coords
--cx/cy = map cell coords
self.sx1=cx1*8
self.sy1=cy1*8
self.plantable=0
self.planted=0
self.percent_complete=0
self.obstacles={}
self.bomb_spawns={}
-- determine level bounds
-- set the pixel bounds in x/y
-- and cell bounds as cx/cy
for cx2=cx1,127 do
if mget(cx2,cy1)==9 then
self.cx2=cx2
self.sx2=cx2*8+8
break
end
end
for cy2=cy1,63 do
if mget(cx1,cy2)==7 then
self.cy2=cy2
self.sy2=cy2*8+8
break
end
end
--map all special tiles
for cy=cy1,self.cy2 do
for cx=cx1,self.cx2 do
--check for plantable tiles
--and set the plantable count
if is_plantable(cx,cy) then
self.plantable+=1
end
--set player spawn
if mget(cx,cy)==64 then
self.spawnx=cx*8+3
self.spawny=cy*8
mset(cx,cy,0)
end
--map obstacles
local tile=mget(cx,cy)
local is_obs=is_wall(tile) or
is_spike(tile)
if is_obs then
self.obstacles[cy]=self.obstacles[cy] or {}
self.obstacles[cy][cx]=true
end
--set flowerheart location
if is_flowerheart(tile) then
self.flowerheart_x=cx*8+3
self.flowerheart_y=cy*8+2
mset(cx,cy,0)
end
--set door location
if tile==35 or tile==36 then
self.door_cx=cx
self.door_cy=cy
self.door_sx=cx*8
self.door_sy=cy*8
self.start_opened = tile==36
end
if tile==18 then
add(
self.bomb_spawns,
{cx*8+3,cy*8+3}
)
mset(cx,cy,0)
end
end
end -- end special tile loop
--bee_index represents at what
--planted number a bee spawns
--i.e. if self.bee_index[5],
--when tile #5 is planted,
--a bee will spawn
self.bee_index={}
--based on the self.num_bees,
--distribute the bee spawning
for i=1,self.num_bees do
local bee_trigger=flr(
i / (self.num_bees+1) *
self.plantable
)
self.bee_index[bee_trigger]=1
end
end
function level_class:open_door()
self.door_open=true
sfx(0,0)
add(
deferred_draws,
self:make_door_anim())
end
function level_class:make_door_anim()
local x,y=self.door_cx*8+5,
self.door_cy*8+2
local frame_count=0
return function()
if frame_count>240 then return end
frame_count+=1
local t=time()
local rays=16
local dx,dy=player.x-x,player.y-y
-- pythag dist to the player
-- divide then mult by 1000
-- to avoid int overflow
local distance=sqrt(
(dx/1000)^2+(dy/1000)^2
)*1000
-- start shrinking the rays
-- after 120 frames
if frame_count>120 then
distance*=1-(frame_count-120)/120
end
-- get the angle to the player
local angle=.5+atan2(dx,dy)
for i=1,rays do
-- tmod is a time offset so
-- that each ray starts at
-- different angle
local tmod=t/rays+i/rays
-- subtract our angle from
-- tmod so that it points
-- towards the player
local dmod_angle=tmod-angle
-- modify distance so that it
-- shrinks while pointing
-- away from the player
local dmod=-.7*cos(dmod_angle)*(1-.2*cos(t/4))
fillp(0b0011100111000110.1)
line(
x,y,
x+dmod*distance*cos(tmod),
y+dmod*distance*sin(tmod),
10)
fillp()
end
end
end
levels={ list={} }
function levels:init()
-- reload map data from cart
reload(0x2000,0x2000,0x1000)
-- level list
levels.list={
add_level(1 ,0 ,0 ,0,"hive entrance"),
add_level(2 ,16,0 ,0,"outer defenses"),
add_level(3 ,32,0 ,0,"inner garden"),
add_level(4 ,48,0 ,0,"viaduct"),
add_level(5 ,64,0 ,0,"over the top"),
add_level(6 ,78,0 ,0,"winding passage"),
add_level(7 ,94,0 ,1,"guard post"),
add_level(8 ,16,16,0,"unsteady grounds"),
add_level(9 ,0 ,16,3,"barracks"),
add_level(10,32,16,0,"catacombs"),
add_level(11,48,16,6,"trench run"),
add_level(12,61,32,1,"hidden tunnel"),
add_level(13,78,16,2,"twisted tower"),
add_level(14,96,32,6,"the throne room"),
add_level(15,0 ,32,2,"escape the hive")
}
-- manually add tutorials
levels.list[3].tutorial={
"press ❎",
"flower the floor",
"unlock the door"
}
levels.list[7].tutorial={
"",
"beware the bee",
"defeat it with three"
}
levels.index=1
levels.door_frame=0
lvl=levels.list[1]
end -- levels:init()
function add_level(i,x,y,b,d)
return level_class.new({
index=i,
cx1=x,
cy1=y,
num_bees=b,
desc=d
})
end
function levels:draw()
-- animate the door
if lvl.door_open then
self.door_frame=(self.door_frame+.0625)%5
mset(lvl.door_cx,lvl.door_cy,37+self.door_frame)
else
self.door_frame=(self.door_frame+.0625)%5
mset(lvl.door_cx,lvl.door_cy,32+self.door_frame)
end
--if exited, only draw the door
if lvl.exited then
map(lvl.door_cx,lvl.door_cy,lvl.door_sx,lvl.door_sy,1,1)
else
-- otherwise, draw whole level
map(
lvl.cx1,lvl.cy1, -- cell coords of level origin
lvl.sx1,lvl.sy1, -- screen coords of level origin
lvl.cx2-lvl.cx1+1,
lvl.cy2-lvl.cy1+1)
end
-- draw plant remainder
-- above a locked exit
if not lvl.door_open then
local rem=""..(lvl.plantable-lvl.planted)
print(rem,lvl.door_sx+(8-#rem*4)/2,lvl.door_sy-6,10)
end
end
function levels:goto_next()
self.index+=1
last_completed=self.index
dset(0,mid(dget(0),last_completed,15))
lvl=self.list[self.index]
-- stop bee buzzing sfx
sfx(-1,3)
if lvl then
current_game_mode=game_mode.game
truncate(deferred_draws)
music(0,0,1)
reset_level()
else -- no lvl means game over
current_game_mode=game_mode.outro
music(13)
end
end
banners={list={}}
function banners:add(level)
local banner={
title="level "..level.index,
caption=level.desc,
height=32,
x=0
}
-- start banner off screen
banner.y=-banner.height
add(self.list,banner)
local anim={
duration=45,
props={x=0,y=64-(banner.height/2)}
}
local seq=coroutine_sequence({
make_animation(banner,anim),
make_delay(60),
function() del(self.list,banner) end
})
add(coroutines,seq)
end
function banners:draw()
--local ox,oy=cam.x-64,cam.y-64
local tm=time()
for _,b in pairs(self.list) do
-- adjust banner x and y for camera position
local x=cam.x-64
local y=cam.y-64+b.y
local width=73
local margin=(128-width)/2
-- draw banner shadow
rectfill(
x+margin+2,y+2,
x+margin+width+2,y+b.height+2,
2 -- color
)
-- draw banner fill
rectfill(
x+margin,y,
x+margin+width,y+b.height,
1 -- color
)
-- animate level title
--for i=1,#b.title do
-- print(
-- sub(b.title,i,i),
-- x+margin+(width-#b.title*6)/2 + (i-1)*6,
-- y+b.height/4+sin(i/#b.title + tm/2),
-- 10)
--end
print(
b.title,
x+margin+(width-#b.title*4)/2,
y+b.height/4,
10)
print(
b.caption,
x+margin+(width-#b.caption*4)/2,
y+b.height*3/4,
7)
end
end
function game_mode.lvl_complete:start()
-- change game mode and exit the level
current_game_mode=game_mode.lvl_complete
lvl.exited=true
music(10,0,15)
cinematic(
1,
120,
function()
levels:goto_next()
end
)
end
function game_mode.lvl_complete:update() end
function game_mode.lvl_complete:draw()
cls()
clouds:draw()
cam:draw()
levels:draw()
for _,e in pairs(entities) do e:draw() end
end
--------------------------------
--visual effects----------------
-- cinematic vignette overlay
function cinematic(color,hold,callback,lines)
local box1={
x=cam.x-64,y=cam.y-64-127,
w=127,h=127,color=color,
params={
props={
x=cam.x-64,
y=player.y-15-127
},
duration=30,
hold=hold-30
},
}
box1.params.draw=function()
draw_box(box1)
end
local box2={
x=cam.x-64,y=cam.y+64,
w=127,h=127,color=color,
params={
props={
x=cam.x-64,
y=player.y+15
},
duration=30,
hold=hold-30
},
}
box2.params.draw=function()
draw_box(box2)
end
deferred_animate(box1,box1.params)
deferred_animate(box2,box2.params)
-- draw text if present
if lines then
local text={x=box2.x,y=box2.y}
deferred_animate(
text,
{
props={
x=text.x,
y=box2.params.props.y+6
},
duration=30,
hold=hold-30,
draw=function()
for i,line in pairs(lines) do
print(
line,
text.x + (64-#line*2),
text.y + 8*(i-1),
7)
end
end
}
)
end
add(
coroutines,
coroutine_sequence({
make_delay(hold),
callback
})
)
end
function poof(x,y,c)
for i=1,64 do
spawnp(
x,y,
cos(i/64), -- vx
sin(i/64), -- vy
1, -- jitter
c, -- color
22 -- duration
)
end
end
--------------------------------
--entities----------------------
-- describes a game object that
-- has a position, velocity,
-- a sprite to draw, collides
-- with map or other entities
-- and can be destroyed
entity_class={
x=0, y=0, vx=0, vy=0,
w=0, h=0, wr=0, hr=0,
scale=1,
anim={timer=0,frames=0,speed=1},
spr_x=0,spr_y=0,
flipx=false,
dead=false,
dying=false,
map_collide=true,
entity_collide=false,
has_gravity=true,
}
function entity_class:new(props)
local entity = setmetatable(
props or {},
{__index=entity_class}
)
return insert(entities,entity)
end
--default map collision callback
--does nothing
function entity_class:m_collide_callback(collision)
end
function entity_class:die()
self.dead=true
del(entities,self)
end
function entity_class:move()
--move through all our x steps,
--then our y steps
local xsteps=abs(self.vx)
local step,coll
for i=xsteps,0,-1 do
step=min(i,1)*sgn(self.vx)
coll=self.map_collide and
m_collide(self,'x',step)
if coll then
self:m_collide_callback(coll)
break
else
if not self.is_player then
self:check_e_collisions()
end
self.x+=step
end
end
if self.has_gravity then
self.vy+=gravity
end
local ysteps=abs(self.vy)
for i=ysteps,0,-1 do
step=min(i,1)*sgn(self.vy)
coll=self.map_collide and
m_collide(self,'y',step)
if coll then
self:m_collide_callback(coll)
break
else
if not self.is_player then
self:check_e_collisions()
end
self.y+=step
end
end
end
function entity_class:check_e_collisions()
for _,entity in pairs(entities) do
if entity ~= self then
if e_collide(self,entity) then
self:e_collide_callback(entity)
end
end
end
end
function entity_class:draw()
self:animate()
end
function entity_class:animate()
if self.dead then return end
-- increment the timer by
-- the anim speed, then mod it
-- by our total frame count to
-- get current 'frame number'
self.anim.timer =
(self.anim.timer +
self.anim.speed) %
self.anim.frames
-- draw the entity's sprite
-- from the sprite sheet
sspr(
--the sprite's location on
--the sheet is based on the
-- spr_x and spr_y attributes
-- spr_x is multiplied by the
-- anim timer because anim
-- frames are laid out
-- x-adjacent on the sheet
self.spr_x+flr(self.anim.timer)*self.w,
self.spr_y,
-- width and height of sprite
self.w,
self.h,
-- the screen coordinates of
-- where sprite is drawn are
-- offset by the width radius
-- and height radius, making
-- the sprite's x and y pos
-- the center of the sprite
self.x-self.wr*self.scale,
self.y-self.hr*self.scale,
-- scale the drawn sprite
self.w*self.scale,
self.h*self.scale,
-- flip drawing on the x axis
-- if self.flipx is true
self.flipx
)
end
--------------------------------
--flowerheart-------------------
flowerheart_class={
name="flowerheart",
w=7,h=7,
wr=3,hr=3,
spr_x=56,spr_y=40,
anim={timer=0,frames=7,speed=.142857143}
}
setmetatable(flowerheart_class,{__index=entity_class})
function flowerheart_class:new(obj)
return setmetatable(
entity_class:new(obj or {}),
{__index=flowerheart_class}
)
end
function flowerheart_class:update()
self:check_e_collisions()
end
tuts_done={}
function flowerheart_class:e_collide_callback(entity)
if not entity.is_player then return end
if lvl.tutorial
and not tuts_done[lvl.index]
then
tuts_done[lvl.index]=1
music(11,0,15)
freeze=true
cinematic(
3,
260,
function()
freeze=false
self:enable_bombing()
music(0,0,1)
sfx(7)
end,
lvl.tutorial
) -- cinematic()
else
self:enable_bombing()
sfx(7)
end
end
function flowerheart_class:enable_bombing()
self:die()
lvl.bombs_disabled=false
poof(self.x,self.y,3)
end
--------------------------------
--player object-----------------
player={}
setmetatable(player,{__index=entity_class})
function player:init()
for k,v in pairs(player_init_vals) do player[k]=v end
self.x=lvl.spawnx
self.y=lvl.spawny
end
player_init_vals={
name="player",
scale=1,
is_player=1,
-- velocity
vx=0,
vy=0,
--player's previous position
--and flip state,
-- for effects rendering
prevx=0,
prevy=0,
prevf=0,
--the "effects" timer
etimer=0,
--the sprite is 3x5, so the
--wr and hr dimensions are
--radii, and x/y is the
--initial center position
wr=1,
hr=2,
w=3,
h=5,
hit_jump=false,
--instantaneous jump velocity
--the "power" of the jump
jumpv=1.5,
--movement states
standing=false,
wallsliding=false,
disable_input=false,
--what direction we're facing
--1 or -1, used when we're
--facing away from a wall
--while sliding
facing=1,
--bomb input state
throw_bomb=false,
is_bombing=false,
--timers used for animation
--states and particle fx
falltimer=7,
landtimer=0,
runtimer=0,
headanimtimer=0,
throwtimer=0,
-- jump buffer timing
jump_buffer=8,
jump_buffer_timer=9,
dead=false,
dying=false,
dying_timer=0,
spr=64,
map_collide=true,
}
-- player sprite numbers------
--64 standing
--65 running 1
--66 running 2
--67 crouching (post landing)
--80 jumping