summaryrefslogtreecommitdiff
path: root/client/chunk.lua
diff options
context:
space:
mode:
Diffstat (limited to 'client/chunk.lua')
-rw-r--r--client/chunk.lua42
1 files changed, 42 insertions, 0 deletions
diff --git a/client/chunk.lua b/client/chunk.lua
new file mode 100644
index 0000000..73fe949
--- /dev/null
+++ b/client/chunk.lua
@@ -0,0 +1,42 @@
+local class = require"common.class"
+local chunk = require"common.chunk"
+local Chunk = require"common.chunk".Chunk
+local CHUNK_SIZE = require"common.constants".CHUNK_SIZE
+
+local ChunkC = class.extend(chunk.Chunk)
+function ChunkC.make(cls,...)
+ local self = chunk.Chunk.make(cls,...)
+ self.imesh = cls.imesh_from_chunk_data(self)
+ return self
+end
+local function tile_to_vattr(tile)
+ -- for now tiles are always numbers
+ -- so this just returns the input
+ -- this function might move to drawing.lua once it does something less trivial
+ assert(type(tile) == "number","can't send non-numerical tile to gpu")
+ return tile
+end
+function ChunkC.imesh_from_chunk_data(the_chunk)
+ local mesh_data = {}
+ for q=0,CHUNK_SIZE-1 do
+ for r =0,CHUNK_SIZE-1 do
+ local t = the_chunk:_atqr(q,r)
+ table.insert(mesh_data,{tile_to_vattr(t)})
+ end
+ end
+
+ local imesh = love.graphics.newMesh({
+ {"tile_type","float",1}
+ }, mesh_data, nil, "dynamic")
+
+ return imesh
+end
+
+function ChunkC.set_at(self,hoffs,tile)
+ Chunk.set_at(self,hoffs,tile)
+
+ local idx = chunk.index(hoffs.q,hoffs.r)
+ self.imesh:setVertexAttribute(idx, 1, tile_to_vattr(tile))
+end
+
+return {ChunkC=ChunkC}