local ____exports = __module.env ____exports.AttributeAndBuffComponent = __TS__DefineClass("AttributeAndBuffComponent", __module) local AttributeAndBuffComponent = ____exports.AttributeAndBuffComponent AttributeAndBuffComponent.name = "AttributeAndBuffComponent" function AttributeAndBuffComponent.prototype.____constructor(self, initialStats) self.baseStats = {} self.buffs = {} self.listeners = {} for k, v in pairs(initialStats) do self.baseStats[k] = v end end function AttributeAndBuffComponent.prototype.OnAttributeChanged(self, cb) self.listeners[#self.listeners + 1] = cb end function AttributeAndBuffComponent.prototype.GetBase(self, attr) return self.baseStats[attr] or 0 end function AttributeAndBuffComponent.prototype.SetBase(self, attr, val) local clamped = val if attr == "HP" then clamped = math.max( 0, math.min( val, self:GetFinal("MaxHP") ) ) end self.baseStats[attr] = clamped self:Notify( attr, self:GetFinal(attr) ) end function AttributeAndBuffComponent.prototype.GetFinal(self, attr) local baseVal = self.baseStats[attr] or 0 local flat = 0 local pct = 0 do local i = 0 while i < #self.buffs do do local b = self.buffs[i + 1] local mods = b.spec.modifiers if mods == nil then goto __continue10 end do local j = 0 while j < #mods do do local m = mods[j + 1] if m.attr ~= attr then goto __continue13 end if m.op == 0 then flat = flat + m.value * b.stacks else pct = pct + m.value * b.stacks end end ::__continue13:: j = j + 1 end end end ::__continue10:: i = i + 1 end end return (baseVal + flat) * (1 + pct) end function AttributeAndBuffComponent.prototype.ApplyBuff(self, spec) local policy = spec.stackPolicy or 0 local maxStacks = spec.maxStacks or 1 do local i = 0 while i < #self.buffs do local existing = self.buffs[i + 1] if existing.spec.id == spec.id then if policy == 2 then return end existing.remaining = spec.duration if policy == 1 and existing.stacks < maxStacks then existing.stacks = existing.stacks + 1 end return end i = i + 1 end end self.buffs[#self.buffs + 1] = {spec = spec, stacks = 1, remaining = spec.duration, tickAccumulator = 0} end function AttributeAndBuffComponent.prototype.RemoveBuff(self, buffId) do local i = #self.buffs - 1 while i >= 0 do if self.buffs[i + 1].spec.id == buffId then table.remove(self.buffs, i + 1) return true end i = i - 1 end end return false end function AttributeAndBuffComponent.prototype.GetBuffStacks(self, buffId) do local i = 0 while i < #self.buffs do if self.buffs[i + 1].spec.id == buffId then return self.buffs[i + 1].stacks end i = i + 1 end end return 0 end function AttributeAndBuffComponent.prototype.Tick(self, dt) do local i = #self.buffs - 1 while i >= 0 do local b = self.buffs[i + 1] b.remaining = b.remaining - dt if b.spec.tickInterval ~= nil and b.spec.tickInterval > 0 and b.spec.hpPerTick ~= nil then b.tickAccumulator = b.tickAccumulator + dt while b.tickAccumulator >= b.spec.tickInterval do b.tickAccumulator = b.tickAccumulator - b.spec.tickInterval local delta = b.spec.hpPerTick * b.stacks self:SetBase( "HP", self:GetBase("HP") + delta ) end end if b.remaining <= 0 then table.remove(self.buffs, i + 1) end i = i - 1 end end end function AttributeAndBuffComponent.prototype.ApplyDamageFrom(self, attacker, skillMultiplier, isCrit, critMultiplier) if critMultiplier == nil then critMultiplier = 1.5 end local atk = attacker:GetFinal("Attack") local def = math.max( 0, self:GetFinal("Defense") ) local raw = atk * skillMultiplier * (isCrit and critMultiplier or 1) local mitigated = math.floor(raw * (100 / (100 + def)) + 0.5) local nextHp = math.max( 0, self:GetBase("HP") - mitigated ) self:SetBase("HP", nextHp) return { rawDamage = raw, mitigatedDamage = mitigated, isCrit = isCrit, targetHpAfter = nextHp, targetKilled = nextHp <= 0 } end function AttributeAndBuffComponent.prototype.Notify(self, attr, val) do local i = 0 while i < #self.listeners do self.listeners[i + 1](attr, val) i = i + 1 end end end function __module.UpdateImports() end