using System.Collections.Generic; using Framework; using Gameplay.Area; using Gameplay.Common; using Gameplay.Effect; using Gameplay.Level; using Gameplay.Performance; using Gameplay.Utils; using LTGame; using UnityEngine; namespace Gameplay.PlayerSkill.State { public class PlayerSkillStateChoosePos : BasePlayerSkillState { private Camera _mainCamera; private Map _map; private int _layerMask; private readonly RaycastHit[] _raycastHits = new RaycastHit[2]; private int _lastMouseCellIndex; private List _cacheAreaCellIndexs = new List(); private AreaChecker _checker = new AreaChecker(); private EffectPlayingData _effectData; private Color _areaColor; public PlayerSkillStateChoosePos(PlayerSkillManager owner) : base(owner, EPlayerSkillState.ChoosePos) { } protected override void _OnEnter(NBaseState exitState, object param) { base._OnEnter(exitState, param); _mainCamera = Camera.main; _map = LevelManager.Instance.CurrentLevel.Map; _layerMask = LayerMask.GetMask("Ground"); _lastMouseCellIndex = -1; _cacheAreaCellIndexs.Clear(); _checker.InitFromPlayerSkill(owner.readySkillConfig, owner.playerUnit.commonData.troopId); _map.ShowInfo(Map.InfoMask.ShowCells, true); if (owner.readySkillConfig.ShowEffectId > 0) { _effectData = EffectManager.instance.PlayEffectById(owner.readySkillConfig.ShowEffectId, Vector3.zero); _effectData.autoDestroy = false; } _areaColor = ColorEx.FromStr(owner.readySkillConfig.ShowAreaColor); EventManager.Instance.Register(EventManager.EventName.INFIGHT_PLAYER_SKILL_RELEASE, _OnRecvRelease); } protected override void _OnRunning() { base._OnRunning(); // 实时获取鼠标指向位置 var mouseCellIndex = _GetCurrentMouseCellIndex(); if (_lastMouseCellIndex != mouseCellIndex) { _lastMouseCellIndex = mouseCellIndex; if (_cacheAreaCellIndexs.Count != 0) { // 还原地块颜色 _map.SetBlocksColor(_cacheAreaCellIndexs, false, Color.clear); } _cacheAreaCellIndexs.Clear(); AreaManager.instance.GetCellIndexs(_lastMouseCellIndex, owner.readySkillConfig.ShowAreaId, _cacheAreaCellIndexs); if (_effectData != null) { _effectData.transform.position = AreaManager.instance.GetCellPosByIndex(_lastMouseCellIndex); } _map.SetBlocksColor(_cacheAreaCellIndexs, true, _areaColor); } _FakeInput(); } protected override void _OnExit(NBaseState exitState, object param) { base._OnExit(exitState, param); if (_cacheAreaCellIndexs.Count != 0) { // 还原地块颜色 _map.SetBlocksColor(_cacheAreaCellIndexs, false, Color.clear); } _map.ShowInfo(Map.InfoMask.ShowCells, false); if (_effectData != null) { _effectData.Destroy(); _effectData = null; } EventManager.Instance.Unregister(EventManager.EventName.INFIGHT_PLAYER_SKILL_RELEASE, _OnRecvRelease); } private void _FakeInput() { // TODO: 临时逻辑, 用于测试 if (Input.GetKeyDown(KeyCode.Q)) { var releaseData = new EDReleasePlayerSkill(); releaseData.isCancel = true; EventManager.Instance.Send(EventManager.EventName.INFIGHT_PLAYER_SKILL_RELEASE, releaseData); } } private void _OnRecvRelease(EDReleasePlayerSkill releaseData) { DebugUtil.Log("接收到释放技能消息"); isFinished = true; var posCanRelease = _checker.CheckAvailable(_lastMouseCellIndex); if (!posCanRelease) { UITipsManager.ShowTips(" "); DebugUtil.Log("当前位置不可释放技能"); nextState = EPlayerSkillState.Idle; return; } if (releaseData.isCancel) { DebugUtil.Log("玩家取消释放技能"); nextState = EPlayerSkillState.Idle; return; } DebugUtil.Log("玩家释放技能"); owner.selectedCellIndex = _lastMouseCellIndex; nextState = EPlayerSkillState.Release; } private int _GetCurrentMouseCellIndex() { var mousePos = Input.mousePosition; var ray = _mainCamera.ScreenPointToRay(mousePos * PerformanceManager.instance.screenScale); var raycast = Physics.RaycastNonAlloc(ray, _raycastHits, 1000, _layerMask); if (raycast <= 0) return -1; var firstHit = _raycastHits[0]; var hitPoint = firstHit.point; if (!_map.WorldPosition2TGSCellIndex(hitPoint, out var cellIndex)) return -1; return cellIndex; } } }