75 lines
1.7 KiB
Lua
75 lines
1.7 KiB
Lua
function init_movement()
|
|
camera_pos = {0, 0}
|
|
|
|
-- [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 = {
|
|
[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
|
|
},
|
|
}
|
|
end
|
|
|
|
|
|
function handle_input()
|
|
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
|
|
|
|
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()
|
|
spr(player_lookup[facing[1]][facing[2]], 64, 64)
|
|
-- todo: animate the character on move
|
|
end
|
|
|
|
-- pos is camera position, meaning the player position is
|
|
-- pos + {8, 8}.
|
|
function legal_move(pos)
|
|
return not fget(mget(pos[1]+8, pos[2]+8), 0)
|
|
end
|