diff options
author | sanine-a <sanine.not@pm.me> | 2023-04-13 10:07:04 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2023-04-13 10:07:04 -0500 |
commit | c09372fa140308194d2615ed97864a4ded54d626 (patch) | |
tree | 8bc9990734b9ce107b4bafe94b9ec19201320275 /honey/ecs.lua | |
parent | 20494a0fe10011601c7573e6a7fa53bd891b1bc0 (diff) |
don't shallow copy component tables
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 |