diff options
Diffstat (limited to 'common/chunk.lua')
-rw-r--r-- | common/chunk.lua | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/common/chunk.lua b/common/chunk.lua index 1d3faa6..affdab8 100644 --- a/common/chunk.lua +++ b/common/chunk.lua @@ -1,9 +1,8 @@ local json = require"common.dkjson" local class = require"common.class" +local coords = require"common.coords" -local CHUNK_SIZE = 64 - --- for now tiles shall be booleans +local CHUNK_SIZE = require"common.constants".CHUNK_SIZE local function index_ok(offq,offr) return 0<=offq and 0<=offr and offq<CHUNK_SIZE and offr<CHUNK_SIZE @@ -17,12 +16,15 @@ local function index(offq,offr) end local Chunk = class() -function Chunk.make(cls,tiles) - return setmetatable({tiles=tiles},cls) +function Chunk.make(cls,cp,tiles) + return setmetatable({cp=cp,tiles=tiles},cls) end function Chunk.at(self,hoffs) - if not index_ok(hoffs.q,hoffs.r) then return nil end - return self.tiles[index(hoffs.q,hoffs.r)] + return self:_atqr(hoffs.q,hoffs.r) +end +function Chunk._atqr(self,q,r) + if not index_ok(q,r) then return nil end + return self.tiles[index(q,r)] end function Chunk.set_at(self,hoffs,tile) if not index_ok(hoffs.q,hoffs.r) then return end @@ -30,17 +32,18 @@ function Chunk.set_at(self,hoffs,tile) end function Chunk.data_packet(self) - return json.encode{t="chunk",tiles=self.tiles} + return json.encode{t="chunk",tiles=self.tiles,u=self.cp.u,v=self.cp.v} end function Chunk.from_packet_data(packet) -- assuming packet has already been json.decoded -- since otherwise how would we know it's a chunk packet - return Chunk:make(packet.tiles) + + local cp = coords.ChunkPos:make(packet.u,packet.v) + return Chunk:make(cp,packet.tiles) end return { Chunk=Chunk, - SIZE=CHUNK_SIZE, index=index, index_ok=index_ok, } |