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:
Pablo Ariel Mayobre 2023-02-14 18:14:23 -03:00
parent 50249d5ad3
commit c4594da19d
2 changed files with 29 additions and 10 deletions

View file

@ -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.