Load multiple chunks and render at once

This commit is contained in:
jordan4ibanez 2017-07-05 04:28:00 -04:00
parent 1dc17e7dfc
commit 68524c482d
4 changed files with 128 additions and 93 deletions

View File

@ -4,9 +4,9 @@ function collision(oldposx,oldposy)
--stairs
if (player.playerx <= map_max and player.playerx > 1) and
(player.playery < map_max and player.playery > 1) and
tiles[player.playerx][player.playery]["block"] == 3 then
loaded_chunks[0][0][player.playerx][player.playery]["block"] == 3 then
player.playery = player.playery - 1
elseif (player.playerx > map_max or player.playerx <= 0) or (player.playery > map_max or player.playery <= 0) or tiles[player.playerx][player.playery]["block"] ~= 1 then
elseif (player.playerx > map_max or player.playerx <= 0) or (player.playery > map_max or player.playery <= 0) or loaded_chunks[0][0][player.playerx][player.playery]["block"] ~= 1 then
player.playerx,player.playery = oldposx,oldposy
--can't move
oof:setPitch(love.math.random(65,100)/100)
@ -28,7 +28,7 @@ function gravity(dt)
return
end
if tiles[player.playerx][player.playery+1]["block"] == 1 then
if loaded_chunks[0][0][player.playerx][player.playery+1]["block"] == 1 then
gravtimer = gravtimer + dt
if gravtimer >= 0.2 then
local oldposx,oldposy = player.playerx,player.playery

85
map.lua
View File

@ -4,8 +4,8 @@ maplib = {}
chunkx,chunky = math.random(-1000,1000),math.random(-1000,1000)
--create tiles
map_max = 25
--tile size
map_max = 35
--ore generation
ore_min = 0 -- the minimum amount of ore that'll be generated in a map block
@ -103,22 +103,44 @@ function maplib.generate_cave(tiles)
end
end
end
end
end
--[[
when walking around seemlessly generate chunks
unload chunks that are not visible <-
or
keep all chunks loaded, just do not render them off screen
create a x * x table, set max loaded chunks (x)
render, and modify everything to work correctly with all loaded chunks
]]--
max_chunks = 1 --x * x chunks loaded --does -1 - 1 (-x to x) -- (x * 2) + 1 to get max chunks in memory
--generates tile blocks
function maplib.createmap()
loaded_chunks = {} --chunks in mememory
for xx = -max_chunks,max_chunks do
loaded_chunks[xx] = {}
for yy = -max_chunks,max_chunks do
if not love.filesystem.exists("map") then
love.filesystem.createDirectory( "map" )
end
local block_exists = love.filesystem.exists("/map/"..chunkx.."_"..chunky..".txt")
local block_exists = love.filesystem.exists("/map/"..chunkx+xx.."_"..chunky+yy..".txt")
local number = 0
@ -126,9 +148,9 @@ function maplib.createmap()
tiles = {}
--generate map block
if not block_exists then
print(chunky)
--print(chunky)
--generate underground
if chunky <= underground then
if chunky+yy <= underground then
for x = 1,map_max do
tiles[x] = {}
for y = 1,map_max do
@ -141,17 +163,17 @@ function maplib.createmap()
maplib.generate_cave(tiles)
--generate the grass top
elseif chunky == earth_max then
local yy = math.random(1,map_max)
elseif chunky+yy == earth_max then
local yer = math.random(1,map_max)
for x = 1,map_max do
tiles[x] = {}
yy = yy + math.random(-1,1)
yer = yer + math.random(-1,1)
for y = 1,map_max do
tiles[x][y] = {}
--generate dirt as debug
if y == yy then
if y == yer then
tiles[x][y]["block"] = 4
elseif y < yy then
elseif y < yer then
tiles[x][y]["block"] = 1
else
tiles[x][y]["block"] = 3
@ -159,8 +181,8 @@ function maplib.createmap()
end
end
--generate dirt under grass
elseif chunky < earth_max and chunky > underground then
print("test")
elseif chunky+yy < earth_max and chunky+yy > underground then
--print("test")
for x = 1, map_max do
tiles[x] = {}
for y = 1,map_max do
@ -170,7 +192,7 @@ function maplib.createmap()
end
end
else
print("generate air")
--print("generate air")
for x = 1,map_max do
tiles[x] = {}
for y = 1,map_max do
@ -182,12 +204,21 @@ function maplib.createmap()
end
--save
love.filesystem.write( "/map/"..chunkx.."_"..chunky..".txt", TSerial.pack(tiles))
love.filesystem.write( "/map/"..chunkx+xx.."_"..chunky+yy..".txt", TSerial.pack(tiles))
loaded_chunks[xx][yy] = tiles
else
tiles = TSerial.unpack(love.filesystem.read("/map/"..chunkx.."_"..chunky..".txt"))
tiles = TSerial.unpack(love.filesystem.read("/map/"..chunkx+xx.."_"..chunky+yy..".txt"))
loaded_chunks[xx][yy] = tiles
end
end
end
--for xx = -max_chunks,max_chunks do
-- for yy = -max_chunks,max_chunks do
-- print(loaded_chunks[xx][yy])
-- end
--end
end
--executed in love.draw to draw map
@ -207,17 +238,21 @@ end
--end
function maplib.draw()
love.graphics.setFont(font)
for xx = -max_chunks,max_chunks do
for yy = -max_chunks,max_chunks do
for x = 1,map_max do
for y = 1,map_max do
love.graphics.setColor(ore[tiles[x][y]["block"]]["rgb"][1],ore[tiles[x][y]["block"]]["rgb"][2],ore[tiles[x][y]["block"]]["rgb"][3],255)
love.graphics.print(ore[tiles[x][y]["block"]]["image"], ((x*scale)-(player.playerx*scale))+((scale*map_max)/2), ((y*scale)-(player.playery*scale))+((scale*map_max)/2))
if x == math.floor(map_max / 2) and y == math.floor(map_max / 2) then
love.graphics.print("X", x*scale, y*scale)
end
end
end
love.graphics.setColor(ore[loaded_chunks[xx][-yy][x][y]["block"]]["rgb"][1],ore[loaded_chunks[xx][-yy][x][y]["block"]]["rgb"][2],ore[loaded_chunks[xx][-yy][x][y]["block"]]["rgb"][3],255)
love.graphics.print(ore[loaded_chunks[xx][-yy][x][y]["block"]]["image"], (((x*scale)-(player.playerx*scale))+((scale*map_max)/2))+(map_max*scale*xx), (((y*scale)-(player.playery*scale))+((scale*map_max)/2))+(map_max*scale*yy))
--if x == math.floor(map_max / 2) and y == math.floor(map_max / 2) then
-- love.graphics.print("X", x*scale, y*scale)
--end
end
end
end
end
end
--[[ a test

View File

@ -101,7 +101,7 @@ function menu.cursor()
local x, y = love.mouse.getPosition( )
love.graphics.circle( "fill", player_drawnx, player_drawny, 4 )
--love.graphics.circle( "fill", player_drawnx, player_drawny, 4 )
--local xx,yy = math.floor(x/scale),math.floor((y+3)/scale)
local xx,yy = math.floor((x-player_drawnx)/scale)*scale,math.floor((y-player_drawny)/scale)*scale

View File

@ -33,7 +33,7 @@ function love.keypressed( key, scancode, isrepeat )
--debug
if key == "f5" then
chunkx,chunky = math.random(-1000,1000),math.random(-1,2)
chunkx,chunky = math.random(-1000,1000),math.random(0,3)
maplib.createmap()
--print("generate random block")
@ -49,7 +49,7 @@ function love.keypressed( key, scancode, isrepeat )
end
print("generating new map")
chunkx,chunky = math.random(-1000,1000),math.random(-1,2)
chunkx,chunky = math.random(-1000,1000),math.random(0,3)
maplib.createmap()
end
@ -68,9 +68,9 @@ function love.keypressed( key, scancode, isrepeat )
end
if key == "1" then
player.selected = 1
elseif key == "2" then
player.selected = 2
elseif key == "2" then
player.selected = 3
end
@ -93,24 +93,24 @@ function mine(key)
if mx ~= -1 and my ~= -1 then
--play sound and remove tile
if left then
if tiles[mx][my]["block"] ~= 1 then
if loaded_chunks[0][0][mx][my]["block"] ~= 1 then
minesound:setPitch(love.math.random(50,100)/100)
minesound:stop()
minesound:play()
tiles[mx][my]["block"] = 1
loaded_chunks[0][0][mx][my]["block"] = 1
player.mining = true
love.filesystem.write( "/map/"..chunkx.."_"..chunky..".txt", TSerial.pack(tiles))
love.filesystem.write( "/map/"..chunkx.."_"..chunky..".txt", TSerial.pack(loaded_chunks[0][0]))
score = score + 1
end
elseif right then
if tiles[mx][my]["block"] == 1 and (mx ~= player.playerx or my ~= player.playery) then
if loaded_chunks[0][0][mx][my]["block"] == 1 and (mx ~= player.playerx or my ~= player.playery) then
placesound:setPitch(love.math.random(50,100)/100)
placesound:stop()
placesound:play()
tiles[mx][my]["block"] = player.selected
loaded_chunks[0][0][mx][my]["block"] = player.selected
player.mining = false
love.filesystem.write( "/map/"..chunkx.."_"..chunky..".txt", TSerial.pack(tiles))
love.filesystem.write( "/map/"..chunkx.."_"..chunky..".txt", TSerial.pack(loaded_chunks[0][0]))
score = score + 1
end
end