diff options
Diffstat (limited to 'honey/ecs.lua')
-rw-r--r-- | honey/ecs.lua | 23 |
1 files changed, 7 insertions, 16 deletions
diff --git a/honey/ecs.lua b/honey/ecs.lua index ef5d6cb..69feb12 100644 --- a/honey/ecs.lua +++ b/honey/ecs.lua @@ -60,27 +60,18 @@ function EntityDb.addComponent(self, id, name, value) -- create the relevant component table if it doesn't exist if not self.components[name] then - self.components[name] = { count=0 } + self.components[name] = { count=0, data={} } end local component = self.components[name] - component[id] = value + component.data[id] = value component.count = component.count + 1 end -local function shallowCopy(tbl) - local copy = {} - for k, v in pairs(tbl) do copy[k] = v end - return copy -end - - -- get all entities with a given component function EntityDb.queryComponent(self, name) - local query = shallowCopy(self.components[name]) - query.count = nil - return query + return self.components[name].data end @@ -89,7 +80,7 @@ function EntityDb.queryEntity(self, id) self:checkIsValid(id) local query = {} for name, component in pairs(self.components) do - query[name] = component[id] + query[name] = component.data[id] end return query end @@ -98,7 +89,7 @@ end -- get a specific component from an entity function EntityDb.getComponent(self, id, name) self:checkIsValid(id) - return self.components[name][id] + return self.components[name].data[id] end @@ -106,8 +97,8 @@ end function EntityDb.removeComponent(self, id, name) self:checkIsValid(id) local component = self.components[name] - if component[id] ~= nil then - component[id] = nil + if component.data[id] ~= nil then + component.data[id] = nil component.count = component.count - 1 if component.count == 0 then self.components[name] = nil |