using System; 
 | 
using System.Collections.Generic; 
 | 
using UnityEngine; 
 | 
  
 | 
//  阵型基础 
 | 
  
 | 
public partial class TeamBase 
 | 
{ 
 | 
    // 该阵容在本阵营的编号,不同阵营的阵容编号可能相同,都是从1开始,一般1V1时每个阵营为1个阵容,多V多时则每个阵营为多个阵容 
 | 
    private int teamIndex = 0; 
 | 
    private uint playerId = 0; 
 | 
  
 | 
    // 本地可变阵型 
 | 
  
 | 
    public bool IsPlayer 
 | 
    { 
 | 
        get { return playerId == 0; } 
 | 
    } 
 | 
  
 | 
  
 | 
    // 服务器下发的阵型和阵容,只能通过网络协议赋值,外部只读 
 | 
    public TeamType teamType 
 | 
    { 
 | 
        get; 
 | 
        private set; 
 | 
    } 
 | 
  
 | 
    private int ShapeType; 
 | 
    public int ServerShapeType { get; private set; } 
 | 
  
 | 
    public TeamHero[] tempHeroes { get; private set; }  = new TeamHero[TeamConst.MaxTeamHeroCount]; 
 | 
  
 | 
    public TeamHero[] serverHeroes { get; private set; } = new TeamHero[TeamConst.MaxTeamHeroCount]; 
 | 
  
 | 
    public TeamBase(TeamType _teamType) 
 | 
    { 
 | 
        teamType = _teamType; 
 | 
        teamIndex = 0; 
 | 
        playerId = PlayerDatas.Instance.baseData.PlayerID; 
 | 
    } 
 | 
  
 | 
    //  这边来的可以没有队伍类型 
 | 
    public TeamBase(HB424_tagSCTurnFightInit.tagSCTurnFightLineup lineUp) 
 | 
    { 
 | 
        teamIndex = lineUp.Num; 
 | 
        playerId = lineUp.OwnerID; 
 | 
        ShapeType = lineUp.ShapeType; 
 | 
        ServerShapeType = lineUp.ShapeType; 
 | 
  
 | 
        for (int i = 0; i < lineUp.ObjCnt; i++) 
 | 
        { 
 | 
            if (i < tempHeroes.Length) 
 | 
            { 
 | 
                var fightObj = lineUp.ObjList[i]; 
 | 
                TeamHero hero = new TeamHero(fightObj, this); 
 | 
                tempHeroes[fightObj.PosNum] = hero; 
 | 
                serverHeroes[fightObj.PosNum] = hero; 
 | 
            } 
 | 
            else 
 | 
            { 
 | 
                Debug.LogError("TeamBase: Too many heroes in lineup, exceeding MaxTeamHeroCount."); 
 | 
            } 
 | 
        } 
 | 
  
 | 
        // 刷新服务器数据 
 | 
        UpdateProperties(); 
 | 
    } 
 | 
  
 | 
  
 | 
    public int GetTeamHeroCount() 
 | 
    { 
 | 
        int count = 0; 
 | 
        for (int i = 0; i < tempHeroes.Length; i++) 
 | 
        { 
 | 
            if (tempHeroes[i] != null) 
 | 
            { 
 | 
                count++; 
 | 
            } 
 | 
        } 
 | 
        return count; 
 | 
    } 
 | 
  
 | 
  
 | 
    public bool IsFull() 
 | 
    { 
 | 
        return GetTeamHeroCount() >= tempHeroes.Length; 
 | 
    } 
 | 
  
 | 
    public bool IsEmpty() 
 | 
    { 
 | 
        return GetTeamHeroCount() == 0; 
 | 
    } 
 | 
  
 | 
  
 | 
    /// <summary> 
 | 
    /// 保存阵容(预留接口) 
 | 
    /// </summary> 
 | 
    public void SaveTeam() 
 | 
    { 
 | 
        if (IsEmpty()) 
 | 
        { 
 | 
            SysNotifyMgr.Instance.ShowTip("HeroFunc3"); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        CB412_tagCSHeroLineupSave savePack = new CB412_tagCSHeroLineupSave(); 
 | 
        savePack.LineupID = (byte)teamType; 
 | 
        savePack.ShapeType = (byte)ShapeType; 
 | 
        savePack.PosCnt = (byte)GetTeamHeroCount(); 
 | 
        savePack.HeroPosList = new CB412_tagCSHeroLineupSave.tagCSHeroLineupPos[savePack.PosCnt]; 
 | 
  
 | 
        int index = 0; 
 | 
        foreach (var hero in tempHeroes) 
 | 
        { 
 | 
            if (hero != null) 
 | 
            { 
 | 
                int posNum = hero.positionNum; 
 | 
                var heroInfo = HeroManager.Instance.GetHero(hero.guid); 
 | 
  
 | 
                if (heroInfo == null) 
 | 
                { 
 | 
                    Debug.LogError($"Hero with GUID {hero.guid} not found in HeroManager."); 
 | 
                    continue; 
 | 
                } 
 | 
  
 | 
                savePack.HeroPosList[index] = new CB412_tagCSHeroLineupSave.tagCSHeroLineupPos 
 | 
                { 
 | 
                    ItemIndex = (ushort)heroInfo.itemHero.gridIndex, 
 | 
                    PosNum = (byte)(posNum + 1) 
 | 
                }; 
 | 
                index++; 
 | 
            } 
 | 
        } 
 | 
        GameNetSystem.Instance.SendInfo(savePack); 
 | 
        SysNotifyMgr.Instance.ShowTip("HeroFunc4"); 
 | 
        //非主线阵容客户端自己做战力变化,主线阵容服务端战力变更会同步推送 
 | 
    } 
 | 
  
 | 
    public void OnChangeShapeType(int newShapeType) 
 | 
    { 
 | 
        ShapeType = newShapeType; 
 | 
    } 
 | 
  
 | 
    public void OnServerChangeShapeType(int newShapeType) 
 | 
    { 
 | 
        ServerShapeType = newShapeType; 
 | 
        ShapeType = newShapeType; 
 | 
    } 
 | 
  
 | 
     
 | 
    public void RefreshServerData(int shapeType, int positionIndex, HeroInfo heroInfo) 
 | 
    { 
 | 
        TeamHero teamHero = heroInfo == null ? null : new TeamHero(heroInfo, positionIndex, this); 
 | 
        SetServerTeamHero(positionIndex, teamHero); 
 | 
        OnServerChangeShapeType(shapeType); 
 | 
    } 
 | 
  
 | 
    public void CreateDefault(List<HeroInfo> heroInfos) 
 | 
    { 
 | 
        teamIndex = 0; 
 | 
        playerId = PlayerDatas.Instance.baseData.PlayerID; 
 | 
        OnServerChangeShapeType(0); 
 | 
  
 | 
        for (int i = 0; i < heroInfos.Count; i++) 
 | 
        { 
 | 
            if (i < tempHeroes.Length) 
 | 
            { 
 | 
                var heroInfo = heroInfos[i]; 
 | 
                TeamHero hero = new TeamHero(heroInfo, i, this); 
 | 
                SetServerTeamHero(i, hero); 
 | 
            } 
 | 
            else 
 | 
            { 
 | 
                Debug.LogError("TeamBase: Too many heroes in lineup"); 
 | 
            } 
 | 
        } 
 | 
    } 
 | 
  
 | 
    public TeamHero GetHero(string guid) 
 | 
    { 
 | 
        foreach (var hero in tempHeroes) 
 | 
        { 
 | 
            if (hero != null && hero.guid == guid) 
 | 
            { 
 | 
                return hero; 
 | 
            } 
 | 
        } 
 | 
        return null; 
 | 
    } 
 | 
  
 | 
    public TeamHero GetHeroByHeroID(int heroId) 
 | 
    {  
 | 
        foreach (var hero in tempHeroes) 
 | 
        { 
 | 
            if (hero != null && hero.heroId == heroId) 
 | 
            { 
 | 
                return hero; 
 | 
            } 
 | 
        } 
 | 
        return null; 
 | 
    } 
 | 
  
 | 
  
 | 
    public TeamHero GetServerHero(string guid) 
 | 
    { 
 | 
        foreach (var hero in serverHeroes) 
 | 
        { 
 | 
            if (hero != null && hero.guid == guid) 
 | 
            { 
 | 
                return hero; 
 | 
            } 
 | 
        } 
 | 
        return null; 
 | 
    } 
 | 
  
 | 
    //  布阵接口 
 | 
    public bool HasHero(string guid) 
 | 
    { 
 | 
        foreach (var hero in tempHeroes) 
 | 
        { 
 | 
            if (hero != null && hero.guid == guid) 
 | 
            { 
 | 
                return true; 
 | 
            } 
 | 
        } 
 | 
        return false; 
 | 
    } 
 | 
  
 | 
    //客户端从0开始,服务端从1开始 
 | 
    public int GetEmptyPosition() 
 | 
    { 
 | 
        for (int i = 0; i < tempHeroes.Length; i++) 
 | 
        { 
 | 
            if (tempHeroes[i] == null) 
 | 
            { 
 | 
                return i; 
 | 
            } 
 | 
        } 
 | 
        return -1; // No empty position 
 | 
    } 
 | 
  
 | 
    //  布阵接口 
 | 
    private void SetTeamHero(int posNum, TeamHero hero) 
 | 
    { 
 | 
        tempHeroes[posNum] = hero; 
 | 
    } 
 | 
  
 | 
    //  布阵接口 
 | 
    private void SetServerTeamHero(int posNum, TeamHero hero) 
 | 
    { 
 | 
        serverHeroes[posNum] = hero; 
 | 
        tempHeroes[posNum] = hero; 
 | 
    } 
 | 
  
 | 
    //  布阵接口: 恢复阵容 
 | 
    public void RestoreTeam() 
 | 
    { 
 | 
        for (int i = 0; i < tempHeroes.Length; i++) 
 | 
        { 
 | 
            tempHeroes[i] = serverHeroes[i]; 
 | 
        } 
 | 
    } 
 | 
     
 | 
  
 | 
    public void AddHero(HeroInfo heroInfo, int targetPosition) 
 | 
    { 
 | 
        if (targetPosition < 0 || targetPosition >= tempHeroes.Length) 
 | 
        { 
 | 
            Debug.LogError("Invalid target position for adding hero."); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        TeamHero targetHero = tempHeroes[targetPosition]; 
 | 
  
 | 
        if (null == targetHero) 
 | 
        { 
 | 
            TeamHero newHero = new TeamHero(heroInfo, targetPosition, this); 
 | 
            SetTeamHero(targetPosition, newHero); 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            SetTeamHero(targetPosition, new TeamHero(heroInfo, targetPosition, this)); 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
  
 | 
    //  add只可能是点下面卡牌 
 | 
    public bool AddHero(HeroInfo heroInfo, out int pos) 
 | 
    { 
 | 
        pos = -1; 
 | 
        if (heroInfo == null || heroInfo.itemHero == null) return false; 
 | 
  
 | 
        // 同一英雄 只能上阵一个 
 | 
        if (GetHeroByHeroID(heroInfo.heroId) != null) 
 | 
        { 
 | 
            SysNotifyMgr.Instance.ShowTip("HeroFunc2"); 
 | 
            return false;  
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            pos = GetEmptyPosition(); 
 | 
  
 | 
            if (pos < 0) 
 | 
            { 
 | 
                SysNotifyMgr.Instance.ShowTip("HeroFunc1"); 
 | 
                return false; // No empty position available 
 | 
            } 
 | 
  
 | 
            TeamHero teamHero = new TeamHero(heroInfo, pos, this); 
 | 
            SetTeamHero(teamHero.positionNum, teamHero); 
 | 
            return true; 
 | 
        } 
 | 
    } 
 | 
  
 | 
  
 | 
    public bool RemoveHero(HeroInfo heroInfo, out int pos) 
 | 
    { 
 | 
        pos = -1; 
 | 
        if (heroInfo == null || heroInfo.itemHero == null) return false; 
 | 
  
 | 
        TeamHero teamHero = GetHero(heroInfo.itemHero.guid); 
 | 
  
 | 
        if (teamHero != null) 
 | 
        { 
 | 
            pos = teamHero.positionNum; 
 | 
            //  从当前队伍里移除该英雄 
 | 
            SetTeamHero(teamHero.positionNum, null); 
 | 
            return true; 
 | 
        } 
 | 
        else 
 | 
        { 
 | 
            return false; 
 | 
        } 
 | 
    } 
 | 
  
 | 
    public bool RemoveHero(TeamHero teamHero) 
 | 
    { 
 | 
        if (teamHero == null) return false; 
 | 
  
 | 
        for (int i = 0; i < tempHeroes.Length; i++) 
 | 
        { 
 | 
            if (tempHeroes[i] != null && tempHeroes[i].guid == teamHero.guid) 
 | 
            { 
 | 
                SetTeamHero(i, null); 
 | 
                return true; // Hero removed successfully 
 | 
            } 
 | 
        } 
 | 
        return false; // Hero not found 
 | 
    } 
 | 
  
 | 
    public bool RemoveHero(int pos) 
 | 
    { 
 | 
        SetTeamHero(pos, null); 
 | 
        return true; 
 | 
    } 
 | 
  
 | 
    public void RemoveAllHeroes() 
 | 
    { 
 | 
        tempHeroes = new TeamHero[TeamConst.MaxTeamHeroCount]; 
 | 
    } 
 | 
  
 | 
  
 | 
    public void SwapPosition(int index1, int index2) 
 | 
    { 
 | 
        if (index1 < 0 || index1 >= tempHeroes.Length || index2 < 0 || index2 >= tempHeroes.Length) 
 | 
        { 
 | 
            Debug.LogError("Invalid indices for swapping positions."); 
 | 
            return; 
 | 
        } 
 | 
  
 | 
        TeamHero temp = tempHeroes[index1]; 
 | 
        tempHeroes[index1] = tempHeroes[index2]; 
 | 
        tempHeroes[index2] = temp; 
 | 
  
 | 
        //  更新位置编号 
 | 
        if (tempHeroes[index1] != null) tempHeroes[index1].positionNum = index1; 
 | 
        if (tempHeroes[index2] != null) tempHeroes[index2].positionNum = index2; 
 | 
    } 
 | 
     
 | 
} 
 |