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-04-16 14:32:11 +08:00
|
|
|
|
local buffListPool = BattleObjectPool.New(function ()
|
|
|
|
|
return BattleList.New()
|
|
|
|
|
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
|
2019-10-23 13:40:57 +08:00
|
|
|
|
buff.disperse = false --该值为true时,buff在下一帧被清除
|
|
|
|
|
buff.cover = false --该值为true时,同类型新buff会覆盖旧buff
|
|
|
|
|
buff.clear = true --该值为false时,buff不可被驱散,除非无条件驱散
|
2019-03-21 14:33:56 +08:00
|
|
|
|
|
|
|
|
|
buff.duration = duration --持续时间为0时buff永久存在
|
2020-04-10 14:52:41 +08:00
|
|
|
|
buff.interval = -1 --间隔帧为0时每回合触发,小于0不触发 默认不触发OnTrigger
|
|
|
|
|
-- buff.framePass = 0
|
|
|
|
|
|
|
|
|
|
buff.roundPass = 0
|
|
|
|
|
|
2019-03-21 14:33:56 +08:00
|
|
|
|
|
|
|
|
|
buff:SetData(...)
|
|
|
|
|
return buff
|
2019-03-12 14:05:45 +08:00
|
|
|
|
end
|
|
|
|
|
|
2020-04-10 14:52:41 +08:00
|
|
|
|
-- 改变buff的轮次
|
|
|
|
|
function Buff:ChangeBuffDuration(type, value)
|
|
|
|
|
-- 永久存在改变无效
|
|
|
|
|
if self.duration <= 0 then
|
|
|
|
|
return
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- 计算
|
|
|
|
|
local finalDuration = BattleUtil.ErrorCorrection(BattleUtil.CountValue(self.duration, value, type))
|
|
|
|
|
self.duration = finalDuration
|
|
|
|
|
if self.roundDuration then
|
|
|
|
|
self.roundDuration = finalDuration
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
-- 发送事件
|
|
|
|
|
self.caster.Event:DispatchEvent(BattleEventName.BuffDurationChange, self)
|
|
|
|
|
end
|
|
|
|
|
|
2019-03-12 14:05:45 +08:00
|
|
|
|
BuffManager = {}
|
|
|
|
|
BuffManager.__index = BuffManager
|
|
|
|
|
|
2019-03-21 14:33:56 +08:00
|
|
|
|
function BuffManager.New()
|
2019-12-31 16:35:42 +08:00
|
|
|
|
local instance = {owner=0, buffQueue = BattleQueue.New(), buffDic = BattleDictionary.New()}
|
2019-03-12 14:05:45 +08:00
|
|
|
|
setmetatable(instance, BuffManager)
|
|
|
|
|
return instance
|
|
|
|
|
end
|
|
|
|
|
|
2020-04-10 14:52:41 +08:00
|
|
|
|
-- 根据类型获取buff的刷新级别
|
|
|
|
|
-- local _TypeToLevel = {
|
|
|
|
|
-- [] = ,
|
|
|
|
|
-- }
|
|
|
|
|
function BuffManager.GetLevelByType(type)
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
2019-04-16 14:32:11 +08:00
|
|
|
|
function BuffManager:Init()
|
2019-04-17 21:04:32 +08:00
|
|
|
|
while self.buffQueue.size > 0 do
|
2019-06-28 19:36:10 +08:00
|
|
|
|
putBuff(self.buffQueue:Dequeue())
|
2019-04-16 14:32:11 +08:00
|
|
|
|
end
|
2019-12-31 16:35:42 +08:00
|
|
|
|
for i = 1, self.buffDic.size do
|
|
|
|
|
local list = self.buffDic.vList[i]
|
2019-04-16 14:32:11 +08:00
|
|
|
|
for j=1, list.size do
|
|
|
|
|
putBuff(list.buffer[j])
|
|
|
|
|
end
|
|
|
|
|
list:Clear()
|
|
|
|
|
buffListPool:Put(list)
|
|
|
|
|
end
|
2019-12-31 16:35:42 +08:00
|
|
|
|
self.buffDic:Clear()
|
2019-03-21 14:33:56 +08:00
|
|
|
|
end
|
|
|
|
|
|
2019-04-16 14:32:11 +08:00
|
|
|
|
function BuffManager:AddBuff(target, buff)
|
|
|
|
|
buff.target = target
|
2019-03-12 14:05:45 +08:00
|
|
|
|
self.buffQueue:Enqueue(buff)
|
2020-04-10 14:52:41 +08:00
|
|
|
|
-- buff.frameDuration = floor(buff.duration * BattleLogic.GameFrameRate)
|
2019-03-12 14:05:45 +08:00
|
|
|
|
--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
|
|
|
|
|
|
2020-04-10 14:52:41 +08:00
|
|
|
|
-- buff.frameInterval = floor(buff.interval * BattleLogic.GameFrameRate)
|
|
|
|
|
|
|
|
|
|
buff.roundDuration = buff.duration
|
|
|
|
|
buff.roundInterval = buff.interval
|
2019-05-13 09:51:36 +08:00
|
|
|
|
buff.caster.Event:DispatchEvent(BattleEventName.BuffCaster, buff)
|
2019-03-12 14:05:45 +08:00
|
|
|
|
end
|
|
|
|
|
|
2019-10-23 13:40:57 +08:00
|
|
|
|
function BuffManager:QueryBuff(target, checkFunc)
|
2019-12-31 16:35:42 +08:00
|
|
|
|
for i=1, self.buffDic.size do
|
|
|
|
|
local list = self.buffDic.vList[i]
|
2019-10-23 13:40:57 +08:00
|
|
|
|
for j=1, list.size do
|
|
|
|
|
local buff = list.buffer[j]
|
|
|
|
|
if buff.target == target then
|
|
|
|
|
if checkFunc then
|
|
|
|
|
checkFunc(buff)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-04-16 14:32:11 +08:00
|
|
|
|
function BuffManager:HasBuff(target, type, checkFunc)
|
2019-12-31 16:35:42 +08:00
|
|
|
|
if self.buffDic.kvList[type] then
|
|
|
|
|
local buffList = self.buffDic.kvList[type]
|
2019-04-17 21:04:32 +08:00
|
|
|
|
for i=1, buffList.size do
|
|
|
|
|
local v = buffList.buffer[i]
|
|
|
|
|
if v.target == target then
|
|
|
|
|
if (not checkFunc or (checkFunc and checkFunc(v))) then
|
|
|
|
|
return true
|
|
|
|
|
end
|
2019-03-12 14:05:45 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return false
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-23 13:40:57 +08:00
|
|
|
|
--func为nil时无条件清除,否则判定clear值,执行func
|
2019-04-16 14:32:11 +08:00
|
|
|
|
function BuffManager:ClearBuff(target, func)
|
2019-12-31 16:35:42 +08:00
|
|
|
|
for i = 1, self.buffDic.size do
|
|
|
|
|
local list = self.buffDic.vList[i]
|
2019-04-16 14:32:11 +08:00
|
|
|
|
if list.size > 0 then
|
2019-04-17 21:04:32 +08:00
|
|
|
|
local idx = 1
|
2019-04-16 14:32:11 +08:00
|
|
|
|
while idx <= list.size do
|
2019-04-17 21:04:32 +08:00
|
|
|
|
local buff = list.buffer[idx]
|
2019-10-23 13:40:57 +08:00
|
|
|
|
if buff.target == target and (not func or (func and buff.clear and func(buff))) then
|
2019-04-16 14:32:11 +08:00
|
|
|
|
buff:OnEnd()
|
|
|
|
|
buff.target.Event:DispatchEvent(BattleEventName.BuffEnd, buff)
|
|
|
|
|
putBuff(buff)
|
|
|
|
|
list:Remove(idx)
|
2019-03-12 14:05:45 +08:00
|
|
|
|
else
|
|
|
|
|
idx = idx + 1
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-23 13:40:57 +08:00
|
|
|
|
function BuffManager:PutBuff(buff)
|
|
|
|
|
putBuff(buff)
|
|
|
|
|
end
|
|
|
|
|
|
2019-04-17 21:04:32 +08:00
|
|
|
|
function BuffManager:GetBuffCount(type)
|
2019-12-31 16:35:42 +08:00
|
|
|
|
if self.buffDic[type] then
|
|
|
|
|
return self.buffDic[type].size
|
2019-03-12 14:05:45 +08:00
|
|
|
|
end
|
|
|
|
|
return 0
|
|
|
|
|
end
|
|
|
|
|
|
2020-04-10 14:52:41 +08:00
|
|
|
|
-- 每帧刷新
|
2019-03-12 14:05:45 +08:00
|
|
|
|
function BuffManager:Update()
|
2020-04-10 14:52:41 +08:00
|
|
|
|
-- 检测上一帧生成的buff,加入管理
|
2019-04-17 21:04:32 +08:00
|
|
|
|
while self.buffQueue.size > 0 do
|
2019-03-12 14:05:45 +08:00
|
|
|
|
local buff = self.buffQueue:Dequeue()
|
|
|
|
|
local buffList
|
2019-12-31 16:35:42 +08:00
|
|
|
|
if not self.buffDic.kvList[buff.type] then
|
2019-04-16 14:32:11 +08:00
|
|
|
|
buffList = buffListPool:Get()
|
2019-12-31 16:35:42 +08:00
|
|
|
|
self.buffDic:Add(buff.type, buffList)
|
2019-03-12 14:05:45 +08:00
|
|
|
|
else
|
2019-12-31 16:35:42 +08:00
|
|
|
|
buffList = self.buffDic.kvList[buff.type]
|
2019-03-12 14:05:45 +08:00
|
|
|
|
end
|
2020-04-10 14:52:41 +08:00
|
|
|
|
|
|
|
|
|
-- 是覆盖类型的buff且有老buff
|
2019-04-01 19:49:18 +08:00
|
|
|
|
if buff.cover and buffList.size > 0 then
|
2019-04-17 21:04:32 +08:00
|
|
|
|
local isCovered = false
|
|
|
|
|
for i=1, buffList.size do
|
|
|
|
|
local oldBuff = buffList.buffer[i]
|
2020-01-14 16:16:09 +08:00
|
|
|
|
if oldBuff.cover and oldBuff.target == buff.target and oldBuff.clear == buff.clear and oldBuff:OnCover(buff) then --判定该效果能否被覆盖
|
2020-04-10 14:52:41 +08:00
|
|
|
|
-- 结束并回收老buff
|
2019-04-17 21:04:32 +08:00
|
|
|
|
oldBuff:OnEnd()
|
2019-04-18 13:25:01 +08:00
|
|
|
|
oldBuff.target.Event:DispatchEvent(BattleEventName.BuffEnd, oldBuff)
|
|
|
|
|
putBuff(oldBuff)
|
2020-04-10 14:52:41 +08:00
|
|
|
|
-- 覆盖老buff
|
2019-04-17 21:04:32 +08:00
|
|
|
|
buffList.buffer[i] = buff
|
|
|
|
|
buff:OnStart()
|
|
|
|
|
buff.target.Event:DispatchEvent(BattleEventName.BuffStart, buff)
|
|
|
|
|
buff.target.Event:DispatchEvent(BattleEventName.BuffCover, buff)
|
|
|
|
|
isCovered = true
|
|
|
|
|
break
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-04-10 14:52:41 +08:00
|
|
|
|
-- 没有覆盖,新buff
|
2019-05-07 20:01:07 +08:00
|
|
|
|
if not isCovered then
|
2019-04-17 21:04:32 +08:00
|
|
|
|
buffList:Add(buff)
|
2019-03-12 14:05:45 +08:00
|
|
|
|
buff:OnStart()
|
2019-04-16 14:32:11 +08:00
|
|
|
|
buff.target.Event:DispatchEvent(BattleEventName.BuffStart, buff)
|
2019-03-12 14:05:45 +08:00
|
|
|
|
end
|
|
|
|
|
else
|
2020-04-10 14:52:41 +08:00
|
|
|
|
-- 新buff
|
2019-05-07 20:01:07 +08:00
|
|
|
|
buffList:Add(buff)
|
|
|
|
|
buff:OnStart()
|
|
|
|
|
buff.target.Event:DispatchEvent(BattleEventName.BuffStart, buff)
|
2019-03-12 14:05:45 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
2020-04-10 14:52:41 +08:00
|
|
|
|
|
|
|
|
|
-- 清除过期buff
|
2019-12-31 16:35:42 +08:00
|
|
|
|
for i=1, self.buffDic.size do
|
|
|
|
|
local buffList = self.buffDic.vList[i]
|
2019-04-17 21:04:32 +08:00
|
|
|
|
if buffList.size > 0 then
|
|
|
|
|
local index = 1
|
|
|
|
|
while index <= buffList.size do
|
|
|
|
|
local buff = buffList.buffer[index]
|
2019-03-12 14:05:45 +08:00
|
|
|
|
if buff.disperse then
|
|
|
|
|
buff:OnEnd()
|
|
|
|
|
buff.target.Event:DispatchEvent(BattleEventName.BuffEnd, buff)
|
2019-04-16 14:32:11 +08:00
|
|
|
|
putBuff(buff)
|
2019-04-17 21:04:32 +08:00
|
|
|
|
buffList:Remove(index)
|
2019-03-12 14:05:45 +08:00
|
|
|
|
else
|
2019-04-17 21:04:32 +08:00
|
|
|
|
index = index + 1
|
2019-03-12 14:05:45 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
2020-04-10 14:52:41 +08:00
|
|
|
|
|
|
|
|
|
-- 每一个人的轮次都刷新
|
|
|
|
|
function BuffManager:TurnUpdate(sort)
|
|
|
|
|
for i=1, self.buffDic.size do
|
|
|
|
|
local buffList = self.buffDic.vList[i]
|
|
|
|
|
if buffList.size > 0 then
|
|
|
|
|
local index = 1
|
|
|
|
|
while index <= buffList.size do
|
|
|
|
|
local buff = buffList.buffer[index]
|
|
|
|
|
local curCamp, curPos = BattleLogic.GetCurTurn()
|
|
|
|
|
if buff.sort == sort -- 当前buff刷新等级
|
|
|
|
|
and buff.target.camp == curCamp -- 当前阵营
|
|
|
|
|
and buff.target.position == curPos then -- 当前位置
|
|
|
|
|
--印记类型的buff不处理更新逻辑
|
|
|
|
|
if buff.roundDuration == 0 and buff.roundInterval < 0 then
|
|
|
|
|
else
|
|
|
|
|
buff.roundPass = buff.roundPass + 1
|
|
|
|
|
if buff.roundDuration == 0 then
|
|
|
|
|
if buff.roundInterval == 0 or buff.roundPass % buff.roundInterval == 0 then
|
|
|
|
|
if not buff:OnTrigger() then
|
|
|
|
|
buff.disperse = true
|
|
|
|
|
end
|
|
|
|
|
buff.target.Event:DispatchEvent(BattleEventName.BuffTrigger, buff)
|
|
|
|
|
end
|
|
|
|
|
elseif buff.roundInterval < 0 then
|
2020-04-16 15:10:04 +08:00
|
|
|
|
if buff.roundPass >= buff.roundDuration then
|
2020-04-10 14:52:41 +08:00
|
|
|
|
buff.disperse = true
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
if buff.roundInterval == 0 or buff.roundPass % buff.roundInterval == 0 then
|
|
|
|
|
if not buff:OnTrigger() then
|
|
|
|
|
buff.disperse = true
|
|
|
|
|
end
|
|
|
|
|
buff.target.Event:DispatchEvent(BattleEventName.BuffTrigger, buff)
|
|
|
|
|
end
|
2020-04-16 15:10:04 +08:00
|
|
|
|
if buff.roundPass >= buff.roundDuration then
|
2020-04-10 14:52:41 +08:00
|
|
|
|
buff.disperse = true
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
buff.target.Event:DispatchEvent(BattleEventName.BuffRoundChange, buff)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
index = index + 1
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|