playerbuffs.lua 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. PlayerBuffs = class()
  2. function PlayerBuffs:init(player)
  3. self.player = player
  4. self.list = {}
  5. end
  6. function PlayerBuffs:update()
  7. table.with(self.list, 'rot')
  8. table.with(self.list, 'update')
  9. end
  10. function PlayerBuffs:add(code, vars)
  11. if self:get(code) then return self:reapply(code, vars) end
  12. local buff = data.buff[code]()
  13. buff.player = self.player
  14. self.list[buff] = buff
  15. table.merge(vars, buff, true)
  16. f.exe(buff.activate, buff)
  17. return buff
  18. end
  19. function PlayerBuffs:remove(buff)
  20. if type(buff) == 'string' then
  21. buff = self:get(buff)
  22. end
  23. if buff then
  24. f.exe(buff and buff.deactivate, buff, self.player)
  25. self.list[buff] = nil
  26. end
  27. end
  28. function PlayerBuffs:get(code)
  29. return next(table.filter(self.list, function(buff) return buff.code == code end))
  30. end
  31. function PlayerBuffs:reapply(code, vars)
  32. local buff = self:get(code)
  33. if buff then
  34. table.merge(vars, buff, true)
  35. return buff
  36. else
  37. return self:add(code, vars)
  38. end
  39. end
  40. function PlayerBuffs:buffsWithTag(tag)
  41. return table.filter(self.list, function(buff) return table.has(buff.tags, tag) end)
  42. end
  43. function PlayerBuffs:prehurt(amount, source, kind)
  44. table.each(self.list, function(buff)
  45. if buff.prehurt then
  46. amount = buff:prehurt(amount, source, kind) or amount
  47. end
  48. end)
  49. return amount
  50. end
  51. function PlayerBuffs:posthurt(amount, source, kind)
  52. table.with(self.list, 'posthurt', amount, source, kind)
  53. return amount
  54. end
  55. function PlayerBuffs:die()
  56. table.with(self.list, 'die')
  57. end