Add interactability with some map objects, and store changed map tiles in a buffer for persistence.

This commit is contained in:
Anna Rose 2019-12-09 21:50:06 -05:00
parent 89a63f59b8
commit c6dd13d332
6 changed files with 120 additions and 92 deletions

View File

@ -1,52 +0,0 @@
-- unavoidable table lookups used by multiple files are stored here.
-- nothing in here should
-- be changed at runtime.
--
-- also has commented "fake constants" that are replaced by actual values, for
-- reference.
--
-- see index_map.md for more "constant" values
function init_constants()
-- block_size = 64
-- biome_size = 128
-- the indices here are sprite numbers.
object_interaction_map = {
-- bush
[3] = {
replacement = 17,
sfx = 13,
drop = 69
},
-- tree
[4] = {
replacement = 14,
sfx = 11,
drop = 65
},
-- big mushroom
[8] = {
replacement = 16,
sfx = 12,
drop = 66
},
-- cactus w/ flower
[10] = {
replacement = 15,
sfx = 12,
drop = 68
},
-- cactus
[13] = {
replacement = 15,
sfx = 12,
drop = 67
}
}
end

74
data.lua Normal file
View File

@ -0,0 +1,74 @@
-- data shared between modules goes here, both constants and mutable data.
-- also contains some functions that act solely on the data
--
-- also has commented "fake constants" that are replaced by actual values, for
-- reference.
--
-- also see index_map.md for more "constant" values
function init_data()
-- block_size = 64
-- biome_size = 128
-- the indices here are sprite numbers.
object_interaction_map = {
-- bush
[3] = {
replacement = 17,
sfx = 13,
drop = 69
},
-- tree
[4] = {
replacement = 14,
sfx = 11,
drop = 65
},
-- big mushroom
[8] = {
replacement = 16,
sfx = 12,
drop = 66
},
-- cactus w/ flower
[10] = {
replacement = 15,
sfx = 12,
drop = 68
},
-- cactus
[13] = {
replacement = 15,
sfx = 12,
drop = 67
}
}
-- initialize a ring buffer of changed positions. In use, this will be keyed
-- using strings of the form mod_buffer["x+y"], using absolute world
-- coordinates. this is to flatten the buffer so that #mod_cache is useful
-- for checking against max_mod_entries.
max_mod_entries = 4096
mod_buffer = {}
end
-- x and y are global coords
function write_map_change(new_sprite, x, y)
local global_x, global_y = calculate_world_pos(x, y)
if #mod_buffer >= max_mod_entries then
-- todo: make the buffer ring
end
mod_buffer[tostr(global_x) .. "+" .. tostr(global_y)] = new_sprite
end
function calculate_world_pos(x, y)
-- player_pos_[xy] is world absolute, so we can get the world pos of map 0,0,
-- then add x and y back in.
return player_pos_x - camera_pos_x - 8 + x,
player_pos_y - camera_pos_y - 8 + y
end

View File

@ -1,7 +1,7 @@
pico-8 cartridge // http://www.pico-8.com pico-8 cartridge // http://www.pico-8.com
version 18 version 18
__lua__ __lua__
#include constants.lua #include data.lua
#include sound.lua #include sound.lua
#include mapgen.lua #include mapgen.lua
#include player.lua #include player.lua
@ -72,7 +72,7 @@ __gfx__
00000000000000000000000000000000000000000000000000000000000880000008800000088000000880000008800000088000000880000008800000088000 00000000000000000000000000000000000000000000000000000000000880000008800000088000000880000008800000088000000880000008800000088000
00000000000000000000000000000000000000000000000000000000000880000001100000011000001100000000110000011000000110000001100000011000 00000000000000000000000000000000000000000000000000000000000880000001100000011000001100000000110000011000000110000001100000011000
__gff__ __gff__
0000000303000000020003000103000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000303000000020003000103000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__sfx__ __sfx__
012000000961009610096100961007600096000a6000b6000c6000e6000f60010600116001260013600146001560017600196001b6001d6001f6002160024600276002a6002d6003060034600396003c6003e600 012000000961009610096100961007600096000a6000b6000c6000e6000f60010600116001260013600146001560017600196001b6001d6001f6002160024600276002a6002d6003060034600396003c6003e600

View File

@ -1,5 +1,5 @@
function _init() function _init()
init_constants() init_data()
init_sound() init_sound()
init_mapgen() init_mapgen()
init_player(32, 32) init_player(32, 32)
@ -11,7 +11,6 @@ end
function _update() function _update()
handle_input() handle_input()
handle_map_update() handle_map_update()
debug_input()
end end
function _draw() function _draw()
@ -41,41 +40,24 @@ function out_of_bounds(pos_x, pos_y)
pos_y < 0 or pos_y > 48 pos_y < 0 or pos_y > 48
end end
function debug_input() function debug_res()
if btn"5" and btn"4" then print("mem: " + stat(0))
if (btnp"0") debug = "res" print("cpu: " + stat(1))
if (btnp"1") debug = "sfx" print("fps: " .. stat(9) .. " / " .. stat(8))
if (btnp"2") debug = "map"
if (btnp"3") debug = nil
end
end end
function render_debug_info() function debug_sfx()
if debug == "res" then print("Music pattern: " .. stat(24))
clip(0, 0, 32, 24) end
rectfill(0, 0, 32, 24, 0)
function debug_map()
print(stat(0), 0, 0, 15) print("Local: " .. camera_pos_x .. " " .. camera_pos_y)
print(stat(1), 0, 8, 15) print("Global: " .. player_pos_x .. " " .. player_pos_y)
print(stat(9), 0, 16, 15) print(get_biome_name(player_pos_x, player_pos_y))
print("/", 12, 16, 15) end
print(stat(8), 20, 16, 15)
elseif debug == "sfx" then function debug_mod_buffer()
clip(0, 0, 16, 8) for k,v in pairs(mod_buffer) do
rectfill(0, 0, 16, 8, 0) print(k .. ": " .. tostr(v))
end
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 end

View File

@ -87,8 +87,12 @@ function get_biome_name(pos_x, pos_y)
end end
-- determine what sprite to render for a given position. -- determine what sprite to render for a given position.
-- todo: this needs the ability to have a list of 'changed' tiles to check against. -- pos_x and pos_y are global coordinates.
function get_tile(pos_x, pos_y) function get_tile(pos_x, pos_y)
-- lookup changes in the change buffer
local modded_sprite = mod_buffer[tostr(pos_x) .. "+" .. tostr(pos_y)]
if (modded_sprite) return modded_sprite
local biome_name = get_biome_name(pos_x, pos_y) local biome_name = get_biome_name(pos_x, pos_y)
local biome = biome_data[biome_name] local biome = biome_data[biome_name]
local uid = generate_uid(pos_x, pos_y) local uid = generate_uid(pos_x, pos_y)

View File

@ -49,6 +49,10 @@ function handle_input()
local new_pos_x, new_pos_y = camera_pos_x, camera_pos_y 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 local new_ppos_x, new_ppos_y = player_pos_x, player_pos_y
if btnp"4" then
interact()
end
if btnp"0" or btnp"1" or btnp"2" or btnp"3" then if btnp"0" or btnp"1" or btnp"2" or btnp"3" then
if btnp"0" then if btnp"0" then
new_pos_x -= 1 -- move left new_pos_x -= 1 -- move left
@ -121,6 +125,22 @@ function can_interact()
return nil, nil return nil, nil
end end
function interact()
-- get the position to interact with
local x, y = can_interact()
if (x == nil or y == nil) return
local sprite = mget(x, y)
local data = object_interaction_map[sprite]
-- todo: figure out playing sound effects, animation?
-- modify the rendered tile
mset(x, y, data.replacement)
-- and write the change to the buffer
write_map_change(data.replacement, x, y)
end
-- returns x,y representing the map-local position the player is facing. -- returns x,y representing the map-local position the player is facing.
function get_position_facing() function get_position_facing()
return camera_pos_x+8+facing_h, camera_pos_y+8+facing_v return camera_pos_x+8+facing_h, camera_pos_y+8+facing_v