summaryrefslogtreecommitdiff
path: root/dyl.p8
blob: 95f2e59084d3c7c59a535a98e9445ca4c42cf03e (plain) (blame)
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
pico-8 cartridge // http://www.pico-8.com
version 43
__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
00000000000000000000000000655000006550000065500000c7c60000c7c60000c7c60000000000000000000000000000000000000000000000000005555555
006c7c0000000000006c7c00006c7c00006c7c00006c7c00005556000055560000555600000000000000000000000000000000000000000000000000055a0755
00c1c100006c7c0000c1c10000c1c10000c1c10000c1c10000055000000550000005500000000000000000000000000000000000000000000000000005508555
005ccc0c00c1c10c005ccc0c00000000000000000000000000000000000004000040000000000000000000000000000000000000000000000000000005505f55
00000000005ccc000000000000400400000004000040000000400400004000000000040000000000000000000000000000000000000000000000000005555555
004004000040040000044000000c0000004c0000000c040000000000000000000000000000000000000000000000000000000000000000000000000005555555
00000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000c7c600000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000555600000000000000000000000000000000000000000000000000000000000000000000000000
007eee00000000000000000000655000000000000000000000055000000000000000000000000000000000000000000000000000000000000000000000000000
00e0e000007eee0000000000006c7c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00eeee0000e0e0000000000000c1c100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000eeee000000000000400400000000000000000000400400000000000000000000000000000000000000000000000000000000000000000000000000
002002000020020000000000000c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00b33330000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0033a3a000b3333000b3333000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
003b33300033a3a00033a3a000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00055b00003b3330003b333000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0005b3000005b3000005b30000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00335350003353500033535000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00500030005000300005030000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000030005c00050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
04444000000000000000300005cc0c50000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0499400000000000000880000511c150000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
04444000000000000087880005111150000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00440000000000000088880005111150000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000008800000555500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00040000000400000000007000000040000000800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00504000005040000000075000030400000008e00005500005090000000900000000000000000000000000000000000000000000000000000000000000000000
0050900005777900000075000000400000008e000055560000555550055555600000000000000000000000000000000000000000000000000000000000000000
005040000500400000975000000404000058e0000006550000550000005500000000000000000000000000000000000000000000000000000000000000000000
00504000005040000049000000400030006500000055500000500000005000000000000000000000000000000000000000000000000000000000000000000000
00040000000400000400000004000000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000007000000040000000800000000000050000000600000000000000000000000000000000000000000000000000000000000000000000
00000000000900000000075000030400000008e00006500000050000000500000000000000000000000000000000000000000000000000000000000000000000
0049440000474400000075000000400000008e000055550000050000000500000000000000000000000000000000000000000000000000000000000000000000
040000400407004000975000000404000058e0000055650000955000009550000000000000000000000000000000000000000000000000000000000000000000
00555500005705000049000000400030006500000005050000055500000555000000000000000000000000000000000000000000000000000000000000000000
00000000000550000400000004000000060000000000000000500000000500000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00056000000600000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000005650600000000000000000000005006500000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000560000000000000000005606056600650000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000060000000000000000650005665000660000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000650000005605000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000056600000000000000006000050000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000600000060000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
77055505000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000004444005555505000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
05550555044004405000055000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000040000400505050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
55077705040550400550050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000004000a400505550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
05550555040000400550050000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000555550000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__sfx__
000100002605026050240502305022050210502105021050220500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00090000000502b3502b3502a35029350293503235032350313503134031330209303032030320303303034031340313403334034340373400f050340502f0502c05000000000000000000000000000000000000