450 lines
20 KiB
Lua
450 lines
20 KiB
Lua
pico-8 cartridge // http://www.pico-8.com
|
||
version 42
|
||
__lua__
|
||
function _init()
|
||
|
||
for i=0,20 do
|
||
printh("")
|
||
end
|
||
|
||
printh("init")
|
||
end
|
||
|
||
function _update60()
|
||
if statechange then
|
||
statechange=false
|
||
states[state]:init()
|
||
end
|
||
states[state]:update()
|
||
|
||
end
|
||
|
||
function _draw()
|
||
states[state]:draw()
|
||
for r in all(states[state].routines) do
|
||
if costatus(r) == "dead" then
|
||
del(states[state].routines,r)
|
||
else
|
||
assert(coresume(r))
|
||
end
|
||
end
|
||
end
|
||
-->8
|
||
--globals
|
||
--global environment
|
||
global=_ENV
|
||
|
||
--timer
|
||
tim=0
|
||
|
||
--class
|
||
class=setmetatable({
|
||
new=function(_ENV,tbl)
|
||
tbl=setmetatable(tbl or {},{
|
||
__index=_ENV
|
||
})
|
||
|
||
tbl:init()
|
||
|
||
return tbl
|
||
end,
|
||
init=function()end
|
||
},{__index=_ENV})
|
||
|
||
|
||
--states
|
||
state="intro"
|
||
statechange=true
|
||
states={
|
||
--state.intro
|
||
intro={
|
||
init=function(self)
|
||
self.x=45
|
||
self.y=128
|
||
self.intro=cocreate(function()
|
||
for i=80,30,-1 do
|
||
self.y=i
|
||
yield()
|
||
end
|
||
end)
|
||
end,
|
||
update=function(self)
|
||
if btnp(🅾️) then
|
||
statechange=true
|
||
state="gameplay"
|
||
end
|
||
if costatus(self.intro)!="dead" then
|
||
coresume(self.intro)
|
||
end
|
||
|
||
end,
|
||
draw=function(self)
|
||
cls()
|
||
cursor(self.x,self.y)
|
||
print("welcome!")
|
||
print("⬅️ left")
|
||
print("➡️ right")
|
||
print("❎ jump")
|
||
print("🅾️ reset")
|
||
print("press 🅾️ to continue")
|
||
end
|
||
},
|
||
--state.gameplay
|
||
gameplay={
|
||
routines={}, --todo add death anims
|
||
init=function()
|
||
pl = player:new()
|
||
tim=time()
|
||
pl.x,pl.y=maps[currentmap]:init()
|
||
add(entities,pl)
|
||
end,
|
||
update=function()
|
||
buttons=btn()
|
||
for entity in all(entities) do
|
||
entity:update()
|
||
end
|
||
end,
|
||
draw=function()
|
||
cls()
|
||
maps[currentmap]:draw()
|
||
for e in all(entities) do
|
||
e:draw()
|
||
end
|
||
cursor(0,100)
|
||
color(7)
|
||
print("time:"..time()-tim)
|
||
print("attempts:"..count(replays))
|
||
|
||
end
|
||
},
|
||
--state.replay
|
||
replay={
|
||
routines={},
|
||
init=function(self)
|
||
printh(#replays)
|
||
|
||
end,
|
||
update=function(self)
|
||
for entity in all(entities) do
|
||
entity:update()
|
||
|
||
end
|
||
if #entities==0 then
|
||
entities={}
|
||
printh("replays:"..#replays)
|
||
for a in all(replays) do
|
||
t={}
|
||
for item in all(a) do
|
||
add(t,item)
|
||
end
|
||
rep=replayer:new({actions=t,c=rnd(16)})
|
||
rep.x,rep.y=maps[currentmap]:init()
|
||
add(entities,rep)
|
||
end
|
||
end
|
||
end,
|
||
draw=function(self)
|
||
cls()
|
||
maps[currentmap]:draw()
|
||
for e in all(entities) do
|
||
e:draw()
|
||
end
|
||
cursor(10,100)
|
||
print("you won! in only "..#replays.." attempts!")
|
||
print("press 🅾️ to go to next level")
|
||
|
||
end,
|
||
|
||
}
|
||
}
|
||
|
||
--entity
|
||
entity=class:new({
|
||
x=1,
|
||
y=1,
|
||
w=8,
|
||
h=8,
|
||
routines={},
|
||
actions={},
|
||
})
|
||
entities={}
|
||
|
||
--btn
|
||
buttons=0
|
||
|
||
--replays
|
||
replays={}
|
||
|
||
--gravity
|
||
gravity=.2
|
||
|
||
--helper functions
|
||
function hitx(e)
|
||
fl=0
|
||
for x=0,e.w do
|
||
x1,y1,y2=(e.x+x)/8,e.y-1/8,(e.y+e.h-1)/8
|
||
fl=fl|fget(mget(x1,y1))|fget(mget(x1,y2))
|
||
end
|
||
return fl
|
||
end
|
||
|
||
function hity(e)
|
||
fl=0
|
||
for y=0,e.h do
|
||
x1,x2,y1=e.x/8,(e.x+e.w)/8,(e.y+y)/8
|
||
fl=fl|fget(mget(x1,y1))|fget(mget(x2,y1))
|
||
end
|
||
return fl
|
||
end
|
||
|
||
function hitbottom(obj)
|
||
fl=0
|
||
for x=0,obj.w,obj.w do
|
||
x1,y1=(obj.x+x)/8,(obj.y+obj.h+1)/8 --1 below
|
||
fl=fl|fget(mget(x1,y1))
|
||
end
|
||
return (fl==1)
|
||
end
|
||
|
||
--restart(win?,actions)
|
||
function restart(w,obj)
|
||
add(replays,obj.actions)
|
||
if w then
|
||
sfx(10,1)
|
||
entities={}
|
||
tim=time()
|
||
for a in all(replays) do
|
||
t={}--this is necessary because it otherwise stores a ref, not a clone
|
||
for item in all(a) do
|
||
add(t,item)
|
||
end
|
||
rep=replayer:new({actions=t,c=rnd(16)})
|
||
rep.x,rep.y=maps[currentmap]:init()
|
||
add(entities,rep)
|
||
end
|
||
state="replay"
|
||
statechange=true
|
||
else
|
||
sfx(9)
|
||
add(states[state].routines,cocreate(function()
|
||
a={16,16,32,32,48,48,48}
|
||
bx,by=obj.x,obj.y
|
||
for x in all(a) do
|
||
printh(bx)
|
||
spr(x,bx,by)
|
||
yield()
|
||
end
|
||
end))
|
||
entities,tim={},time()
|
||
sx,sy=maps[currentmap]:init()
|
||
add(entities,player:new({actions={},x=sx,y=sy}))
|
||
|
||
end
|
||
end
|
||
|
||
|
||
-->8
|
||
--characters
|
||
|
||
--npc
|
||
npc=entity:new({
|
||
spd=.5,
|
||
s=0,--sprite
|
||
v=.5,
|
||
c=8,
|
||
f=false,
|
||
update=function(_ENV)
|
||
end,
|
||
draw=function(_ENV)
|
||
pal(8,c)
|
||
spr(s,x-(8-w)/2,y-(8-h)/2,1,1,f)
|
||
pal()
|
||
end
|
||
|
||
})
|
||
|
||
player=npc:new({
|
||
x=6,
|
||
y=6,
|
||
s=0,
|
||
yv=0,
|
||
xv=0,
|
||
-- w=6,
|
||
-- h=6,
|
||
j=3, --jump height
|
||
friction=.5,
|
||
actions={},
|
||
update=function(self)
|
||
-- self.xv,self.yv=0,0 --l,r,u,d
|
||
self.xv*=self.friction
|
||
self:grav()
|
||
self:move()
|
||
self.x+=self.xv
|
||
self.y+=self.yv
|
||
if (self.xv>0) self.f=false
|
||
if (self.xv<0) self.f=true
|
||
end,
|
||
grav=function(self)
|
||
self.yv+=gravity
|
||
end,
|
||
jump=function(self)
|
||
if (hitbottom(self)) then
|
||
sfx(8,-1,0,rnd(16))
|
||
self.yv-=self.j
|
||
end
|
||
end,
|
||
move=function(self)
|
||
local b=global.buttons
|
||
if ((b&0b0001)!=0) self.xv-=self.v
|
||
if ((b&0b0010)!=0) self.xv+=self.v
|
||
if ((b&0x10)!=0) self:jump()
|
||
if (btnp(❎)) restart(false,self)
|
||
t=player:new({x=self.x+self.xv,y=self.y+self.yv})
|
||
fx,fy=hitx(t),hity(t)
|
||
if (((fx|fy)&0x4)!=0) restart(false,self)
|
||
if (((fx|fy)&0x2)!=0) restart(true,self)
|
||
-- if ((fx&0x1)!=0) self.xv=0
|
||
if ((fy&0x1)!=0) self.yv=0
|
||
|
||
add(self.actions,b)
|
||
end
|
||
})
|
||
|
||
replayer=player:new({
|
||
i=1,
|
||
alive=true,
|
||
c=8,
|
||
move=function(self)
|
||
local b = self.actions[1] or 0x0
|
||
if ((b&0x1)!=0) self.xv-=self.v
|
||
if ((b&0x2)!=0) self.xv+=self.v
|
||
if ((b&0x10)!=0) self:jump()
|
||
|
||
t=player:new({x=self.x+self.xv,y=self.y+self.yv})
|
||
fx,fy=hitx(t),hity(t)
|
||
if ((fx&0x1)!=0) self.xv=0
|
||
if ((fy&0x1)!=0) self.yv=0
|
||
deli(self.actions,1)
|
||
if (#self.actions==0) then
|
||
del(entities,self)
|
||
sfx(9)
|
||
end
|
||
|
||
end
|
||
})
|
||
-->8
|
||
--maps
|
||
maps={
|
||
forrest={
|
||
x=0,
|
||
y=0,
|
||
pl={},
|
||
init=function(self)
|
||
map(self.x,self.y)
|
||
for x=0,15 do
|
||
for y=0,15 do
|
||
if (mget(x,y)==0) then
|
||
px,py=x*8,y*8
|
||
end
|
||
end
|
||
end
|
||
return px,py
|
||
end,
|
||
draw=function(self)
|
||
pal(pl)
|
||
map(self.x,self.y)
|
||
|
||
for x=0,128,8 do
|
||
for y=0,128,8 do
|
||
if fget(mget(x/8,y/8),0x0) then
|
||
if (not fget(mget(x/8,y/8-1),0x0)) then
|
||
sspr(16,8,8,2,x,y)
|
||
end
|
||
end
|
||
end
|
||
end
|
||
end,
|
||
}
|
||
}
|
||
currentmap="forrest"
|
||
__gfx__
|
||
000000008888888855555555eeeeeeee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
000000008888888855555555eeeeeeee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
088888808888888855555555eeeeeeee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
088080808888888855555555eeeeeeee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
088080808888888855555555eeeeeeee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
088888808888888855555555eeeeeeee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
088888808888888855555555eeeeeeee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
008008008888888855555555eeeeeeee000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
0000000000000000b3bb33bb00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00000000000075503b00000b00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00000000000005c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00080000000005500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
0000800000000bb00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
0000000000008b800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00000000000080800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00888080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00000000000000009999999900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00000000000000009999999900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00800800000000009999999900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00808000000000009999999900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00088800000000009999999900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00808000000000009999999900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00008800000000009999999900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
00000000000000009999999900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
__gff__
|
||
0004010200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
__map__
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222220222222222032222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222220222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222020222220222222222222222220222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222220222220222022222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222202220222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222220222222222222202222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
0222222222220222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
0022220222220222022222022222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222220222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
0022022222220222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
0202020202020201010101010101010102222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222220000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
__sfx__
|
||
000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
001000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
0001000029050340502f0502505016050090500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
0004000017750097500c750157502c750147501475014750057500a75005750057500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|
||
000500001a2501e2501f2502325000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
|