392 lines
11 KiB
Lua
392 lines
11 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()
|
|||
|
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.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("战斗调试停止")
|
|||
|
LogBlue("-------------")
|
|||
|
BattleLogic.Event:DispatchEvent(BattleEventName.DebugStop)
|
|||
|
return
|
|||
|
end
|
|||
|
-- 第一次进入 或者 本轮结束 初始化流程状态
|
|||
|
if CurRound == 0 or (CurSkillPos[0] == 6 and CurSkillPos[1] == 6) then
|
|||
|
CurRound = CurRound + 1
|
|||
|
CurCamp = 0
|
|||
|
CurSkillPos[0] = 0
|
|||
|
CurSkillPos[1] = 0
|
|||
|
LogGreen("轮数变化 CurRound = "..CurRound)
|
|||
|
-- 轮数变化
|
|||
|
BattleLogic.Event:DispatchEvent(BattleEventName.BattleRoundChange, CurRound)
|
|||
|
else
|
|||
|
-- 切换阵营
|
|||
|
CurCamp = (CurCamp + 1) % 2
|
|||
|
end
|
|||
|
|
|||
|
LogGreen("阵营变化 CurCamp = "..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("未找到释放技能位置")
|
|||
|
BattleLogic.TurnRoundNextFrame()
|
|||
|
return
|
|||
|
end
|
|||
|
|
|||
|
LogGreen("释放技能位置 CurPos = "..CurSkillPos[CurCamp])
|
|||
|
|
|||
|
-- 如果角色无法释放技能
|
|||
|
if not SkillRole:IsAvailable() then
|
|||
|
BattleLogic.BuffMgr:TurnUpdate(1) -- 计算恢复血量
|
|||
|
BattleLogic.BuffMgr:TurnUpdate(2) -- 计算持续伤害(除去流血)
|
|||
|
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)
|
|||
|
if BattleLogic.IsOpenBattleRecord then
|
|||
|
BattleLogic.RecordDelayTrigger(delayTime, curFrame + delayFrame)
|
|||
|
end
|
|||
|
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.IsEnd = true
|
|||
|
BattleLogic.Result = result
|
|||
|
BattleLogic.Event:DispatchEvent(BattleEventName.BattleEnd, result)
|
|||
|
end
|
|||
|
--
|
|||
|
function BattleLogic.Clear()
|
|||
|
-- 清空角色
|
|||
|
RoleManager.Clear()
|
|||
|
-- 清空事件
|
|||
|
while tbActionList.size > 0 do
|
|||
|
actionPool:Put(tbActionList.buffer[tbActionList.size])
|
|||
|
tbActionList:Remove(tbActionList.size)
|
|||
|
end
|
|||
|
end
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
-------++++++++++++++++++++++++++++ 战斗记录相关
|
|||
|
--Record专用
|
|||
|
function BattleLogic.GetRecord()
|
|||
|
return record
|
|||
|
end
|
|||
|
|
|||
|
--Record专用
|
|||
|
function BattleLogic.RecordSeed(seed)
|
|||
|
table.insert(record, string.format("frame:%d, seed:%d", curFrame, seed))
|
|||
|
end
|
|||
|
|
|||
|
--Record专用
|
|||
|
function BattleLogic.RecordRoleProperty(uid, camp, name, value, delta)
|
|||
|
table.insert(record, string.format("frame:%d, uid:%d, camp:%d, name:%s, value:%d, delta:%d", curFrame, uid, camp, name, value, delta))
|
|||
|
end
|
|||
|
|
|||
|
--Record专用
|
|||
|
function BattleLogic.RecordMul(args)
|
|||
|
local str = ""
|
|||
|
for i=1, #args do
|
|||
|
str = str..tostring(args[i]) .. (i == #args and "" or "#")
|
|||
|
end
|
|||
|
table.insert(record, string.format("frame:%d, mulArgs:%s", curFrame, str))
|
|||
|
end
|
|||
|
|
|||
|
--Record专用
|
|||
|
function BattleLogic.RecordEffect(caster, target, type, args, interval)
|
|||
|
local cUid = tostring(caster.uid) or "cTeam"
|
|||
|
local tUid = tostring(target.uid) or "tTeam"
|
|||
|
local str = ""
|
|||
|
for i=1, #args do
|
|||
|
str = str..tostring(args[i]) .. (i == #args and "" or "#")
|
|||
|
end
|
|||
|
table.insert(record, string.format("frame:%d, caster:%s, target:%s, effectType:%d, args:%s, interval:%d", curFrame, cUid, tUid, type, str, interval))
|
|||
|
end
|
|||
|
|
|||
|
--Record专用
|
|||
|
function BattleLogic.RecordDelayTrigger(delayFrame, triggerFrame)
|
|||
|
table.insert(record, string.format("frame:%d, delayTime:%f, triggerFrame:%d", curFrame, delayFrame, triggerFrame))
|
|||
|
end
|
|||
|
|
|||
|
--Record专用
|
|||
|
function BattleLogic.GenerateRecordFile()
|
|||
|
local time = string.format("%d-%d-%d-%d-%d-%d",
|
|||
|
os.date("%Y"),
|
|||
|
os.date("%m"),
|
|||
|
os.date("%d"),
|
|||
|
os.date("%H"),
|
|||
|
os.date("%M"),
|
|||
|
os.date("%S"))
|
|||
|
local file = io.open("BattleRecord/"..time..".txt", "a")
|
|||
|
for i=1, #record do
|
|||
|
file:write(record[i].."\n")
|
|||
|
end
|
|||
|
io.close(file)
|
|||
|
end
|