2019-12-07 21:30:41 +00:00
|
|
|
-- This module assumes all objects and the player are 1x1 sprite in size.
|
|
|
|
-- Larger sprites will probably work for objects, but not for the player.
|
2019-12-07 20:36:32 +00:00
|
|
|
--
|
|
|
|
-- exported variables:
|
2019-12-09 21:35:45 +00:00
|
|
|
-- player_pos_[xy] # absolute position of the player on the 'world map'
|
|
|
|
-- camera_pos_[xy] # Represents top-left position of the pico-8 camera on
|
|
|
|
-- # the pico-8 map
|
|
|
|
-- facing_[vh] # Current facing of the player. Values of
|
|
|
|
-- # each element can be -1 (up or left), 0 (neutral),
|
|
|
|
-- # or 1 (down or right)
|
2019-12-07 20:36:32 +00:00
|
|
|
--
|
|
|
|
-- functions:
|
2019-12-09 21:35:45 +00:00
|
|
|
-- init_player(x,y) # call this in _init(). Sets player starting position,
|
|
|
|
-- # assumes the top-left of the pico-8 map is currently
|
|
|
|
-- # world position 0,0
|
|
|
|
-- handle_input() # call in _update() to handle movement key presses.
|
|
|
|
-- # Assumes any sprite with flag 0 set is an obstacle.
|
|
|
|
-- draw_player() # call in _draw(). Draws the player sprite.
|
2019-12-07 20:36:32 +00:00
|
|
|
|
2019-12-09 21:35:45 +00:00
|
|
|
function init_player(start_pos_x, start_pos_y)
|
|
|
|
player_pos_x, player_pos_y = start_pos_x, start_pos_y
|
|
|
|
camera_pos_x, camera_pos_y = start_pos_x-8, start_pos_y-8
|
2019-12-07 20:36:32 +00:00
|
|
|
|
2019-12-09 21:35:45 +00:00
|
|
|
facing_v = 1
|
|
|
|
facing_h = 0
|
2019-12-07 20:36:32 +00:00
|
|
|
|
|
|
|
-- this is a constant for looking up player sprites by facing
|
|
|
|
player_lookup = {
|
|
|
|
[0] = {
|
2019-12-09 21:35:45 +00:00
|
|
|
[0] = 0xffff, -- error state
|
|
|
|
[0xffff] = 122, -- left
|
2019-12-07 20:36:32 +00:00
|
|
|
[1] = 123, -- right
|
|
|
|
},
|
2019-12-09 21:35:45 +00:00
|
|
|
[0xffff] = {
|
2019-12-07 20:36:32 +00:00
|
|
|
[0] = 120, -- up
|
2019-12-09 21:35:45 +00:00
|
|
|
[0xffff] = 124, -- up-left
|
2019-12-07 20:36:32 +00:00
|
|
|
[1] = 125, -- up-right
|
|
|
|
},
|
|
|
|
[1] = {
|
|
|
|
[0] = 121, -- down
|
2019-12-09 21:35:45 +00:00
|
|
|
[0xffff] = 126, -- down-left
|
2019-12-07 20:36:32 +00:00
|
|
|
[1] = 127, -- down-right
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function handle_input()
|
2019-12-09 21:35:45 +00:00
|
|
|
local new_pos_x, new_pos_y = camera_pos_x, camera_pos_y
|
|
|
|
local new_ppos_x, new_ppos_y = player_pos_x, player_pos_y
|
2019-12-07 20:36:32 +00:00
|
|
|
|
2019-12-09 21:35:45 +00:00
|
|
|
if btnp"0" or btnp"1" or btnp"2" or btnp"3" then
|
|
|
|
if btnp"0" then
|
|
|
|
new_pos_x -= 1 -- move left
|
|
|
|
new_ppos_x -= 1
|
|
|
|
facing_h = 0xffff
|
2019-12-07 20:36:32 +00:00
|
|
|
end
|
2019-12-09 21:35:45 +00:00
|
|
|
if btnp"1" then
|
|
|
|
new_pos_x += 1 -- move right
|
|
|
|
new_ppos_x += 1 -- move right
|
|
|
|
facing_h = 1
|
2019-12-07 20:36:32 +00:00
|
|
|
end
|
2019-12-09 21:35:45 +00:00
|
|
|
if not (btnp"0" or btnp"1") then
|
|
|
|
facing_h = 0
|
2019-12-07 20:36:32 +00:00
|
|
|
end
|
|
|
|
|
2019-12-09 21:35:45 +00:00
|
|
|
if btnp"2" then
|
|
|
|
new_pos_y -= 1 -- move up
|
|
|
|
new_ppos_y -= 1 -- move up
|
|
|
|
facing_v = 0xffff
|
2019-12-07 20:36:32 +00:00
|
|
|
end
|
2019-12-09 21:35:45 +00:00
|
|
|
if btnp"3" then
|
|
|
|
new_pos_y += 1 -- move down
|
|
|
|
new_ppos_y += 1 -- move down
|
|
|
|
facing_v = 1
|
2019-12-07 20:36:32 +00:00
|
|
|
end
|
2019-12-09 21:35:45 +00:00
|
|
|
if not (btnp"2" or btnp"3") then
|
|
|
|
facing_v = 0
|
2019-12-07 20:36:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-09 21:35:45 +00:00
|
|
|
if legal_move(new_pos_x, new_pos_y) then
|
|
|
|
camera_pos_x, camera_pos_y = new_pos_x, new_pos_y
|
|
|
|
player_pos_x, player_pos_y = new_ppos_x, new_ppos_y
|
2019-12-07 20:36:32 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function draw_player()
|
2019-12-09 21:35:45 +00:00
|
|
|
spr(player_lookup[facing_v][facing_h], 64, 64)
|
2019-12-07 20:36:32 +00:00
|
|
|
-- todo: animate the character on move
|
2019-12-09 21:12:48 +00:00
|
|
|
|
|
|
|
-- draw the player's HUD
|
2019-12-09 21:35:45 +00:00
|
|
|
if fget(mget(player_pos_x, player_pos_y), 1) then
|
2019-12-09 21:12:48 +00:00
|
|
|
-- flag 1 represents an interactable (read: destructible) sprite.
|
|
|
|
-- need a map of destructible map objects, appropriate sfx, replacement
|
|
|
|
-- sprites.
|
|
|
|
end
|
|
|
|
|
|
|
|
-- todo: introduce the concept of an inventory here
|
2019-12-07 20:36:32 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- pos is camera position, meaning the map-relative player
|
|
|
|
-- position is pos + {8, 8}.
|
2019-12-09 21:35:45 +00:00
|
|
|
function legal_move(pos_x, pos_y)
|
|
|
|
return not fget(mget(pos_x+8, pos_y+8), 0)
|
2019-12-07 20:36:32 +00:00
|
|
|
end
|