Optimization pass for number of tokens used - eliminate unnecessary tables, constants.
This commit is contained in:
parent
753700c144
commit
130c393c51
62
main.lua
62
main.lua
|
@ -1,8 +1,8 @@
|
|||
function _init()
|
||||
init_sound()
|
||||
init_mapgen()
|
||||
init_player({block_size/2, block_size/2})
|
||||
generate_map({0, 0})
|
||||
init_player(32, 32)
|
||||
generate_map(0, 0)
|
||||
debug = false
|
||||
end
|
||||
|
||||
|
@ -10,13 +10,13 @@ end
|
|||
function _update()
|
||||
handle_input()
|
||||
handle_map_update()
|
||||
if (btn(5) and btn(4) and btnp(3)) debug = not debug
|
||||
debug_input()
|
||||
end
|
||||
|
||||
function _draw()
|
||||
rectfill(0, 0, 127, 127, 0)
|
||||
-- the screen is 128x128 pixels, so it fits 16x16 sprites
|
||||
map(camera_pos[1], camera_pos[2], 0, 0, 16, 16)
|
||||
map(camera_pos_x, camera_pos_y, 0, 0, 16, 16)
|
||||
draw_player()
|
||||
if debug then
|
||||
render_debug_info()
|
||||
|
@ -26,25 +26,55 @@ end
|
|||
-- decide whether we need to regenerate the map.
|
||||
-- if so, call generate_map appropriately and reset coordinates.
|
||||
function handle_map_update()
|
||||
if out_of_bounds(camera_pos) then -- out_of_bounds() checks all screen bounds
|
||||
if out_of_bounds(camera_pos_x, camera_pos_y) then
|
||||
-- out_of_bounds() checks all screen bounds
|
||||
-- we need to regenerate the map, so we generate a map chunk that
|
||||
-- places the player in the middle of it.
|
||||
generate_map({player_pos[1]-block_size/2, player_pos[2]-block_size/2})
|
||||
camera_pos = {block_size/2 - 8, block_size/2 - 8}
|
||||
generate_map(player_pos_x-32, player_pos_y-32)
|
||||
camera_pos_x, camera_pos_y = 24
|
||||
end
|
||||
end
|
||||
|
||||
function out_of_bounds(pos)
|
||||
return pos[1] < 0 or pos[1] > block_size-16 or pos[2] < 0 or pos[2] > block_size-16
|
||||
function out_of_bounds(pos_x, pos_y)
|
||||
return pos_x < 0 or pos_x > 48 or
|
||||
pos_y < 0 or pos_y > 48
|
||||
end
|
||||
|
||||
function debug_input()
|
||||
if btn"5" and btn"4" then
|
||||
if (btnp"0") debug = "res"
|
||||
if (btnp"1") debug = "sfx"
|
||||
if (btnp"2") debug = "map"
|
||||
if (btnp"3") debug = nil
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
function render_debug_info()
|
||||
clip(0, 0, 32, 24)
|
||||
rectfill(0, 0, 32, 24, 0)
|
||||
print(stat(0), 0, 0, 15)
|
||||
print(camera_pos[1], 0, 8, 15)
|
||||
print(camera_pos[2], 16, 8, 15)
|
||||
print(player_pos[1], 0, 16, 15)
|
||||
print(player_pos[2], 16, 16, 15)
|
||||
if debug == "res" then
|
||||
clip(0, 0, 32, 24)
|
||||
rectfill(0, 0, 32, 24, 0)
|
||||
|
||||
print(stat(0), 0, 0, 15)
|
||||
print(stat(1), 0, 8, 15)
|
||||
print(stat(9), 0, 16, 15)
|
||||
print("/", 12, 16, 15)
|
||||
print(stat(8), 20, 16, 15)
|
||||
elseif debug == "sfx" then
|
||||
clip(0, 0, 16, 8)
|
||||
rectfill(0, 0, 16, 8, 0)
|
||||
|
||||
print(stat(24), 0, 0, 15)
|
||||
elseif debug == "map" then
|
||||
clip(0, 0, 32, 24)
|
||||
rectfill(0, 0, 32, 24, 0)
|
||||
|
||||
print(camera_pos_x, 0, 0, 15)
|
||||
print(camera_pos_y, 16, 0, 15)
|
||||
print(player_pos_x, 0, 8, 15)
|
||||
print(player_pos_y, 16, 8, 15)
|
||||
print(get_biome_name(player_pos_x, player_pos_y), 0, 16, 15)
|
||||
end
|
||||
|
||||
clip()
|
||||
end
|
||||
|
|
40
mapgen.lua
40
mapgen.lua
|
@ -2,8 +2,8 @@
|
|||
|
||||
function init_mapgen()
|
||||
uid_seed = 2229 -- arbitrarily chosen number
|
||||
block_size = 64
|
||||
biome_size = 128
|
||||
-- block_size = 64
|
||||
-- biome_size = 128
|
||||
|
||||
-- Metadata for different biomes
|
||||
-- frequencies don't have to add up to 100, but they should by convention
|
||||
|
@ -29,10 +29,6 @@ function init_mapgen()
|
|||
-- so the biome_metadata array needs to have its entries appear consistently.
|
||||
biome_list = {"grassland", "forest", "desert"}
|
||||
|
||||
init_biomes()
|
||||
end
|
||||
|
||||
function init_biomes()
|
||||
-- this is the frequency list for the biomes themselves
|
||||
biome_metadata = {}
|
||||
|
||||
|
@ -66,31 +62,33 @@ end
|
|||
|
||||
-- generates a unique identifier for a position
|
||||
-- uses srand() and rand() to get an unpredictable value (is this too slow?)
|
||||
function generate_uid(pos)
|
||||
srand((pos[1] + uid_seed) * (pos[2] + (uid_seed^2)))
|
||||
return flr(rnd(-1))
|
||||
-- the two seed values are randomly chosen. Change them and change the world.
|
||||
function generate_uid(pos_x, pos_y)
|
||||
srand((pos_x + 2229) * (pos_y + 12295))
|
||||
return flr(rnd(0xffff))
|
||||
end
|
||||
|
||||
-- given an {x,y} position, calculates the aligned starting position for the
|
||||
-- biome that position is in.
|
||||
function calculate_biome_pos(pos)
|
||||
return {flr(pos[1] / biome_size), flr(pos[2] / biome_size)}
|
||||
-- biomes are currently defined to be 128x128
|
||||
function calculate_biome_pos(pos_x, pos_y)
|
||||
return flr(pos_x / 128), flr(pos_y / 128)
|
||||
end
|
||||
|
||||
-- determines which biome a given world map position should be,
|
||||
-- returns the object out of the biome_data table
|
||||
function get_biome_name(pos)
|
||||
local biome_pos = calculate_biome_pos(pos)
|
||||
local uid = generate_uid(biome_pos)
|
||||
function get_biome_name(pos_x, pos_y)
|
||||
local biome_pos_x, biome_pos_y = calculate_biome_pos(pos_x, pos_y)
|
||||
local uid = generate_uid(biome_pos_x, biome_pos_y)
|
||||
return biome_metadata[(uid % #biome_metadata) + 1]
|
||||
end
|
||||
|
||||
-- determine what sprite to render for a given position.
|
||||
-- todo: this needs the ability to have a list of 'changed' tiles to check against.
|
||||
function get_tile(pos)
|
||||
local biome_name = get_biome_name(pos)
|
||||
function get_tile(pos_x, pos_y)
|
||||
local biome_name = get_biome_name(pos_x, pos_y)
|
||||
local biome = biome_data[biome_name]
|
||||
local uid = generate_uid(pos)
|
||||
local uid = generate_uid(pos_x, pos_y)
|
||||
|
||||
return biome["tile_lookup"][(uid % #biome["tile_lookup"]) + 1]
|
||||
end
|
||||
|
@ -100,10 +98,10 @@ end
|
|||
-- writes block_size x block_size tiles
|
||||
-- after a call to generate_map you should always center the camera/player over the map, i.e.
|
||||
-- camera at { (block_size / 2) - 8, (block_size / 2) - 8 }
|
||||
function generate_map(start)
|
||||
for x=0,block_size-1 do
|
||||
for y=0,block_size-1 do
|
||||
mset(x, y, get_tile({start[1]+x, start[2]+y}))
|
||||
function generate_map(start_x, start_y)
|
||||
for x=0,63 do
|
||||
for y=0,63 do
|
||||
mset(x, y, get_tile(start_x+x, start_y+y))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
104
player.lua
104
player.lua
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user