From 68524c482db88612273c99c39be070f642fd7c9f Mon Sep 17 00:00:00 2001 From: jordan4ibanez Date: Wed, 5 Jul 2017 04:28:00 -0400 Subject: [PATCH] Load multiple chunks and render at once --- collision.lua | 6 +- map.lua | 193 +++++++++++++++++++++++++++++--------------------- menu.lua | 2 +- player.lua | 20 +++--- 4 files changed, 128 insertions(+), 93 deletions(-) diff --git a/collision.lua b/collision.lua index 43eab50..6210f01 100644 --- a/collision.lua +++ b/collision.lua @@ -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 diff --git a/map.lua b/map.lua index f7ec42e..2d95826 100644 --- a/map.lua +++ b/map.lua @@ -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,90 +103,121 @@ 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 - if not love.filesystem.exists("map") then - love.filesystem.createDirectory( "map" ) - end - - local block_exists = love.filesystem.exists("/map/"..chunkx.."_"..chunky..".txt") - - - local number = 0 - local val = 0 - tiles = {} - --generate map block - if not block_exists then - print(chunky) - --generate underground - if chunky <= underground then - for x = 1,map_max do - tiles[x] = {} - for y = 1,map_max do - tiles[x][y] = {} - tiles[x][y]["block"] = 2 - - end - end - maplib.generate_ore(tiles) + for xx = -max_chunks,max_chunks do + loaded_chunks[xx] = {} + for yy = -max_chunks,max_chunks do + - maplib.generate_cave(tiles) - --generate the grass top - elseif chunky == earth_max then - local yy = math.random(1,map_max) - for x = 1,map_max do - tiles[x] = {} - yy = yy + math.random(-1,1) - for y = 1,map_max do - tiles[x][y] = {} - --generate dirt as debug - if y == yy then - tiles[x][y]["block"] = 4 - elseif y < yy then - tiles[x][y]["block"] = 1 - else - tiles[x][y]["block"] = 3 - end - end + if not love.filesystem.exists("map") then + love.filesystem.createDirectory( "map" ) end - --generate dirt under grass - elseif chunky < earth_max and chunky > underground then - print("test") - for x = 1, map_max do - tiles[x] = {} - for y = 1,map_max do - tiles[x][y] = {} - tiles[x][y]["block"] = 3 - - end - end - else - print("generate air") - for x = 1,map_max do - tiles[x] = {} - for y = 1,map_max do - tiles[x][y] = {} - tiles[x][y]["block"] = 1 - + + local block_exists = love.filesystem.exists("/map/"..chunkx+xx.."_"..chunky+yy..".txt") + + + local number = 0 + local val = 0 + tiles = {} + --generate map block + if not block_exists then + --print(chunky) + --generate underground + if chunky+yy <= underground then + for x = 1,map_max do + tiles[x] = {} + for y = 1,map_max do + tiles[x][y] = {} + tiles[x][y]["block"] = 2 + + end + end + maplib.generate_ore(tiles) + + maplib.generate_cave(tiles) + --generate the grass top + elseif chunky+yy == earth_max then + local yer = math.random(1,map_max) + for x = 1,map_max do + tiles[x] = {} + yer = yer + math.random(-1,1) + for y = 1,map_max do + tiles[x][y] = {} + --generate dirt as debug + if y == yer then + tiles[x][y]["block"] = 4 + elseif y < yer then + tiles[x][y]["block"] = 1 + else + tiles[x][y]["block"] = 3 + end + end + end + --generate dirt under grass + 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 + tiles[x][y] = {} + tiles[x][y]["block"] = 3 + + end + end + else + --print("generate air") + for x = 1,map_max do + tiles[x] = {} + for y = 1,map_max do + tiles[x][y] = {} + tiles[x][y]["block"] = 1 + + end + end end + + --save + 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+xx.."_"..chunky+yy..".txt")) + loaded_chunks[xx][yy] = tiles end end - - --save - love.filesystem.write( "/map/"..chunkx.."_"..chunky..".txt", TSerial.pack(tiles)) - else - - tiles = TSerial.unpack(love.filesystem.read("/map/"..chunkx.."_"..chunky..".txt")) 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 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) + 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[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 diff --git a/menu.lua b/menu.lua index 96d7146..5fa8226 100644 --- a/menu.lua +++ b/menu.lua @@ -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 diff --git a/player.lua b/player.lua index dd577d7..1092719 100644 --- a/player.lua +++ b/player.lua @@ -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