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

182 lines
6.3 KiB
C#
Raw Normal View History

2023-12-06 15:32:45 +08:00
using System.Collections.Generic;
using AOT.PostProcess.BlackArea;
2023-12-06 17:05:07 +08:00
using Framework;
2023-12-06 15:32:45 +08:00
using Gameplay.Area;
2023-12-06 17:05:07 +08:00
using Gameplay.Common;
using Gameplay.Effect;
2024-10-28 15:03:03 +08:00
using Gameplay.Guide;
2023-12-06 15:32:45 +08:00
using Gameplay.Level;
2024-01-10 19:04:01 +08:00
using Gameplay.Performance;
using Gameplay.Utils;
2023-12-06 15:32:45 +08:00
using LTGame;
using UnityEngine;
namespace Gameplay.PlayerSkill.State
2023-12-06 12:22:59 +08:00
{
public class PlayerSkillStateChoosePos : BasePlayerSkillState
{
2023-12-06 15:32:45 +08:00
private Camera _mainCamera;
private Map _map;
private int _layerMask;
private readonly RaycastHit[] _raycastHits = new RaycastHit[2];
2023-12-07 13:08:39 +08:00
2023-12-06 15:32:45 +08:00
private int _lastMouseCellIndex;
private List<int> _cacheAreaCellIndexs = new List<int>();
2023-12-07 13:08:39 +08:00
private AreaChecker _checker = new AreaChecker();
private EffectPlayingData _effectData;
private Color _areaColor;
2023-12-06 15:32:45 +08:00
public PlayerSkillStateChoosePos(PlayerSkillManager owner) : base(owner, EPlayerSkillState.ChoosePos)
2023-12-06 12:22:59 +08:00
{
2023-12-07 13:08:39 +08:00
2023-12-06 12:22:59 +08:00
}
2023-12-06 15:32:45 +08:00
protected override void _OnEnter(NBaseState exitState,
object param)
{
base._OnEnter(exitState, param);
2024-07-05 16:14:23 +08:00
DebugUtil.LogG("Enter PlayerSkillStateChoosePos");
2023-12-06 15:32:45 +08:00
_mainCamera = Camera.main;
_map = LevelManager.Instance.CurrentLevel.Map;
_layerMask = LayerMask.GetMask("Ground");
_lastMouseCellIndex = -1;
_cacheAreaCellIndexs.Clear();
2023-12-08 13:28:03 +08:00
_checker.InitFromPlayerSkill(owner.readySkillConfig, owner.playerUnit.commonData.troopId);
2023-12-07 13:08:39 +08:00
2023-12-06 15:32:45 +08:00
_map.ShowInfo(Map.InfoMask.ShowCells, true);
2023-12-07 13:08:39 +08:00
if (owner.readySkillConfig.ShowEffectId > 0)
{
_effectData = EffectManager.instance.PlayEffectById(owner.readySkillConfig.ShowEffectId, Vector3.zero);
_effectData.autoDestroy = false;
}
_areaColor = ColorEx.FromStr(owner.readySkillConfig.ShowAreaColor);
2023-12-06 17:05:07 +08:00
EventManager.Instance.Register<EDReleasePlayerSkill>(EventManager.EventName.INFIGHT_PLAYER_SKILL_RELEASE, _OnRecvRelease);
2023-12-06 15:32:45 +08:00
}
protected override void _OnRunning()
{
base._OnRunning();
2023-12-07 13:08:39 +08:00
2023-12-06 15:32:45 +08:00
// 实时获取鼠标指向位置
var mouseCellIndex = _GetCurrentMouseCellIndex();
if (_lastMouseCellIndex != mouseCellIndex)
{
_lastMouseCellIndex = mouseCellIndex;
if (_cacheAreaCellIndexs.Count != 0)
{
// 还原地块颜色
_map.SetBlocksColor(_cacheAreaCellIndexs, false, Color.clear);
}
2023-12-07 13:08:39 +08:00
2023-12-06 15:32:45 +08:00
_cacheAreaCellIndexs.Clear();
AreaManager.instance.GetCellIndexs(_lastMouseCellIndex, owner.readySkillConfig.ShowAreaId, _cacheAreaCellIndexs);
2023-12-07 13:08:39 +08:00
if (_effectData != null)
{
2024-11-15 14:32:41 +08:00
_effectData.transform.position = AreaManager.instance.GetWorldPosByIndex(_lastMouseCellIndex);
}
_map.SetBlocksColor(_cacheAreaCellIndexs, true, _areaColor);
2023-12-06 15:32:45 +08:00
}
2023-12-06 17:05:07 +08:00
_FakeInput();
2023-12-06 15:32:45 +08:00
}
protected override void _OnExit(NBaseState exitState,
object param)
{
base._OnExit(exitState, param);
2023-12-07 13:08:39 +08:00
2023-12-06 17:05:07 +08:00
if (_cacheAreaCellIndexs.Count != 0)
{
// 还原地块颜色
_map.SetBlocksColor(_cacheAreaCellIndexs, false, Color.clear);
}
2023-12-06 15:32:45 +08:00
_map.ShowInfo(Map.InfoMask.ShowCells, false);
2023-12-07 13:08:39 +08:00
if (_effectData != null)
{
_effectData.Destroy();
_effectData = null;
}
2023-12-06 17:05:07 +08:00
EventManager.Instance.Unregister<EDReleasePlayerSkill>(EventManager.EventName.INFIGHT_PLAYER_SKILL_RELEASE, _OnRecvRelease);
2023-12-06 15:32:45 +08:00
}
2023-12-06 17:05:07 +08:00
private void _FakeInput()
2023-12-06 15:32:45 +08:00
{
2023-12-06 17:05:07 +08:00
// TODO: 临时逻辑, 用于测试
if (Input.GetKeyDown(KeyCode.Q))
{
var releaseData = new EDReleasePlayerSkill();
releaseData.isCancel = true;
EventManager.Instance.Send(EventManager.EventName.INFIGHT_PLAYER_SKILL_RELEASE, releaseData);
}
}
2023-12-07 13:08:39 +08:00
2023-12-06 17:05:07 +08:00
private void _OnRecvRelease(EDReleasePlayerSkill releaseData)
{
2024-07-05 16:14:23 +08:00
EventManager.Instance.Send(EventManager.EventName.Guide_UseSkill);
DebugUtil.LogG("接收到释放技能消息");
2023-12-06 17:05:07 +08:00
isFinished = true;
2024-10-28 15:03:03 +08:00
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;
}
}
2023-12-07 13:08:39 +08:00
if (!posCanRelease)
2023-12-06 17:05:07 +08:00
{
2024-09-26 14:33:36 +08:00
UITipsManager.ShowTips("当前位置不可释放技能");
2023-12-07 19:30:36 +08:00
DebugUtil.Log("当前位置不可释放技能");
2024-07-05 16:14:23 +08:00
EventManager.Instance.Send(EventManager.EventName.Guide_CancelPlayerSkill);
2023-12-06 17:05:07 +08:00
nextState = EPlayerSkillState.Idle;
2023-12-07 13:08:39 +08:00
return;
2023-12-06 17:05:07 +08:00
}
2023-12-07 13:08:39 +08:00
if (releaseData.isCancel)
2023-12-06 17:05:07 +08:00
{
2023-12-07 19:30:36 +08:00
DebugUtil.Log("玩家取消释放技能");
2024-07-05 16:14:23 +08:00
EventManager.Instance.Send(EventManager.EventName.Guide_CancelPlayerSkill);
2023-12-07 13:08:39 +08:00
nextState = EPlayerSkillState.Idle;
return;
2023-12-06 17:05:07 +08:00
}
2023-12-07 19:30:36 +08:00
DebugUtil.Log("玩家释放技能");
2024-07-05 16:14:23 +08:00
EventManager.Instance.Send(EventManager.EventName.Guide_UsePlayerSkill);
2023-12-08 13:28:03 +08:00
owner.selectedCellIndex = _lastMouseCellIndex;
2023-12-07 13:08:39 +08:00
nextState = EPlayerSkillState.Release;
2023-12-06 15:32:45 +08:00
}
private int _GetCurrentMouseCellIndex()
{
var mousePos = Input.mousePosition;
2023-12-07 13:08:39 +08:00
2024-01-10 19:04:01 +08:00
var ray = _mainCamera.ScreenPointToRay(mousePos * PerformanceManager.instance.screenScale);
2023-12-07 19:04:49 +08:00
var raycast = Physics.RaycastNonAlloc(ray, _raycastHits, 1000, _layerMask);
2023-12-06 15:32:45 +08:00
if (raycast <= 0) return -1;
var firstHit = _raycastHits[0];
var hitPoint = firstHit.point;
if (!_map.WorldPosition2TGSCellIndex(hitPoint, out var cellIndex)) return -1;
return cellIndex;
}
2023-12-06 12:22:59 +08:00
}
}