using System.Collections.Generic;
|
using UnityEngine;
|
using LitJson;
|
|
|
public class BattleManager : GameSystemManager<BattleManager>
|
{
|
public StoryBattleField storyBattleField = null;
|
|
// 同时只能有一场战斗在进行 guid, battlefield
|
protected Dictionary<string, BattleField> battleFields = new Dictionary<string, BattleField>();
|
|
protected LogicUpdate logicUpdate = new LogicUpdate();
|
|
public override void Init()
|
{
|
base.Init();
|
// StartStoryBattle();
|
logicUpdate.Start(Run);
|
DTC0403_tagPlayerLoginLoadOK.playerLoginOkEvent += OnPlayerLoginOk;
|
}
|
|
public override void Release()
|
{
|
base.Release();
|
logicUpdate.Destroy();
|
DTC0403_tagPlayerLoginLoadOK.playerLoginOkEvent -= OnPlayerLoginOk;
|
}
|
|
protected void OnPlayerLoginOk()
|
{
|
ulong exAttr1 = PlayerDatas.Instance.GetPlayerDataByType(PlayerDataType.ExAttr1);
|
ulong exAttr2 = PlayerDatas.Instance.GetPlayerDataByType(PlayerDataType.ExAttr2);
|
|
int MapID = 0;
|
int FuncLineID = (int)exAttr2;
|
|
CreateStoryBattle(MapID, FuncLineID, null, null);
|
}
|
|
// 上游戏的时候 等战斗阵容更新完毕 创建主线副本 敌方的数据可以暂时不显示 己方表现为睡觉
|
// 如果主线副本存在 那么维持当前的副本不变
|
protected void CreateStoryBattle(int MapID, int FuncLineID, JsonData extendData, List<TeamBase> blueTeamList = null)
|
{
|
if (null == storyBattleField)
|
{
|
storyBattleField = new StoryBattleField();
|
|
storyBattleField.guid = string.Empty; // 主线副本的guid为空
|
var redTeamList = new List<TeamBase>();
|
redTeamList.Add(TeamManager.Instance.GetTeam(TeamType.Story));
|
storyBattleField.Init(MapID, FuncLineID, extendData, redTeamList, blueTeamList);
|
}
|
}
|
|
public void OnBattleClose(BattleField _battleField)
|
{
|
|
}
|
|
|
#region 截断网络派发包 只收入当前包的后续 b425是主线的 非主线的包并不会走b425
|
private bool allow = true;
|
|
private Queue<GameNetPackBasic> packQueue = new Queue<GameNetPackBasic>();
|
|
public bool IsCanDistributePackage(GameNetPackBasic _package)
|
{
|
if (_package is HB425_tagSCTurnFightReportSign)
|
{
|
HB425_tagSCTurnFightReportSign pkg = _package as HB425_tagSCTurnFightReportSign;
|
|
// 0-战报片段开始;1-战报片段结束;
|
if (pkg.Sign == 0)
|
{
|
allow = false;
|
}
|
else
|
{
|
allow = true;
|
|
// 发送战报片段结束包
|
AnalysisPackQueueAndDistribute();
|
|
}
|
}
|
else
|
{
|
if (!allow)
|
{
|
packQueue.Enqueue(_package);
|
}
|
}
|
|
return allow;
|
}
|
|
protected void AnalysisPackQueueAndDistribute()
|
{
|
List<GameNetPackBasic> packQueueSnapshot = new List<GameNetPackBasic>(packQueue);
|
|
List<GameNetPackBasic> newPackList = new List<GameNetPackBasic>();
|
|
// 这里已经是按照Dequeue的顺序了
|
for (int i = 0; i < packQueueSnapshot.Count; i++)
|
{
|
GameNetPackBasic pack = packQueueSnapshot[i];
|
|
// 碰到B421 截断 往下收集b421里的全部内容
|
if (pack is HB421_tagMCTurnFightObjAction)
|
{
|
HB421_tagMCTurnFightObjAction b421Pack = pack as HB421_tagMCTurnFightObjAction;
|
|
List<GameNetPackBasic> b421PackList = new List<GameNetPackBasic>();
|
|
i++; // 跳过当前的B421包
|
|
for (; i < packQueueSnapshot.Count; i++)
|
{
|
GameNetPackBasic nextPack = packQueueSnapshot[i];
|
if (nextPack is HB421_tagMCTurnFightObjAction)
|
{
|
// 遇到了其他B421 启动角色的Action开始,
|
// B421后再碰到B421一定是有一个人的行动结束了 回退一个位置
|
i--;
|
break;
|
}
|
else
|
{
|
b421PackList.Add(nextPack);
|
}
|
}
|
|
// 可能没用了 主要就是利用一下skill的combine 暂留 看之后还有没有别的需求
|
CustomB421ActionPack actionPack = CustomB421ActionPack.CreateB421ActionPack(GetGUID(b421Pack.packUID), b421PackList);
|
|
while (actionPack.actionPacks.Count > 0)
|
{
|
GameNetPackBasic actionPackItem = actionPack.actionPacks.Dequeue();
|
newPackList.Add(actionPackItem);
|
}
|
}
|
else
|
{
|
newPackList.Add(pack);
|
}
|
}
|
|
|
// b421跟b426的包已经处理完了
|
packQueue = new Queue<GameNetPackBasic>(newPackList);
|
|
DistributeNextPackage();
|
}
|
|
public bool DistributeNextPackage()
|
{
|
if (packQueue.Count > 0)
|
{
|
GameNetPackBasic pack = packQueue.Dequeue();
|
|
if (pack is CustomHB426CombinePack)
|
{
|
CustomHB426CombinePack combinePack = pack as CustomHB426CombinePack;
|
combinePack.Distribute();
|
}
|
else
|
{
|
PackageRegedit.Distribute(pack);
|
}
|
|
return true;
|
}
|
else
|
{
|
return false;
|
}
|
}
|
|
public void OnConnected()
|
{
|
if (!allow)
|
{
|
allow = true;
|
packQueue.Clear();
|
|
// 重新发送要上一组战斗包的请求
|
// TODO YYL
|
}
|
}
|
|
#endregion
|
|
#region 战报部分
|
|
protected Dictionary<string, Queue<GameNetPackBasic>> battleReportDict = new Dictionary<string, Queue<GameNetPackBasic>>();
|
|
protected Dictionary<string, List<ulong>> battlePackRelationList = new Dictionary<string, List<ulong>>();
|
|
public void PushPackage(string guid, GameNetPackBasic vNetPack)
|
{
|
Queue<GameNetPackBasic> queue = null;
|
|
if (!battleReportDict.TryGetValue(guid, out queue))
|
{
|
queue = new Queue<GameNetPackBasic>();
|
}
|
|
queue.Enqueue(vNetPack);
|
|
List<ulong> uidList = null;
|
|
if (!battlePackRelationList.TryGetValue(guid, out uidList))
|
{
|
uidList = new List<ulong>();
|
}
|
|
uidList.Add(vNetPack.packUID);
|
}
|
|
public BattleField GetBattleField(ulong packUID)
|
{
|
return GetBattleField(GetGUID(packUID));
|
}
|
|
public BattleField GetBattleField(string guid)
|
{
|
BattleField battleField = null;
|
battleFields.TryGetValue(guid, out battleField);
|
if (battleField == null)
|
{
|
battleField = storyBattleField;
|
}
|
return battleField;
|
}
|
|
|
|
public string GetGUID(ulong packUID)
|
{
|
foreach (var kv in battlePackRelationList)
|
{
|
if (kv.Value.Contains(packUID))
|
{
|
return kv.Key;
|
}
|
}
|
return string.Empty;
|
}
|
|
|
public void DistributeNextReportPackage(string guid)
|
{
|
Queue<GameNetPackBasic> queue = null;
|
|
if (!battleReportDict.TryGetValue(guid, out queue))
|
{
|
Debug.LogError("DistributeNextReportPackage could not find queue for guid : " + guid);
|
return;
|
}
|
|
PackageRegedit.Distribute(queue.Dequeue());
|
|
if (queue.Count <= 0)
|
{
|
battleReportDict.Remove(guid);
|
battlePackRelationList.Remove(guid);
|
}
|
}
|
#endregion
|
|
public BattleField CreateBattleField(string guid, int MapID, int FuncLineID, JsonData extendData, List<TeamBase> redTeamList, List<TeamBase> blueTeamList)
|
{
|
BattleField battleField = null;
|
|
if (battleFields.TryGetValue(guid, out battleField))
|
{
|
Debug.LogError("战场已存在 先进行销毁");
|
battleField.Destroy();
|
}
|
|
if (string.IsNullOrEmpty(guid))
|
{
|
battleField = storyBattleField;
|
}
|
else
|
{
|
battleField = BattleFieldFactory.CreateBattleField(guid, MapID, FuncLineID, extendData, redTeamList, blueTeamList);
|
}
|
|
battleFields.Add(guid, battleField);
|
|
battleField.Init(MapID, FuncLineID, extendData, redTeamList, blueTeamList);
|
|
return battleField;
|
}
|
|
|
// public void OnTurnFightObjAction(battleType, vNetData.TurnNum, (int)vNetData.ObjID)
|
|
|
public void Run()
|
{
|
if (null != storyBattleField)
|
{
|
storyBattleField.Run();
|
}
|
|
foreach (var battleField in battleFields)
|
{
|
battleField.Value?.Run();
|
}
|
}
|
|
}
|