changed: rebuilt player input management
This commit is contained in:
@@ -1,49 +1,54 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NodeCanvas.Framework;
|
||||
using ParadoxNotion;
|
||||
using ParadoxNotion.Design;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
|
||||
namespace NodeCanvas.Tasks.Conditions {
|
||||
|
||||
[Category("Reset/Input")]
|
||||
[Description("Check if input condition was matched this frame")]
|
||||
[Description("Check if input condition was matched this frame by phase.")]
|
||||
public class CheckInput : ConditionTask<Transform>{
|
||||
public BBParameter<string> actionName;
|
||||
public BBParameter<string> actionValue;
|
||||
|
||||
public BBParameter<InputActionPhase> actionPhase;
|
||||
|
||||
private SignalDefinition signalDefinition;
|
||||
protected override string info {
|
||||
get { return "Player Input"; }
|
||||
get { return $"Input <b>{actionName.value}</b> was <b>{actionPhase.value}</b>"; }
|
||||
}
|
||||
|
||||
private InputAction action;
|
||||
|
||||
//Use for initialization. This is called only once in the lifetime of the task.
|
||||
//Return null if init was successfull. Return an error string otherwise
|
||||
|
||||
protected override string OnInit(){
|
||||
try {
|
||||
signalDefinition = Resources.Load<SignalDefinition>("InputSignal");
|
||||
} catch (Exception e) {
|
||||
Debug.LogError($"Error finding the Input Signal defintion: {e.Message}");
|
||||
throw;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
//Called whenever the condition gets enabled.
|
||||
protected override void OnEnable(){
|
||||
action = agent.GetComponent<PlayerInput>().actions.FindAction(actionName.value);
|
||||
protected override void OnEnable() {
|
||||
signalDefinition.onInvoke -= OnSignalInvoke;
|
||||
signalDefinition.onInvoke += OnSignalInvoke;
|
||||
}
|
||||
|
||||
//Called whenever the condition gets disabled.
|
||||
protected override void OnDisable() {
|
||||
|
||||
signalDefinition.onInvoke -= OnSignalInvoke;
|
||||
}
|
||||
|
||||
//Called once per frame while the condition is active.
|
||||
//Return whether the condition is success or failure.
|
||||
protected override bool OnCheck() {
|
||||
// if (action.type == InputActionType.Button){
|
||||
if (action.WasPressedThisFrame()) { return true; }
|
||||
// } else if (action.type == InputActionType.Value) {
|
||||
//
|
||||
// }
|
||||
|
||||
return false;
|
||||
|
||||
void OnSignalInvoke(Transform sender, Transform receiver, bool isGlobal, params object[] args){
|
||||
// Take the input arguments as objects and convert them to InputAction & Phases
|
||||
InputAction thisAction = (InputAction)args[0];
|
||||
InputActionPhase thisActionPhase = (InputActionPhase)args[1];
|
||||
|
||||
if (actionName.value == thisAction.name && actionPhase.value == thisActionPhase) {
|
||||
YieldReturn(true);
|
||||
}
|
||||
}
|
||||
|
||||
protected override bool OnCheck() { return false; }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using UnityEditor;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
@@ -6,11 +7,15 @@ using UnityEngine.UIElements;
|
||||
using NodeCanvas;
|
||||
using NodeCanvas.Framework;
|
||||
using ParadoxNotion;
|
||||
using Sirenix.OdinInspector;
|
||||
|
||||
public class PlayerControls : MonoBehaviour{
|
||||
// References
|
||||
private Player thisPlayer;
|
||||
|
||||
private PlayerInput input;
|
||||
public SignalDefinition inputSignal;
|
||||
|
||||
// TODO: Turn these into accessors
|
||||
public Vector2 rawMoveInput;
|
||||
public Vector2 rawLookInput;
|
||||
@@ -20,6 +25,31 @@ public class PlayerControls : MonoBehaviour{
|
||||
void Awake(){
|
||||
thisPlayer = GetComponent<Player>();
|
||||
graph = GetComponent<GraphOwner>();
|
||||
input = GetComponent<PlayerInput>();
|
||||
|
||||
// Add the delegates for each method
|
||||
foreach (InputAction action in input.actions) {
|
||||
action.started += SendToGraph;
|
||||
action.canceled += SendToGraph;
|
||||
action.performed += SendToGraph;
|
||||
}
|
||||
}
|
||||
|
||||
// Remove the delegates for each method
|
||||
void OnDisable(){
|
||||
foreach (InputAction action in input.actions) {
|
||||
action.started -= SendToGraph;
|
||||
action.canceled -= SendToGraph;
|
||||
action.performed -= SendToGraph;
|
||||
}
|
||||
}
|
||||
|
||||
// This will call the OnSignalInvoke for this type of Signal Defintion. CheckInput is the recieving end.
|
||||
public void SendToGraph(InputAction.CallbackContext ctx){
|
||||
inputSignal.Invoke(transform, transform, false, new object[]{
|
||||
ctx.action,
|
||||
ctx.phase
|
||||
});
|
||||
}
|
||||
|
||||
public void OnMove(InputValue value){
|
||||
|
||||
Reference in New Issue
Block a user