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

366 lines
11 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

RoleLogic = {}
RoleLogic.__index = RoleLogic
--local RoleLogic = RoleLogic
--local RoleDataName = RoleDataName
--local BattleLogic = BattleLogic
local Random = Random
local floor = math.floor
local max = math.max
local min = math.min
local skillPool = BattleObjectPool.New(function ()
return Skill:New()
end)
function RoleLogic.New()
local instance = {uid=0,roleData=0,data=RoleData.New(),camp=0,name=0,aiIndex=1,position=0,sp=0,spPass=0,
shield=BattleList.New(),
exCalDmgList=BattleList.New(),
proTranList=BattleList.New(),
buffFilter=BattleList.New(),
Event=BattleEvent:New(),passiveList={},isDead=false,IsDebug=false,enable=false}
setmetatable(instance, RoleLogic)
return instance
end
function RoleLogic:Init(uid, data, position)
self.uid = uid
self.position = position
self.roleData = data
self.data:Init(self, data.property)
self.isDead = self:GetRoleData(RoleDataName.Hp) <= 0
self.camp = data.camp --阵营 0我方 1敌方
self.name = data.name
self.shield:Clear() --护盾列表
self.exCalDmgList:Clear() --额外计算伤害列表
self.buffFilter:Clear() --buff屏蔽列表
self.proTranList:Clear() --属性转换列表
self.Event:ClearEvent()
if data.skill and #data.skill > 0 then
self.skill = skillPool:Get()
self.skill:Init(self, data.skill, 1)
end
if data.superSkill and #data.superSkill > 0 then
self.superSkill = skillPool:Get()
self.superSkill:Init(self, data.superSkill, 2)
end
--首次读条时间=速度/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)
-- 怒气值
self.Rage = 4 -- 当前怒气值
self.RageGrow = 2 -- 普通技能怒气成长
self.SuperSkillRage = 4 -- 技能需要释放的怒气值
self.NoRageRate = 0 -- 不消耗怒气值的概率
-- 技能释放列表 技能改为按列表顺序释放 方便添加追加技能
self.SkillList = {}
self.CurSkillCastIndex = 0
self.SkillCastDoneFunc = nil
--
self.passiveList = {}
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
BattleUtil.Passivity[id](self, args)
-- 加入被动列表
table.insert(self.passiveList, {id, args})
end
end
self.aiOrder = data.ai
self.aiIndex = 1
self.aiTempCount = 0
self.IsDebug = false
self.enable = true --眩晕
self.ctrl_slient = false --沉默
self.lockTarget = nil --嘲讽
self.ctrl_noheal = false --禁疗
self.ctrl_blind = false --致盲
self.ctrl_palsy = false --麻痹
end
-- 添加一个被动技能
function RoleLogic:AddPassive(id, args, isCover)
--判断是否可以叠加
if not isCover then
-- 不可以叠加, 如果重复则不再加入
for _, pst in ipairs(self.passiveList) do
if pst[1] == id then
return
end
end
end
-- 被动生效
BattleUtil.Passivity[id](self, args)
-- 加入被动列表
table.insert(self.passiveList, {id, args})
end
--
function RoleLogic:CanCastSkill()
return self.sp >= self.spPass and not self.IsDebug and BattleLogic.GetSkillUsable(self.camp)
end
-- 废弃的方法
function RoleLogic:GetSkillCD()
return max(self.spPass - self.sp, 0)
end
-- 废弃的方法
function RoleLogic:AddSkillCD(value, type)
self.Event:DispatchEvent(BattleEventName.RoleCDChanged)
if value == 0 then --为0直接清CD
self.sp = self.spPass
return
end
local cdTotal = self.spPass
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最大值
self.spPass = self.spPass + delta
else --减cd减cd当前值
delta = -delta
self.sp = min(self.sp + delta, self.spPass)
end
end
-- 改变怒气值
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
--怒气值不可为负值
self.Rage = max(self.Rage + delta, 0)
end
function RoleLogic:GetRoleData(property)
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
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
end
end
return tarPro
end
--proA替换的属性factor系数proB被替换的属性, duration持续时间
--读取proB属性时得到的值为proB + proA * factor
function RoleLogic:AddPropertyTransfer(proA, factor, proB, ct, duration)
local proTran = {proName = proB, tranProName = proA, tranFactor = factor, changeType = ct}
self.proTranList:Add(proTran)
local index = self.proTranList.size
BattleLogic.WaitForTrigger(duration, function ()
self.proTranList:Remove(index)
end)
end
function RoleLogic:AddBuff(buff)
if self.isDead then
BattleLogic.BuffMgr:PutBuff(buff)
return
end
for i=1, self.buffFilter.size do
if self.buffFilter.buffer[i](buff) then
BattleLogic.BuffMgr:PutBuff(buff)
return
end
end
BattleLogic.BuffMgr:AddBuff(self, buff)
end
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
-- 判断角色是否可以释放技能
function RoleLogic:IsAvailable()
-- 眩晕 -- 麻痹 -- 死亡
if not self.enable or self.ctrl_palsy or self.isDead then
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
-- 释放普通攻击
if skill.type == BattleSkillType.Normal and self.skill then
-- 释放普技
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
-- 释放大技能
elseif skill.type == BattleSkillType.Special and self.superSkill then
-- 先消耗怒气
if skill.isRage then
-- 检测被动技能对怒气消耗的影响
local costRage = self.SuperSkillRage
local noRageRate = self.NoRageRate
local _RageCostPassivity = function(finalRate, finalCost)
noRageRate = finalRate
costRage = finalCost
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)
-- 没有符合条件的技能直接进入下一个技能检测
else
_CheckNext()
end
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
-- 沉默 怒气值不足 没有绝技 释放普通技能
if self.ctrl_slient or self.Rage < self.SuperSkillRage or not self.superSkill then
self:AddSkill(BattleSkillType.Normal, true, false, nil)
self:CheckCastSkill()
return
end
-- 释放大技能 消耗怒气值
self:AddSkill(BattleSkillType.Special, true, false, nil)
self:CheckCastSkill()
end
-- 强制释放技能,追加技能时使用
-- type 追加的技能类型 1=普技 2=特殊技
-- targets 追加技能的目标 nil则自动选择目标
-- func 追加技能释放完成回调
function RoleLogic:ForceCastSkill(type, targets, func)
if type == 1 and self.skill then
self.skill:Cast(func)
elseif type == 2 and self.superSkill then
self.superSkill:Cast(func)
else
if func then func() end
end
end
-- 技能释放完毕
function RoleLogic:SkillCastDone()
BattleLogic.WaitForTrigger(1, function()
if self.SkillCastDoneFunc then
self.SkillCastDoneFunc()
end
end)
end
function RoleLogic:Update()
if not self.enable then
return
end
-- self.updateFunc()
end