summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--honey/ecs.lua23
-rw-r--r--honey/ecs.test.lua8
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))