miduo_server/luafight/Modules/Battle/Logic/Base/Buff.lua

222 lines
7.3 KiB
Lua
Raw Normal View History

2019-03-12 14:05:45 +08:00
Buff = {}
local floor = math.floor
2019-03-21 14:33:56 +08:00
local buffPoolList = {}
local getBuff = function(type)
if not buffPoolList[type] then
buffPoolList[type] = BattleObjectPool.New(function (type)
return require("Modules/Battle/Logic/Buff/"..type):New()
end)
end
return buffPoolList[type]:Get(type)
end
local putBuff = function(buff)
if buffPoolList[buff.type] then
buffPoolList[buff.type]:Put(buff)
end
end
2019-03-12 14:05:45 +08:00
function Buff:New()
local o = {}
setmetatable(o, self)
self.__index = self
return o
end
function Buff.Create(caster, type, duration, ...)
2019-03-21 14:33:56 +08:00
local buff = getBuff(type)
buff.type = type
buff.id = BattleLogic.GenerateBuffId()
buff.caster = caster
buff.disperse = false
buff.cover = false
buff.duration = duration --持续时间为0时buff永久存在
buff.interval = -1 --间隔帧为0时每帧触发小于0不触发 默认不触发OnTrigger
buff.maxCount = 0 --限定效果最大数量
buff.framePass = 0
buff:SetData(...)
return buff
2019-03-12 14:05:45 +08:00
end
2019-03-21 14:33:56 +08:00
local queuePool = BattleObjectPool.New(function ()
return BattleQueue.New()
end)
local dicPool = BattleObjectPool.New(function ()
return BattleDictionary.New()
end)
2019-03-12 14:05:45 +08:00
BuffManager = {}
BuffManager.__index = BuffManager
2019-03-21 14:33:56 +08:00
function BuffManager.New()
local instance = {owner=0, buffQueue = queuePool:Get(), buffList = dicPool:Get()}
2019-03-12 14:05:45 +08:00
setmetatable(instance, BuffManager)
return instance
end
2019-03-21 14:33:56 +08:00
function BuffManager:Init(owner)
self.owner = owner
end
function BuffManager:Dispose(owner)
self.buffQueue:Clear()
self.buffList:Clear()
queuePool:Put(self.buffQueue)
dicPool:Put(self.buffList)
self.owner = owner
end
2019-03-12 14:05:45 +08:00
function BuffManager:AddBuff(buff)
self.buffQueue:Enqueue(buff)
if not buff.exCtrlTime then
buff.exCtrlTime = 0
end
buff.frameDuration = floor(buff.duration * (1 + buff.exCtrlTime) * BattleLogic.GameFrameRate)
--if buff.isBuff then --TODO:增益buff持续时间加成
-- local buffBocus = buff.caster:GetRoleData(RoleDataName.BuffBocus)
-- buff.frameDuration = floor(buff.duration * (1 + buffBocus + buff.exCtrlTime) * BattleLogic.GameFrameRate)
--end
--
--if buff.isDeBuff then --TODO:减益buff持续时间减免
-- local debuffReduce = self.owner:GetRoleData(RoleDataName.DebuffReduce)
-- buff.frameDuration = floor(buff.duration * (1 - debuffReduce - buff.exCtrlTime) * BattleLogic.GameFrameRate)
--end
buff.frameInterval = floor(buff.interval * BattleLogic.GameFrameRate)
end
function BuffManager:HasBuff(type, checkFunc)
local v
for i=1, self.buffList:Count() do
v = self.buffList.vList[i]
2019-03-21 14:33:56 +08:00
if v.size > 0 then
if v.buffer[1].type == type and (not checkFunc or (checkFunc and checkFunc(v.buffer[1]))) then
2019-03-12 14:05:45 +08:00
return true
end
end
end
return false
end
function BuffManager:ClearBuff(func)
for i = 1, self.buffList.size do
local k = self.buffList.kList[i]
local v = self.buffList.vList[i]
2019-03-21 14:33:56 +08:00
if v.size > 0 then
2019-03-12 14:05:45 +08:00
local removeCount = 0
local idx = 1
2019-03-21 14:33:56 +08:00
while idx <= v.size do
if func(v.buffer[idx]) then
putBuff:Put(v.buffer[idx])
v:Remove(idx)
2019-03-12 14:05:45 +08:00
removeCount = removeCount + 1
else
idx = idx + 1
end
end
if removeCount > 0 then
2019-03-21 14:33:56 +08:00
self.owner.Event:DispatchEvent(BattleEventName.BuffCountChange, k, v.size)
2019-03-12 14:05:45 +08:00
end
end
end
end
function BuffManager:GetBuffCount(id)
if self.buffList[id] then
2019-03-21 14:33:56 +08:00
return self.buffList[id].size
2019-03-12 14:05:45 +08:00
end
return 0
end
function BuffManager:Update()
for i=1, self.buffQueue.size do
local buff = self.buffQueue:Dequeue()
if not buff.caster then
--logErrorTrace("buff's caster can't be nil!")
return
end
local buffList
if not self.buffList.kvList[buff.id] then
2019-03-21 14:33:56 +08:00
buffList = BattleList.New()
2019-03-12 14:05:45 +08:00
self.buffList:Add(buff.id, buffList)
else
buffList = self.buffList.kvList[buff.id]
end
2019-03-21 14:33:56 +08:00
local count = buffList.size
2019-03-12 14:05:45 +08:00
if buff.cover and count > 0 then
2019-03-21 14:33:56 +08:00
if buffList.buffer[1]:OnCover(buff) then --判定该效果能否被覆盖
buffList.buffer[1]:OnEnd(self.owner)
self.owner.Event:DispatchEvent(BattleEventName.BuffEnd, buffList.buffer[1])
buffList.buffer[1] = buff
2019-03-12 14:05:45 +08:00
buff.target = self.owner
buff:OnStart()
self.owner.Event:DispatchEvent(BattleEventName.BuffStart, buff)
self.owner.Event:DispatchEvent(BattleEventName.BuffCover, buff)
end
else
if buff.maxCount == 0 or count < buff.maxCount then --限制相同效果的数量为0则不限制
2019-03-21 14:33:56 +08:00
buffList:Add(buff)
2019-03-12 14:05:45 +08:00
buff.target = self.owner
buff:OnStart()
self.owner.Event:DispatchEvent(BattleEventName.BuffStart, buff)
self.owner.Event:DispatchEvent(BattleEventName.BuffCountChange, buff.id, count)
end
end
end
for i = 1, self.buffList.size do
local k = self.buffList.kList[i]
local v = self.buffList.vList[i]
2019-03-21 14:33:56 +08:00
if v.size > 0 then
2019-03-12 14:05:45 +08:00
local removeCount = 0
local idx = 1
local buff
2019-03-21 14:33:56 +08:00
while idx <= v.size do
buff = v.buffer[idx]
2019-03-12 14:05:45 +08:00
--印记类型的buff不处理更新逻辑
if buff.frameDuration == 0 and buff.frameInterval < 0 then
else
buff.framePass = buff.framePass + 1
if buff.frameDuration == 0 then
if buff.frameInterval == 0 or buff.framePass % buff.frameInterval == 0 then
if not buff:OnTrigger() then
buff.disperse = true
end
buff.target.Event:DispatchEvent(BattleEventName.BuffTrigger, buff)
end
elseif buff.frameInterval < 0 then
if buff.framePass > buff.frameDuration then
buff.disperse = true
end
else
if buff.frameInterval == 0 or buff.framePass % buff.frameInterval == 0 then
if not buff:OnTrigger() then
buff.disperse = true
end
buff.target.Event:DispatchEvent(BattleEventName.BuffTrigger, buff)
end
if buff.framePass > buff.frameDuration then
buff.disperse = true
end
end
end
if buff.disperse then
buff:OnEnd()
buff.target.Event:DispatchEvent(BattleEventName.BuffEnd, buff)
2019-03-21 14:33:56 +08:00
putBuff(v.buffer[idx])
v:Remove(idx)
2019-03-12 14:05:45 +08:00
removeCount = removeCount + 1
else
idx = idx + 1
end
end
if removeCount > 0 then
2019-03-21 14:33:56 +08:00
self.owner.Event:DispatchEvent(BattleEventName.BuffCountChange, k, v.size)
2019-03-12 14:05:45 +08:00
end
end
end
end