359 lines
10 KiB
Lua
359 lines
10 KiB
Lua
BattleLogic = {}
|
||
|
||
local floor = math.floor
|
||
local min = math.min
|
||
-- local objList = BattleDictionary.New()
|
||
local removeObjList = BattleList.New()
|
||
|
||
local MaxRound = 20
|
||
local CurRound = 0
|
||
local CurCamp = 0
|
||
local CurSkillPos = {}
|
||
local PosList = {}
|
||
|
||
local curFrame
|
||
|
||
BattleLogic.Type = 0 --1 故事副本 2 地图探索 3 竞技场 4 秘境boss 5 解锁秘境 6 公会战 7 血战 8 兽潮 9巅峰战
|
||
BattleLogic.IsEnd = false
|
||
BattleLogic.Result = -1
|
||
BattleLogic.Event = BattleEvent.New()
|
||
BattleLogic.BuffMgr = BuffManager.New()
|
||
|
||
local _IsDebug
|
||
|
||
local fightData
|
||
local record
|
||
local optionRecord
|
||
|
||
--是否开启战斗日志
|
||
BattleLogic.IsOpenBattleRecord = false
|
||
--逻辑帧频
|
||
BattleLogic.GameFrameRate = 30
|
||
BattleLogic.GameDeltaTime = 1 / BattleLogic.GameFrameRate
|
||
--总波次
|
||
BattleLogic.TotalOrder = 0
|
||
--当前波次
|
||
BattleLogic.CurOrder = 0
|
||
|
||
|
||
local actionPool = BattleObjectPool.New(function ()
|
||
return { 0, 0 }
|
||
end)
|
||
local skillPool = BattleObjectPool.New(function ()
|
||
return Skill:New()
|
||
end)
|
||
local tbActionList = BattleList.New()
|
||
|
||
local rolePool = BattleObjectPool.New(function ()
|
||
return RoleLogic.New()
|
||
end)
|
||
|
||
function BattleLogic.Init(data, optionData, maxRound)
|
||
if BattleLogic.IsOpenBattleRecord then
|
||
record = {}
|
||
end
|
||
|
||
fightData = data
|
||
if optionData then
|
||
optionRecord = optionData
|
||
else
|
||
optionRecord = {}
|
||
end
|
||
|
||
BattleLogic.CurOrder = 0
|
||
BattleLogic.TotalOrder = #data.enemyData
|
||
|
||
BattleLogic.Clear()
|
||
|
||
curFrame = 0
|
||
|
||
CurRound = 0
|
||
MaxRound = maxRound or 20
|
||
|
||
_IsDebug = false
|
||
|
||
BattleLogic.Event:ClearEvent()
|
||
BattleLogic.BuffMgr:Init()
|
||
BattleLogic.IsEnd = false
|
||
BattleLogic.Result = -1
|
||
|
||
RoleManager.Init()
|
||
SkillManager.Init()
|
||
OutDataManager.Init(fightData)
|
||
PassiveManager.Init()
|
||
BattleLogManager.Init(fightData)
|
||
end
|
||
|
||
-- 检测先手阵营
|
||
function BattleLogic.CheckFirstCamp()
|
||
-- 默认我方先手
|
||
BattleLogic.FirstCamp = 0
|
||
-- 数据不存在时,兼容老战斗数据
|
||
if not fightData.playerData.firstCamp and not fightData.enemyData[BattleLogic.CurOrder].firstCamp then
|
||
return
|
||
end
|
||
-- 敌方先手
|
||
if fightData.playerData.firstCamp == 0 and fightData.enemyData[BattleLogic.CurOrder].firstCamp == 1 then
|
||
BattleLogic.FirstCamp = 1
|
||
end
|
||
end
|
||
|
||
function BattleLogic.StartOrder()
|
||
--
|
||
BattleLogic.CurOrder = BattleLogic.CurOrder + 1
|
||
if BattleLogic.CurOrder == 1 then
|
||
local playerData = fightData.playerData
|
||
local enemyData = fightData.enemyData[BattleLogic.CurOrder]
|
||
for i=1, #playerData do
|
||
RoleManager.AddRole(playerData[i], playerData[i].position)
|
||
end
|
||
for i=1, #enemyData do
|
||
RoleManager.AddRole(enemyData[i], enemyData[i].position)
|
||
end
|
||
|
||
else
|
||
RoleManager.ClearEnemy()
|
||
local orderList = fightData.enemyData[BattleLogic.CurOrder]
|
||
for i=1, #orderList do
|
||
RoleManager.AddRole(orderList[i], orderList[i].position)
|
||
end
|
||
end
|
||
-- 检测先后手
|
||
BattleLogic.CheckFirstCamp()
|
||
-- 开始战斗,延时一帧执行,避免战斗还没开始就释放了技能
|
||
BattleLogic.TurnRoundNextFrame()
|
||
end
|
||
|
||
-- 获取当前轮数
|
||
function BattleLogic.GetCurRound()
|
||
-- body
|
||
return CurRound, MaxRound
|
||
end
|
||
|
||
-- 获取当前轮次信息
|
||
function BattleLogic.GetCurTurn()
|
||
-- body
|
||
return CurCamp, CurSkillPos[CurCamp]
|
||
end
|
||
|
||
-- 设置是否是debug
|
||
function BattleLogic.SetIsDebug(isDebug)
|
||
_IsDebug = isDebug
|
||
end
|
||
function BattleLogic.GetIsDebug()
|
||
return _IsDebug
|
||
end
|
||
|
||
-- 下一帧开始下一轮
|
||
local _TurnRoundFlag = 0
|
||
function BattleLogic.TurnRoundNextFrame()
|
||
_TurnRoundFlag = 0
|
||
end
|
||
|
||
-- 检测是否要轮转
|
||
function BattleLogic.CheckTurnRound()
|
||
if _TurnRoundFlag == 2 then
|
||
return
|
||
end
|
||
_TurnRoundFlag = _TurnRoundFlag + 1
|
||
if _TurnRoundFlag == 2 then
|
||
BattleLogic.TurnRound()
|
||
end
|
||
end
|
||
|
||
-- 开始轮转
|
||
-- debugTurn 用于判断是否是debug轮转的参数
|
||
function BattleLogic.TurnRound(debugTurn)
|
||
if BattleLogic.GetIsDebug() and not debugTurn then
|
||
LogGreen(Language[10227])
|
||
LogBlue("-------------")
|
||
BattleLogic.Event:DispatchEvent(BattleEventName.DebugStop)
|
||
return
|
||
end
|
||
-- 第一次进入 或者 本轮结束 初始化流程状态
|
||
if CurRound == 0 or (CurSkillPos[0] == 6 and CurSkillPos[1] == 6) then
|
||
CurRound = CurRound + 1
|
||
CurCamp = BattleLogic.FirstCamp -- 判断先手阵营
|
||
CurSkillPos[0] = 0
|
||
CurSkillPos[1] = 0
|
||
LogGreen(Language[10228]..CurRound)
|
||
--
|
||
BattleLogManager.Log(
|
||
"Round Change",
|
||
"round", CurRound
|
||
)
|
||
-- 轮数变化
|
||
BattleLogic.Event:DispatchEvent(BattleEventName.BattleRoundChange, CurRound)
|
||
else
|
||
-- 切换阵营
|
||
CurCamp = (CurCamp + 1) % 2
|
||
end
|
||
|
||
LogGreen(Language[10229]..CurCamp)
|
||
--
|
||
BattleLogManager.Log(
|
||
"Camp Change",
|
||
"camp", CurCamp
|
||
)
|
||
-- 当前阵营下一释放技能的位置
|
||
local cpos = CurSkillPos[CurCamp] + 1
|
||
-- 找到下一个释放技能的人
|
||
local SkillRole
|
||
for p = cpos, 6 do
|
||
-- 保存当前位置
|
||
CurSkillPos[CurCamp] = p
|
||
-- 自己阵营中的位置 + 自己阵营的ID * 6 = 自己在PosList中的位置
|
||
local role = RoleManager.GetRole(CurCamp, p) --PosList[p + (CurCamp * 6)]
|
||
if role and not role:IsRealDead() then
|
||
SkillRole = role
|
||
break
|
||
end
|
||
|
||
-- 如果当前位置不能释放技能也需要走buff轮转
|
||
BattleLogic.BuffMgr:TurnUpdate(1) -- 计算恢复血量
|
||
BattleLogic.BuffMgr:TurnUpdate(2) -- 计算持续伤害(除去流血)
|
||
BattleLogic.BuffMgr:TurnUpdate(3) -- 计算持续伤害(流血)
|
||
BattleLogic.BuffMgr:TurnUpdate(4) -- 计算其他buff
|
||
BattleLogic.BuffMgr:PassUpdate() -- 计算buff轮数
|
||
end
|
||
-- 如果找不到下一个人,直接交换阵营
|
||
if not SkillRole then
|
||
LogGreen(Language[10230])
|
||
BattleLogManager.Log( "No Skill Position" )
|
||
BattleLogic.TurnRoundNextFrame()
|
||
return
|
||
end
|
||
|
||
LogGreen(Language[10231]..CurSkillPos[CurCamp])
|
||
--
|
||
BattleLogManager.Log(
|
||
"Position Change",
|
||
"position", CurSkillPos[CurCamp]
|
||
)
|
||
|
||
-- buff计算
|
||
BattleLogic.BuffMgr:TurnUpdate(1) -- 计算恢复血量
|
||
BattleLogic.BuffMgr:TurnUpdate(2) -- 计算持续伤害(除去流血)
|
||
|
||
-- 如果角色无法释放技能
|
||
if not SkillRole:IsAvailable() then
|
||
BattleLogic.BuffMgr:TurnUpdate(3) -- 计算持续伤害(流血)
|
||
BattleLogic.BuffMgr:TurnUpdate(4) -- 计算其他buff
|
||
BattleLogic.BuffMgr:PassUpdate() -- 计算buff轮数
|
||
BattleLogic.TurnRoundNextFrame() -- 下一个
|
||
return
|
||
end
|
||
|
||
-- 行动
|
||
SkillRole.Event:DispatchEvent(BattleEventName.RoleTurnStart, SkillRole) -- 开始行动
|
||
BattleLogic.Event:DispatchEvent(BattleEventName.RoleTurnStart, SkillRole) -- 开始行动
|
||
-- BattleLogic.BuffMgr:TurnUpdate(1) -- 计算恢复血量
|
||
-- BattleLogic.BuffMgr:TurnUpdate(2) -- 计算持续伤害(除去流血)
|
||
-- 释放技能后,递归交换阵营
|
||
SkillRole:CastSkill(function()
|
||
BattleLogic.BuffMgr:TurnUpdate(3) -- 计算持续伤害(流血)
|
||
BattleLogic.BuffMgr:TurnUpdate(4) -- 计算其他buff
|
||
BattleLogic.BuffMgr:PassUpdate() -- 计算buff轮数
|
||
SkillRole.Event:DispatchEvent(BattleEventName.RoleTurnEnd, SkillRole) -- 行动结束
|
||
BattleLogic.Event:DispatchEvent(BattleEventName.RoleTurnEnd, SkillRole) -- 开始行动
|
||
BattleLogic.TurnRoundNextFrame()
|
||
end)
|
||
end
|
||
|
||
|
||
function BattleLogic.WaitForTrigger(delayTime, action)
|
||
delayTime = BattleUtil.ErrorCorrection(delayTime)
|
||
local delayFrame = floor(delayTime * BattleLogic.GameFrameRate + 0.5)
|
||
if delayFrame == 0 then --0延迟的回调直接调用
|
||
action()
|
||
return
|
||
end
|
||
local item = actionPool:Get()
|
||
item[1] = curFrame + delayFrame
|
||
item[2] = action
|
||
tbActionList:Add(item)
|
||
end
|
||
|
||
|
||
function BattleLogic.CurFrame()
|
||
return curFrame
|
||
end
|
||
|
||
function BattleLogic.Update()
|
||
curFrame = curFrame + 1
|
||
local roleResult = RoleManager.GetResult()
|
||
if roleResult == 0 then
|
||
BattleLogic.BattleEnd(0)
|
||
return
|
||
end
|
||
if CurRound > MaxRound then
|
||
BattleLogic.BattleEnd(0)
|
||
return
|
||
end
|
||
if roleResult == 1 then
|
||
if BattleLogic.CurOrder == BattleLogic.TotalOrder then
|
||
BattleLogic.BattleEnd(1)
|
||
else
|
||
BattleLogic.Event:DispatchEvent(BattleEventName.BattleOrderChange, BattleLogic.CurOrder + 1)
|
||
BattleLogic.StartOrder()
|
||
end
|
||
end
|
||
|
||
-- 检测帧事件(技能命中,伤害计算,buff生成)
|
||
local index = 1
|
||
while index <= tbActionList.size do
|
||
local action = tbActionList.buffer[index]
|
||
if action[1] <= curFrame then
|
||
action[2]()
|
||
actionPool:Put(action)
|
||
tbActionList:Remove(index)
|
||
else
|
||
index = index + 1
|
||
end
|
||
end
|
||
-- 检测buff
|
||
BattleLogic.BuffMgr:Update()
|
||
---
|
||
-- 检测角色状态
|
||
RoleManager.Update()
|
||
|
||
-- 检测死亡
|
||
if RoleManager.CheckDead() then -- 单独用一帧执行死亡
|
||
return
|
||
end
|
||
-- 检测复活
|
||
if RoleManager.CheckRelive() then -- 单独用一帧执行复活
|
||
return
|
||
end
|
||
|
||
-- 检测技能释放
|
||
-- 检测轮转
|
||
BattleLogic.CheckTurnRound()
|
||
-- 技能
|
||
SkillManager.Update()
|
||
|
||
end
|
||
|
||
-- 战斗结束
|
||
function BattleLogic.BattleEnd(result)
|
||
--
|
||
BattleLogic.Event:DispatchEvent(BattleEventName.BeforeBattleEnd, result)
|
||
--
|
||
BattleLogic.IsEnd = true
|
||
BattleLogic.Result = result
|
||
BattleLogic.Event:DispatchEvent(BattleEventName.BattleEnd, result)
|
||
-- 战斗日志写入
|
||
if not BattleLogic.GetIsDebug() then
|
||
BattleLogManager.WriteFile()
|
||
end
|
||
end
|
||
--
|
||
function BattleLogic.Clear()
|
||
-- 清空角色
|
||
RoleManager.Clear()
|
||
-- 清空事件
|
||
while tbActionList.size > 0 do
|
||
actionPool:Put(tbActionList.buffer[tbActionList.size])
|
||
tbActionList:Remove(tbActionList.size)
|
||
end
|
||
end |