using System; using System.Collections.Generic; using AOT.PostProcess.BlackArea; using Framework; using Gameplay.Character; using Gameplay.Fight.Capture; using Gameplay.Level; using Gameplay.Unit; using Gameplay.Unit.Data; 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 { get; set; } public bool isVehicleBuffActive; private bool _lastVehicleBuffActive; public ReadyAreaManager(Map map) { _map = map; isShow = false; // TODO: 这里应该从配置中读取 areaColor = ColorEx.FromStr("#b6d53b50"); _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); } _GetPosFromVehicle(); _map.SetBlocksColor(_vehicleAroundCellIndexs, true, areaColor); } private void _GetPosFromVehicle() { if (_vehicleAroundCellIndexs == null) { _vehicleAroundCellIndexs = new List(); } else { _vehicleAroundCellIndexs.Clear(); } var totalAround = AreaManager.instance.GetCellIndexsAround(_lastVehicleCellIndex, _followVehicle.aroundReadyRange); var map = LevelManager.Instance.CurrentLevel.Map; for (int i = 0; i < totalAround.Count; i++) { var newIndex = totalAround[i]; if (map.GetRuntimeBlockProperty(newIndex, out var runtimeBlockProperty)) { if (runtimeBlockProperty.isEmptyBlock) { continue; } if (!runtimeBlockProperty.selectable) { continue; } if (runtimeBlockProperty.banAttack) { continue; } _vehicleAroundCellIndexs.Add(newIndex); } } } 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; } _AutoLightUpReadyArea(); } private void _LightUpList(List cellList) { for (int i = 0; i < cellList.Count; i++) { var cellIndex = cellList[i]; BlackAreaManager.instance.LightUpCell(cellIndex, 0.1f); } } private void _AutoLightUpReadyArea() { switch (_areaStatus) { case EReadyAreaStatus.FromMap: _LightUpList(_mapReadyCellIndexs); if (isVehicleBuffActive) { _LightUpList(_vehicleAroundCellIndexs); } break; case EReadyAreaStatus.AroundVehicle: if (_followVehicle == null) { return; } _LightUpList(_vehicleAroundCellIndexs); break; case EReadyAreaStatus.Both: _LightUpList(_mapReadyCellIndexs); if (_followVehicle == null) { return; } _LightUpList(_vehicleAroundCellIndexs); 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, GameUnit unit = null) { 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: _SetCurrentVehicle(unit); _GetCurrentVehicle(); _map.SetBlocksColor(_vehicleAroundCellIndexs, true, areaColor); break; case EReadyAreaStatus.Both: _map.SetBlocksColor(_mapReadyCellIndexs, true, areaColor); _SetCurrentVehicle(unit); _GetCurrentVehicle(); _map.SetBlocksColor(_vehicleAroundCellIndexs, true, areaColor); break; } } private void _GetCurrentVehicle() { _lastVehicleCellIndex = _followVehicle.transData.cellIndex; _GetPosFromVehicle(); } private void _SetCurrentVehicle(GameUnit downUnit) { if (downUnit.gameunitType != EGameUnitType.Character) { throw new Exception("不是角色1"); } var character = downUnit as Character.Character; if (character == null) { throw new Exception("不是角色2"); } _followVehicle = character.runtimeData.belongVehicle; _lastVehicleCellIndex = -1; } 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; } } } }