Optimization pass for number of tokens used - eliminate unnecessary tables, constants.
This commit is contained in:
parent
753700c144
commit
130c393c51
3 changed files with 117 additions and 89 deletions
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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue