Files
Fishing2/Assets/Scripts/Fishing2/Camera/InputComponent.cs
2025-09-05 00:12:46 +08:00

156 lines
5.1 KiB
C#

using System;
using NBC.Entitas;
using NBC.Entitas.Interface;
using UnityEngine.InputSystem;
namespace NBF.Fishing2
{
public class InputComponent : Entity
{
public PlayerInputControl PlayerInputControl { get; set; }
/// <summary>
/// 执行输入事件
/// </summary>
public event Action<string> OnUIPerformed;
/// <summary>
/// 执行输入事件完毕
/// </summary>
public event Action<string> OnUICanceled;
/// <summary>
/// 执行输入事件
/// </summary>
public event Action<string> OnPlayerPerformed;
/// <summary>
/// 执行输入事件完毕
/// </summary>
public event Action<string> OnPlayerCanceled;
/// <summary>
/// 手柄输入
/// </summary>
public static bool IsControllerInput;
/// <summary>
/// ui阻止游戏输入
/// </summary>
public static bool IsUIStopInput;
public void OnUIButtonPerformed(InputAction.CallbackContext context)
{
OnUIPerformed?.Invoke(context.action.name);
}
public void OnUIButtonCanceled(InputAction.CallbackContext context)
{
OnUICanceled?.Invoke(context.action.name);
}
public void OnPlayerButtonPerformed(InputAction.CallbackContext context)
{
if (IsUIStopInput) return;
var actionName = context.action.name;
// if (actionName == "Op1")
// {
// OnOp1Action?.Invoke(true);
// }
// else if (actionName == "Op2")
// {
// OnOp2Action?.Invoke(true);
// }
OnPlayerPerformed?.Invoke(actionName);
}
public void OnPlayerButtonCanceled(InputAction.CallbackContext context)
{
if (IsUIStopInput) return;
var actionName = context.action.name;
// if (actionName == "Op1")
// {
// OnOp1Action?.Invoke(false);
// }
// else if (actionName == "Op2")
// {
// OnOp2Action?.Invoke(false);
// }
//
OnPlayerCanceled?.Invoke(actionName);
}
}
public static class InputComponentSystem
{
public class InputComponentAwakeSystem : AwakeSystem<InputComponent>
{
protected override void Awake(InputComponent self)
{
self.PlayerInputControl = new PlayerInputControl();
self.PlayerInputControl.Enable();
foreach (var actionMap in self.PlayerInputControl.asset.actionMaps)
{
actionMap.Enable();
if (actionMap.name == "UI")
{
foreach (var action in actionMap.actions)
{
if (action.type == InputActionType.Button)
{
action.performed += self.OnUIButtonPerformed;
action.canceled += self.OnUIButtonCanceled;
}
}
}
else if (actionMap.name == "Player")
{
foreach (var action in actionMap.actions)
{
if (action.type == InputActionType.Button)
{
action.performed += self.OnPlayerButtonPerformed;
action.canceled += self.OnPlayerButtonCanceled;
}
}
}
}
}
}
public class InputComponentDestroySystem : DestroySystem<InputComponent>
{
protected override void Destroy(InputComponent self)
{
foreach (var actionMap in self.PlayerInputControl.asset.actionMaps)
{
actionMap.Enable();
if (actionMap.name == "UI")
{
foreach (var action in actionMap.actions)
{
if (action.type == InputActionType.Button)
{
action.performed -= self.OnUIButtonPerformed;
action.canceled -= self.OnUIButtonCanceled;
}
}
}
else if (actionMap.name == "Player")
{
foreach (var action in actionMap.actions)
{
if (action.type == InputActionType.Button)
{
action.performed -= self.OnPlayerButtonPerformed;
action.canceled -= self.OnPlayerButtonCanceled;
}
}
}
}
}
}
}
}