Add some rudimentary code for facing and collision detection.
This commit is contained in:
parent
bbd3c089d9
commit
546de63412
2 changed files with 107 additions and 6 deletions
54
movement.lua
54
movement.lua
|
@ -1,14 +1,53 @@
|
|||
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, -- this state is an error
|
||||
[{-1, 0}] = 120, -- up
|
||||
[{1, 0}] = 121, -- down
|
||||
[{0, -1}] = 122, -- left
|
||||
[{0, 1}] = 123, -- right
|
||||
[{-1, -1}] = 124, -- up-left
|
||||
[{-1, 1}] = 125, -- up-right
|
||||
[{1, -1}] = 126, -- down-left
|
||||
[{1, 1}] = 127 -- down-right
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
function handle_input()
|
||||
new_pos = camera_pos
|
||||
if (btnp(0)) new_pos[1] -= 1 -- move left
|
||||
if (btnp(1)) new_pos[1] += 1 -- move right
|
||||
if (btnp(2)) new_pos[2] -= 1 -- move up
|
||||
if (btnp(3)) new_pos[2] += 1 -- move down
|
||||
|
||||
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
|
||||
|
@ -18,9 +57,12 @@ function handle_input()
|
|||
end
|
||||
|
||||
function draw_player()
|
||||
|
||||
spr(player_lookup[facing], 64, 64)
|
||||
-- todo: animate the character on move
|
||||
end
|
||||
|
||||
-- pos is a camera position, meaning the player position is
|
||||
-- pos + {8, 8}
|
||||
function legal_move(pos)
|
||||
return true
|
||||
return fget(mget(pos[1]+8, pos[2]+8), 0)
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue