a_pleasant_stroll/movement.lua

75 lines
1.7 KiB
Lua
Raw Normal View History

function init_movement(start_pos)
camera_pos = start_pos
-- [1] is vertical facing, [2] is horizontal, -1 is up/left, 0 is neutral,
-- and 1 is down/right
facing = {1, 0}
-- this is a constant for looking up player sprites by facing
player_lookup = {
2019-12-06 03:18:45 +00:00
[0] = {
[0] = -1, -- error state
[-1] = 122, -- left
[1] = 123, -- right
},
[-1] = {
[0] = 120, -- up
[-1] = 124, -- up-left
[1] = 125, -- up-right
},
[1] = {
[0] = 121, -- down
[-1] = 126, -- down-left
[1] = 127, -- down-right
},
}
2019-12-04 23:48:04 +00:00
end
function handle_input()
2019-12-06 04:02:37 +00:00
local new_pos = {camera_pos[1], camera_pos[2]}
if btnp(0) or btnp(1) or btnp(2) or btnp(3) then
if btnp(0) then
new_pos[1] -= 1 -- move left
facing[2] = -1
end
if btnp(1) then
new_pos[1] += 1 -- move right
facing[2] = 1
end
if not (btnp(0) or btnp(1)) then
facing[2] = 0
end
if btnp(2) then
new_pos[2] -= 1 -- move up
facing[1] = -1
end
if btnp(3) then
new_pos[2] += 1 -- move down
facing[1] = 1
end
if not (btnp(2) or btnp(3)) then
facing[1] = 0
end
end
2019-12-04 23:48:04 +00:00
if legal_move(new_pos) then
camera_pos = new_pos
end
-- todo: determine whether we need to regen map and what that means
end
function draw_player()
2019-12-06 03:18:45 +00:00
spr(player_lookup[facing[1]][facing[2]], 64, 64)
-- todo: animate the character on move
2019-12-04 23:48:04 +00:00
end
2019-12-06 03:18:45 +00:00
-- pos is camera position, meaning the player position is
-- pos + {8, 8}.
2019-12-04 23:48:04 +00:00
function legal_move(pos)
2019-12-06 04:02:37 +00:00
return not fget(mget(pos[1]+8, pos[2]+8), 0)
2019-12-04 23:48:04 +00:00
end