yyl
2025-07-21 5bc2cc9a3e007b96a0de96e70e87f25bc5a254a2
Main/System/Team/TeamBase.cs
@@ -1,78 +1,71 @@
using System;
using System.Collections.Generic;
using UnityEngine;
//  阵型基础
public partial class TeamBase
{
    // 该阵容在本阵营的编号,不同阵营的阵容编号可能相同,都是从1开始,一般1V1时每个阵营为1个阵容,多V多时则每个阵营为多个阵容
    private int teamIndex = 0;
    private uint playerId = 0;
    // 区分一下阵营跟阵容 阵营为一个TeamBase列表 可能是多个玩家组合而成
    // 阵容为一个TeamBase 是玩家的一个队伍
    private int teamIndex = 0;//该阵营的第几个队伍
    private uint playerId = 0; //该阵容的玩家id
    private int ShapeType; //本阵容阵型,0为默认阵型,可扩展不同的阵型,如boss特殊战斗阵型,或者其他不同站位的阵型
    // 本地可变阵型
    public bool IsPlayer
    {
        get
        {
            return playerId == 0;
        }
        get { return playerId == 0; }
    }
    public TeamHero[] teamHeros = new TeamHero[TeamConst.MaxTeamSlotCount];
    public TeamBase()
    // 服务器下发的阵型和阵容,只能通过网络协议赋值,外部只读
    public TeamType teamType
    {
        get;
        private set;
    }
    private int ShapeType;
    public int ServerShapeType { get; private set; }
    public TeamHero[] teamHeros = new TeamHero[TeamConst.MaxTeamHeroCount];
    public TeamHero[] serverData { 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 < teamHeros.Length)
            {
                TeamHero hero = new TeamHero(lineUp.ObjList[i], this);
                AddTeamHero(hero);
                var fightObj = lineUp.ObjList[i];
                TeamHero hero = new TeamHero(fightObj, this);
                teamHeros[fightObj.PosNum] = hero;
                serverData[fightObj.PosNum] = hero;
            }
            else
            {
                Debug.LogError("TeamBase: Too many heroes in lineup, exceeding MaxTeamHeroCount.");
            }
        }
        Update();
        // 刷新服务器数据
        UpdateProperties();
    }
    protected void Update()
    {
        //  检查国籍
        //  检查羁绊
        //  检查阵型
        //  更新队伍英雄属性
        foreach (var teamHero in teamHeros)
        {
            teamHero.Update();
        }
        //  更新队伍属性
        // UpdateProperties();
        // CalculatePower();
    }
    public void SetShapeType(int shapeType)
    {
        ShapeType = shapeType;
    }
    public int GetTeamHeroCount()
    {
@@ -84,80 +77,9 @@
                count++;
            }
        }
        return count;
    }
    public bool SwapTeamHero(int index1, int index2)
    {
        if (index1 < 0 || index1 >= teamHeros.Length || index2 < 0 || index2 >= teamHeros.Length)
        {
            return false;
        }
        TeamHero temp = teamHeros[index1];
        teamHeros[index1] = teamHeros[index2];
        teamHeros[index2] = temp;
        temp.heroIndex = index2;
        teamHeros[index1].heroIndex = index1;
        return true;
    }
    public void AddHeroInfos(List<HeroInfo> heroInfos)
    {
        for (int i = 0; i < heroInfos.Count; i++)
        {
            AddHeroInfo(heroInfos[i]);
        }
        UpdateProperties();
    }
    public bool AddHeroInfo(HeroInfo heroInfo)
    {
        if (heroInfo == null)
        {
            return false;
        }
        for (int i = 0; i < teamHeros.Length; i++)
        {
            if (teamHeros[i] == null)
            {
                teamHeros[i] = new TeamHero();
                teamHeros[i].heroInfo = heroInfo;
                teamHeros[i].heroIndex = i;
                UpdateProperties();
                return true;
            }
        }
        return false;
    }
    public void AddTeamHero(TeamHero teamHero)
    {
        if (null == teamHero)
        {
            return;
        }
        teamHeros[teamHero.heroIndex] = teamHero;
    }
    public bool RemoveTeamHero(int index)
    {
        if (index < 0 || index >= teamHeros.Length)
        {
            return false;
        }
        teamHeros[index] = null;
        return true;
    }
    public bool IsFull()
    {
@@ -170,18 +92,248 @@
    }
#if UNITY_EDITOR
    public void FillWithFakeData()
    /// <summary>
    /// 保存阵容(预留接口)
    /// </summary>
    public void SaveTeam()
    {
        for (int i = 0; i < TeamConst.MaxTeamHeroCount; i++)
        if (IsEmpty())
        {
            TeamHero hero = new TeamHero();
            hero.curHp = 100;
            hero.heroInfo = new HeroInfo();
            hero.teamBase = this;
            hero.heroIndex = i;
            teamHeros[i] = hero;
            Debug.LogError("Cannot save an empty team. You should at least have one hero in the team.");
            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];
        foreach (var hero in teamHeros)
        {
            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[posNum] = new CB412_tagCSHeroLineupSave.tagCSHeroLineupPos
                {
                    ItemIndex = (ushort)heroInfo.itemHero.gridIndex,
                    PosNum = (byte)posNum
                };
            }
        }
        GameNetSystem.Instance.SendInfo(savePack);
    }
    public void OnChangeShapeType(int newShapeType)
    {
        ShapeType = newShapeType;
    }
    public void OnServerChangeShapeType(int newShapeType)
    {
        ServerShapeType = newShapeType;
        ShapeType = newShapeType;
    }
    //  hero info could be null if the hero is removed from the team
    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 < teamHeros.Length)
            {
                var heroInfo = heroInfos[i];
                TeamHero hero = new TeamHero(heroInfo, i, this);
                SetServerTeamHero(i, hero);
            }
            else
            {
                Debug.LogError("TeamBase: Too many heroes in lineup");
            }
        }
    }
#endif
    public TeamHero GetHero(string guid)
    {
        foreach (var hero in teamHeros)
        {
            if (hero != null && hero.guid == guid)
            {
                return hero;
            }
        }
        return null;
    }
    public TeamHero GetServerHero(string guid)
    {
        foreach (var hero in serverData)
        {
            if (hero != null && hero.guid == guid)
            {
                return hero;
            }
        }
        return null;
    }
    //  布阵接口
    public bool HasHero(string guid)
    {
        foreach (var hero in teamHeros)
        {
            if (hero != null && hero.guid == guid)
            {
                return true;
            }
        }
        return false;
    }
    public int GetEmptyPosition()
    {
        for (int i = 0; i < teamHeros.Length; i++)
        {
            if (teamHeros[i] == null)
            {
                return i;
            }
        }
        return -1; // No empty position
    }
    //  布阵接口
    public void SetTeamHero(int posNum, TeamHero hero)
    {
        teamHeros[posNum] = hero;
    }
    //  布阵接口
    public void SetServerTeamHero(int posNum, TeamHero hero)
    {
        serverData[posNum] = hero;
        teamHeros[posNum] = hero;
    }
    public void AddHero(HeroInfo heroInfo, int targetPosition)
    {
        if (targetPosition < 0 || targetPosition >= teamHeros.Length)
        {
            Debug.LogError("Invalid target position for adding hero.");
            return;
        }
        TeamHero targetHero = teamHeros[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)
    {
        if (heroInfo == null || heroInfo.itemHero == null) return false;
        //  如果当前英雄已经在队伍里了 就不处理了
        if (GetHero(heroInfo.itemHero.guid) != null)
        {
            return false; // Hero already in team
        }
        else
        {
            int emptyPosition = GetEmptyPosition();
            if (emptyPosition < 0)
            {
                Debug.LogError("No empty position available in the team.");
                return false; // No empty position available
            }
            TeamHero teamHero = new TeamHero(heroInfo, GetEmptyPosition(), this);
            SetTeamHero(teamHero.positionNum, teamHero);
            return true;
        }
    }
    public bool RemoveHero(HeroInfo heroInfo)
    {
        if (heroInfo == null || heroInfo.itemHero == null) return false;
        TeamHero teamHero = GetHero(heroInfo.itemHero.guid);
        if (teamHero != null)
        {
            //  从当前队伍里移除该英雄
            SetTeamHero(teamHero.positionNum, null);
            return true;
        }
        else
        {
            return false;
        }
    }
    public bool RemoveHero(TeamHero teamHero)
    {
        if (teamHero == null) return false;
        for (int i = 0; i < teamHeros.Length; i++)
        {
            if (teamHeros[i] != null && teamHeros[i].guid == teamHero.guid)
            {
                SetTeamHero(i, null);
                return true; // Hero removed successfully
            }
        }
        return false; // Hero not found
    }
    public void SwapPosition(int index1, int index2)
    {
        if (index1 < 0 || index1 >= teamHeros.Length || index2 < 0 || index2 >= teamHeros.Length)
        {
            Debug.LogError("Invalid indices for swapping positions.");
            return;
        }
        TeamHero temp = teamHeros[index1];
        teamHeros[index1] = teamHeros[index2];
        teamHeros[index2] = temp;
        //  更新位置编号
        if (teamHeros[index1] != null) teamHeros[index1].positionNum = index1;
        if (teamHeros[index2] != null) teamHeros[index2].positionNum = index2;
    }
}