Push slightly broken continuous loaded chunks

This commit is contained in:
jordan4ibanez 2017-07-17 03:32:42 -04:00
parent d57e656e61
commit 89233f56eb
5 changed files with 140 additions and 61 deletions

107
map.lua
View File

@ -3,7 +3,7 @@ maplib = {}
--chunkx,chunky = math.random(-1000,1000),math.random(-1000,-1000) --chunkx,chunky = math.random(-1000,1000),math.random(-1000,-1000)
chunkx,chunky = 5,1 chunkx,chunky = 0,0
--tile size --tile size
map_max = 25 map_max = 25
@ -100,25 +100,48 @@ function maplib.save_chunks()
end end
--maplib.save_chunks() --maplib.save_chunks()
--watches and tells game to load up map tiles --watches and tells game to load up map tiles
buffer = {}
function maplib.load_chunks() function maplib.load_chunks()
for xx = -max_chunks,max_chunks do
for yy = -max_chunks,max_chunks do for xx = chunkx-max_chunks,chunkx+max_chunks do
--tell thread to load chunk if buffer[xx] == nil then
buffer[xx] = {}
end
if loaded_chunks[xx] == nil then
loaded_chunks[xx] = {}
end
for yy = chunky-max_chunks,chunky+max_chunks do
--print(xx,yy)
if loaded_chunks[xx][yy] == nil and buffer[xx][yy] == nil then
--print("MABLIB LOADCHUNKS")
--print("push load")
savechannel:push{"load_old",max_chunks,loaded_chunks,xx,yy}
buffer[xx][yy] = true
end
end
end
local count = loadchannel:getCount()
--tell thread to load chunk
if count > 0 then
--for i = 1,count do
print(count)
local chunkeyy = loadchannel:pop() local chunkeyy = loadchannel:pop()
local data,xxer,yyer local data,xxer,yyer
if chunkeyy then if chunkeyy then
data = chunkeyy[1] data = chunkeyy[1]
xxer = chunkeyy[2] xxer = chunkeyy[2]
yyer = chunkeyy[3] yyer = chunkeyy[3]
end end
if xxer == xx and yyer == yy and loaded_chunks[xx][yy] == nil then
loaded_chunks[xx][yy] = data loaded_chunks[xxer][yyer] = data
elseif loaded_chunks[xx][yy] == nil then --end
savechannel:push{"load_old",max_chunks,chunkx,chunky,loaded_chunks,xx,yy}
end
end
end end
end end
@ -195,23 +218,23 @@ 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 --max_chunks = 1 --x * x chunks loaded --does -1 - 1 (-x to x) -- (x * 2) + 1 to get max chunks in memory
--create stuff
if loaded_chunks == nil then
loaded_chunks = {} --chunks in mememory
end
if not love.filesystem.exists("map") then
love.filesystem.createDirectory( "map" )
end
--generates tile blocks --generates tile blocks
function maplib.createmap() function maplib.createmap()
for xx = chunkx-max_chunks,chunkx+max_chunks do
loaded_chunks = {} --chunks in mememory --if loaded_chunks[xx] == nil then
-- loaded_chunks[xx] = {}
for xx = -max_chunks,max_chunks do --end
loaded_chunks[xx] = {} for yy = chunky-max_chunks,chunky+max_chunks do
for yy = -max_chunks,max_chunks do
local block_exists = love.filesystem.exists("/map/"..xx.."_"..yy..".txt")
if not love.filesystem.exists("map") then
love.filesystem.createDirectory( "map" )
end
local block_exists = love.filesystem.exists("/map/"..chunkx+xx.."_"..chunky+yy..".txt")
local number = 0 local number = 0
@ -239,6 +262,9 @@ function maplib.createmap()
for x = 1,map_max do for x = 1,map_max do
tiles[x] = {} tiles[x] = {}
yer = yer + math.random(-1,1) yer = yer + math.random(-1,1)
if yer == 1 then
yer = 2
end
for y = 1,map_max do for y = 1,map_max do
tiles[x][y] = {} tiles[x][y] = {}
--generate dirt as debug --generate dirt as debug
@ -277,8 +303,9 @@ function maplib.createmap()
end end
--save --save
love.filesystem.write( "/map/"..chunkx+xx.."_"..chunky+yy..".txt", TSerial.pack(tiles)) --love.filesystem.write( "/map/"..xx.."_"..yy..".txt", TSerial.pack(tiles))
loaded_chunks[xx][yy] = tiles savechannel:push{"save_new",tiles,xx,yy}
--loaded_chunks[xx][yy] = tiles
end end
end end
end end
@ -307,13 +334,14 @@ end
--end --end
--drawing the map --drawing the map
function maplib.draw() function maplib.draw()
love.graphics.setFont(font) --love.graphics.setFont(font)
for xx = -max_chunks,max_chunks do for xx = chunkx-max_chunks,chunkx+max_chunks do
for yy = -max_chunks,max_chunks do for yy = chunky-max_chunks,chunky+max_chunks do
--draw sky --draw sky
if chunky-yy > underground then
if yy > underground then
love.graphics.setColor(65,105,225,255) love.graphics.setColor(65,105,225,255)
love.graphics.rectangle( "fill", player_drawnx+scale+(map_max*scale*xx)-(player.playerx*scale), player_drawny+scale+(map_max*scale*yy)-(player.playery*scale), scale*map_max,scale*map_max ) love.graphics.rectangle( "fill", player_drawnx+scale+(map_max*scale*(xx-chunkx))-(player.playerx*scale), player_drawny+scale+(map_max*scale*(chunky-yy))-(player.playery*scale), scale*map_max,scale*map_max )
love.graphics.setColor(255, 255, 255,255) love.graphics.setColor(255, 255, 255,255)
end end
--love.graphics.setColor(255,255,255) --love.graphics.setColor(255,255,255)
@ -325,14 +353,15 @@ function maplib.draw()
--local drawx = (((x*scale)-(player.playerx*scale))+((scale*map_max)/2))+(map_max*scale*xx)+offsetx --local drawx = (((x*scale)-(player.playerx*scale))+((scale*map_max)/2))+(map_max*scale*xx)+offsetx
--local drawy = (((y*scale)-((player.playery)*scale))+((scale*map_max)/2))+(map_max*scale*yy)+offsety-4 --local drawy = (((y*scale)-((player.playery)*scale))+((scale*map_max)/2))+(map_max*scale*yy)+offsety-4
local drawx = player_drawnx+(x*scale)+(map_max*scale*xx)-(player.playerx*scale) local drawx = player_drawnx+(x*scale)+(map_max*scale*(xx-chunkx))-(player.playerx*scale)
local drawy = player_drawny+(y*scale)+(map_max*scale*yy)-(player.playery*scale) local drawy = player_drawny+(y*scale)+(map_max*scale*(chunky-yy))-(player.playery*scale)
if drawx >= -scale and drawx < screenwidth and drawy >= -scale and drawy < screenheight then if drawx >= -scale and drawx < screenwidth and drawy >= -scale and drawy < screenheight then
if loaded_chunks[xx][-yy] and loaded_chunks[xx][-yy][x] and loaded_chunks[xx][-yy][x][y] then if loaded_chunks[xx] and loaded_chunks[xx][yy] and loaded_chunks[xx][yy][x] and loaded_chunks[xx][yy][x][y] then
love.graphics.draw(texture_table[loaded_chunks[xx][-yy][x][y]["block"]], drawx,drawy,0, scale/16, scale/16) --print("Drawing")
love.graphics.draw(texture_table[loaded_chunks[xx][yy][x][y]["block"]], drawx,drawy,0, scale/16, scale/16)
end end
end end
--love.graphics.print(,, ) --love.graphics.print(,, )
@ -369,11 +398,11 @@ function maplib.liquid_flow(dt)
local after_table = {} local after_table = {}
if flowtimer > 0.1 then if flowtimer > 0.1 then
flowtimer = 0 flowtimer = 0
for xx = -max_chunks,max_chunks do for xx = chunkx-max_chunks,chunkx+max_chunks do
--if not after_table[xx] then --if not after_table[xx] then
-- after_table[xx] = {} -- after_table[xx] = {}
--end --end
for yy = -max_chunks,max_chunks do for yy = chunky-max_chunks,chunky+max_chunks do
--if not after_table[xx][yy] then --if not after_table[xx][yy] then
-- after_table[xx][yy] = {} -- after_table[xx][yy] = {}
--end --end

View File

@ -1,10 +1,11 @@
while true do while true do
--load old tiles --load old tiles
print("load old") print("load old")
--tiles = TSerial.unpack(love.filesystem.read("/map/"..chunkx+xx.."_"..chunky+yy..".txt")) --tiles = TSerial.unpack(love.filesystem.read("/map/"..chunkx+xx.."_"..chunky+yy..".txt"))
savechannel:push{"load_old","/map/"..chunkx+xx.."_"..chunky+yy..".txt"} savechannel:push{"load_old","/map/"..xx.."_"..yy..".txt"}
local tiles = loadchannel:pop() local tiles = loadchannel:pop()
print(tiles) print(tiles)

View File

@ -16,6 +16,8 @@ end
function physics.player_mod_y(value) function physics.player_mod_y(value)
--print(player.inertiay+value) --print(player.inertiay+value)
--print(player.on_block) --print(player.on_block)
player.inertiay = player.inertiay + value
if player.on_block == true then if player.on_block == true then
--print("jump") --print("jump")
player.inertiay = player.inertiay + value player.inertiay = player.inertiay + value
@ -26,7 +28,45 @@ end
function physics.player_x_apply(dt) function physics.player_x_apply(dt)
--local oldposx,oldposy = player.playerx,player.playery --local oldposx,oldposy = player.playerx,player.playery
collision(player.playerx,player.playery)
--DEBUG
--
--collision(player.playerx,player.playery)
--
player.playery = player.playery + player.inertiay
player.playerx = player.playerx + player.inertiax
--collisionx(oldposx) --collisionx(oldposx)
--print(player.inertiax) --print(player.inertiax)
if math.abs(player.inertiax) <= 0.005 then if math.abs(player.inertiax) <= 0.005 then
@ -70,9 +110,9 @@ function physics.gravity()
end end
end end
else else
print("player in unloaded chunk") --print("player in unloaded chunk")
player.inertiay = 0 --player.inertiay = 0
player.inertiax = 0 --player.inertiax = 0
end end
end end

View File

@ -31,7 +31,7 @@ function move(dt)
if love.keyboard.isDown("w") then if love.keyboard.isDown("w") then
--jump() --jump()
physics.player_mod_y(-0.15) physics.player_mod_y(-0.01)
end end
if love.keyboard.isDown("s") then if love.keyboard.isDown("s") then
physics.player_mod_y(0.01) physics.player_mod_y(0.01)

View File

@ -12,41 +12,50 @@ dofile("tserial.lua")
while true do while true do
--print("test") --print("test")
if savechannel:getCount( ) > 0 then if savechannel:getCount( ) > 0 then
--print("failure")
local x = savechannel:pop() local x = savechannel:pop()
local doing = x[1] local doing = x[1]
if doing == "saving_old" then if doing == "saving_old" then
print("saving old chunks") --print("saving old chunks")
local max_chunks = x[2] local max_chunks = x[2]
local chunkx = x[3] local chunkx = x[3]
local chunky = x[4] local chunky = x[4]
local loaded_chunks = x[5] local loaded_chunks = x[5]
print(max_chunks,chunkx,chunky,loaded_chunks) --print(max_chunks,chunkx,chunky,loaded_chunks)
for xx = -max_chunks,max_chunks do for xx = chunkx-max_chunks,chunkx+max_chunks do
for yy = -max_chunks,max_chunks do for yy = chunky-max_chunks,chunky+max_chunks do
if loaded_chunks[xx] and loaded_chunks[xx][yy] then if loaded_chunks[xx] and loaded_chunks[xx][yy] then
love.filesystem.write( "/map/"..chunkx+xx.."_"..chunky+yy..".txt", TSerial.pack(loaded_chunks[xx][yy])) love.filesystem.write( "/map/"..xx.."_"..yy..".txt", TSerial.pack(loaded_chunks[xx][yy]))
print("saving:"..chunkx+xx.."_"..chunky+yy) print("saving:"..xx.."_"..yy)
end end
end end
end end
elseif doing == "load_old" then elseif doing == "load_old" then
print("load old") --print("load old")
print("loading old chunks")
local max_chunks = x[2] local max_chunks = x[2]
local chunkx = x[3] local loaded_chunks = x[3]
local chunky = x[4] local xx = x[4]
local loaded_chunks = x[5] local yy = x[5]
local xx = x[6]
local yy = x[7]
--print("pushing") --print("pushing")
local file = love.filesystem.read("/map/"..chunkx+xx.."_"..chunky+yy..".txt") --print(xx,yy)
local file = love.filesystem.read("/map/"..xx.."_"..yy..".txt")
--print("failure")
--print(file)
loadchannel:push{TSerial.unpack(file),xx,yy} loadchannel:push{TSerial.unpack(file),xx,yy}
print("succesfully loaded "..chunkx+xx.."_"..chunky+yy) --print("succesfully loaded "..xx.."_"..yy)
--print("file doesn't exist") --print("file doesn't exist")
elseif doing == "save_new" then
--print("saving new")
local data = x[2]
local xx = x[3]
local yy = x[4]
love.filesystem.write( "/map/"..xx.."_"..yy..".txt", TSerial.pack(data))
loadchannel:push{data,xx,yy}
end end
end end