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 | |
parent | 4f450ca94522f5c26a631fc4d19bd6eec420cbec (diff) |
switch to uids
Diffstat (limited to 'honey')
-rw-r--r-- | honey/ecs.lua | 23 | ||||
-rw-r--r-- | honey/ecs.test.lua | 8 |
2 files changed, 25 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 diff --git a/honey/ecs.test.lua b/honey/ecs.test.lua index fbdba30..0577dcd 100644 --- a/honey/ecs.test.lua +++ b/honey/ecs.test.lua @@ -1,8 +1,13 @@ +local testCount = 0 +local failCount = 0 + local function test(msg, f) + testCount = testCount + 1 local success, error = xpcall(f, debug.traceback) if success then print(msg .. "\t\t[OK]") else + failCount = failCount + 1 print(msg .. "\t\t[FAIL]") print(error) end @@ -222,3 +227,6 @@ test("EntityDb.deleteEntity() correctly removes an entity", function() local query = db:queryEntity(id1) end)) end) + + +print(string.format("ran %d tests, %d failed", testCount, failCount)) |