yyl
2025-07-29 6577831ab2883bd05448ce0b1f9f913a9944fc78
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
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 = 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())
        {
            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 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[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 < 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 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;
    }
 
    public int GetEmptyPosition()
    {
        for (int i = 0; i < tempHeroes.Length; i++)
        {
            if (tempHeroes[i] == null)
            {
                return i;
            }
        }
        return -1; // No empty position
    }
 
    //  布阵接口
    public void SetTeamHero(int posNum, TeamHero hero)
    {
        tempHeroes[posNum] = hero;
    }
 
    //  布阵接口
    public void SetServerTeamHero(int posNum, TeamHero hero)
    {
        serverHeroes[posNum] = hero;
        tempHeroes[posNum] = hero;
    }
 
    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)
    {
        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 < 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 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;
    }
    
}