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
--- usage:
--- addModules( {logic.new(), graphics.new(), ...} )
--- addBehavior( {logic.new(), graphics.new(), ...} )
---
--- or you may chain this if you are a wannabe haskell kiddo
function character:addBehavior(modules)
for _, module in ipairs(modules) do
if module.dependencies then
for _, dep in ipairs(module.dependencies) do
--- @param behaviors Behavior[]
--- @return Character | nil
function character:addBehavior(behaviors)
for _, b in ipairs(behaviors) do
if b.dependencies then
for _, dep in ipairs(b.dependencies) do
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 .. "\"!")
end
end
end
module.owner = self
table.insert(self.behaviors, module)
self._behaviorsIdx[module.id] = #self.behaviors
b.owner = self
table.insert(self.behaviors, b)
self._behaviorsIdx[b.id] = #self.behaviors
end
return self
end