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 = 5,1
chunkx,chunky = 0,0
--tile size
map_max = 25
@ -100,25 +100,48 @@ function maplib.save_chunks()
end
--maplib.save_chunks()
--watches and tells game to load up map tiles
buffer = {}
function maplib.load_chunks()
for xx = -max_chunks,max_chunks do
for yy = -max_chunks,max_chunks do
--tell thread to load chunk
for xx = chunkx-max_chunks,chunkx+max_chunks do
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 data,xxer,yyer
if chunkeyy then
data = chunkeyy[1]
xxer = chunkeyy[2]
yyer = chunkeyy[3]
end
if xxer == xx and yyer == yy and loaded_chunks[xx][yy] == nil then
loaded_chunks[xx][yy] = data
elseif loaded_chunks[xx][yy] == nil then
savechannel:push{"load_old",max_chunks,chunkx,chunky,loaded_chunks,xx,yy}
end
end
loaded_chunks[xxer][yyer] = data
--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
--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
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+xx.."_"..chunky+yy..".txt")
function maplib.createmap()
for xx = chunkx-max_chunks,chunkx+max_chunks do
--if loaded_chunks[xx] == nil then
-- loaded_chunks[xx] = {}
--end
for yy = chunky-max_chunks,chunky+max_chunks do
local block_exists = love.filesystem.exists("/map/"..xx.."_"..yy..".txt")
local number = 0
@ -239,6 +262,9 @@ function maplib.createmap()
for x = 1,map_max do
tiles[x] = {}
yer = yer + math.random(-1,1)
if yer == 1 then
yer = 2
end
for y = 1,map_max do
tiles[x][y] = {}
--generate dirt as debug
@ -277,8 +303,9 @@ function maplib.createmap()
end
--save
love.filesystem.write( "/map/"..chunkx+xx.."_"..chunky+yy..".txt", TSerial.pack(tiles))
loaded_chunks[xx][yy] = tiles
--love.filesystem.write( "/map/"..xx.."_"..yy..".txt", TSerial.pack(tiles))
savechannel:push{"save_new",tiles,xx,yy}
--loaded_chunks[xx][yy] = tiles
end
end
end
@ -307,13 +334,14 @@ end
--end
--drawing the map
function maplib.draw()
love.graphics.setFont(font)
for xx = -max_chunks,max_chunks do
for yy = -max_chunks,max_chunks do
--love.graphics.setFont(font)
for xx = chunkx-max_chunks,chunkx+max_chunks do
for yy = chunky-max_chunks,chunky+max_chunks do
--draw sky
if chunky-yy > underground then
if yy > underground then
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)
end
--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 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 drawy = player_drawny+(y*scale)+(map_max*scale*yy)-(player.playery*scale)
local drawx = player_drawnx+(x*scale)+(map_max*scale*(xx-chunkx))-(player.playerx*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 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)
if loaded_chunks[xx] and loaded_chunks[xx][yy] and loaded_chunks[xx][yy][x] and loaded_chunks[xx][yy][x][y] then
--print("Drawing")
love.graphics.draw(texture_table[loaded_chunks[xx][yy][x][y]["block"]], drawx,drawy,0, scale/16, scale/16)
end
end
--love.graphics.print(,, )
@ -369,11 +398,11 @@ function maplib.liquid_flow(dt)
local after_table = {}
if flowtimer > 0.1 then
flowtimer = 0
for xx = -max_chunks,max_chunks do
for xx = chunkx-max_chunks,chunkx+max_chunks do
--if not after_table[xx] then
-- after_table[xx] = {}
--end
for yy = -max_chunks,max_chunks do
for yy = chunky-max_chunks,chunky+max_chunks do
--if not after_table[xx][yy] then
-- after_table[xx][yy] = {}
--end

View File

@ -1,10 +1,11 @@
while true do
--load old tiles
print("load old")
--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()
print(tiles)

View File

@ -16,6 +16,8 @@ end
function physics.player_mod_y(value)
--print(player.inertiay+value)
--print(player.on_block)
player.inertiay = player.inertiay + value
if player.on_block == true then
--print("jump")
player.inertiay = player.inertiay + value
@ -26,7 +28,45 @@ end
function physics.player_x_apply(dt)
--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)
--print(player.inertiax)
if math.abs(player.inertiax) <= 0.005 then
@ -70,9 +110,9 @@ function physics.gravity()
end
end
else
print("player in unloaded chunk")
player.inertiay = 0
player.inertiax = 0
--print("player in unloaded chunk")
--player.inertiay = 0
--player.inertiax = 0
end
end

View File

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

View File

@ -12,41 +12,50 @@ dofile("tserial.lua")
while true do
--print("test")
if savechannel:getCount( ) > 0 then
--print("failure")
local x = savechannel:pop()
local doing = x[1]
if doing == "saving_old" then
print("saving old chunks")
--print("saving old chunks")
local max_chunks = x[2]
local chunkx = x[3]
local chunky = x[4]
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 yy = -max_chunks,max_chunks do
for xx = chunkx-max_chunks,chunkx+max_chunks do
for yy = chunky-max_chunks,chunky+max_chunks do
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]))
print("saving:"..chunkx+xx.."_"..chunky+yy)
love.filesystem.write( "/map/"..xx.."_"..yy..".txt", TSerial.pack(loaded_chunks[xx][yy]))
print("saving:"..xx.."_"..yy)
end
end
end
elseif doing == "load_old" then
print("load old")
print("loading old chunks")
--print("load old")
local max_chunks = x[2]
local chunkx = x[3]
local chunky = x[4]
local loaded_chunks = x[5]
local xx = x[6]
local yy = x[7]
local loaded_chunks = x[3]
local xx = x[4]
local yy = x[5]
--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}
print("succesfully loaded "..chunkx+xx.."_"..chunky+yy)
--print("succesfully loaded "..xx.."_"..yy)
--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