do animations right for different player states

This commit is contained in:
Colin McMillen 2021-07-08 19:17:07 -04:00
parent da645cefc6
commit 5022dfbe5a

87
borb.p8
View File

@ -3,14 +3,21 @@ version 32
__lua__ __lua__
-- game code -- game code
-- player states enum
walk = 0
stand = 1
fly = 2
function init_world() function init_world()
player = {} player = {
player.x = 60 x = 60,
player.y = 112 y = 112,
player.vy = 0 vy = 0,
player.flipx = false flipx = false,
player.spr = 0 spr = 0,
player.moves = 0 state = stand,
time = 0
}
end end
function _init() function _init()
@ -29,6 +36,12 @@ function _update()
update_player() update_player()
end end
function set_state(state)
if (player.state == state) return
player.state = state
player.time = 0
end
function _draw() function _draw()
cls(12) cls(12)
camera_x = mid(0, 896, player.x - 64) camera_x = mid(0, 896, player.x - 64)
@ -44,30 +57,46 @@ function draw_bg()
end end
function update_player() function update_player()
if (btn(0)) then
player.x -= 1
player.moves += 1
player.flipx = true
end
if (btn(1)) then
player.x += 1
player.moves += 1
player.flipx = false
end
if (btnp(🅾️)) then if (btnp(🅾️)) then
player.vy = -5 player.vy = -5
player.jumping = true set_state(fly)
end
dx = bool2int(btn(1)) - bool2int(btn(0))
if (dx != 0) then
if player.state == fly then
dx *= 2
end
player.x += dx
player.flipx = dx < 0
if player.state == stand then
set_state(walk)
end
elseif player.state == walk then
set_state(stand)
end end
player.y += player.vy player.y += player.vy
player.vy += 0.5 player.vy += 0.5
if (player.y > 112) then if (player.y > 112) then
player.y = 112 player.y = 112
player.vy = 0 player.vy = 0
player.jumping = false if player.state == fly then
set_state(stand)
end end
player.spr = (player.moves / 3) % 3 + 1 end
if (player.x < 0) player.x = 0
if (player.x > 1016) player.x = 1016 player.x = mid(0, player.x, 1016)
-- update sprite
if player.state == stand then
player.spr = 1
elseif player.state == walk then
player.spr = (player.time / 2) % 2 + 1
else
player.spr = (player.time / 2) % 3 + 1
end
player.time += 1
end end
function draw_player() function draw_player()
@ -97,15 +126,19 @@ peach = 15
function print_ctr(s,y,c) function print_ctr(s,y,c)
print(s,64 - #s * 2,y,c) print(s,64 - #s * 2,y,c)
end end
function bool2int(b)
return b and 1 or 0
end
__gfx__ __gfx__
00000000ffff00ffffff00ffffff00ff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000ffff00ffffff00ffffff00ff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000fff0880ffff0880ffff0880f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000fff0880ffff0880ffff0880f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000ffff800fffff800fffff800f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000ffff800fffff800fffff800f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000fff0800800008008fff08008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000fff08008fff0800800008008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000ff08888ff888888fff08888f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000ff08888fff08888ff888888f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000f088888fff88888ff088888f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000f088888ff088888fff88888f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000088888fffff888ff088888ff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000088888ff088888fffff888ff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000fff4f4fffff4f4ffff4f4fff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000fff4f4ffff4f4ffffff4f4ff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000