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
|
||||
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.__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, ...)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
if not hadComponent then
|
||||
e:__dirty()
|
||||
end
|
||||
end
|
||||
|
||||
local function remove(e, name)
|
||||
if e[name] then
|
||||
e[name]:removed()
|
||||
|
||||
e[name] = nil
|
||||
|
||||
e:__dirty()
|
||||
end
|
||||
end
|
||||
|
||||
--- Gives an Entity a Component.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue