Advertisement
Cykada

Modifier Generic Barrier

Apr 8th, 2024 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.20 KB | Source Code | 0 0
  1. modifier_generic_barrier = class({})
  2. -- Created by Cykada (@oniisama on discord)
  3. -- Version 1.2
  4.  
  5. -- require("path/modifier_generic_barrier") in your modifier file
  6. -- and set it to be modifier_name = class(modifier_generic_barrier)
  7.  
  8.  
  9. --* Functions that are meant to be replaced/used when creating the barrier:
  10.  
  11. --! barrier:GetBarrierType() → DAMAGE_TYPES | mandatory
  12. --?  Determines what kind of damage it will block, based on the damage type (ie; DAMAGE_TYPE_PURE for all damage)
  13.  
  14. --! barrier:GetBarrierMaxHealth() → int | mandatory
  15. --? The max value of the barrier, current health is set to max by default
  16.  
  17. --* barrier:OnBarrierCreated(kv) → nil | optional
  18. --? The same as a normal OnCreated(kv) function,  must be used instead of OnCreated()!
  19.  
  20. --* barrier:OnBarrierRefresh(kv) → nil | optional
  21. --? The same as a normal OnRefresh(kv), OnCreated() is automatically called on refresh!
  22.  
  23. --* barrier:DeclareBonusFunctions() → {modifier_properties} | optional
  24. --? The same as a normal DeclareFunctions(), must be used instead of DeclareFunctions()!
  25.  
  26. --* barrier:AddBonusCustomTransmitterData() → {properties} | optional
  27. --? The same as a normal AddCustomTransmitterData, you don't need to add a HandleCustomTransmitterData() function!
  28.  
  29. --* barrier:OnBarrierDamagedFilter(event) → bool [Server side only] | optional
  30. --? Event for when the barrier takes damage, acts like a filter for the barrier
  31. --? The parent unit will take the damage if you return false, so set the event.damage to 0 if you don't want that
  32.  
  33. --* barrier:IsPersistent() → bool | optional
  34. --? Whether the barrier should be destroyed once the health reaches 0
  35.  
  36. --* barrier:ShowOnZeroHP() → bool | optional
  37. --? Whether the barrier should be visible when the health is at 0, also controls whether IsBarrier() is true at 0 hp
  38.  
  39. --* barrier:GetBarrierInitialHealth() → int | optional
  40. --? What the starting value of the barrier is, defaults to the max health
  41.  
  42.  
  43.  
  44. --* Function list
  45.  
  46. --* barrier:SetBarrierMaxHealth(int) → nil
  47. --* barrier:GetBarrierMaxHealth() → int
  48. --* barrier:SetBarrierHealth(int) → nil
  49. --* barrier:GetBarrierHealth() → int
  50. --* barrier:GetBarrierInitialHealth() → int
  51. --* barrier:SetBarrierType(DAMAGE_TYPES) → nil
  52. --* barrier:GetBarrierType() → DAMAGE_TYPES | use 'IsBarrierFor(DAMAGE_TYPES)' instead!
  53. --* barrier:IsBarrier() → true
  54. --* barrier:IsBarrierFor(DAMAGE_TYPES) → bool
  55. --* barrier:OnBarrierDamagedFilter(event) → bool
  56.  
  57.  
  58.  
  59. --* Options
  60.  
  61. function modifier_generic_barrier:GetBarrierOptions()
  62.    
  63.     --? All damage barriers will not block physical or magical (doesn't effect HP Removal)
  64.     self.ALL_BARRIER_ONLY_BLOCKS_PURE = false
  65.    
  66.     --? All damage barriers will block HP removal damage
  67.     self.ALL_BARRIER_BLOCKS_HP_REMOVAL = false
  68.  
  69. end
  70.  
  71.  
  72. --=======================================================================================
  73.  
  74.  
  75. function modifier_generic_barrier:OnCreated(kv)
  76.     self:GetBarrierOptions()
  77.     self:OnBarrierCreated(kv)
  78.  
  79.     if not IsServer() then return end
  80.    
  81.     if not self.__barrier_type then self.__barrier_type = self:GetBarrierType() end
  82.     if not self.__max_barrier_health then self.__max_barrier_health = self:GetBarrierMaxHealth() end
  83.    
  84.     if not self.__current_barrier_health then
  85.         self.__current_barrier_health = self:GetBarrierInitialHealth()
  86.     elseif self:GetBarrierInitialHealth() > self.__current_barrier_health then
  87.         self.__current_barrier_health = self:GetBarrierInitialHealth()
  88.     end
  89.  
  90.     self:SetHasCustomTransmitterData(true)
  91. end
  92. function modifier_generic_barrier:OnBarrierCreated(kv) end
  93.  
  94. function modifier_generic_barrier:OnRefresh(kv)
  95.     self:OnCreated(kv)
  96.     self:OnBarrierRefresh(kv)
  97.     if IsServer() then self:SendBuffRefreshToClients() end
  98. end
  99. function modifier_generic_barrier:OnBarrierRefresh(kv) end
  100.  
  101. function modifier_generic_barrier:AddCustomTransmitterData()
  102.     local data = {
  103.         __barrier_type = self.__barrier_type,
  104.         __max_barrier_health = self.__max_barrier_health,
  105.         __current_barrier_health = self.__current_barrier_health
  106.     }
  107.  
  108.     local bonus_data = self:AddBonusCustomTransmitterData()
  109.     if #bonus_data > 0 then for k,v in pairs(bonus_data) do data[k] = v end end
  110.     return data
  111. end
  112. function modifier_generic_barrier:AddBonusCustomTransmitterData() return {} end
  113. function modifier_generic_barrier:HandleCustomTransmitterData(data) for k,v in pairs(data) do self[k] = v end end
  114.  
  115. function modifier_generic_barrier:BarrierUpdate()
  116.     if not self:IsPersistent() then
  117.         if self.__current_barrier_health <= 0 then return self:Destroy() end
  118.     else self.__current_barrier_health = math.max(0, self.__current_barrier_health) end
  119.     if self ~= nil then
  120.         self.__current_barrier_health = math.min( self.__current_barrier_health, self.__max_barrier_health )
  121.         self:SendBuffRefreshToClients()
  122.     end
  123. end
  124.  
  125. function modifier_generic_barrier:SetBarrierType(dmg_type)
  126.     self.__barrier_type = dmg_type
  127.     self:BarrierUpdate()
  128. end
  129.  
  130. function modifier_generic_barrier:GetBarrierType() return DAMAGE_TYPE_ALL end
  131.  
  132. function modifier_generic_barrier:IsPersistent() return false end
  133. function modifier_generic_barrier:ShowOnZeroHP() return false end
  134.  
  135. function modifier_generic_barrier:IsBarrier()
  136.     if not self:ShowOnZeroHP() and self.__current_barrier_health <= 0 then return false else return true end
  137. end
  138.  
  139. function modifier_generic_barrier:IsBarrierFor(dmg_type)
  140.     if self:IsPersistent() then
  141.         if self.__current_barrier_health <= 0 and not self:ShowOnZeroHP() then
  142.             return false
  143.         end
  144.     end
  145.  
  146.     local b = self.__barrier_type
  147.     if b == dmg_type then return true end
  148.     if IsClient() then return false end
  149.  
  150.     if b == 4 or b == 7 or b == 8 then
  151.         if not self.ALL_BARRIER_BLOCKS_HP_REMOVAL and dmg_type == 8 then return false end
  152.         if self.ALL_BARRIER_ONLY_BLOCKS_PURE and (dmg_type == 1 or dmg_type == 2) then return false end
  153.         return true
  154.     end
  155.        
  156.     return false
  157. end
  158.  
  159. function modifier_generic_barrier:SetBarrierMaxHealth(amount)
  160.     self.__max_barrier_health = math.ceil(amount)
  161.     self:BarrierUpdate()
  162. end
  163. function modifier_generic_barrier:GetBarrierMaxHealth() return self.__max_barrier_health end
  164. function modifier_generic_barrier:GetBarrierInitialHealth() return self.__max_barrier_health end
  165.  
  166. function modifier_generic_barrier:SetBarrierHealth(amount)
  167.     self.__current_barrier_health = math.ceil(amount)
  168.     self:BarrierUpdate()
  169. end
  170. function modifier_generic_barrier:GetBarrierHealth() return self.__current_barrier_health end
  171.  
  172. function modifier_generic_barrier:DeclareFunctions()
  173.     local funcs = {
  174.         MODIFIER_PROPERTY_INCOMING_PHYSICAL_DAMAGE_CONSTANT,
  175.         MODIFIER_PROPERTY_INCOMING_SPELL_DAMAGE_CONSTANT,
  176.         MODIFIER_PROPERTY_INCOMING_DAMAGE_CONSTANT,
  177.     }
  178.  
  179.     local bonus_funcs = self:DeclareBonusFunctions()
  180.     if #bonus_funcs > 0 then for _,func in ipairs(bonus_funcs) do table.insert(funcs, func) end end
  181.     return funcs
  182. end
  183. function modifier_generic_barrier:DeclareBonusFunctions() return {} end
  184.  
  185. function modifier_generic_barrier:GetModifierIncomingPhysicalDamageConstant(keys) return self:DoBarrierBlock(1, keys) end
  186. function modifier_generic_barrier:GetModifierIncomingSpellDamageConstant(keys) return self:DoBarrierBlock(2, keys) end
  187. function modifier_generic_barrier:GetModifierIncomingDamageConstant(keys) return self:DoBarrierBlock(4, keys) end
  188.  
  189. function modifier_generic_barrier:DoBarrierBlock(d, keys)
  190.     if IsClient() then
  191.         if not self:IsBarrierFor(d) then return end
  192.         if keys.report_max then return self.__max_barrier_health else return self.__current_barrier_health end
  193.     else
  194.         if not self:IsBarrierFor(keys.damage_type) then return 0 end
  195.         if not self:OnBarrierDamagedFilter(keys) then return keys.damage end
  196.     end
  197.  
  198.     self.__current_barrier_health = self.__current_barrier_health - keys.damage
  199.     Timers:CreateTimer(function() self:BarrierUpdate() end)
  200.  
  201.     if self.__current_barrier_health <= 0 then
  202.         return keys.damage + self.__current_barrier_health
  203.     else return -keys.damage end
  204. end
  205.  
  206. function modifier_generic_barrier:OnBarrierDamagedFilter(keys) return true end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement