using System; 
 | 
using System.Collections; 
 | 
using System.Collections.Generic; 
 | 
using UnityEngine; 
 | 
using System.Linq; 
 | 
  
 | 
public class BattleObjMgr 
 | 
{ 
 | 
    //  死亡不可以将BattleObject移出字典/列表 
 | 
    public List<BattleObject> redCampList => new List<BattleObject>(redCampDict.Values); 
 | 
    public List<BattleObject> blueCampList => new List<BattleObject>(blueCampDict.Values); 
 | 
    private Dictionary<int, BattleObject> redCampDict = new Dictionary<int, BattleObject>(); 
 | 
    private Dictionary<int, BattleObject> blueCampDict = new Dictionary<int, BattleObject>(); 
 | 
  
 | 
    protected Dictionary<int, BattleObject> allBattleObjDict = new Dictionary<int, BattleObject>(); 
 | 
  
 | 
    protected BattleField battleField; 
 | 
  
 | 
    public void Init(BattleField _battleField, TeamBase _redTeam, TeamBase _blueTeam) 
 | 
    { 
 | 
        battleField = _battleField; 
 | 
        ReloadTeam(_redTeam, BattleCamp.Red); 
 | 
        ReloadTeam(_blueTeam, BattleCamp.Blue); 
 | 
    } 
 | 
  
 | 
    public void ReloadTeam(TeamBase teamBase, BattleCamp _camp) 
 | 
    { 
 | 
        var posNodeList = _camp == BattleCamp.Red ? battleField.battleRootNode.redTeamNodeList : battleField.battleRootNode.blueTeamNodeList; 
 | 
        var campDict = _camp == BattleCamp.Red ? redCampDict : blueCampDict; 
 | 
        CreateTeam(posNodeList, campDict, teamBase, _camp); 
 | 
    } 
 | 
  
 | 
    protected void CreateTeam(List<GameObject> posNodeList, Dictionary<int, BattleObject> campDict, TeamBase teamBase, BattleCamp _Camp) 
 | 
    { 
 | 
        DestroyTeam(campDict); 
 | 
        if (teamBase == null) 
 | 
        { 
 | 
            return; 
 | 
        } 
 | 
  
 | 
  
 | 
        for (int i = 0; i < teamBase.serverHeroes.Length; i++) 
 | 
        { 
 | 
            TeamHero teamHero = teamBase.serverHeroes[i]; 
 | 
            if (teamHero != null) 
 | 
            { 
 | 
  
 | 
                BattleObject battleObj = BattleObjectFactory.CreateBattleObject(battleField, posNodeList, teamHero, _Camp); 
 | 
                allBattleObjDict.Add(battleObj.ObjID, battleObj); 
 | 
                campDict.Add(teamHero.positionNum, battleObj); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
     
 | 
    public BattleObject GetBattleObject(int objId) 
 | 
    { 
 | 
        if (allBattleObjDict.TryGetValue(objId, out BattleObject battleObj)) 
 | 
        { 
 | 
            return battleObj; 
 | 
        } 
 | 
        return null; 
 | 
    } 
 | 
  
 | 
    public List<BattleObject> GetBattleObjList(BattleCamp _Camp) 
 | 
    { 
 | 
        if (_Camp == BattleCamp.Red) 
 | 
        { 
 | 
            return redCampList; 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            return blueCampList; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    public List<BattleObject> GetBattleObjList(H0604_tagUseSkillAttack tagUseSkillAttack) 
 | 
    { 
 | 
        List<BattleObject> retList = new List<BattleObject>(); 
 | 
        foreach (var hurt in tagUseSkillAttack.HurtList) 
 | 
        { 
 | 
            BattleObject obj = GetBattleObject((int)hurt.ObjID); 
 | 
            if (null != obj) 
 | 
            { 
 | 
                retList.Add(obj); 
 | 
            } 
 | 
        } 
 | 
  
 | 
        return retList; 
 | 
    } 
 | 
  
 | 
    public void DestroyTeam(BattleCamp battleCamp) 
 | 
    { 
 | 
        Dictionary<int, BattleObject> campDict = battleCamp == BattleCamp.Red ? redCampDict : blueCampDict; 
 | 
        if (campDict == null) 
 | 
        { 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        DestroyTeam(campDict); 
 | 
    } 
 | 
  
 | 
    public void DestroyObjIds(uint[] objIDs) 
 | 
    { 
 | 
        if (objIDs == null || objIDs.Length == 0) 
 | 
        { 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        foreach (var objID in objIDs) 
 | 
        { 
 | 
            if (allBattleObjDict.TryGetValue((int)objID, out BattleObject battleObj)) 
 | 
            { 
 | 
                allBattleObjDict.Remove((int)objID); 
 | 
                redCampDict.Remove((int)objID); 
 | 
                blueCampDict.Remove((int)objID); 
 | 
                BattleObjectFactory.DestroyBattleObject((int)objID, battleObj); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
    protected void DestroyTeam(Dictionary<int, BattleObject> campDict) 
 | 
    { 
 | 
        foreach (var item in campDict) 
 | 
        { 
 | 
            BattleObject battleObj = item.Value; 
 | 
            if (battleObj != null) 
 | 
            { 
 | 
                allBattleObjDict.Remove(battleObj.ObjID); 
 | 
                BattleObjectFactory.DestroyBattleObject(item.Key, battleObj); 
 | 
            } 
 | 
        } 
 | 
        campDict.Clear(); 
 | 
    } 
 | 
  
 | 
    //  空闲状态 
 | 
    public virtual void HaveRest(BattleCamp _Camp) 
 | 
    { 
 | 
        //  休息状态 
 | 
        if (_Camp == BattleCamp.Red) 
 | 
        { 
 | 
            foreach (var item in redCampDict.Values) 
 | 
            { 
 | 
                item.HaveRest(); 
 | 
            } 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            foreach (var item in blueCampDict.Values) 
 | 
            { 
 | 
                item.HaveRest(); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
    public virtual void Release() 
 | 
    { 
 | 
        DestroyTeam(redCampDict); 
 | 
        DestroyTeam(blueCampDict); 
 | 
    } 
 | 
  
 | 
    public void Run() 
 | 
    { 
 | 
        foreach (var item in redCampDict) 
 | 
        { 
 | 
            item.Value.Run(); 
 | 
        } 
 | 
  
 | 
        foreach (var item in blueCampDict) 
 | 
        { 
 | 
            item.Value.Run(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    public virtual void ResumeGame() 
 | 
    { 
 | 
        foreach (var obj in redCampDict.Values) 
 | 
        { 
 | 
            obj.Resume(); 
 | 
        } 
 | 
  
 | 
        foreach(var obj in blueCampDict.Values) 
 | 
        { 
 | 
            obj.Resume(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    public virtual void PauseGame() 
 | 
    { 
 | 
        foreach (var obj in redCampDict.Values) 
 | 
        { 
 | 
            obj.Pause(); 
 | 
        } 
 | 
  
 | 
        foreach(var obj in blueCampDict.Values) 
 | 
        { 
 | 
            obj.Pause(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    public List<BattleObject> GetEnemyList(BattleObject _battleObj) 
 | 
    { 
 | 
        if (_battleObj.Camp == BattleCamp.Red) 
 | 
        { 
 | 
            return blueCampList; 
 | 
        } 
 | 
  
 | 
        return redCampList; 
 | 
    } 
 | 
  
 | 
    public List<BattleObject> GetFriendlyList(BattleObject _battleObj) 
 | 
    { 
 | 
        if (_battleObj.Camp == BattleCamp.Red) 
 | 
        { 
 | 
            return redCampList; 
 | 
        } 
 | 
  
 | 
        return blueCampList; 
 | 
    } 
 | 
  
 | 
#if UNITY_EDITOR_STOP_USING 
 | 
    public void ReviveAll() 
 | 
    { 
 | 
        foreach (var kv in allBattleObjDict) 
 | 
        { 
 | 
            kv.Value.EditorRevive(); 
 | 
        } 
 | 
    } 
 | 
  
 | 
    public List<BattleObject> GetTargetList(BattleObject battleObj, SkillTargetType targetType, SkillTargetRangeType rangeType) 
 | 
    { 
 | 
        List<BattleObject> returnList = new List<BattleObject>(); 
 | 
  
 | 
        switch (targetType) 
 | 
        { 
 | 
            case SkillTargetType.Self: 
 | 
                returnList.Add(battleObj); 
 | 
                break; 
 | 
            case SkillTargetType.Own: 
 | 
                returnList.AddRange(GetFriendlyList(battleObj)); 
 | 
                break; 
 | 
            case SkillTargetType.Enemy: 
 | 
                returnList.AddRange(GetEnemyList(battleObj)); 
 | 
                break; 
 | 
            case SkillTargetType.OwnExceptSelf: 
 | 
                List<BattleObject> friendlyList = GetFriendlyList(battleObj); 
 | 
                friendlyList.Remove(battleObj); 
 | 
                returnList.AddRange(friendlyList); 
 | 
                break; 
 | 
            default: 
 | 
                break; 
 | 
        } 
 | 
  
 | 
        //普攻为群攻时,以当前单体时默认攻击对象为主对象,其它为溅射单位; 
 | 
  
 | 
        switch (rangeType) 
 | 
        { 
 | 
            case SkillTargetRangeType.Front: 
 | 
                //暂时没有召唤物 
 | 
                // 放在第7格的BOSS后排的。。位置放在正中间 
 | 
                // a)前排,1、2、3号为前排,前排全部阵亡后,4、5、6号即是前排也是后排 7其实也是后排 
 | 
                List<BattleObject> frontList = new List<BattleObject>(from BO in returnList where BO.teamHero.positionNum < 3 && !BO.IsDead() select BO); 
 | 
                if (frontList.Count == 0) 
 | 
                { 
 | 
                    frontList.AddRange(returnList); 
 | 
                } 
 | 
                returnList = frontList;                 
 | 
                break; 
 | 
            case SkillTargetRangeType.Back: 
 | 
                List<BattleObject> backList = new List<BattleObject>(from BO in returnList where BO.teamHero.positionNum >= 3 && !BO.IsDead() select BO); 
 | 
                if (backList.Count == 0) 
 | 
                { 
 | 
                    backList.AddRange(returnList); 
 | 
                } 
 | 
                returnList = backList;      
 | 
                break; 
 | 
            case SkillTargetRangeType.Random: 
 | 
  
 | 
                int randomNumber = 3; 
 | 
                returnList = new List<BattleObject>(from BO in returnList where !BO.IsDead() select BO); 
 | 
                returnList = returnList.Shuffle(); 
 | 
                while (returnList.Count > randomNumber) 
 | 
                { 
 | 
                    returnList.RemoveAt(0); 
 | 
                } 
 | 
                break; 
 | 
            case SkillTargetRangeType.All: 
 | 
                break; 
 | 
            case SkillTargetRangeType.HighestAttack: 
 | 
                returnList = new List<BattleObject>(from BO in returnList where !BO.IsDead() select BO); 
 | 
                BattleObject mostHighestAttckObj = null; 
 | 
                //  客户端不包含数据 这里取第一个 
 | 
                mostHighestAttckObj = returnList[0]; 
 | 
                returnList.Clear(); 
 | 
                if (mostHighestAttckObj != null) 
 | 
                    returnList.Add(mostHighestAttckObj); 
 | 
                break; 
 | 
            case SkillTargetRangeType.LowestHP: 
 | 
                returnList = new List<BattleObject>(from BO in returnList where !BO.IsDead() select BO); 
 | 
                BattleObject loweastHpObj = null; 
 | 
                for (int i = 0; i < returnList.Count; i++) 
 | 
                { 
 | 
                    if (loweastHpObj == null) 
 | 
                    { 
 | 
                        loweastHpObj = returnList[i]; 
 | 
                        continue; 
 | 
                    } 
 | 
                    if (returnList[i].teamHero.curHp < loweastHpObj.teamHero.curHp) 
 | 
                    { 
 | 
                        loweastHpObj = returnList[i]; 
 | 
                    } 
 | 
                } 
 | 
                returnList.Clear(); 
 | 
                if (null != loweastHpObj) 
 | 
                    returnList.Add(loweastHpObj); 
 | 
                break; 
 | 
            case SkillTargetRangeType.Deadman: 
 | 
                returnList = new List<BattleObject>(from BO in returnList where BO.IsDead() select BO); 
 | 
                break; 
 | 
            default: 
 | 
                break; 
 | 
        } 
 | 
  
 | 
        return returnList; 
 | 
    } 
 | 
#endif 
 | 
} 
 |