yyl
7 天以前 c124d98bdf9659cf764bebb799bee42c30eb152f
Main/System/Team/TeamBase.cs
@@ -28,9 +28,9 @@
    private int ShapeType;
    public int ServerShapeType { get; private set; }
    public TeamHero[] teamHeros = new TeamHero[TeamConst.MaxTeamHeroCount];
    public TeamHero[] tempHeroes { get; private set; }  = new TeamHero[TeamConst.MaxTeamHeroCount];
    public TeamHero[] serverData { get; private set; } = new TeamHero[TeamConst.MaxTeamHeroCount];
    public TeamHero[] serverHeroes { get; private set; } = new TeamHero[TeamConst.MaxTeamHeroCount];
    public TeamBase(TeamType _teamType)
    {
@@ -49,12 +49,12 @@
        for (int i = 0; i < lineUp.ObjCnt; i++)
        {
            if (i < teamHeros.Length)
            if (i < tempHeroes.Length)
            {
                var fightObj = lineUp.ObjList[i];
                TeamHero hero = new TeamHero(fightObj, this);
                teamHeros[fightObj.PosNum] = hero;
                serverData[fightObj.PosNum] = hero;
                tempHeroes[hero.positionNum] = hero;
                serverHeroes[hero.positionNum] = hero;
            }
            else
            {
@@ -70,9 +70,9 @@
    public int GetTeamHeroCount()
    {
        int count = 0;
        for (int i = 0; i < teamHeros.Length; i++)
        for (int i = 0; i < tempHeroes.Length; i++)
        {
            if (teamHeros[i] != null)
            if (tempHeroes[i] != null)
            {
                count++;
            }
@@ -83,7 +83,7 @@
    public bool IsFull()
    {
        return GetTeamHeroCount() >= teamHeros.Length;
        return GetTeamHeroCount() >= tempHeroes.Length;
    }
    public bool IsEmpty()
@@ -99,7 +99,7 @@
    {
        if (IsEmpty())
        {
            Debug.LogError("Cannot save an empty team. You should at least have one hero in the team.");
            SysNotifyMgr.Instance.ShowTip("HeroFunc3");
            return;
        }
@@ -109,7 +109,8 @@
        savePack.PosCnt = (byte)GetTeamHeroCount();
        savePack.HeroPosList = new CB412_tagCSHeroLineupSave.tagCSHeroLineupPos[savePack.PosCnt];
        foreach (var hero in teamHeros)
        int index = 0;
        foreach (var hero in tempHeroes)
        {
            if (hero != null)
            {
@@ -122,15 +123,17 @@
                    continue;
                }
                savePack.HeroPosList[posNum] = new CB412_tagCSHeroLineupSave.tagCSHeroLineupPos
                savePack.HeroPosList[index] = new CB412_tagCSHeroLineupSave.tagCSHeroLineupPos
                {
                    ItemIndex = (ushort)heroInfo.itemHero.gridIndex,
                    PosNum = (byte)posNum
                    PosNum = (byte)(posNum + 1)
                };
                index++;
            }
        }
        GameNetSystem.Instance.SendInfo(savePack);
        SysNotifyMgr.Instance.ShowTip("HeroFunc4");
        //非主线阵容客户端自己做战力变化,主线阵容服务端战力变更会同步推送
    }
    public void OnChangeShapeType(int newShapeType)
@@ -144,7 +147,7 @@
        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);
@@ -160,7 +163,7 @@
        for (int i = 0; i < heroInfos.Count; i++)
        {
            if (i < teamHeros.Length)
            if (i < tempHeroes.Length)
            {
                var heroInfo = heroInfos[i];
                TeamHero hero = new TeamHero(heroInfo, i, this);
@@ -175,7 +178,7 @@
    public TeamHero GetHero(string guid)
    {
        foreach (var hero in teamHeros)
        foreach (var hero in tempHeroes)
        {
            if (hero != null && hero.guid == guid)
            {
@@ -184,10 +187,24 @@
        }
        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 serverData)
        foreach (var hero in serverHeroes)
        {
            if (hero != null && hero.guid == guid)
            {
@@ -200,7 +217,7 @@
    //  布阵接口
    public bool HasHero(string guid)
    {
        foreach (var hero in teamHeros)
        foreach (var hero in tempHeroes)
        {
            if (hero != null && hero.guid == guid)
            {
@@ -210,11 +227,82 @@
        return false;
    }
    public bool HasHeroInServer(string guid)
    {
        foreach (var hero in serverHeroes)
        {
            if (hero != null && hero.guid == guid)
            {
                return true;
            }
        }
        return false;
    }
    public TeamHero GetServerHeroByIndex(int index)
    {
        if (index < 0 || index >= serverHeroes.Length)
        {
            return null;
        }
        return serverHeroes[index];
    }
    //获取所有上阵的武将
    public List<TeamHero> GetHerosOnTeam()
    {
        List<TeamHero> tmpHeros = new List<TeamHero>();
        foreach (var hero in tempHeroes)
        {
            if (hero != null)
            {
                tmpHeros.Add(hero);
            }
        }
        return tmpHeros;
    }
    public TeamHero GetNextServerHero(string guid)
    {
        if (string.IsNullOrEmpty(guid))
        {
            //取第一个
            foreach (var hero in serverHeroes)
            {
                if (hero != null)
                {
                    return hero;
                }
            }
            return null;
        }
        else
        {
            //取下一个
            bool findNext = false;
            foreach (var hero in serverHeroes)
            {
                if (hero != null && hero.guid == guid)
                {
                    findNext = true;
                }
                else if (findNext && hero != null)
                {
                    return hero;
                }
            }
            //没找到 取第一个
            return GetNextServerHero("");
        }
    }
    //客户端从0开始,服务端从1开始
    public int GetEmptyPosition()
    {
        for (int i = 0; i < teamHeros.Length; i++)
        for (int i = 0; i < tempHeroes.Length; i++)
        {
            if (teamHeros[i] == null)
            if (tempHeroes[i] == null)
            {
                return i;
            }
@@ -222,78 +310,114 @@
        return -1; // No empty position
    }
    //  布阵接口
    public void SetTeamHero(int posNum, TeamHero hero)
    {
        teamHeros[posNum] = hero;
    public int GetPosition(string guid)
    {
        foreach (var hero in serverHeroes)
        {
            if (hero != null && hero.guid == guid)
            {
                return hero.positionNum;
            }
        }
        return  -1;
    }
    //  布阵接口
    public void SetServerTeamHero(int posNum, TeamHero hero)
    private void SetTeamHero(int posNum, TeamHero hero)
    {
        serverData[posNum] = hero;
        teamHeros[posNum] = hero;
        tempHeroes[posNum] = hero;
    }
    public void AddHero(HeroInfo heroInfo, int targetPosition)
    //  布阵接口
    private void SetServerTeamHero(int posNum, TeamHero hero)
    {
        if (targetPosition < 0 || targetPosition >= teamHeros.Length)
        serverHeroes[posNum] = hero;
        tempHeroes[posNum] = hero;
    }
    //  布阵接口: 恢复阵容
    public void RestoreTeam()
    {
        for (int i = 0; i < tempHeroes.Length; i++)
        {
            tempHeroes[i] = serverHeroes[i];
        }
    }
    //checkLock :是否验证上阵人数限制
    public void AddHero(HeroInfo heroInfo, int targetPosition, bool checkLock)
    {
        if (targetPosition < 0 || targetPosition >= tempHeroes.Length)
        {
            Debug.LogError("Invalid target position for adding hero.");
            return;
        }
        TeamHero targetHero = teamHeros[targetPosition];
        if (checkLock)
        {
            var lockCnt = HeroUIManager.Instance.lockIndexList.Count;
            if (lockCnt > 0 && TeamConst.MaxTeamHeroCount - GetHerosOnTeam().Count <= lockCnt)
            {
                HeroUIManager.Instance.ShowUnLockTip(HeroUIManager.Instance.lockIndexList[0]);
                return;
            }
        }
        if (null == targetHero)
        {
            TeamHero newHero = new TeamHero(heroInfo, targetPosition, this);
            SetTeamHero(targetPosition, newHero);
        }
        else
        {
            SetTeamHero(targetPosition, new TeamHero(heroInfo, targetPosition, this));
        }
        SetTeamHero(targetPosition, new TeamHero(heroInfo, targetPosition, this));
    }
    //checkLock :是否验证上阵人数限制
    //  add只可能是点下面卡牌
    public bool AddHero(HeroInfo heroInfo)
    public bool AddHero(HeroInfo heroInfo, out int pos, bool checkLock)
    {
        pos = -1;
        if (heroInfo == null || heroInfo.itemHero == null) return false;
        //  如果当前英雄已经在队伍里了 就不处理了
        if (GetHero(heroInfo.itemHero.guid) != null)
        if (checkLock)
        {
            return false; // Hero already in team
            var lockCnt = HeroUIManager.Instance.lockIndexList.Count;
            if (lockCnt > 0 && TeamConst.MaxTeamHeroCount - GetHerosOnTeam().Count <= lockCnt)
            {
                HeroUIManager.Instance.ShowUnLockTip(HeroUIManager.Instance.lockIndexList[0]);
                return false;
            }
        }
        // 同一英雄 只能上阵一个
        if (GetHeroByHeroID(heroInfo.heroId) != null)
        {
            SysNotifyMgr.Instance.ShowTip("HeroFunc2");
            return false;
        }
        else
        {
            int emptyPosition = GetEmptyPosition();
            pos = GetEmptyPosition();
            if (emptyPosition < 0)
            if (pos < 0)
            {
                Debug.LogError("No empty position available in the team.");
                SysNotifyMgr.Instance.ShowTip("HeroFunc1");
                return false; // No empty position available
            }
            TeamHero teamHero = new TeamHero(heroInfo, GetEmptyPosition(), this);
            TeamHero teamHero = new TeamHero(heroInfo, pos, this);
            SetTeamHero(teamHero.positionNum, teamHero);
            return true;
        }
    }
    public bool RemoveHero(HeroInfo heroInfo)
    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;
@@ -308,9 +432,9 @@
    {
        if (teamHero == null) return false;
        for (int i = 0; i < teamHeros.Length; i++)
        for (int i = 0; i < tempHeroes.Length; i++)
        {
            if (teamHeros[i] != null && teamHeros[i].guid == teamHero.guid)
            if (tempHeroes[i] != null && tempHeroes[i].guid == teamHero.guid)
            {
                SetTeamHero(i, null);
                return true; // Hero removed successfully
@@ -319,21 +443,33 @@
        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 >= teamHeros.Length || index2 < 0 || index2 >= teamHeros.Length)
        if (index1 < 0 || index1 >= tempHeroes.Length || index2 < 0 || index2 >= tempHeroes.Length)
        {
            Debug.LogError("Invalid indices for swapping positions.");
            return;
        }
        TeamHero temp = teamHeros[index1];
        teamHeros[index1] = teamHeros[index2];
        teamHeros[index2] = temp;
        TeamHero temp = tempHeroes[index1];
        tempHeroes[index1] = tempHeroes[index2];
        tempHeroes[index2] = temp;
        //  更新位置编号
        if (teamHeros[index1] != null) teamHeros[index1].positionNum = index1;
        if (teamHeros[index2] != null) teamHeros[index2].positionNum = index2;
        if (tempHeroes[index1] != null) tempHeroes[index1].positionNum = index1;
        if (tempHeroes[index2] != null) tempHeroes[index2].positionNum = index2;
    }
    
}