a_pleasant_stroll/player.lua

134 lines
4.0 KiB
Lua
Raw Normal View History

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:
-- 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:
-- 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
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
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] = {
[0] = 0xffff, -- error state
[0xffff] = 122, -- left
2019-12-07 20:36:32 +00:00
[1] = 123, -- right
},
[0xffff] = {
2019-12-07 20:36:32 +00:00
[0] = 120, -- up
[0xffff] = 124, -- up-left
2019-12-07 20:36:32 +00:00
[1] = 125, -- up-right
},
[1] = {
[0] = 121, -- down
[0xffff] = 126, -- down-left
2019-12-07 20:36:32 +00:00
[1] = 127, -- down-right
},
}
end
function handle_input()
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
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
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
if not (btnp"0" or btnp"1") then
facing_h = 0
2019-12-07 20:36:32 +00:00
end
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
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
if not (btnp"2" or btnp"3") then
facing_v = 0
2019-12-07 20:36:32 +00:00
end
end
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()
spr(player_lookup[facing_v][facing_h], 64, 64)
2019-12-07 20:36:32 +00:00
-- todo: animate the character on move
-- draw the player's HUD
local interact_x, interact_y = can_interact()
if interact_x and interact_y then
spr(119, 0, 120)
end
-- todo: introduce the concept of an inventory here
2019-12-07 20:36:32 +00:00
end
-- uses global variables instead of arguments
-- returns x,y for the map-local coordinates of the interactable object
-- nil,nil if nothing to interact with
function can_interact()
-- flag 1 represents an interactable (read: destructible) sprite.
-- need a map of destructible map objects, appropriate sfx, replacement
-- sprites.
local player_rel_x, player_rel_y = player_pos_x+8, player_pos_y+8
-- check the tile the player is standing on
if fget(mget(player_rel_x, player_rel_y), 1) then
return player_rel_x, player_rel_y
end
-- check the tile the player is facing
faced_tile_x, faced_tile_y = get_position_facing()
if fget(mget(faced_tile_x, faced_tile_y), 1) then
return faced_tile_x, faced_tile_y
end
return nil, nil
end
-- returns x,y representing the map-local position the player is facing.
function get_position_facing()
return camera_pos_x+8+facing_h, camera_pos_y+8+facing_v
end
2019-12-07 20:36:32 +00:00
-- pos is camera position, meaning the map-relative player
-- position is pos + {8, 8}.
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