Optimization pass for number of tokens used - eliminate unnecessary tables, constants.

This commit is contained in:
2019-12-09 16:35:45 -05:00
parent 753700c144
commit 130c393c51
3 changed files with 117 additions and 89 deletions

View File

@ -2,43 +2,43 @@
-- Larger sprites will probably work for objects, but not for the player.
--
-- exported variables:
-- player_pos # integer tuple {x,y}. absolute position of the player on the
-- # 'world map'
-- camera_pos # integer tuple {x,y}. Represents top-left position of the
-- # pico-8 camera on the pico-8 map
-- facing # integer tuple {v,h}. Current facing of the player. Values of
-- # each element can be -1 (up or left), 0 (neutral),
-- # or 1 (down or right)
-- 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)
--
-- 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.
-- 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.
function init_player(start_pos)
player_pos = start_pos
camera_pos = {start_pos[1]-8, start_pos[2]-8}
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
facing = {1, 0}
facing_v = 1
facing_h = 0
-- this is a constant for looking up player sprites by facing
player_lookup = {
[0] = {
[0] = -1, -- error state
[-1] = 122, -- left
[0] = 0xffff, -- error state
[0xffff] = 122, -- left
[1] = 123, -- right
},
[-1] = {
[0xffff] = {
[0] = 120, -- up
[-1] = 124, -- up-left
[0xffff] = 124, -- up-left
[1] = 125, -- up-right
},
[1] = {
[0] = 121, -- down
[-1] = 126, -- down-left
[0xffff] = 126, -- down-left
[1] = 127, -- down-right
},
}
@ -46,51 +46,51 @@ end
function handle_input()
local new_pos = {camera_pos[1], camera_pos[2]}
local new_ppos = {player_pos[1], player_pos[2]}
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
if btnp(0) or btnp(1) or btnp(2) or btnp(3) then
if btnp(0) then
new_pos[1] -= 1 -- move left
new_ppos[1] -= 1
facing[2] = -1
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
end
if btnp(1) then
new_pos[1] += 1 -- move right
new_ppos[1] += 1 -- move right
facing[2] = 1
if btnp"1" then
new_pos_x += 1 -- move right
new_ppos_x += 1 -- move right
facing_h = 1
end
if not (btnp(0) or btnp(1)) then
facing[2] = 0
if not (btnp"0" or btnp"1") then
facing_h = 0
end
if btnp(2) then
new_pos[2] -= 1 -- move up
new_ppos[2] -= 1 -- move up
facing[1] = -1
if btnp"2" then
new_pos_y -= 1 -- move up
new_ppos_y -= 1 -- move up
facing_v = 0xffff
end
if btnp(3) then
new_pos[2] += 1 -- move down
new_ppos[2] += 1 -- move down
facing[1] = 1
if btnp"3" then
new_pos_y += 1 -- move down
new_ppos_y += 1 -- move down
facing_v = 1
end
if not (btnp(2) or btnp(3)) then
facing[1] = 0
if not (btnp"2" or btnp"3") then
facing_v = 0
end
end
if _legal_move(new_pos) then
camera_pos = new_pos
player_pos = new_ppos
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
end
end
function draw_player()
spr(player_lookup[facing[1]][facing[2]], 64, 64)
spr(player_lookup[facing_v][facing_h], 64, 64)
-- todo: animate the character on move
-- draw the player's HUD
if fget(mget(player_pos[1], player_pos[2]), 1) then
if fget(mget(player_pos_x, player_pos_y), 1) then
-- flag 1 represents an interactable (read: destructible) sprite.
-- need a map of destructible map objects, appropriate sfx, replacement
-- sprites.
@ -101,6 +101,6 @@ end
-- pos is camera position, meaning the map-relative player
-- position is pos + {8, 8}.
function _legal_move(pos)
return not fget(mget(pos[1]+8, pos[2]+8), 0)
function legal_move(pos_x, pos_y)
return not fget(mget(pos_x+8, pos_y+8), 0)
end