Add in loading on seperate thread

This commit is contained in:
jordan4ibanez 2017-07-12 05:25:10 -04:00
parent 390d753c37
commit 17245f556f
7 changed files with 244 additions and 161 deletions

View File

@ -44,6 +44,7 @@ function collision(oldposx,oldposy)
--print(chunkerx, chunkery, "|", squarex,squarey)
--print( loaded_chunks[chunkerx][chunkery][squarex][squarey]["block"])
--if (squarex1 > map_max or squarex1 <= 0) or (squarey1 > map_max or squarey1 <= 0) or ore[loaded_chunks[0][0][squarex1][squarey1]["block"]]["collide"] ~= false then
if loaded_chunks[chunkerx] and loaded_chunks[chunkerx][chunkery] and loaded_chunks[chunkerx][chunkery][squarex] and loaded_chunks[chunkerx][chunkery][squarex][squarey] then
if ore[loaded_chunks[chunkerx][chunkery][squarex][squarey]["block"]]["collide"] ~= false then
player.playery = oldposy
if r == 2 then
@ -56,6 +57,7 @@ function collision(oldposx,oldposy)
end
end
end
end
if fall == true then
player.on_block = false
end
@ -92,6 +94,7 @@ function collision(oldposx,oldposy)
end
--if (squarex1 > map_max or squarex1 <= 0) or (squarey1 > map_max or squarey1 <= 0) or ore[loaded_chunks[0][0][squarex1][squarey1]["block"]]["collide"] ~= false then
if loaded_chunks[chunkerx] and loaded_chunks[chunkerx][chunkery] and loaded_chunks[chunkerx][chunkery][squarex] and loaded_chunks[chunkerx][chunkery][squarex][squarey] then
if ore[loaded_chunks[chunkerx][chunkery][squarex][squarey]["block"]]["collide"] ~= false then
player.inertiax = 0
player.playerx = oldposx
@ -99,6 +102,7 @@ function collision(oldposx,oldposy)
end
end
end
end
end

8
creating_map_save.lua Normal file
View File

@ -0,0 +1,8 @@
savechannel = love.thread.getChannel("save")
dofile("tserial.lua")
while true do
if savechannel:getCount( ) > 0 then
love.filesystem.write( "/map/"..chunkx+xx.."_"..chunky+yy..".txt", TSerial.pack(tiles))
end

View File

@ -76,6 +76,8 @@ function love.quit( )
end
function love.update(dt)
maplib.load_chunks()
fpsGraph:update(dt)
memGraph:update(dt)
-- Update our custom graph

40
map.lua
View File

@ -2,7 +2,8 @@
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
--tile size
map_max = 25
@ -88,14 +89,39 @@ savethread = love.thread.newThread("saving.lua")
savechannel = love.thread.getChannel("save")
loadchannel = love.thread.getChannel("load")
savethread:start()
--saves all memory data into file
function maplib.save_chunks()
--print("saving chunks")
savechannel:push{max_chunks,chunkx,chunky,loaded_chunks}
savechannel:push{"saving_old",max_chunks,chunkx,chunky,loaded_chunks}
end
--maplib.save_chunks()
--watches and tells game to load up map tiles
function maplib.load_chunks()
for xx = -max_chunks,max_chunks do
for yy = -max_chunks,max_chunks do
--tell thread to load chunk
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
end
end
--generates ore
function maplib.generate_ore(tiles)
@ -253,10 +279,6 @@ function maplib.createmap()
--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
end
@ -309,8 +331,10 @@ function maplib.draw()
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)
end
end
--love.graphics.print(,, )
--if x == math.floor(map_max / 2) and y == math.floor(map_max / 2) then
-- love.graphics.print("X", x*scale, y*scale)
@ -362,6 +386,8 @@ function maplib.liquid_flow(dt)
--if not after_table[xx][yy][x][y] then
-- after_table[xx][yy][x][y] = {}
--end
if loaded_chunks[xx][-yy] and loaded_chunks[xx][-yy][x] and loaded_chunks[xx][-yy][x][y] then
local block = loaded_chunks[xx][-yy][x][y]["block"]
@ -480,6 +506,8 @@ function maplib.liquid_flow(dt)
after_table[xx-1][-yy][map_max][y]["xleft"] = "xleft"
end
--love.graphics.draw(texture_table[loaded_chunks[xx][-yy][x][y]["block"]], (((x*scale)-(player.playerx*scale))+((scale*map_max)/2))+(map_max*scale*xx)+offsetx, (((y*scale)-(player.playery*scale))+((scale*map_max)/2))+(map_max*scale*yy)+offsety-4,0, scale/16, scale/16)
end
end
end
end

12
map_load.lua Normal file
View File

@ -0,0 +1,12 @@
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"}
local tiles = loadchannel:pop()
print(tiles)
loaded_chunks[xx][yy] = tiles
end

View File

@ -129,6 +129,7 @@ function mine(key)
--play sound and remove tile
if left then
--print(mx,my)
if loaded_chunks[selected_chunkx] and loaded_chunks[selected_chunkx][selected_chunky] and loaded_chunks[selected_chunkx][selected_chunky][mx] and loaded_chunks[selected_chunkx][selected_chunky][mx][my] then
if loaded_chunks[selected_chunkx][selected_chunky][mx][my]["block"] ~= 1 then
minesound:setPitch(love.math.random(50,100)/100)
minesound:stop()
@ -139,7 +140,9 @@ function mine(key)
score = score + 1
end
end
elseif right then
if loaded_chunks[selected_chunkx] and loaded_chunks[selected_chunkx][selected_chunky] and loaded_chunks[selected_chunkx][selected_chunky][mx] and loaded_chunks[selected_chunkx][selected_chunky][mx][my] then
if loaded_chunks[selected_chunkx][selected_chunky][mx][my]["block"] == 1 and (mx ~= player.playerx or my ~= player.playery) then
placesound:setPitch(love.math.random(50,100)/100)
placesound:stop()
@ -151,6 +154,7 @@ function mine(key)
end
end
end
end
end
function player.move_camera(dt)

View File

@ -1,4 +1,7 @@
--how the map saves and loads
savechannel = love.thread.getChannel("save")
loadchannel = love.thread.getChannel("load")
dofile("tserial.lua")
@ -7,22 +10,44 @@ dofile("tserial.lua")
--debug info
while true do
print("test")
if savechannel:getCount( ) > 0 then
print("saving chunks")
local x = savechannel:pop()
local max_chunks = x[1]
local chunkx = x[2]
local chunky = x[3]
local loaded_chunks = x[4]
local doing = x[1]
if doing == "saving_old" then
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)
for xx = -max_chunks,max_chunks do
for yy = -max_chunks,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)
end
end
end
elseif doing == "load_old" then
print("load old")
print("loading old chunks")
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]
--print("pushing")
local file = love.filesystem.read("/map/"..chunkx+xx.."_"..chunky+yy..".txt")
loadchannel:push{TSerial.unpack(file),xx,yy}
print("succesfully loaded "..chunkx+xx.."_"..chunky+yy)
--print("file doesn't exist")
end
end
end