diff --git a/concord/component.lua b/concord/component.lua index 7a9db98..5122e9a 100644 --- a/concord/component.lua +++ b/concord/component.lua @@ -47,31 +47,39 @@ function Component.new(name, populate) return componentClass end --- Internal: Populates a Component with values +-- Internal: Populates a Component with values. function Component:__populate() -- luacheck: ignore end +-- Callback: When the Component gets removed or replaced in an Entity. +function Component:removed() -- luacheck: ignore +end + +-- Callback: When the Component gets serialized as part of an Entity. function Component:serialize() local data = Utils.shallowCopy(self, {}) --This values shouldn't be copied over - data.__componentClass = nil - data.__isComponent = nil + data.__componentClass = nil + data.__entity = nil + data.__isComponent = nil data.__isComponentClass = nil return data end +-- Callback: When the Component gets deserialized from serialized data. function Component:deserialize(data) Utils.shallowCopy(data, self) end -- Internal: Creates a new Component. -- @return A new Component -function Component:__new() +function Component:__new(entity) local component = setmetatable({ __componentClass = self, + __entity = entity, __isComponent = true, __isComponentClass = false, }, self.__mt) @@ -82,8 +90,8 @@ end -- Internal: Creates and populates a new Component. -- @param ... Varargs passed to the populate function -- @return A new populated Component -function Component:__initialize(...) - local component = self:__new() +function Component:__initialize(entity, ...) + local component = self:__new(entity) self.__populate(component, ...) diff --git a/concord/entity.lua b/concord/entity.lua index 21a09cb..7bdbdc2 100644 --- a/concord/entity.lua +++ b/concord/entity.lua @@ -35,17 +35,28 @@ function Entity.new(world) end local function give(e, name, componentClass, ...) - local component = componentClass:__initialize(...) + local component = componentClass:__initialize(e, ...) + local hadComponent = not not e[name] + + if hadComponent then + e[name]:removed() + end e[name] = component - e:__dirty() + if not hadComponent then + e:__dirty() + end end local function remove(e, name) - e[name] = nil + if e[name] then + e[name]:removed() - e:__dirty() + e[name] = nil + + e:__dirty() + end end --- Gives an Entity a Component.