mirror of
https://github.com/Keyslam-Group/Concord.git
synced 2025-09-02 12:24:11 -04:00
Add Component:removed() callback
Fixes #37 I also added a reference to the Entity inside the Component which will help with #38
This commit is contained in:
parent
50249d5ad3
commit
c4594da19d
2 changed files with 29 additions and 10 deletions
|
@ -47,31 +47,39 @@ function Component.new(name, populate)
|
||||||
return componentClass
|
return componentClass
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Internal: Populates a Component with values
|
-- Internal: Populates a Component with values.
|
||||||
function Component:__populate() -- luacheck: ignore
|
function Component:__populate() -- luacheck: ignore
|
||||||
end
|
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()
|
function Component:serialize()
|
||||||
local data = Utils.shallowCopy(self, {})
|
local data = Utils.shallowCopy(self, {})
|
||||||
|
|
||||||
--This values shouldn't be copied over
|
--This values shouldn't be copied over
|
||||||
data.__componentClass = nil
|
data.__componentClass = nil
|
||||||
data.__isComponent = nil
|
data.__entity = nil
|
||||||
|
data.__isComponent = nil
|
||||||
data.__isComponentClass = nil
|
data.__isComponentClass = nil
|
||||||
|
|
||||||
return data
|
return data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Callback: When the Component gets deserialized from serialized data.
|
||||||
function Component:deserialize(data)
|
function Component:deserialize(data)
|
||||||
Utils.shallowCopy(data, self)
|
Utils.shallowCopy(data, self)
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Internal: Creates a new Component.
|
-- Internal: Creates a new Component.
|
||||||
-- @return A new Component
|
-- @return A new Component
|
||||||
function Component:__new()
|
function Component:__new(entity)
|
||||||
local component = setmetatable({
|
local component = setmetatable({
|
||||||
__componentClass = self,
|
__componentClass = self,
|
||||||
|
|
||||||
|
__entity = entity,
|
||||||
__isComponent = true,
|
__isComponent = true,
|
||||||
__isComponentClass = false,
|
__isComponentClass = false,
|
||||||
}, self.__mt)
|
}, self.__mt)
|
||||||
|
@ -82,8 +90,8 @@ end
|
||||||
-- Internal: Creates and populates a new Component.
|
-- Internal: Creates and populates a new Component.
|
||||||
-- @param ... Varargs passed to the populate function
|
-- @param ... Varargs passed to the populate function
|
||||||
-- @return A new populated Component
|
-- @return A new populated Component
|
||||||
function Component:__initialize(...)
|
function Component:__initialize(entity, ...)
|
||||||
local component = self:__new()
|
local component = self:__new(entity)
|
||||||
|
|
||||||
self.__populate(component, ...)
|
self.__populate(component, ...)
|
||||||
|
|
||||||
|
|
|
@ -35,17 +35,28 @@ function Entity.new(world)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function give(e, name, componentClass, ...)
|
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[name] = component
|
||||||
|
|
||||||
e:__dirty()
|
if not hadComponent then
|
||||||
|
e:__dirty()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
local function remove(e, name)
|
local function remove(e, name)
|
||||||
e[name] = nil
|
if e[name] then
|
||||||
|
e[name]:removed()
|
||||||
|
|
||||||
e:__dirty()
|
e[name] = nil
|
||||||
|
|
||||||
|
e:__dirty()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Gives an Entity a Component.
|
--- Gives an Entity a Component.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue