a_pleasant_stroll/main.lua

51 lines
1.3 KiB
Lua
Raw Normal View History

2019-12-04 23:48:04 +00:00
function _init()
2019-12-09 01:43:32 +00:00
init_sound()
2019-12-04 23:48:04 +00:00
init_mapgen()
2019-12-07 20:36:32 +00:00
init_player({block_size/2, block_size/2})
2019-12-04 23:48:04 +00:00
generate_map({0, 0})
debug = false
2019-12-04 23:48:04 +00:00
end
function _update()
handle_input()
2019-12-07 20:36:32 +00:00
handle_map_update()
2019-12-04 23:48:04 +00:00
if (btn(5) and btn(4) and btnp(3)) debug = not debug
end
function _draw()
rectfill(0, 0, 127, 127, 0)
-- the screen is 128x128 pixels, so it fits 16x16 sprites
2019-12-04 23:48:04 +00:00
map(camera_pos[1], camera_pos[2], 0, 0, 16, 16)
draw_player()
if debug then
2019-12-06 03:03:05 +00:00
render_debug_info()
2019-12-04 23:48:04 +00:00
end
end
2019-12-06 03:03:05 +00:00
2019-12-07 20:36:32 +00:00
-- 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
2019-12-06 03:03:05 +00:00
function render_debug_info()
2019-12-07 20:36:32 +00:00
clip(0, 0, 32, 24)
rectfill(0, 0, 32, 24, 0)
2019-12-06 03:03:05 +00:00
print(stat(0), 0, 0, 15)
print(camera_pos[1], 0, 8, 15)
print(camera_pos[2], 16, 8, 15)
2019-12-07 20:36:32 +00:00
print(player_pos[1], 0, 16, 15)
print(player_pos[2], 16, 16, 15)
2019-12-06 03:03:05 +00:00
clip()
end