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
version 18
__lua__
#include constants.lua
#include data.lua
#include sound.lua
#include mapgen.lua
#include player.lua
@ -72,7 +72,7 @@ __gfx__
00000000000000000000000000000000000000000000000000000000000880000008800000088000000880000008800000088000000880000008800000088000
00000000000000000000000000000000000000000000000000000000000880000001100000011000001100000000110000011000000110000001100000011000
__gff__
0000000303000000020003000103000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000303000000020003000103000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
__sfx__
012000000961009610096100961007600096000a6000b6000c6000e6000f60010600116001260013600146001560017600196001b6001d6001f6002160024600276002a6002d6003060034600396003c6003e600

View File

@ -1,5 +1,5 @@
function _init()
init_constants()
init_data()
init_sound()
init_mapgen()
init_player(32, 32)
@ -11,7 +11,6 @@ end
function _update()
handle_input()
handle_map_update()
debug_input()
end
function _draw()
@ -41,41 +40,24 @@ function out_of_bounds(pos_x, pos_y)
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
function debug_res()
print("mem: " + stat(0))
print("cpu: " + stat(1))
print("fps: " .. stat(9) .. " / " .. stat(8))
end
function debug_sfx()
print("Music pattern: " .. stat(24))
end
function render_debug_info()
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)
function debug_map()
print("Local: " .. camera_pos_x .. " " .. camera_pos_y)
print("Global: " .. player_pos_x .. " " .. player_pos_y)
print(get_biome_name(player_pos_x, player_pos_y))
end
clip()
function debug_mod_buffer()
for k,v in pairs(mod_buffer) do
print(k .. ": " .. tostr(v))
end
end

View File

@ -87,8 +87,12 @@ function get_biome_name(pos_x, pos_y)
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.
-- pos_x and pos_y are global coordinates.
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 = biome_data[biome_name]
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_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" then
new_pos_x -= 1 -- move left
@ -121,6 +125,22 @@ function can_interact()
return nil, nil
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.
function get_position_facing()
return camera_pos_x+8+facing_h, camera_pos_y+8+facing_v