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 committed by Pablo Mayobre
parent 651472413b
commit 0dfc922e9a
No known key found for this signature in database
GPG key ID: 13A2F589D013E0E7
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.