using System; using System.Collections.Generic; namespace ET { public class BattleMgrCompnentAwakeSystem : AwakeSystem { public override void Awake(BattleMgrCompnent self) { BattleMgrCompnent.Instance = self; } } public static class BattleMgrCompnentSystem { public static T CreateBattle(this BattleMgrCompnent self,Team team) where T : BattleBase { T entity = EntityFactory.CreateWithParent(self, team); self.Add(entity.battleType, entity); foreach (Unit unit in team.GetUnits()) { unit.BattleId = entity.Id; } if (!self.teamBattleTypeDic.TryAdd(entity.Id, entity)) { self.teamBattleTypeDic[entity.Id] = entity; Log.Error($"************\n***********\n 【{team.GetMemberName()}】 teamBattleTypeDic has the key {entity.Id} ,battleType is {entity.battleType}\n************\n***********"); } return entity; } public static T GetBattle(this BattleMgrCompnent self,BattleType battleType, long id) where T : BattleBase { if (!self.battleDic.TryGetValue((byte)battleType, out Dictionary dic)) { return null; } dic.TryGetValue(id, out BattleBase entity); return entity as T; } public static BattleBase GetBattle(this BattleMgrCompnent self,BattleType battleType, long id) { if (!self.battleDic.TryGetValue((byte)battleType, out Dictionary dic)) { return null; } dic.TryGetValue(id, out BattleBase entity); return entity; } public static BattleBase GetBattle(this BattleMgrCompnent self,Unit unit) { if (!self.teamBattleTypeDic.TryGetValue(unit.BattleId, out BattleBase battle)) { Log.Error($"battleType is null"); return null; } return battle; } public static void NotifyUnitDead(this BattleMgrCompnent self,Unit unit) { try { if (!self.teamBattleTypeDic.TryGetValue(unit.BattleId, out BattleBase battleBase)) { Log.Error($"battleType is null"); return; } BattleHelper.UnitDead(battleBase, unit).Coroutine(); } catch (Exception e) { Log.Error(e); } } public static void NotifyUnitHurt(this BattleMgrCompnent self,Unit unit, int hp) { try { if (!self.teamBattleTypeDic.TryGetValue(unit.BattleId, out BattleBase battleBase)) { Log.Error($"battleType is null"); return; } if (battleBase is FamilyBossBattle battle) { battle.ChangeBossHp(unit, hp); } } catch (Exception e) { Log.Error(e); } } public static void RemoveBattle(this BattleMgrCompnent self,T entity) where T : BattleBase { self.Remove(entity.battleType, entity.Id); if (!self.teamBattleTypeDic.Remove(entity.Id)) { Log.Error($"【{entity.team}】 teamBattleTypeDic con't remove the battle which entityId :{entity.Id}"); } entity.Dispose(); } private static void Add(this BattleMgrCompnent self,BattleType battleType, BattleBase entity) { if (!self.battleDic.TryGetValue((byte)battleType, out Dictionary dic)) { self.battleDic[(byte)battleType] = dic = new Dictionary(); } if (!dic.TryAdd(entity.Id, entity)) { dic[entity.Id] = entity; Log.Error($"************\n***********\n 【{entity.team}】battleDic has the key {entity.Id} ,battleType is {battleType}\n************\n***********"); } } private static void Remove(this BattleMgrCompnent self,BattleType battleType, long id) { if (!self.battleDic.TryGetValue((byte)battleType, out Dictionary dic)) { Log.Error($"con't find the type: {battleType} in battleDic"); return; } if (!dic.Remove(id)) { Log.Error($"con't remove the battle which id :{id}"); } } } }