summaryrefslogtreecommitdiff
path: root/honey/ecs.lua
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-04-12 21:31:37 -0500
committersanine <sanine.not@pm.me>2023-04-12 21:31:37 -0500
commit20494a0fe10011601c7573e6a7fa53bd891b1bc0 (patch)
treed4d0f9eaef76c7af926e318478695edc0ebbb389 /honey/ecs.lua
parent4a737a44777d440682699dcad35c04a23205730e (diff)
wrap entityDb and systemDb into single Database class
Diffstat (limited to 'honey/ecs.lua')
-rw-r--r--honey/ecs.lua113
1 files changed, 56 insertions, 57 deletions
diff --git a/honey/ecs.lua b/honey/ecs.lua
index 72cc41d..ef5d6cb 100644
--- a/honey/ecs.lua
+++ b/honey/ecs.lua
@@ -5,62 +5,6 @@ setmetatable(module, {__index=_G})
setfenv(1, module)
---===== Components =====--
-
--- Components provide a kind-of-type-safe way of storing values, since they are
--- guaranteed to have a specific set of keys and no other values in them. They also serialize
--- nicely for storage.
-
-Component = {}
-
-function Component.newFactory(name, params, serialize)
- -- create the metatable for this component type
- local metatable = {}
- metatable.__tostring = serialize or Component.serialize -- nice serialization
- metatable.__newindex = function(tbl, index, value)
- if params[index] ~= nil then
- tbl[index] = value
- else
- -- throw an error if we try to set a key that is not in the valid set
- error(string.format("%q is not a valid key for a component of type %q", index, name))
- end
- end
-
- -- the factory function for creating components
- return function(tbl)
- local tbl = tbl or {}
- local self = { __type=name }
-
- -- ensure that all keys are present
- for k, v in pairs(params) do
- self[k] = v
- end
- setmetatable(self, metatable)
-
- -- set the keys from the factory call
- for k, v in pairs(tbl) do
- self[k] = v
- end
-
- return self
- end
-end
-
-
-function Component.serialize(self)
- local str = "{"
- for k, v in pairs(self) do
- if type(v) == "string" then
- str = str .. string.format("%s=%q,", k, v)
- else
- str = str .. string.format("%s=%s,", k, tostring(v))
- end
- end
- str = string.sub(str, 1, -2) .. "}"
- return str
-end
-
-
--===== EntityDb =====--
@@ -188,10 +132,11 @@ SystemDb = {}
SystemDb.__index = SystemDb
-function SystemDb.new(_)
+function SystemDb.new(_, entityDb)
local self = {
systems = {},
sorted = {},
+ entityDb = entityDb,
}
setmetatable(self, SystemDb)
return self
@@ -212,6 +157,8 @@ function SystemDb.addSystem(self, systemFunc, params)
if type(systemFunc) == "table" then
system = systemFunc
else
+ local params = params or {}
+ params.db = self.entityDb
system = systemFunc(params)
end
@@ -243,4 +190,56 @@ function SystemDb.removeSystem(self, id)
end
+--===== Database =====--
+
+Database = {}
+Database.__index = Database
+
+function Database.new(_)
+ local self = {}
+ self.entityDb = EntityDb()
+ self.systemDb = SystemDb(self.entityDb)
+ setmetatable(self, Database)
+ return self
+end
+setmetatable(Database, {__call=Database.new})
+
+
+function Database.isValidEntity(self, id)
+ return self.entityDb:checkIsValid(id)
+end
+function Database.createEntity(self, id)
+ return self.entityDb:createEntity(id)
+end
+function Database.addComponent(self, id, name, value)
+ return self.entityDb:addComponent(id, name, value)
+end
+function Database.queryComponent(self, name)
+ return self.entityDb:queryComponent(name)
+end
+function Database.queryEntity(self, id)
+ return self.entityDb:queryEntity(id)
+end
+function Database.getComponent(self, id, name)
+ return self.entityDb:getComponent(id, name)
+end
+function Database.removeComponent(self, id, name)
+ return self.entityDb:removeComponent(id, name)
+end
+function Database.deleteEntity(self, id)
+ return self.entityDb.deleteEntity(id)
+end
+
+
+function Database.addSystem(self, func, params)
+ return self.systemDb:addSystem(func, params)
+end
+function Database.update(self, dt)
+ return self.systemDb:update(dt)
+end
+function Database.removeSystem(self, id)
+ return self.systemDb:removeSystem(id)
+end
+
+
return module