refactor Character:addBehavior

This commit is contained in:
PeaAshMeter 2025-09-28 23:47:50 +03:00
parent 9fe2276d04
commit dd84e157bd

View File

@ -74,22 +74,24 @@ function character:try(behavior, fn)
end end
--- usage: --- usage:
--- addModules( {logic.new(), graphics.new(), ...} ) --- addBehavior( {logic.new(), graphics.new(), ...} )
--- ---
--- or you may chain this if you are a wannabe haskell kiddo --- or you may chain this if you are a wannabe haskell kiddo
function character:addBehavior(modules) --- @param behaviors Behavior[]
for _, module in ipairs(modules) do --- @return Character | nil
if module.dependencies then function character:addBehavior(behaviors)
for _, dep in ipairs(module.dependencies) do for _, b in ipairs(behaviors) do
if b.dependencies then
for _, dep in ipairs(b.dependencies) do
if not self:has(dep) then if not self:has(dep) then
return print("[Character]: cannot add \"" .. module.id .. return print("[Character]: cannot add \"" .. b.id ..
"\" for a character (Id = " .. self.id .. "): needs \"" .. dep.id .. "\"!") "\" for a character (Id = " .. self.id .. "): needs \"" .. dep.id .. "\"!")
end end
end end
end end
module.owner = self b.owner = self
table.insert(self.behaviors, module) table.insert(self.behaviors, b)
self._behaviorsIdx[module.id] = #self.behaviors self._behaviorsIdx[b.id] = #self.behaviors
end end
return self return self
end end