|  |  |  | 
|---|
|  |  |  | using System.Collections.Generic; | 
|---|
|  |  |  | using System.Linq; | 
|---|
|  |  |  | using UnityEngine; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | using System; | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public class TeamManager : GameSystemManager<TeamManager> | 
|---|
|  |  |  | { | 
|---|
|  |  |  | protected Dictionary<TeamType, TeamBase> teamDict = new Dictionary<TeamType, TeamBase>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public Action<TeamType> OnTeamChange = null; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public override void Init() | 
|---|
|  |  |  | { | 
|---|
|  |  |  | base.Init(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | HeroManager.Instance.onHeroChangeEvent += onHeroChangeEvent; | 
|---|
|  |  |  | HeroManager.Instance.onHeroDeleteEvent += onHeroDeleteEvent; | 
|---|
|  |  |  | DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent += OnBeforePlayerDataInitialize; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public override void Release() | 
|---|
|  |  |  | { | 
|---|
|  |  |  | base.Release(); | 
|---|
|  |  |  | HeroManager.Instance.onHeroChangeEvent += onHeroChangeEvent; | 
|---|
|  |  |  | HeroManager.Instance.onHeroDeleteEvent += onHeroDeleteEvent; | 
|---|
|  |  |  | DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent += OnBeforePlayerDataInitialize; | 
|---|
|  |  |  | DTC0102_tagCDBPlayer.beforePlayerDataInitializeEvent -= OnBeforePlayerDataInitialize; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | protected void OnBeforePlayerDataInitialize() | 
|---|
|  |  |  | 
|---|
|  |  |  | teamDict.Clear(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | protected void onHeroChangeEvent(HeroInfo heroInfo) | 
|---|
|  |  |  | public void OnHeroChangeEvent(HB124_tagSCLineupInfo vNetData) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //  英雄当前所有在的队伍 | 
|---|
|  |  |  | List<int> heroTeams = heroInfo.itemHero.GetUseData(81); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //  当前英雄所在的队伍信息 <队伍类型, <队形, 位置>> | 
|---|
|  |  |  | Dictionary<TeamType, KeyValuePair<int, int>> teamTypeShapeTypePositionDict = new Dictionary<TeamType, KeyValuePair<int, int>>(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //  处理当前记录在英雄信息里的队伍信息 | 
|---|
|  |  |  | if (null != heroTeams) | 
|---|
|  |  |  | var heroPack = PackManager.Instance.GetSinglePack(PackType.Hero); | 
|---|
|  |  |  | HashSet<TeamType> teamTypeSet = new HashSet<TeamType>(); | 
|---|
|  |  |  | for (int i = 0; i < vNetData.LineupCnt; i++) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | foreach (var teamMsg in heroTeams) | 
|---|
|  |  |  | TeamType teamType = (TeamType)vNetData.LineupList[i].LineupID; | 
|---|
|  |  |  | teamTypeSet.Add(teamType); | 
|---|
|  |  |  | var team = GetTeam(teamType); | 
|---|
|  |  |  | for (int j = 0; j < vNetData.LineupList[i].HeroCnt; j++) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | // 所在阵容信息列表 [阵容类型*10000+阵型类型*100+位置编号, ...] | 
|---|
|  |  |  | int teamType = teamMsg / 10000; | 
|---|
|  |  |  | int shapeType = (teamMsg % 10000) / 100; | 
|---|
|  |  |  | int positionIndex = teamMsg % 100 - 1;   //布阵位置:服务端为 1  客户端为0 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (teamTypeShapeTypePositionDict.ContainsKey((TeamType)teamType)) | 
|---|
|  |  |  | int index = vNetData.LineupList[i].HeroItemIndexList[j]; | 
|---|
|  |  |  | HeroInfo hero; | 
|---|
|  |  |  | if (index == 0) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //  队伍类型相同,更新阵型和位置 | 
|---|
|  |  |  | Debug.LogError("当前英雄拥有两个相同的队伍信息: " + teamType + " " + shapeType + " " + positionIndex + ", hero guid is " + heroInfo.itemHero.guid); | 
|---|
|  |  |  | hero = null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //  队伍类型不同,添加新的 | 
|---|
|  |  |  | KeyValuePair<int, int> shapeTypePosition = new KeyValuePair<int, int>(shapeType, positionIndex); | 
|---|
|  |  |  | teamTypeShapeTypePositionDict.Add((TeamType)teamType, shapeTypePosition); | 
|---|
|  |  |  | var item = heroPack.GetItemByIndex(vNetData.LineupList[i].HeroItemIndexList[j] - 1); | 
|---|
|  |  |  | if (item == null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | hero = null; | 
|---|
|  |  |  | Debug.LogError("没有对应的武将数据!"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | hero = HeroManager.Instance.GetHero(item.guid); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | team.RefreshServerData(vNetData.LineupList[i].ShapeType, j, hero); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //  遍历当前所有队伍 判断当前队伍里是否有该英雄 | 
|---|
|  |  |  | //  如果有的话 根据英雄里的信息当前是否该英雄还在队伍里 是否发生变化 | 
|---|
|  |  |  | //     =>1.阵型发生变化 2.位置发生变化 | 
|---|
|  |  |  | //  如果没有的话 就说明该英雄被移出队伍了 | 
|---|
|  |  |  | foreach (var team in teamDict.Values) | 
|---|
|  |  |  | foreach (var tt in teamTypeSet) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //  检查一下当前队伍是否有该英雄 | 
|---|
|  |  |  | //  如果有的话 读取一下当前是否该英雄还在队伍里 位置是否发生变化 | 
|---|
|  |  |  | TeamHero teamHero = team.GetHero(heroInfo.itemHero.guid); | 
|---|
|  |  |  | if (teamHero != null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | if ((teamTypeShapeTypePositionDict.ContainsKey(team.teamType))) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | KeyValuePair<int, int> shapeTypePosition = teamTypeShapeTypePositionDict[team.teamType]; | 
|---|
|  |  |  | //  更新队伍信息 | 
|---|
|  |  |  | // 可以判断teamHero的positionNum是否跟shapeTypePosition.Value一致 判断是否变位置了 | 
|---|
|  |  |  | // 可以判断teamHero的ServerShapeType是否跟shapeTypePosition.Key一致 判断是否变阵型了 | 
|---|
|  |  |  | team.RefreshServerData(shapeTypePosition.Key, shapeTypePosition.Value, heroInfo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //  队伍里有这个英雄,但是在队伍信息里没有了 置空 (被移出队伍) | 
|---|
|  |  |  | team.RemoveHero(teamHero.positionNum); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //   原来队伍里没这个英雄 | 
|---|
|  |  |  | else | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //  如果当前队伍类型在英雄的所在阵容信息列表里有的话 | 
|---|
|  |  |  | //   就说明队伍里新增了这个英雄 (新增进队伍) | 
|---|
|  |  |  | if (teamTypeShapeTypePositionDict.ContainsKey(team.teamType)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | KeyValuePair<int, int> shapeTypePosition = teamTypeShapeTypePositionDict[team.teamType]; | 
|---|
|  |  |  | team.RefreshServerData(shapeTypePosition.Key, shapeTypePosition.Value, heroInfo); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //  遍历英雄所在的队伍信息列表 新增一下当前储存的队伍里没有的队伍 | 
|---|
|  |  |  | foreach (var teamTypeShapeTypePosition in teamTypeShapeTypePositionDict) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //  如果当前队伍类型在队伍字典里没有的话 | 
|---|
|  |  |  | if (!teamDict.ContainsKey(teamTypeShapeTypePosition.Key)) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | //  新建一个队伍 | 
|---|
|  |  |  | TeamBase team = new TeamBase(teamTypeShapeTypePosition.Key); | 
|---|
|  |  |  | team.RefreshServerData(teamTypeShapeTypePosition.Value.Key, teamTypeShapeTypePosition.Value.Value, heroInfo); | 
|---|
|  |  |  | teamDict.Add(teamTypeShapeTypePosition.Key, team); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | protected void onHeroDeleteEvent(HeroInfo heroInfo) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | List<int> heroTeams = heroInfo.itemHero.GetUseData(81); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | foreach (int teamMsg in heroTeams) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | // 所在阵容信息列表 [阵容类型*10000+阵型类型*100+位置编号, ...] | 
|---|
|  |  |  | int teamType = teamMsg / 10000; | 
|---|
|  |  |  | int shapeType = (teamMsg % 10000) / 100; | 
|---|
|  |  |  | int positionIndex = teamMsg % 100 - 1; //布阵位置:服务端为 1  客户端为0 | 
|---|
|  |  |  |  | 
|---|
|  |  |  | TeamBase team = GetTeam((TeamType)teamType); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if (team != null) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | team.RefreshServerData(shapeType, positionIndex, null); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | OnTeamChange?.Invoke(tt); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public bool HasTeam(TeamType teamType) | 
|---|
|  |  |  | { | 
|---|
|  |  |  | return teamDict.ContainsKey(teamType); | 
|---|