using System.Collections.Generic; using Framework; using Gameplay.Character; using Gameplay.Level; using Gameplay.Unit; using Gameplay.Unit.Data; using UnityEngine; namespace Gameplay.Fight.Capture { /// /// 用于管理战场内的所有占领点 /// 仅在战场内使用 /// public class CaptureManager { public List captureInsts = new List(); private bool _needUpdate; public static CaptureManager instance { get; private set; } public static void CreateInstance() { instance = new CaptureManager(); } private CaptureManager() { } public void Init() { _needUpdate = true; _RegisterEvents(); _InitCaptureAreas(); } private void _InitCaptureAreas() { var levelData = LevelManager.Instance.CurrentLevel.GetLevelData(); var captureProgress = levelData.CapturePoints; foreach (var captureInfo in captureProgress) { _AddCaptureInst(captureInfo.CapturePoint, captureInfo.CaptureProgress); } } private void _AddCaptureInst(int cellIndex, int captureValue) { var newInst = new CaptureInst(cellIndex, captureValue); captureInsts.Add(newInst); } private void _RegisterEvents() { EventManager.Instance.Register(EventManager.EventName.INFIGHT_UNIT_CELLINDEX_CHANGE, _OnAnyCellIndexChange); } private void _UnRegisterEvents() { EventManager.Instance.Unregister(EventManager.EventName.INFIGHT_UNIT_CELLINDEX_CHANGE, _OnAnyCellIndexChange); } private void _OnAnyCellIndexChange(GameUnit unit) { _needUpdate = true; } private List _cacheSelectUnits = new List(10); private List _FilterUnits() { var allUnits = GameUnitManager.instance.infightUnits.unitList; var selectUnits = _cacheSelectUnits; selectUnits.Clear(); for (int i = 0; i < allUnits.Count; i++) { var checkUnit = allUnits[i]; if (!checkUnit.commonData.canControl) { continue; } if (checkUnit.gameunitType is not (EGameUnitType.Character or EGameUnitType.Vehicle)) { continue; } if (checkUnit.commonData.troopId != ETroopsId.Self) { continue; } if (checkUnit.statusData.isOnVehicle) { continue; } if (checkUnit.statusData.isDead) { continue; } selectUnits.Add(checkUnit); } return selectUnits; } public void LogicUpdate(float dt) { if (_needUpdate) { var checkUnits = _FilterUnits(); for (int i = 0; i < captureInsts.Count; i++) { var captureInst = captureInsts[i]; captureInst.UpdateInAreaUnits(checkUnits); } _needUpdate = false; } for (int i = 0; i < captureInsts.Count; i++) { var captureInst = captureInsts[i]; captureInst.LogicUpdate(dt); // if (captureInst.isCaptured) // { // captureInst.Dispose(); // captureInsts.RemoveAt(i); // i--; // } } } public void Dispose() { _UnRegisterEvents(); for (int i = 0; i < captureInsts.Count; i++) { var captureInst = captureInsts[i]; captureInst.Dispose(); } captureInsts.Clear(); instance = null; } } }