2021-04-08 20:09:59 +08:00
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 ) ;
2021-04-11 19:50:39 +08:00
foreach ( Unit unit in team . GetUnits ( ) )
2021-04-08 20:09:59 +08:00
{
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
{
2021-04-11 19:50:39 +08:00
if ( ! self . battleDic . TryGetValue ( ( byte ) battleType , out Dictionary < long , BattleBase > dic ) )
2021-04-08 20:09:59 +08:00
{
return null ;
}
2021-04-11 19:50:39 +08:00
dic . TryGetValue ( id , out BattleBase entity ) ;
2021-04-08 20:09:59 +08:00
return entity as T ;
}
public static BattleBase GetBattle ( this BattleMgrCompnent self , BattleType battleType , long id )
{
2021-04-11 19:50:39 +08:00
if ( ! self . battleDic . TryGetValue ( ( byte ) battleType , out Dictionary < long , BattleBase > dic ) )
2021-04-08 20:09:59 +08:00
{
return null ;
}
2021-04-11 19:50:39 +08:00
dic . TryGetValue ( id , out BattleBase entity ) ;
2021-04-08 20:09:59 +08:00
return entity ;
}
public static BattleBase GetBattle ( this BattleMgrCompnent self , Unit unit )
{
2021-04-11 19:50:39 +08:00
if ( ! self . teamBattleTypeDic . TryGetValue ( unit . BattleId , out BattleBase battle ) )
2021-04-08 20:09:59 +08:00
{
Log . Error ( $"battleType is null" ) ;
return null ;
}
return battle ;
}
public static void NotifyUnitDead ( this BattleMgrCompnent self , Unit unit )
{
try
{
2021-04-11 19:50:39 +08:00
if ( ! self . teamBattleTypeDic . TryGetValue ( unit . BattleId , out BattleBase battleBase ) )
2021-04-08 20:09:59 +08:00
{
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
{
2021-04-11 19:50:39 +08:00
if ( ! self . teamBattleTypeDic . TryGetValue ( unit . BattleId , out BattleBase battleBase ) )
2021-04-08 20:09:59 +08:00
{
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 )
{
2021-04-11 19:50:39 +08:00
if ( ! self . battleDic . TryGetValue ( ( byte ) battleType , out Dictionary < long , BattleBase > dic ) )
2021-04-08 20:09:59 +08:00
{
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 )
{
2021-04-11 19:50:39 +08:00
if ( ! self . battleDic . TryGetValue ( ( byte ) battleType , out Dictionary < long , BattleBase > dic ) )
2021-04-08 20:09:59 +08:00
{
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}" ) ;
}
}
}
}