summaryrefslogtreecommitdiff
path: root/honey/ecs.lua
diff options
context:
space:
mode:
Diffstat (limited to 'honey/ecs.lua')
-rw-r--r--honey/ecs.lua23
1 files changed, 17 insertions, 6 deletions
diff --git a/honey/ecs.lua b/honey/ecs.lua
index 62acce0..8aa5c5c 100644
--- a/honey/ecs.lua
+++ b/honey/ecs.lua
@@ -1,3 +1,5 @@
+math.randomseed(os.time())
+
local module = {}
setmetatable(module, {__index=_G})
setfenv(1, module)
@@ -61,6 +63,12 @@ end
--===== EntityDb =====--
+
+-- EntityDb is a database of entities and their associated components
+-- it should be quite efficient to query for all entities with a given component, and reasonably
+-- efficient to query for all components of a given entity
+
+
EntityDb = {}
EntityDb.__index = EntityDb
@@ -78,17 +86,20 @@ setmetatable(EntityDb, {__call=EntityDb.new})
function EntityDb.checkIsValid(self, id)
if not self.entities[id] then
- error(string.format("invalid entity id: 0x%x", id))
+ error(string.format("invalid entity id: %s", tostring(id)))
end
end
-local guid = (function()
- local id = 0
- return function() id=id+1 return id end
-end)()
+local function uid()
+ local template ='xxxx:xxxx:xxxx'
+ return string.gsub(template, 'x', function (c)
+ local v = math.random(0, 0xf)
+ return string.format('%x', v)
+ end)
+end
function EntityDb.createEntity(self)
- local id = guid()
+ local id = uid()
self.entities[id] = true
return id
end