Implement multiple biomes.

This commit is contained in:
Anna Rose 2019-12-07 16:30:41 -05:00
parent a8b26c113d
commit 0b10b2df67
3 changed files with 74 additions and 42 deletions

View File

@ -5,14 +5,14 @@ __lua__
#include player.lua #include player.lua
#include main.lua #include main.lua
__gfx__ __gfx__
000000000000000033333333335555333344443338333333333333e3383333830000000000000000000000000000000000000000000000000000000000000000 000000000000000033333333335555333344443338333333333333e3383333833333333399999999999899999994999499999999999999990000000000000000
00000000000000003333333335554553344444438a833383333e3e9e888338880000000000000000000000000000000000000000000000000000000000000000 00000000000000003333333335554553344444438a833383333e3e9e888338883333333399999999998a89999499949999999999999999990000000000000000
0070070000000000333333335555555534444443383338a833e9e3e3373333730000000000000000000000000000000000000000000000000000000000000000 0070070000000000333333335555555534444443383338a833e9e3e33733337333333333999999999998b9b99949994999944999999bb9b90000000000000000
000770000000000033333333554554553344443333333383333e3333333333330000000000000000000000000000000000000000000000000000000000000000 000770000000000033333333554554553344443333333383333e33333333333333378333999999999b9bb9b999994999994444999b9bb9b90000000000000000
00077000000000003333333355555555333553333333833333333333333338330000000000000000000000000000000000000000000000000000000000000000 000770000000000033333333555555553335533333338333333333333333383333888733999999999b9bbbb999499994944444499b9bbbb90000000000000000
00700700000000003333333355545555333553333338a8333e333e33333388830000000000000000000000000000000000000000000000000000000000000000 00700700000000003333333355545555333553333338a8333e333e333333888338788883999999999bbbb99999994999944444499bbbb9990000000000000000
000000000000000033333333355555533335533333338333e9e3e9e3333337330000000000000000000000000000000000000000000000000000000000000000 000000000000000033333333355555533335533333338333e9e3e9e3333337333337733399999999999bb9999499994999444499999bb9990000000000000000
0000000000000000333333333355553333555533333333333e333e33333333330000000000000000000000000000000000000000000000000000000000000000 0000000000000000333333333355553333555533333333333e333e33333333333337733399999999999bb9999999999999944999999bb9990000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
@ -70,5 +70,5 @@ __gfx__
00000000000000000000000000000000000000000000000000000000000000000000000000700700007000000000070000000000000000007000000000000007 00000000000000000000000000000000000000000000000000000000000000000000000000700700007000000000070000000000000000007000000000000007
00000000000000000000000000000000000000000000000000000000000000000000000000077000000700000000700000000000000000000777700000077770 00000000000000000000000000000000000000000000000000000000000000000000000000077000000700000000700000000000000000000777700000077770
__gff__ __gff__
0000000303000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000303000000020003000103000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

View File

@ -3,8 +3,9 @@
function init_mapgen() function init_mapgen()
uid_seed = 2229 -- arbitrarily chosen number uid_seed = 2229 -- arbitrarily chosen number
block_size = 64 block_size = 64
biome_size = 128
-- Sprite ratios for different biomes, of the form { "biome_name": {frequency, sprite index} ... } } -- Metadata for different biomes
-- frequencies don't have to add up to 100, but they should by convention -- frequencies don't have to add up to 100, but they should by convention
-- --
-- Sprites are: -- Sprites are:
@ -14,31 +15,58 @@ function init_mapgen()
-- * 5 - red flowers -- * 5 - red flowers
-- * 6 - pink flowers -- * 6 - pink flowers
-- * 7 - mushrooms -- * 7 - mushrooms
biomes = { grassland = { {40, 2}, {28, 5}, {28, 6}, {3, 3}, {1, 4} }, -- * 8 - big mushroom
forest = { {55, 2}, {25, 4}, {5, 3}, {5, 5}, {5, 7}, {5, 6} }, -- * 9 - sand
} -- * 10 - cactus with flower
-- * 11 - pebbles
-- * 12 - rock
-- * 13 - cactus
biome_data = {
grassland = {
biome_frequency = 75,
tile_frequencies = { {40, 2}, {28, 5}, {28, 6}, {3, 3}, {1, 4} }
},
forest = {
biome_frequency = 20,
tile_frequencies = { {60, 2}, {20, 4}, {5, 3}, {5, 5}, {4, 7}, {5, 6}, {1, 8} }
},
desert = {
biome_frequency = 5,
tile_frequencies = { {80, 9}, {10, 11}, {6, 13}, {3, 12}, {1, 10} },
}
}
build_biome("forest") init_biomes()
end end
-- use biome1 if mix == false function init_biomes()
function build_biome(biome1, biome2, mix) -- this is the frequency list for the biomes themselves
if mix == true then biome_metadata = {}
else
frequencies = biomes[biome1]
end
-- global variable used for lookup during mapgen for k,v in pairs(biome_data) do
-- build_biome() writes the biome_lookup table such -- add the biome's name N times to the biome metadata 'hat'
-- that biome_lookup[uid] returns a sprite index, for i=1,v["biome_frequency"] do
-- weighted by the frequency information for current biome. add(biome_metadata, k)
biome_lookup = {} end
for i=1,#frequencies do
local tuple=frequencies[i] build_biome(k, v)
end
end
-- build the lookup table for a given biome, based on the biome_meta data for
-- that string.
function build_biome(biome_name, data)
local meta_frequencies = data["tile_frequencies"]
local tile_lookup = {}
for i=1,#meta_frequencies do
local tuple = meta_frequencies[i]
for j=1,tuple[1] do for j=1,tuple[1] do
add(biome_lookup, tuple[2]) add(tile_lookup, tuple[2])
end end
end end
data["tile_lookup"] = tile_lookup
end end
-- generates a unique identifier for a position -- generates a unique identifier for a position
@ -48,9 +76,22 @@ function generate_uid(pos)
return flr(rnd(-1)) return flr(rnd(-1))
end end
-- determine what sprite to render for a given position -- determines which biome a given world map position should be,
-- returns the object out of the biome_data table
function get_biome(pos)
local biome_pos = {flr(pos[1] / biome_size), flr(pos[2] / biome_size)}
local uid = generate_uid(biome_pos)
local biome_name = biome_metadata[(uid % #biome_metadata) + 1]
return biome_data[biome_name]
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.
function get_tile(pos) function get_tile(pos)
return biome_lookup[(generate_uid(pos) % #biome_lookup) + 1] local biome = get_biome(pos)
local uid = generate_uid(pos)
return biome["tile_lookup"][(uid % #biome["tile_lookup"]) + 1]
end end
-- generate the map and writes to the map area from 0 - block_size, -- generate the map and writes to the map area from 0 - block_size,

View File

@ -1,5 +1,5 @@
-- this module assumes all objects and the player are -- This module assumes all objects and the player are 1x1 sprite in size.
-- 1x1 sprite in size. larger sprites are not currently supported -- Larger sprites will probably work for objects, but not for the player.
-- --
-- exported variables: -- exported variables:
-- player_pos # integer tuple {x,y}. absolute position of the player on the -- player_pos # integer tuple {x,y}. absolute position of the player on the
@ -10,10 +10,6 @@
-- # each element can be -1 (up or left), 0 (neutral), -- # each element can be -1 (up or left), 0 (neutral),
-- # or 1 (down or right) -- # or 1 (down or right)
-- --
-- exported constants:
-- player_lookup # used internally to look up the sprite to render for a
-- # given facing
--
-- functions: -- functions:
-- init_player({x,y}) # call this in _init(). Sets player starting position, -- init_player({x,y}) # call this in _init(). Sets player starting position,
-- # assumes the top-left of the pico-8 map is currently -- # assumes the top-left of the pico-8 map is currently
@ -23,14 +19,9 @@
-- draw_player() # call in _draw(). Draws the player sprite. -- draw_player() # call in _draw(). Draws the player sprite.
function init_player(start_pos) function init_player(start_pos)
-- camera_pos is the relative coordinate of the camera
-- on the currently generated map chunk. player_pos is
-- the *absolute* world coordinates of the player.
player_pos = start_pos player_pos = start_pos
camera_pos = {start_pos[1]-8, start_pos[2]-8} camera_pos = {start_pos[1]-8, start_pos[2]-8}
-- [1] is vertical facing, [2] is horizontal, -1 is up/left, 0 is neutral,
-- and 1 is down/right
facing = {1, 0} facing = {1, 0}
-- this is a constant for looking up player sprites by facing -- this is a constant for looking up player sprites by facing