Implement infinite scrolling.
This commit is contained in:
parent
7393e5fbe9
commit
a8b26c113d
4 changed files with 128 additions and 81 deletions
27
main.lua
27
main.lua
|
@ -1,6 +1,6 @@
|
|||
function _init()
|
||||
init_mapgen()
|
||||
init_movement({block_size/2, block_size/2})
|
||||
init_player({block_size/2, block_size/2})
|
||||
generate_map({0, 0})
|
||||
debug = false
|
||||
end
|
||||
|
@ -8,6 +8,7 @@ end
|
|||
|
||||
function _update()
|
||||
handle_input()
|
||||
handle_map_update()
|
||||
if (btn(5) and btn(4) and btnp(3)) debug = not debug
|
||||
end
|
||||
|
||||
|
@ -21,14 +22,28 @@ function _draw()
|
|||
end
|
||||
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
|
||||
-- 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}
|
||||
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
|
||||
end
|
||||
|
||||
function render_debug_info()
|
||||
clip(0, 0, 32, 32)
|
||||
rectfill(0, 0, 32, 32, 0)
|
||||
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(facing[1], 0, 16, 15)
|
||||
print(facing[2], 16, 16, 15)
|
||||
print(fget(mget(camera_pos[1]+8, camera_pos[2]+8)), 0, 24, 15)
|
||||
print(player_pos[1], 0, 16, 15)
|
||||
print(player_pos[2], 16, 16, 15)
|
||||
clip()
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue