miduo_server/luafight/Modules/Battle/Logic/RoleLogic.lua

414 lines
13 KiB
Lua
Raw Normal View History

2019-03-12 14:05:45 +08:00
RoleLogic = {}
RoleLogic.__index = RoleLogic
--local RoleLogic = RoleLogic
--local RoleDataName = RoleDataName
--local BattleLogic = BattleLogic
local Random = Random
local floor = math.floor
local max = math.max
2019-06-06 17:23:18 +08:00
local min = math.min
2019-03-12 14:05:45 +08:00
2019-03-21 14:33:56 +08:00
local skillPool = BattleObjectPool.New(function ()
return Skill:New()
end)
function RoleLogic.New()
2019-10-28 12:05:04 +08:00
local instance = {uid=0,roleData=0,data=RoleData.New(),camp=0,name=0,aiIndex=1,position=0,sp=0,spPass=0,
2019-03-26 15:11:47 +08:00
shield=BattleList.New(),
exCalDmgList=BattleList.New(),
2019-05-07 20:01:07 +08:00
proTranList=BattleList.New(),
2019-03-26 15:11:47 +08:00
buffFilter=BattleList.New(),
2020-04-16 15:10:04 +08:00
Event=BattleEvent:New(),passiveList={},isDead=false,IsDebug=false}
2019-03-12 14:05:45 +08:00
setmetatable(instance, RoleLogic)
2019-03-21 14:33:56 +08:00
return instance
end
2019-10-10 11:03:42 +08:00
function RoleLogic:Init(uid, data, position)
2019-03-21 14:33:56 +08:00
self.uid = uid
2019-10-10 11:03:42 +08:00
self.position = position
2019-03-21 14:33:56 +08:00
self.roleData = data
self.data:Init(self, data.property)
2019-08-06 14:37:17 +08:00
self.isDead = self:GetRoleData(RoleDataName.Hp) <= 0
2019-03-21 14:33:56 +08:00
self.camp = data.camp --阵营 0我方 1敌方
self.name = data.name
2020-04-17 20:02:49 +08:00
self.element = data.element
2019-03-12 14:05:45 +08:00
2019-03-21 14:33:56 +08:00
self.shield:Clear() --护盾列表
self.exCalDmgList:Clear() --额外计算伤害列表
2019-03-26 15:11:47 +08:00
self.buffFilter:Clear() --buff屏蔽列表
2019-05-07 20:01:07 +08:00
self.proTranList:Clear() --属性转换列表
2019-03-12 14:05:45 +08:00
2019-03-21 14:33:56 +08:00
self.Event:ClearEvent()
2019-03-12 14:05:45 +08:00
if data.skill and #data.skill > 0 then
2019-03-21 14:33:56 +08:00
self.skill = skillPool:Get()
2019-10-10 11:03:42 +08:00
self.skill:Init(self, data.skill, 1)
2019-03-12 14:05:45 +08:00
end
if data.superSkill and #data.superSkill > 0 then
2019-03-21 14:33:56 +08:00
self.superSkill = skillPool:Get()
2019-10-10 11:03:42 +08:00
self.superSkill:Init(self, data.superSkill, 2)
2019-03-12 14:05:45 +08:00
end
2019-10-23 13:40:57 +08:00
--首次读条时间=速度/20*(等级+10
self.sp = 0
local time = self:GetRoleData(RoleDataName.Speed)/(20*(self:GetRoleData(RoleDataName.Level)+10))
self.spPass = floor( BattleUtil.ErrorCorrection(time) * BattleLogic.GameFrameRate)
2020-04-10 14:52:41 +08:00
-- 技能释放列表 技能改为按列表顺序释放 方便添加追加技能
self.SkillList = {}
self.CurSkillCastIndex = 0
self.SkillCastDoneFunc = nil
--
self.passiveList = {}
2019-05-09 17:50:38 +08:00
if data.passivity and #data.passivity > 0 then
for i = 1, #data.passivity do
local v = data.passivity[i]
local id = v[1]
local args = {}
for j = 2, #v do
args[j-1] = v[j]
end
2020-04-17 20:02:49 +08:00
if BattleUtil.Passivity[id] then
BattleUtil.Passivity[id](self, args)
-- 加入被动列表
table.insert(self.passiveList, {id, args})
else
print("被动技能"..id.."不存在")
end
2019-05-09 17:50:38 +08:00
end
2019-03-12 14:05:45 +08:00
end
2020-04-16 15:10:04 +08:00
-- 初始化怒气值默认2
self.Rage = 2 + self:GetRoleData(RoleDataName.InitRage)-- 当前怒气值
self.RageGrow = 2 -- 普通技能怒气成长
self.SuperSkillRage = 4 -- 技能需要释放的怒气值
self.NoRageRate = 0 -- 不消耗怒气值的概率
2020-04-10 14:52:41 +08:00
2019-08-05 15:58:48 +08:00
self.aiOrder = data.ai
self.aiIndex = 1
self.aiTempCount = 0
2019-03-21 14:33:56 +08:00
self.IsDebug = false
2019-06-28 11:28:45 +08:00
2020-04-10 14:52:41 +08:00
self.lockTarget = nil --嘲讽
2020-04-16 15:10:04 +08:00
self.ctrl_dizzy = false --眩晕 不能释放所有技能
self.ctrl_slient = false --沉默 只能1技能
self.ctrl_palsy = false --麻痹 只能2技能
2020-04-10 14:52:41 +08:00
self.ctrl_noheal = false --禁疗
self.ctrl_blind = false --致盲
end
-- 添加一个被动技能
2020-04-16 15:10:04 +08:00
function RoleLogic:AddPassive(id, args, isRepeat)
2020-04-10 14:52:41 +08:00
--判断是否可以叠加
2020-04-16 15:10:04 +08:00
if not isRepeat then
2020-04-10 14:52:41 +08:00
-- 不可以叠加, 如果重复则不再加入
for _, pst in ipairs(self.passiveList) do
if pst[1] == id then
return
2019-04-17 21:04:32 +08:00
end
end
end
2020-04-10 14:52:41 +08:00
-- 被动生效
BattleUtil.Passivity[id](self, args)
-- 加入被动列表
table.insert(self.passiveList, {id, args})
2019-04-17 21:04:32 +08:00
end
2020-04-10 14:52:41 +08:00
--
2019-04-17 21:04:32 +08:00
function RoleLogic:CanCastSkill()
2019-10-23 13:40:57 +08:00
return self.sp >= self.spPass and not self.IsDebug and BattleLogic.GetSkillUsable(self.camp)
2019-03-12 14:05:45 +08:00
end
2020-04-10 14:52:41 +08:00
-- 废弃的方法
2019-06-05 15:25:06 +08:00
function RoleLogic:GetSkillCD()
2019-10-23 13:40:57 +08:00
return max(self.spPass - self.sp, 0)
2019-06-05 15:25:06 +08:00
end
2020-04-10 14:52:41 +08:00
-- 废弃的方法
2019-06-06 17:23:18 +08:00
function RoleLogic:AddSkillCD(value, type)
self.Event:DispatchEvent(BattleEventName.RoleCDChanged)
if value == 0 then --为0直接清CD
2019-10-23 13:40:57 +08:00
self.sp = self.spPass
2019-06-06 17:23:18 +08:00
return
end
2019-10-23 13:40:57 +08:00
local cdTotal = self.spPass
2019-06-06 17:23:18 +08:00
local delta = 0
if type == 1 then --加算
delta = floor(value * BattleLogic.GameFrameRate)
elseif type == 2 then --乘加算(百分比属性加算)
delta = floor(value * cdTotal)
elseif type == 3 then --减算
delta = -floor(value * BattleLogic.GameFrameRate)
elseif type == 4 then --乘减算(百分比属性减算)
delta = -floor(value * cdTotal)
end
if delta > 0 then --加cd加cd最大值
2019-10-23 13:40:57 +08:00
self.spPass = self.spPass + delta
2019-06-06 17:23:18 +08:00
else --减cd减cd当前值
delta = -delta
2019-10-23 13:40:57 +08:00
self.sp = min(self.sp + delta, self.spPass)
2019-06-06 17:23:18 +08:00
end
end
2020-04-10 14:52:41 +08:00
-- 改变怒气值
function RoleLogic:AddRage(value, type)
local delta = 0
if type == 1 then --加算
delta = value
elseif type == 2 then --乘加算(百分比属性加算)
delta = floor(value * self.SuperSkillRage)
elseif type == 3 then --减算
delta = -value
elseif type == 4 then --乘减算(百分比属性减算)
delta = -floor(value * self.SuperSkillRage)
end
2020-04-16 15:10:04 +08:00
--
self.Event:DispatchEvent(BattleEventName.RoleRageChange, delta)
2020-04-10 14:52:41 +08:00
--怒气值不可为负值
self.Rage = max(self.Rage + delta, 0)
end
2019-03-12 14:05:45 +08:00
function RoleLogic:GetRoleData(property)
2019-05-07 20:01:07 +08:00
local tarPro = self.data:GetData(property)
local item
for i=1, self.proTranList.size do
item = self.proTranList.buffer[i]
if item.proName == property then
2019-09-01 12:11:18 +08:00
local value
if item.changeType == 1 then --加算
value = item.tranFactor
elseif item.changeType == 2 then --乘加算(百分比属性加算)
value = BattleUtil.ErrorCorrection(self.data:GetData(item.tranProName) * item.tranFactor)
elseif item.changeType == 3 then --减算
value = -item.tranFactor
elseif item.changeType == 4 then --乘减算(百分比属性减算)
value = -BattleUtil.ErrorCorrection(self.data:GetData(item.tranProName) * item.tranFactor)
end
tarPro = tarPro + value
2019-05-07 20:01:07 +08:00
end
end
return tarPro
end
--proA替换的属性factor系数proB被替换的属性, duration持续时间
--读取proB属性时得到的值为proB + proA * factor
2019-09-01 12:11:18 +08:00
function RoleLogic:AddPropertyTransfer(proA, factor, proB, ct, duration)
local proTran = {proName = proB, tranProName = proA, tranFactor = factor, changeType = ct}
2019-05-07 20:01:07 +08:00
self.proTranList:Add(proTran)
local index = self.proTranList.size
2020-04-16 15:10:04 +08:00
if duration then
BattleLogic.WaitForTrigger(duration, function ()
self:RemovePropertyTransfer(index, proTran)
end)
end
return index, proTran
end
-- 删除临时属性
function RoleLogic:RemovePropertyTransfer(index, tran)
if index <= self.proTranList.size and tran == self.proTranList.buffer[index] then
2019-05-07 20:01:07 +08:00
self.proTranList:Remove(index)
2020-04-16 15:10:04 +08:00
end
2019-03-12 14:05:45 +08:00
end
2020-04-16 15:10:04 +08:00
2019-03-12 14:05:45 +08:00
function RoleLogic:AddBuff(buff)
2019-04-16 14:32:11 +08:00
if self.isDead then
2019-12-31 16:35:42 +08:00
BattleLogic.BuffMgr:PutBuff(buff)
2019-04-16 14:32:11 +08:00
return
end
2019-03-26 15:11:47 +08:00
for i=1, self.buffFilter.size do
if self.buffFilter.buffer[i](buff) then
2019-12-31 16:35:42 +08:00
BattleLogic.BuffMgr:PutBuff(buff)
2019-03-26 15:11:47 +08:00
return
end
end
2019-04-16 14:32:11 +08:00
BattleLogic.BuffMgr:AddBuff(self, buff)
2019-03-12 14:05:45 +08:00
end
2019-03-21 14:33:56 +08:00
function RoleLogic:Dispose()
if self.skill then
self.skill:Dispose()
skillPool:Put(self.skill)
self.skill = nil
end
if self.superSkill then
self.superSkill:Dispose()
skillPool:Put(self.superSkill)
self.superSkill = nil
end
end
2020-04-10 14:52:41 +08:00
-- 判断角色是否可以释放技能
function RoleLogic:IsAvailable()
2020-04-16 15:10:04 +08:00
-- 眩晕 -- 死亡
if self.ctrl_dizzy or self.isDead then
return false
end
-- 沉默麻痹同时存在
if self.ctrl_palsy and self.ctrl_slient then
return false
end
-- 麻痹同时怒气不足
if self.ctrl_palsy and self.Rage < self.SuperSkillRage then
2020-04-10 14:52:41 +08:00
return false
end
return true
end
-- 检测自身的技能释放
function RoleLogic:CheckCastSkill()
-- 角色不可用直接结束技能释放
if not self:IsAvailable() then
self:SkillCastDone()
return
end
-- 没有要释放的技能了结束技能释放
if self.CurSkillCastIndex >= #self.SkillList then
self:SkillCastDone()
return
end
-- 拿到下一个技能
self.CurSkillCastIndex = self.CurSkillCastIndex + 1
local skill = self.SkillList[self.CurSkillCastIndex]
-- 技能释放回调(递归检测下一个技能)
local _CheckNext = function ()
self:CheckCastSkill()
end
2020-04-16 15:10:04 +08:00
-- 没有麻痹,释放普通攻击
if skill.type == BattleSkillType.Normal and not self.ctrl_palsy then
2020-04-10 14:52:41 +08:00
-- 释放普技
self.skill:Cast(_CheckNext, skill.targets, skill.isAdd)
-- 后成长怒气
if skill.isRage then
-- 检测被动技能对怒气成长的影响
local grow = self.RageGrow
local _RageGrowPassivity = function(finalGrow)
grow = finalGrow
end
self.Event:DispatchEvent(BattleEventName.RoleRageGrow, grow, _RageGrowPassivity)
--
self.Rage = self.Rage + grow
end
2020-04-16 15:10:04 +08:00
-- 没有沉默,释放大技能
elseif skill.type == BattleSkillType.Special and not self.ctrl_slient then
2020-04-10 14:52:41 +08:00
-- 先消耗怒气
if skill.isRage then
2020-04-16 15:10:04 +08:00
if self.Rage < self.SuperSkillRage then
-- 怒气值不足不能释放技能
_CheckNext()
return
end
2020-04-10 14:52:41 +08:00
-- 检测被动技能对怒气消耗的影响
local costRage = self.SuperSkillRage
local noRageRate = self.NoRageRate
2020-04-16 15:10:04 +08:00
local _RageCostPassivity = function(rate, cost)
noRageRate = noRageRate + rate
costRage = costRage + cost
2020-04-10 14:52:41 +08:00
end
self.Event:DispatchEvent(BattleEventName.RoleRageCost, costRage, noRageRate, _RageCostPassivity)
-- 计算消耗怒气的概率,并消耗怒气
local costRate = 1 - noRageRate
costRate = costRate > 1 and 1 or costRate
costRate = costRate < 0 and 0 or costRate
BattleUtil.RandomAction(costRate, function()
self.Rage = self.Rage - costRage
end)
end
-- 释放绝技
self.superSkill:Cast(_CheckNext, skill.targets, skill.isAdd)
-- 没有符合条件的技能直接进入下一个技能检测
2019-08-05 15:58:48 +08:00
else
2020-04-10 14:52:41 +08:00
_CheckNext()
2019-08-05 15:58:48 +08:00
end
2019-09-02 13:16:49 +08:00
2020-04-10 14:52:41 +08:00
end
-- 加入一个技能
-- type 加入的技能类型
-- targets 指定目标
-- isAdd 是否是追加技能
-- isRage 是否正常操作怒气值
function RoleLogic:AddSkill(type, isRage, isAdd, targets)
local skill = {
type = type,
isRage = isRage,
isAdd = isAdd,
targets = targets,
}
table.insert(self.SkillList, skill)
end
-- 正常触发技能
function RoleLogic:CastSkill(func)
self.SkillList = {}
self.CurSkillCastIndex = 0
self.SkillCastDoneFunc = func
2020-04-16 15:10:04 +08:00
-- 没有沉默
if not self.ctrl_slient and self.Rage >= self.SuperSkillRage then
-- 释放大技能
self:AddSkill(BattleSkillType.Special, true, false, nil)
self:CheckCastSkill()
return
end
-- 没有麻痹 释放普通技能
if not self.ctrl_palsy then
2020-04-10 14:52:41 +08:00
self:AddSkill(BattleSkillType.Normal, true, false, nil)
self:CheckCastSkill()
return
end
self:CheckCastSkill()
end
2020-04-16 15:10:04 +08:00
-- 强制释放技能,测试技能时使用
2020-04-10 14:52:41 +08:00
-- type 追加的技能类型 1=普技 2=特殊技
-- targets 追加技能的目标 nil则自动选择目标
-- func 追加技能释放完成回调
function RoleLogic:ForceCastSkill(type, targets, func)
2020-04-16 15:10:04 +08:00
self.SkillList = {}
self.CurSkillCastIndex = 0
self.SkillCastDoneFunc = func
-- 清除技能控制
self.ctrl_dizzy = false --眩晕 不能释放技能
self.ctrl_slient = false --沉默 只能1技能
self.ctrl_palsy = false --麻痹 只能2技能
-- 释放技能
2020-04-10 14:52:41 +08:00
if type == 1 and self.skill then
2020-04-16 15:10:04 +08:00
self:AddSkill(BattleSkillType.Normal, true, false, nil)
self:CheckCastSkill()
2020-04-10 14:52:41 +08:00
elseif type == 2 and self.superSkill then
2020-04-16 15:10:04 +08:00
if self.Rage < self.SuperSkillRage then
self.Rage = self.SuperSkillRage
end
self:AddSkill(BattleSkillType.Special, true, false, nil)
self:CheckCastSkill()
2019-08-05 15:58:48 +08:00
else
2020-04-10 14:52:41 +08:00
if func then func() end
2019-08-05 15:58:48 +08:00
end
2020-04-10 14:52:41 +08:00
end
2019-08-05 15:58:48 +08:00
2020-04-10 14:52:41 +08:00
-- 技能释放完毕
function RoleLogic:SkillCastDone()
BattleLogic.WaitForTrigger(1, function()
if self.SkillCastDoneFunc then
self.SkillCastDoneFunc()
2019-08-05 15:58:48 +08:00
end
2020-04-10 14:52:41 +08:00
end)
2019-08-05 15:58:48 +08:00
end
2019-03-12 14:05:45 +08:00
function RoleLogic:Update()
2020-04-10 14:52:41 +08:00
end