结构大修改,改成朴实无华的结构,不过度架构。能跑就行
This commit is contained in:
222
Assets/Scripts/Fishing2~/Common/InputComponent.cs
Normal file
222
Assets/Scripts/Fishing2~/Common/InputComponent.cs
Normal file
@@ -0,0 +1,222 @@
|
||||
using System;
|
||||
using Fantasy.Entitas;
|
||||
using Fantasy.Entitas.Interface;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace NBF.Fishing2
|
||||
{
|
||||
public enum ControllerType
|
||||
{
|
||||
KeyboardMouse = 0,
|
||||
GamePad = 1
|
||||
}
|
||||
|
||||
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 event Action<InputAction.CallbackContext> OnPlayerValuePerformed;
|
||||
|
||||
/// <summary>
|
||||
/// 执行输入事件完毕
|
||||
/// </summary>
|
||||
public event Action<InputAction.CallbackContext> OnPlayerValueCanceled;
|
||||
|
||||
/// <summary>
|
||||
/// 触发交互游戏对象
|
||||
/// </summary>
|
||||
public event Action<InteractiveObject> OnInteractiveObjectAction;
|
||||
|
||||
/// <summary>
|
||||
/// 手柄输入
|
||||
/// </summary>
|
||||
public static bool IsControllerInput;
|
||||
|
||||
/// <summary>
|
||||
/// ui阻止游戏输入
|
||||
/// </summary>
|
||||
public static bool IsUIStopInput;
|
||||
|
||||
|
||||
#region Event
|
||||
|
||||
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;
|
||||
}
|
||||
else if (action.type == InputActionType.Value)
|
||||
{
|
||||
action.performed += self.OnInputPlayerValuePerformed;
|
||||
action.canceled += self.OnInputPlayerValueCanceled;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
else if (action.type == InputActionType.Value)
|
||||
{
|
||||
action.performed -= self.OnInputPlayerValuePerformed;
|
||||
action.canceled -= self.OnInputPlayerValueCanceled;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public void OnUIButtonPerformed(InputAction.CallbackContext context)
|
||||
{
|
||||
OnUIPerformed?.Invoke(context.action.name);
|
||||
}
|
||||
|
||||
private void OnUIButtonCanceled(InputAction.CallbackContext context)
|
||||
{
|
||||
OnUICanceled?.Invoke(context.action.name);
|
||||
}
|
||||
|
||||
private 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);
|
||||
}
|
||||
|
||||
private 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);
|
||||
}
|
||||
|
||||
private void OnInputPlayerValuePerformed(InputAction.CallbackContext context)
|
||||
{
|
||||
if (IsUIStopInput) return;
|
||||
OnPlayerValuePerformed?.Invoke(context);
|
||||
}
|
||||
|
||||
private void OnInputPlayerValueCanceled(InputAction.CallbackContext context)
|
||||
{
|
||||
if (IsUIStopInput) return;
|
||||
OnPlayerValueCanceled?.Invoke(context);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
|
||||
public Vector2 GetMovementInput()
|
||||
{
|
||||
if (IsUIStopInput) return Vector2.zero;
|
||||
return PlayerInputControl.Player.Move?.ReadValue<Vector2>() ?? Vector2.zero;
|
||||
}
|
||||
|
||||
public Vector2 GetLookInput()
|
||||
{
|
||||
if (IsUIStopInput) return Vector2.zero;
|
||||
return PlayerInputControl.Player.Look?.ReadValue<Vector2>() ?? Vector2.zero;
|
||||
}
|
||||
|
||||
public void SendUIInput(string actionName)
|
||||
{
|
||||
OnUIPerformed?.Invoke(actionName);
|
||||
OnUICanceled?.Invoke(actionName);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user