Skip to content

Instantly share code, notes, and snippets.

@Achie72
Last active December 29, 2023 11:29
Show Gist options
  • Save Achie72/b9d57406ecbf9fa1555e14470002a82e to your computer and use it in GitHub Desktop.
Save Achie72/b9d57406ecbf9fa1555e14470002a82e to your computer and use it in GitHub Desktop.
Crumbling Crypt - PICO-8 - Build map from sprites
-- Made for Crumbling Crypt: https://ko-fi.com/Post/Crumbling-Crypt-1--A-minimalist-Roguelike-B0B1SPL7E
-- Original version for my game called Kumatori Panic!: https://ko-fi.com/post/Kumatori-Panic-4--Eggs-Better-Movement-Lore-Ac-N4N0N6K7W
function build_room(corner_sprite_id, corner, _x, _y)
--printh("building room at: ".._x.." ".._y)
local idString = ""
-- indicate where the given sprite starts on
-- the sprite sheet.
local xStart = 0
local yStart = 0
xStart = corner_sprite_id*8
yStart = 32
--printh("corner "..corner.." sprite "..spr)
-- gather pixel color values
--printh("gathering colors")
-- we start with y so we gather the x coords first
-- so our mset builder down below builds correctly
for y=0,7 do
for x=0,7 do
-- gather the colors from the sprite.
-- it starts from the sprite number*8, and all of them are at y=32 on the sprite sheet
-- but this could be configurated later for more
idString = idString..sget(xStart+x, yStart+y)..","
end
end
-- where to start the build based on which corner we
-- are doing at the moment. Top left corner is 0,0, top right is 8,8,
-- bottom right is 8,8, bottom left is 0,8
local mapCornerTable = {{0,0}, {8,0}, {8,8}, {0,8}}
local mapBuildStartX = mapCornerTable[corner+1][1]+_x
local mapBuildStartY = mapCornerTable[corner+1][2]+_y
local x,y = 0,0
-- now we just loop over the colors and place the
-- tiles corresponding to the color.
for val in all(split(idString)) do
--printh("setting: "..x..","..y.."to: "..val)
if val == 1 then
--dark blue - wall
mset(x+mapBuildStartX,y+mapBuildStartY, 1)
--printh("set at: "..x+mapBuildStartX.." "..y+mapBuildStartY)
elseif val == 2 then
-- dark red, enemy
if rnd() > 0.3-(player.level/10) then
--mset(x+mapBuildStartX,y+mapBuildStartY, 5)
add_enemy(x+mapBuildStartX,y+mapBuildStartY)
end
elseif val == 3 then
-- dark green, pickup
if rnd() > 0.5 then
add_pickup(x+mapBuildStartX,y+mapBuildStartY, rnd({1,2}))
end
elseif val == 0 then
-- this is needed for an ugly little trick I'll tell
-- you later, you don't really need it as of now
mset(x+mapBuildStartX,y+mapBuildStartY, 0)
end
x+=1
-- this is to convert the 1D array
-- into 2D world coordinates. At every 8th tile we go
-- to the next row.
if x > 7 then
x = 0
y += 1
end
end
end
@Achie72
Copy link
Author

Achie72 commented Dec 29, 2023

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment