127 lines
4.6 KiB
C#
127 lines
4.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace ET
|
|
{
|
|
public class BattleMgrCompnentAwakeSystem : AwakeSystem<BattleMgrCompnent>
|
|
{
|
|
public override void Awake(BattleMgrCompnent self)
|
|
{
|
|
BattleMgrCompnent.Instance = self;
|
|
}
|
|
}
|
|
public static class BattleMgrCompnentSystem
|
|
{
|
|
public static T CreateBattle<T>(this BattleMgrCompnent self,Team team) where T : BattleBase
|
|
{
|
|
T entity = EntityFactory.CreateWithParent<T, Team>(self, team);
|
|
self.Add(entity.battleType, entity);
|
|
foreach (var 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<T>(this BattleMgrCompnent self,BattleType battleType, long id) where T : BattleBase
|
|
{
|
|
if (!self.battleDic.TryGetValue((byte)battleType, out var dic))
|
|
{
|
|
return null;
|
|
}
|
|
dic.TryGetValue(id, out var entity);
|
|
return entity as T;
|
|
}
|
|
public static BattleBase GetBattle(this BattleMgrCompnent self,BattleType battleType, long id)
|
|
{
|
|
if (!self.battleDic.TryGetValue((byte)battleType, out var dic))
|
|
{
|
|
return null;
|
|
}
|
|
dic.TryGetValue(id, out var entity);
|
|
return entity;
|
|
}
|
|
public static BattleBase GetBattle(this BattleMgrCompnent self,Unit unit)
|
|
{
|
|
if (!self.teamBattleTypeDic.TryGetValue(unit.BattleId, out var 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 var 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 var 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<T>(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 var dic))
|
|
{
|
|
self.battleDic[(byte)battleType] = dic = new Dictionary<long, BattleBase>();
|
|
}
|
|
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 var 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}");
|
|
}
|
|
}
|
|
}
|
|
}
|