diff options
-rw-r--r-- | honey/ecs.lua | 23 | ||||
-rw-r--r-- | main.lua | 2 |
2 files changed, 8 insertions, 17 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 @@ -44,7 +44,7 @@ package.loaded['baseRotationScript'] = function(db, id, dt) end package.loaded['cameraRotationScript'] = function(db, id, dt) local transform = db:getComponent(id, "transform") - transform.matrix:translate(Vec3{0, 0, -0.2*math.sin(math.pi * glfw.GetTime())}) + transform.matrix:identity():translate(Vec3{0, 0, -6 + math.sin(math.pi * glfw.GetTime())}) end |