Open-Terrarium/map.lua

110 lines
2.3 KiB
Lua
Raw Normal View History

2017-06-19 02:15:21 -04:00
--the map library
maplib = {}
2017-06-22 03:38:12 -04:00
chunkx,chunky = 0,0
2017-06-19 02:15:21 -04:00
--create tiles
mapheight = 48
2017-06-19 02:46:07 -04:00
mapwidth = 30
2017-06-19 02:15:21 -04:00
tiles = {}
2017-06-22 00:52:28 -04:00
2017-06-22 03:38:12 -04:00
--makes player move to next map section
function maplib.new_block()
if player.playerx < 1 then
chunkx = chunkx - 1
maplib.createmap() -- create a new block
player.playerx = mapwidth -- put player on other side of screen
print(" block x -1")
return false
elseif player.playerx > mapwidth then
chunkx = chunkx + 1
maplib.createmap() -- create a new block
player.playerx = 1 -- put player on other side of screen
print("block x +1")
return false
elseif player.playery < 1 then
chunky = chunky - 1
maplib.createmap() -- create a new block
player.playery = mapheight -- put player on other side of screen
print(" block y -1")
return false
elseif player.playery > mapheight then
chunky = chunky + 1
maplib.createmap() -- create a new block
player.playery = 1 -- put player on other side of screen
print("block y +1")
return false
end
return true
end
2017-06-22 00:52:28 -04:00
--generates tile blocks
2017-06-19 02:15:21 -04:00
function maplib.createmap()
2017-06-22 03:38:12 -04:00
if not love.filesystem.exists("map") then
love.filesystem.createDirectory( "map" )
end
local block_exists = love.filesystem.exists("/map/"..chunkx.."_"..chunky..".txt")
2017-06-22 03:38:12 -04:00
if not block_exists then
for x = 1,mapwidth do
tiles[x] = {}
for y = 1,mapheight do
local value = love.math.noise( x/mapwidth, y/mapheight )
tiles[x][y] = {}
if value > 0.5 then
tiles[x][y]["block"] = 1--love.math.random(0,1)
else
tiles[x][y]["block"] = 0
end
end
end
2017-06-22 03:38:12 -04:00
2017-06-22 00:52:28 -04:00
2017-06-22 03:38:12 -04:00
love.filesystem.write( "/map/"..chunkx.."_"..chunky..".txt", TSerial.pack(tiles))
else
2017-06-22 03:38:12 -04:00
tiles = TSerial.unpack(love.filesystem.read("/map/"..chunkx.."_"..chunky..".txt"))
end
2017-06-19 02:15:21 -04:00
end
--executed in love.draw to draw map
function maplib.draw()
2017-06-19 02:46:07 -04:00
love.graphics.setFont(font)
2017-06-19 02:15:21 -04:00
love.graphics.setColor(255,255,255,255)
for x = 1,mapwidth do
for y = 1,mapheight do
2017-06-20 01:44:23 -04:00
local graphic
if tiles[x][y]["block"] == 0 then
2017-06-20 04:16:34 -04:00
graphic = "" --air
2017-06-20 01:44:23 -04:00
elseif tiles[x][y]["block"] == 1 then
2017-06-20 04:16:34 -04:00
graphic = "#" --stone
elseif tiles[x][y]["block"] == 2 then
graphic = "/" --stairs
2017-06-20 01:44:23 -04:00
end
love.graphics.print(graphic, x*scale, y*scale)
2017-06-19 02:15:21 -04:00
end
end
end
--[[ a test
for x = 1,10 do
local line = ""
for y = 1,10 do
line = line..tiles.x.y
end
print(line.."\n")
end
]]--