diff options
author | sanine-a <sanine.not@pm.me> | 2023-03-28 13:55:59 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2023-03-28 13:55:59 -0500 |
commit | d1c2a881f55b80603f6a7772a2c32394d23e795a (patch) | |
tree | 945def73f0ef2a1f31fbb9f6756515a9e8a73445 /honey/ecs.lua | |
parent | 4f450ca94522f5c26a631fc4d19bd6eec420cbec (diff) |
switch to uids
Diffstat (limited to 'honey/ecs.lua')
-rw-r--r-- | honey/ecs.lua | 23 |
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 |