using System.Collections.Generic; using System.Linq; using UnityEngine; public class TeamManager : GameSystemManager { protected Dictionary teamDict = new Dictionary(); 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; } protected void OnBeforePlayerDataInitialize() { teamDict.Clear(); } protected void onHeroChangeEvent(HeroInfo heroInfo) { // 英雄当前所有在的队伍 List heroTeams = heroInfo.itemHero.GetUseData(81); if (heroTeams == null || heroTeams.Count == 0) { return; } // 当前英雄所在的队伍信息 <队伍类型, <队形, 位置>> Dictionary> teamTypeShapeTypePositionDict = new Dictionary>(); // 处理当前记录在英雄信息里的队伍信息 if (null != heroTeams) { foreach (var teamMsg in heroTeams) { // 所在阵容信息列表 [阵容类型*10000+阵型类型*100+位置编号, ...] int teamType = teamMsg / 10000; int shapeType = (teamMsg % 10000) / 100; int positionIndex = teamMsg % 100; if (teamTypeShapeTypePositionDict.ContainsKey((TeamType)teamType)) { // 队伍类型相同,更新阵型和位置 Debug.LogError("当前英雄拥有两个相同的队伍信息: " + teamType + " " + shapeType + " " + positionIndex + ", hero guid is " + heroInfo.itemHero.guid); } else { // 队伍类型不同,添加新的 KeyValuePair shapeTypePosition = new KeyValuePair(shapeType, positionIndex); teamTypeShapeTypePositionDict.Add((TeamType)teamType, shapeTypePosition); } } } // 遍历当前所有队伍 判断当前队伍里是否有该英雄 // 如果有的话 根据英雄里的信息当前是否该英雄还在队伍里 是否发生变化 // =>1.阵型发生变化 2.位置发生变化 // 如果没有的话 就说明该英雄被移出队伍了 foreach (var team in teamDict.Values) { // 检查一下当前队伍是否有该英雄 // 如果有的话 读取一下当前是否该英雄还在队伍里 位置是否发生变化 TeamHero teamHero = team.GetHero(heroInfo.itemHero.guid); if (teamHero != null) { if ((teamTypeShapeTypePositionDict.ContainsKey(team.teamType))) { KeyValuePair shapeTypePosition = teamTypeShapeTypePositionDict[team.teamType]; // 更新队伍信息 // 可以判断teamHero的positionNum是否跟shapeTypePosition.Value一致 判断是否变位置了 // 可以判断teamHero的ServerShapeType是否跟shapeTypePosition.Key一致 判断是否变阵型了 team.RefreshServerData(shapeTypePosition.Key, shapeTypePosition.Value, heroInfo); } else { // 队伍里有这个英雄,但是在队伍信息里没有了 置空 (被移出队伍) team.SetTeamHero(teamHero.positionNum, null); } } // 原来队伍里没这个英雄 else { // 如果当前队伍类型在英雄的所在阵容信息列表里有的话 // 就说明队伍里新增了这个英雄 (新增进队伍) if (teamTypeShapeTypePositionDict.ContainsKey(team.teamType)) { KeyValuePair shapeTypePosition = teamTypeShapeTypePositionDict[team.teamType]; team.RefreshServerData(shapeTypePosition.Key, shapeTypePosition.Value, null); } } } // 遍历英雄所在的队伍信息列表 新增一下当前储存的队伍里没有的队伍 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 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; TeamBase team = GetTeam((TeamType)teamType); if (team != null) { team.RefreshServerData(shapeType, positionIndex, null); } } } public bool HasTeam(TeamType teamType) { return teamDict.ContainsKey(teamType); } public TeamBase GetTeam(TeamType teamType) { TeamBase team = null; if (!teamDict.TryGetValue(teamType, out team)) { // 测试用 // team = new TeamBase(teamType); // team.CreateDefault(HeroManager.Instance.GetPowerfulHeroList()); // teamDict.Add(teamType, team); } return team; } }