summaryrefslogtreecommitdiff
path: root/common.lua
diff options
context:
space:
mode:
Diffstat (limited to 'common.lua')
-rw-r--r--common.lua17
1 files changed, 17 insertions, 0 deletions
diff --git a/common.lua b/common.lua
new file mode 100644
index 0000000..7e67daf
--- /dev/null
+++ b/common.lua
@@ -0,0 +1,17 @@
+local class = require 'r.class'
+
+local SIZE = 64
+local ChunkMap = class()
+function ChunkMap.make(cls) return setmetatable({d={}},cls) end
+local function key(pos) return pos.x .. ':' .. pos.y end
+function ChunkMap.add(self,pos,val) self.d[key(pos)] = val end
+function ChunkMap.remove(self,pos) self:add(pos,nil) end
+function ChunkMap.get(self,pos) return self.d[key(pos)] end
+local function idx(pos) return 1+pos.x*SIZE+pos.y end
+function ChunkMap.tile(self,pos)
+ local cp = (pos/SIZE):floor() local ix = idx(pos-cp*SIZE)
+ return self:get(cp)[ix] end
+
+return {
+ ChunkMap = ChunkMap,
+}