NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Fight/Capture/CaptureManager.cs

148 lines
4.1 KiB
C#
Raw Normal View History

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