yyl
2025-06-12 5ca88372febc0ebab5cbdb5f922c59a646b8fb94
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class BattleObjMgr : Singleton<BattleObjMgr>
{
    private List<BattleObject> redCampList => new List<BattleObject>(redCampDict.Values);
    private 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>();
 
    public void Init(int _levelId, TeamBase _redTeam, TeamBase _blueTeam = null)
    {
        if (_levelId == 0 && _blueTeam == null)
        {
            Debug.LogError("BattleObjMgr Init Error: _levelId == 0 && _blueTeam == null 关卡id没有(不是PVE) 也没有蓝色队伍信息(也不是PVP))");
            return;
        }
 
        if (_levelId != 0 && _blueTeam != null)
        {
            Debug.LogError("BattleObjMgr Init Error: _levelId!= 0 && _blueTeam != null 关卡id有(是PVE) 也有蓝色队伍信息(也不是PVP))");
            return;
        }
 
        if (_levelId != 0)
        {
            _blueTeam = new TeamBase();
            _blueTeam.InitByLevelId(_levelId);
        }
 
        CreateTeam(redCampDict, _redTeam);
        CreateTeam(blueCampDict, _blueTeam);
    } 
 
    protected void CreateTeam(Dictionary<int, BattleObject> campDict, TeamBase teamBase)
    {
        DestroyTeam(campDict);
        for (int i = 0; i < teamBase.teamCards.Length; i++)
        {
            TeamCard teamCard = teamBase.teamCards[i];
            if (teamCard != null)
            {
                BattleObject battleObj = BattleObjectFactory.CreateBattleObject(teamCard);
                battleObj.Init(teamCard);
                campDict.Add(teamCard.cardIndex, battleObj);
            }
        }
    }
 
    protected void DestroyTeam(Dictionary<int, BattleObject> campDict)
    {
        foreach (var item in campDict)
        {
            BattleObject battleObj = item.Value;
            if (battleObj!= null)
            {
                BattleObjectFactory.DestroyBattleObject(battleObj);
            }
        }
        campDict.Clear();
    }
 
    public 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();
        }
    }
}