using System.Collections.Generic; using Framework; using Gameplay.Character; using Gameplay.Fight.Capture; using Gameplay.Level; using Gameplay.Unit; using Gameplay.Utils; using Gameplay.Vehicle.Impl; using UnityEngine; namespace Gameplay.Area.Ready { public class ReadyAreaManager { public Map _map; private EReadyAreaStatus _areaStatus; private List _mapReadyCellIndexs; public List mapReadyCellIndexs => _mapReadyCellIndexs; private List _vehicleAroundCellIndexs; private int _lastVehicleCellIndex; public bool isShow { get; private set; } public Color areaColor { get; private set; } private NormalVehicle _followVehicle; public bool isVehicleBuffActive; private bool _lastVehicleBuffActive; public ReadyAreaManager(Map map) { _map = map; isShow = false; // TODO: 这里应该从配置中读取 areaColor = ColorEx.FromStr("#FF960072"); _InitReadyAreaFromLevel(); } public void Init() { EventManager.Instance.Register(EventManager.EventName.INFIGHT_CAPTURE_FINSIHED, _OnCaptureArea); } public void Dispose() { EventManager.Instance.Unregister(EventManager.EventName.INFIGHT_CAPTURE_FINSIHED, _OnCaptureArea); } private void _InitReadyAreaFromLevel() { _mapReadyCellIndexs = new List(); var levelData = LevelManager.Instance.CurrentLevel.GetLevelData(); var areaId = levelData.preparingRegionId; var area = _map.MapData.GetRegion(areaId); if (area == null) { DebugUtil.LogError("读取准备区域失败"); return; } var cellIndexs = area.positions; foreach (var cellIndex in cellIndexs) { if (_mapReadyCellIndexs.Contains(cellIndex)) { continue; } _mapReadyCellIndexs.Add(cellIndex); } } private void _OnCaptureArea(CaptureInst inst) { if (_mapReadyCellIndexs.Contains(inst.cellIndex)) { return; } _mapReadyCellIndexs.Add(inst.cellIndex); } private void _UpdateWithVehicle() { // 需要更新载具周边区域 if (_followVehicle == null) { return; } var currentVehicleCellIndex = _followVehicle.transData.cellIndex; if (currentVehicleCellIndex == _lastVehicleCellIndex) { return; } _lastVehicleCellIndex = currentVehicleCellIndex; for (int i = 0; i < _vehicleAroundCellIndexs.Count; i++) { var oldIndex = _vehicleAroundCellIndexs[i]; if (_areaStatus == EReadyAreaStatus.Both) { if (_mapReadyCellIndexs.Contains(oldIndex)) { continue; } } _map.SetBlockColor(oldIndex, false, areaColor); } _vehicleAroundCellIndexs = AreaManager.instance.GetCellIndexsAround(_lastVehicleCellIndex, _followVehicle.aroundReadyRange); _map.SetBlocksColor(_vehicleAroundCellIndexs, true, areaColor); } private void _UpdateOnlyMap() { if (isVehicleBuffActive) { _UpdateWithVehicle(); } else { if (_lastVehicleBuffActive != isVehicleBuffActive) { _lastVehicleBuffActive = isVehicleBuffActive; // 移除载具区域显示 for (int i = 0; i < _vehicleAroundCellIndexs.Count; i++) { var oldIndex = _vehicleAroundCellIndexs[i]; if (_mapReadyCellIndexs.Contains(oldIndex)) { continue; } _map.SetBlockColor(oldIndex, false, areaColor); } } } } public void LogicUpdate(float dt) { if (!isShow) return; switch (_areaStatus) { case EReadyAreaStatus.FromMap: _UpdateOnlyMap(); break; case EReadyAreaStatus.Both: case EReadyAreaStatus.AroundVehicle: _UpdateWithVehicle(); break; } } public bool CheckIsInReadyArea(int cellIndex) { switch (_areaStatus) { case EReadyAreaStatus.FromMap: if (_mapReadyCellIndexs.Contains(cellIndex)) { return true; } if (isVehicleBuffActive) { if (_vehicleAroundCellIndexs.Contains(cellIndex)) { return true; } } break; case EReadyAreaStatus.AroundVehicle: if (_followVehicle == null) { return false; } if (_vehicleAroundCellIndexs.Contains(cellIndex)) { return true; } break; case EReadyAreaStatus.Both: if (_mapReadyCellIndexs.Contains(cellIndex)) { return true; } if (_followVehicle == null) { return false; } if (_vehicleAroundCellIndexs.Contains(cellIndex)) { return true; } break; } return false; } public void ShowReadyArea(EReadyAreaStatus showStatus = EReadyAreaStatus.FromMap) { if (isShow) { if (_areaStatus == showStatus) { // 重复的调用 } else { // 需要切换显示状态 HideReadyArea(); } } isShow = true; _areaStatus = showStatus; switch (_areaStatus) { case EReadyAreaStatus.FromMap: _map.SetBlocksColor(_mapReadyCellIndexs, true, areaColor); if (isVehicleBuffActive) { _lastVehicleBuffActive = true; _GetCurrentVehicle(); _map.SetBlocksColor(_vehicleAroundCellIndexs, true, areaColor); } _lastVehicleBuffActive = false; break; case EReadyAreaStatus.AroundVehicle: _GetCurrentVehicle(); _map.SetBlocksColor(_vehicleAroundCellIndexs, true, areaColor); break; case EReadyAreaStatus.Both: _map.SetBlocksColor(_mapReadyCellIndexs, true, areaColor); _GetCurrentVehicle(); _map.SetBlocksColor(_vehicleAroundCellIndexs, true, areaColor); break; } } private void _GetCurrentVehicle() { _followVehicle = GameUnitManager.instance.vehicleManager.GetVehicleByTroop(ETroopsId.Self); if (_followVehicle == null) { DebugUtil.LogError("没有找到己方载具"); _vehicleAroundCellIndexs = new List(); return; } _lastVehicleCellIndex = _followVehicle.transData.cellIndex; _vehicleAroundCellIndexs = AreaManager.instance.GetCellIndexsAround(_lastVehicleCellIndex, _followVehicle.aroundReadyRange); } public void HideReadyArea() { if (!isShow) { return; } isShow = false; switch (_areaStatus) { case EReadyAreaStatus.FromMap: _map.SetBlocksColor(_mapReadyCellIndexs, false, areaColor); if (isVehicleBuffActive) { _map.SetBlocksColor(_vehicleAroundCellIndexs, false, areaColor); } break; case EReadyAreaStatus.AroundVehicle: _map.SetBlocksColor(_vehicleAroundCellIndexs, false, areaColor); break; case EReadyAreaStatus.Both: _map.SetBlocksColor(_mapReadyCellIndexs, false, areaColor); _map.SetBlocksColor(_vehicleAroundCellIndexs, false, areaColor); break; } } } }