Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- modifier_generic_barrier = class({})
- -- Created by Cykada (@oniisama on discord)
- -- Version 1.2
- -- require("path/modifier_generic_barrier") in your modifier file
- -- and set it to be modifier_name = class(modifier_generic_barrier)
- --* Functions that are meant to be replaced/used when creating the barrier:
- --! barrier:GetBarrierType() → DAMAGE_TYPES | mandatory
- --? Determines what kind of damage it will block, based on the damage type (ie; DAMAGE_TYPE_PURE for all damage)
- --! barrier:GetBarrierMaxHealth() → int | mandatory
- --? The max value of the barrier, current health is set to max by default
- --* barrier:OnBarrierCreated(kv) → nil | optional
- --? The same as a normal OnCreated(kv) function, must be used instead of OnCreated()!
- --* barrier:OnBarrierRefresh(kv) → nil | optional
- --? The same as a normal OnRefresh(kv), OnCreated() is automatically called on refresh!
- --* barrier:DeclareBonusFunctions() → {modifier_properties} | optional
- --? The same as a normal DeclareFunctions(), must be used instead of DeclareFunctions()!
- --* barrier:AddBonusCustomTransmitterData() → {properties} | optional
- --? The same as a normal AddCustomTransmitterData, you don't need to add a HandleCustomTransmitterData() function!
- --* barrier:OnBarrierDamagedFilter(event) → bool [Server side only] | optional
- --? Event for when the barrier takes damage, acts like a filter for the barrier
- --? The parent unit will take the damage if you return false, so set the event.damage to 0 if you don't want that
- --* barrier:IsPersistent() → bool | optional
- --? Whether the barrier should be destroyed once the health reaches 0
- --* barrier:ShowOnZeroHP() → bool | optional
- --? Whether the barrier should be visible when the health is at 0, also controls whether IsBarrier() is true at 0 hp
- --* barrier:GetBarrierInitialHealth() → int | optional
- --? What the starting value of the barrier is, defaults to the max health
- --* Function list
- --* barrier:SetBarrierMaxHealth(int) → nil
- --* barrier:GetBarrierMaxHealth() → int
- --* barrier:SetBarrierHealth(int) → nil
- --* barrier:GetBarrierHealth() → int
- --* barrier:GetBarrierInitialHealth() → int
- --* barrier:SetBarrierType(DAMAGE_TYPES) → nil
- --* barrier:GetBarrierType() → DAMAGE_TYPES | use 'IsBarrierFor(DAMAGE_TYPES)' instead!
- --* barrier:IsBarrier() → true
- --* barrier:IsBarrierFor(DAMAGE_TYPES) → bool
- --* barrier:OnBarrierDamagedFilter(event) → bool
- --* Options
- function modifier_generic_barrier:GetBarrierOptions()
- --? All damage barriers will not block physical or magical (doesn't effect HP Removal)
- self.ALL_BARRIER_ONLY_BLOCKS_PURE = false
- --? All damage barriers will block HP removal damage
- self.ALL_BARRIER_BLOCKS_HP_REMOVAL = false
- end
- --=======================================================================================
- function modifier_generic_barrier:OnCreated(kv)
- self:GetBarrierOptions()
- self:OnBarrierCreated(kv)
- if not IsServer() then return end
- if not self.__barrier_type then self.__barrier_type = self:GetBarrierType() end
- if not self.__max_barrier_health then self.__max_barrier_health = self:GetBarrierMaxHealth() end
- if not self.__current_barrier_health then
- self.__current_barrier_health = self:GetBarrierInitialHealth()
- elseif self:GetBarrierInitialHealth() > self.__current_barrier_health then
- self.__current_barrier_health = self:GetBarrierInitialHealth()
- end
- self:SetHasCustomTransmitterData(true)
- end
- function modifier_generic_barrier:OnBarrierCreated(kv) end
- function modifier_generic_barrier:OnRefresh(kv)
- self:OnCreated(kv)
- self:OnBarrierRefresh(kv)
- if IsServer() then self:SendBuffRefreshToClients() end
- end
- function modifier_generic_barrier:OnBarrierRefresh(kv) end
- function modifier_generic_barrier:AddCustomTransmitterData()
- local data = {
- __barrier_type = self.__barrier_type,
- __max_barrier_health = self.__max_barrier_health,
- __current_barrier_health = self.__current_barrier_health
- }
- local bonus_data = self:AddBonusCustomTransmitterData()
- if #bonus_data > 0 then for k,v in pairs(bonus_data) do data[k] = v end end
- return data
- end
- function modifier_generic_barrier:AddBonusCustomTransmitterData() return {} end
- function modifier_generic_barrier:HandleCustomTransmitterData(data) for k,v in pairs(data) do self[k] = v end end
- function modifier_generic_barrier:BarrierUpdate()
- if not self:IsPersistent() then
- if self.__current_barrier_health <= 0 then return self:Destroy() end
- else self.__current_barrier_health = math.max(0, self.__current_barrier_health) end
- if self ~= nil then
- self.__current_barrier_health = math.min( self.__current_barrier_health, self.__max_barrier_health )
- self:SendBuffRefreshToClients()
- end
- end
- function modifier_generic_barrier:SetBarrierType(dmg_type)
- self.__barrier_type = dmg_type
- self:BarrierUpdate()
- end
- function modifier_generic_barrier:GetBarrierType() return DAMAGE_TYPE_ALL end
- function modifier_generic_barrier:IsPersistent() return false end
- function modifier_generic_barrier:ShowOnZeroHP() return false end
- function modifier_generic_barrier:IsBarrier()
- if not self:ShowOnZeroHP() and self.__current_barrier_health <= 0 then return false else return true end
- end
- function modifier_generic_barrier:IsBarrierFor(dmg_type)
- if self:IsPersistent() then
- if self.__current_barrier_health <= 0 and not self:ShowOnZeroHP() then
- return false
- end
- end
- local b = self.__barrier_type
- if b == dmg_type then return true end
- if IsClient() then return false end
- if b == 4 or b == 7 or b == 8 then
- if not self.ALL_BARRIER_BLOCKS_HP_REMOVAL and dmg_type == 8 then return false end
- if self.ALL_BARRIER_ONLY_BLOCKS_PURE and (dmg_type == 1 or dmg_type == 2) then return false end
- return true
- end
- return false
- end
- function modifier_generic_barrier:SetBarrierMaxHealth(amount)
- self.__max_barrier_health = math.ceil(amount)
- self:BarrierUpdate()
- end
- function modifier_generic_barrier:GetBarrierMaxHealth() return self.__max_barrier_health end
- function modifier_generic_barrier:GetBarrierInitialHealth() return self.__max_barrier_health end
- function modifier_generic_barrier:SetBarrierHealth(amount)
- self.__current_barrier_health = math.ceil(amount)
- self:BarrierUpdate()
- end
- function modifier_generic_barrier:GetBarrierHealth() return self.__current_barrier_health end
- function modifier_generic_barrier:DeclareFunctions()
- local funcs = {
- MODIFIER_PROPERTY_INCOMING_PHYSICAL_DAMAGE_CONSTANT,
- MODIFIER_PROPERTY_INCOMING_SPELL_DAMAGE_CONSTANT,
- MODIFIER_PROPERTY_INCOMING_DAMAGE_CONSTANT,
- }
- local bonus_funcs = self:DeclareBonusFunctions()
- if #bonus_funcs > 0 then for _,func in ipairs(bonus_funcs) do table.insert(funcs, func) end end
- return funcs
- end
- function modifier_generic_barrier:DeclareBonusFunctions() return {} end
- function modifier_generic_barrier:GetModifierIncomingPhysicalDamageConstant(keys) return self:DoBarrierBlock(1, keys) end
- function modifier_generic_barrier:GetModifierIncomingSpellDamageConstant(keys) return self:DoBarrierBlock(2, keys) end
- function modifier_generic_barrier:GetModifierIncomingDamageConstant(keys) return self:DoBarrierBlock(4, keys) end
- function modifier_generic_barrier:DoBarrierBlock(d, keys)
- if IsClient() then
- if not self:IsBarrierFor(d) then return end
- if keys.report_max then return self.__max_barrier_health else return self.__current_barrier_health end
- else
- if not self:IsBarrierFor(keys.damage_type) then return 0 end
- if not self:OnBarrierDamagedFilter(keys) then return keys.damage end
- end
- self.__current_barrier_health = self.__current_barrier_health - keys.damage
- Timers:CreateTimer(function() self:BarrierUpdate() end)
- if self.__current_barrier_health <= 0 then
- return keys.damage + self.__current_barrier_health
- else return -keys.damage end
- end
- function modifier_generic_barrier:OnBarrierDamagedFilter(keys) return true end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement