NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/PlayerSkill/State/PlayerSkillStateChoosePos.cs

182 lines
6.3 KiB
C#

using System.Collections.Generic;
using AOT.PostProcess.BlackArea;
using Framework;
using Gameplay.Area;
using Gameplay.Common;
using Gameplay.Effect;
using Gameplay.Guide;
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<int> _cacheAreaCellIndexs = new List<int>();
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);
DebugUtil.LogG("Enter PlayerSkillStateChoosePos");
_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<EDReleasePlayerSkill>(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<EDReleasePlayerSkill>(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)
{
EventManager.Instance.Send(EventManager.EventName.Guide_UseSkill);
DebugUtil.LogG("接收到释放技能消息");
isFinished = true;
bool posCanRelease = _checker.CheckAvailable(_lastMouseCellIndex);
if (GuideManager.Instance.IsGuiding && GuideManager.Instance.IsControlFloor) // 引导释放指挥官技能特殊处理
posCanRelease &= _lastMouseCellIndex == GuideManager.Instance.FloorIndex;
if (!owner.readySkillConfig.CanReleaseOnDark)
{
if (!BlackAreaManager.instance.CheckIsInLightArea(_lastMouseCellIndex))
{
posCanRelease = false;
}
}
if (!posCanRelease)
{
UITipsManager.ShowTips("当前位置不可释放技能");
DebugUtil.Log("当前位置不可释放技能");
EventManager.Instance.Send(EventManager.EventName.Guide_CancelPlayerSkill);
nextState = EPlayerSkillState.Idle;
return;
}
if (releaseData.isCancel)
{
DebugUtil.Log("玩家取消释放技能");
EventManager.Instance.Send(EventManager.EventName.Guide_CancelPlayerSkill);
nextState = EPlayerSkillState.Idle;
return;
}
DebugUtil.Log("玩家释放技能");
EventManager.Instance.Send(EventManager.EventName.Guide_UsePlayerSkill);
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;
}
}
}