summaryrefslogtreecommitdiff
path: root/dyl.p8
diff options
context:
space:
mode:
Diffstat (limited to 'dyl.p8')
-rw-r--r--dyl.p8306
1 files changed, 6 insertions, 300 deletions
diff --git a/dyl.p8 b/dyl.p8
index 95f2e59..eb1464a 100644
--- a/dyl.p8
+++ b/dyl.p8
@@ -1,311 +1,17 @@
pico-8 cartridge // http://www.pico-8.com
version 43
__lua__
+#include util.lua
+#include math.lua
+#include entity.lua
+#include world.lua
+#include collisions.lua
+#include dyl.lua
------------------------
-- don't you leave --
-- emprespresso, 2026 --
------------------------
-walk_speed=35 -- powerup increase?
-step_t=time()
-step_dt=0
-
-id=0
-function next_id()
- i=id
- id=id+1
- return i
-end
-
-types={
- player=0,
- sword=1,
- enemy=2,
-}
-
-sword={
- id=next_id(),
- typ=types.sword,
- theta=0, -- pi/2=1,discrete [0,1]
- pos={x=10,y=10},
- spritepos={x=10,y=10},
- states={
- equip={seq={83},dt=1},
- },
- state="equip",
- state_t=step_t,
- reflect={x=false,y=false},
-}
-
-player={
- id=next_id(),
- typ=types.player,
- pos={x=50,y=50},
- spritepos={x=10,y=10},
- vel={x=0,y=0},
- los={x=1,y=0}, -- lineofsight
- states={
- idlex={seq={0,1},dt=1},
- idlepy={seq={3,19},dt=1},
- idleny={seq={6,22},dt=1},
- walkx={seq={2,0},dt=0.20},
- walkpy={seq={4,5},dt=0.20},
- walkny={seq={7,8},dt=0.20},
- },
- state="idle",
- state_t=step_t,
- equipped={sword},
- reflect={x=false,y=false},
-}
-
-enemy={
- id=next_id(),
- typ=types.enemy,
- pos={x=20,y=20},
- spritepos={x=20,y=20},
- vel={x=0,y=0},
- los={x=-1,y=0}, -- lineofsight
- states={
- idlex={seq={32,33},dt=1},
- idlepy={seq={32,33},dt=1},
- idleny={seq={32,33},dt=1},
- walkx={seq={32,34},dt=0.28},
- walkpy={seq={32,34},dt=0.28},
- walkny={seq={32,34},dt=0.28},
- },
- state="idle",
- state_t=step_t,
- reflect={x=false,y=false},
- knockback={vector={x=0,y=0},remaining=0}
-}
-
-entities={player,sword,enemy}
-
-sw,sh=8,8
-function is_colliding(a,b)
- ax1,bx1=a.pos.x,b.pos.x
- ax2,bx2=ax1+sw,bx1+sw
- ay1,by1=a.pos.y,b.pos.y
- ay2,by2=ay1+sh,by1+sh
- return (ax1<bx2 and ax2>bx1 and
- ay1<by2 and ay2>by1)
-end
-
-function handle_collision(a,b)
- if b.typ==types.enemy then
- --print(b.id,100,100)
- end
-end
-
-function run_collisions()
- for _ai,a in ipairs(entities) do
- for _bi,b in ipairs(entities) do
- if a.id==b.id then
- goto continue
- end
- if is_colliding(a,b) then
- handle_collision(a,b)
- end
- ::continue::
- end
- end
-end
-
-function enter_state(entity, state)
- if state==entity.state then
- return
- end
- entity.tstate=step_t
- entity.state=state
-end
-
--- prevent cobblestoning during
--- non-manhattan movement
--- by "lagging" the sprite
--- behind the physical position
---
-function update_spritepos(entity)
- -- step in only x or y.
- -- we can snap to the grid
- -- without cobblestoning.
- if entity.vel.y==0 or entity.vel.x==0 then
- entity.spritepos.x=entity.pos.x
- entity.spritepos.y=entity.pos.y
- return
- end
-
- nv=normalize(entity.vel)
- dx,dy=entity.pos.x-entity.spritepos.x,entity.pos.y-entity.spritepos.y
- adx,ady=abs(dx),abs(dy)
-
- yslow=abs(entity.vel.y)<abs(entity.vel.x)
- xslow=abs(entity.vel.x)<=abs(entity.vel.y)
-
- pushx,pushy=false,false
- jerkdelta=1
- if adx>=jerkdelta and xslow
- and ady>=1 then
- pushx,pushy=true,true
- elseif ady>=jerkdelta and yslow
- and adx>=1 then
- pushx,pushy=true,true
- elseif xslow and ady>=1 then
- pushy=true
- elseif yslow and adx>=1 then
- pushx=true
- end
-
- if pushx then
- func=flr
- if dx<0 then func=ceil end
- entity.spritepos.x=entity.spritepos.x+func(dx)
- end
- if pushy then
- func=flr
- if dy<0 then func=ceil end
- entity.spritepos.y=entity.spritepos.y+func(dy)
- end
-end
-
-suffixes={"ny","","py"}
-function update(entity)
- if entity.vel != nil then
- integral_vel=vec_scale(entity.vel,step_dt)
- entity.pos=vec_add(entity.pos,integral_vel)
- update_spritepos(entity)
-
- dx,dy=entity.vel.x,entity.vel.y
- ndx,ndy=normalize_scalar(dx),normalize_scalar(dy)
-
- if ndy!=0 then
- walksuffix=suffixes[ndy+2]
- enter_state(entity,"walk"..walksuffix)
- elseif ndx!=0 then
- enter_state(entity,"walkx")
- end
-
- if ndx!=0 then
- entity.los={x=ndx,y=0}
- end
- if ndy!=0 then
- entity.los={x=0,y=ndy}
- end
- entity.reflect.x=(entity.los.x<0)
-
- if ndx==0 and ndy==0 then
- if entity.los.x!=0 then
- enter_state(entity,"idlex")
- elseif entity.los.y!=0 then
- suffix=suffixes[entity.los.y+2]
- enter_state(entity,"idle"..suffix)
- end
- end
- end
-
- if entity.equipped!=nil then
- foreach(entity.equipped,
- equipped_from(entity))
- end
-end
-
-function equipped_from(entity)
- return function(equipped)
- if entity.los.y!=0 then
- equipped.theta=1
- else
- equipped.theta=0
- end
- equipped.reflect={
- x=(entity.los.x<0 or entity.los.y<0),
- y=(entity.los.y>0)
- }
- equipped.pos.y=entity.pos.y+entity.los.y*6
- equipped.pos.x=entity.pos.x+entity.los.x*6
- equipped.spritepos.x=flr(entity.spritepos.x+entity.los.x*6)
- equipped.spritepos.y=flr(entity.spritepos.y+entity.los.y*6)
- end
-end
-
-function get_frame(anim,t,theta)
- len=rawlen(anim.seq)
- frames_passed=(t/anim.dt)
- idx=flr(1+(frames_passed%len))
- dspritey=0
- if theta==1 then dspritey=16 end
- return anim.seq[idx]+dspritey
-end
-
-function draw(entity)
- a=entity.states[entity.state]
- t=step_t-entity.state_t
- f=get_frame(a,t,entity.theta)
- spr(f,
- entity.spritepos.x,entity.spritepos.y,
- 1,1,
- entity.reflect.x,entity.reflect.y
- )
-end
-
-
-function handle_input()
- dx,dy=0,0
- if btn(⬅️) then dx=dx-1 end
- if btn(➡️) then dx=dx+1 end
- if btn(⬇️) then dy=dy+1 end
- if btn(⬆️) then dy=dy-1 end
-
- player.vel=
- vec_scale(
- normalize({x=dx,y=dy}),
- walk_speed
- )
-end
-
-function _init()
-
-end
-
-function _update()
- cls(0)
- old_step=step_t
- step_t=time()
- step_dt=step_t-old_step
-
- handle_input()
- run_collisions()
- foreach(entities,update)
-end
-
-function _draw()
- foreach(entities,draw)
-end
-
-function magnitude(vec)
- return sqrt(vec.x*vec.x+vec.y*vec.y)
-end
-
-function normalize_scalar(val)
- if val!=0 then
- return val/abs(val)
- end
- return val
-end
-
-function vec_scale(vec,s)
- return {x=vec.x*s,y=vec.y*s}
-end
-
-function vec_add(a,b)
- return {x=a.x+b.x,y=a.y+b.y}
-end
-
-function normalize(vec)
- m=magnitude(vec)
- if m!=0 then
- return {x=vec.x/m,y=vec.y/m}
- end
- return vec
-end
__gfx__
00000000000000000000000000000000000000000000000000000c0000000c0000000c0000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005555555