buffs.lua 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. local Buffs = class()
  2. function Buffs:init()
  3. self.buffs = {}
  4. end
  5. function Buffs:update()
  6. table.with(self.buffs, 'update')
  7. end
  8. function Buffs:add(player, code)
  9. local buff = self:get(player, code)
  10. if buff then return buff.stack and buff:stack() or buff end
  11. buff = new(data.buff[code])
  12. buff.owner = player
  13. f.exe(buff.activate, buff)
  14. table.insert(self.buffs, buff)
  15. return buff
  16. end
  17. function Buffs:remove(player, code, stack)
  18. local buff, i = self:get(player, code)
  19. if buff then
  20. f.exe(stack and buff.unstack or buff.deactivate, buff)
  21. if not stack then table.remove(self.buffs, i) end
  22. end
  23. end
  24. function Buffs:removeAll(player)
  25. for i = #self.buffs, 1, -1 do
  26. local buff = self.buffs[i]
  27. if buff.owner == player then
  28. f.exe(buff.deactivate, buff)
  29. table.remove(self.buffs, i)
  30. end
  31. end
  32. end
  33. function Buffs:get(player, code)
  34. for i = 1, #self.buffs do
  35. local buff = self.buffs[i]
  36. if buff.owner == player and buff.code == code then
  37. return buff, i
  38. end
  39. end
  40. end
  41. return Buffs