From d1c2a881f55b80603f6a7772a2c32394d23e795a Mon Sep 17 00:00:00 2001 From: sanine-a Date: Tue, 28 Mar 2023 13:55:59 -0500 Subject: switch to uids --- honey/ecs.lua | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'honey/ecs.lua') 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 -- cgit v1.2.1