1
0
Fork 0

implement ActionType with void actions

This commit is contained in:
lightling 2022-07-11 18:17:41 -04:00
parent 2bae0582f9
commit b517bd6b87
Signed by: lightling
GPG key ID: 016F11E0AA296B67
2 changed files with 29 additions and 6 deletions

View file

@ -15,6 +15,8 @@ namespace Goldenwere.GWSU.CTRL.CHAR.InputSystemModule
public Action<InputAction.CallbackContext> started; public Action<InputAction.CallbackContext> started;
} }
public bool preferToggledInputs;
private Dictionary<Guid, Callbacks> registeredCallbacks; private Dictionary<Guid, Callbacks> registeredCallbacks;
private Dictionary<Guid, Callbacks> RegisteredCallbacks private Dictionary<Guid, Callbacks> RegisteredCallbacks
@ -33,16 +35,35 @@ namespace Goldenwere.GWSU.CTRL.CHAR.InputSystemModule
{ {
if (TryGetReference(_action.Emitter, out InputActionReference _reference)) if (TryGetReference(_action.Emitter, out InputActionReference _reference))
{ {
Action<InputAction.CallbackContext> canceled = (InputAction.CallbackContext ctx) =>
{
switch (_action.Type)
{
case ModuleActionType.HeldOrPressed:
if (!preferToggledInputs)
{
_action.Listener.Invoke();
}
break;
case ModuleActionType.Held:
_action.Listener.Invoke();
break;
}
};
Action<InputAction.CallbackContext> performed = (InputAction.CallbackContext ctx) => Action<InputAction.CallbackContext> performed = (InputAction.CallbackContext ctx) =>
{ {
_action.Listener.Invoke(); _action.Listener.Invoke();
}; };
_reference.action.canceled += canceled;
_reference.action.performed += performed; _reference.action.performed += performed;
RegisteredCallbacks.Add RegisteredCallbacks.Add
( (
_action.GUID, _action.GUID,
new Callbacks new Callbacks
{ {
canceled = canceled,
performed = performed, performed = performed,
} }
); );
@ -102,7 +123,9 @@ namespace Goldenwere.GWSU.CTRL.CHAR.InputSystemModule
{ {
if (CheckForCallback(_id)) if (CheckForCallback(_id))
{ {
_reference.action.canceled -= RegisteredCallbacks[_id].canceled;
_reference.action.performed -= RegisteredCallbacks[_id].performed; _reference.action.performed -= RegisteredCallbacks[_id].performed;
_reference.action.started -= RegisteredCallbacks[_id].started;
RegisteredCallbacks.Remove(_id); RegisteredCallbacks.Remove(_id);
} }
} }

View file

@ -10,34 +10,34 @@ namespace Goldenwere.GWSU.CTRL.CHAR.InputSystemModule.SampleProject
[SerializeField] private ScriptableObject emitterOnJump; [SerializeField] private ScriptableObject emitterOnJump;
[SerializeField] private ScriptableObject emitterOnMovement; [SerializeField] private ScriptableObject emitterOnMovement;
private BaseModuleAction<bool> actionOnCrouch; private BaseModuleAction actionOnCrouch;
private BaseModuleAction actionOnJump; private BaseModuleAction actionOnJump;
private BaseModuleAction<Vector2> actionOnMovement; private BaseModuleAction<Vector2> actionOnMovement;
private void Awake() private void Awake()
{ {
actionOnCrouch = new BaseModuleAction<bool>(OnCrouch, emitterOnCrouch, ModuleActionType.HeldOrPressed); actionOnCrouch = new BaseModuleAction(OnCrouch, emitterOnCrouch, ModuleActionType.HeldOrPressed);
actionOnJump = new BaseModuleAction(OnJump, emitterOnJump, ModuleActionType.Pressed); actionOnJump = new BaseModuleAction(OnJump, emitterOnJump, ModuleActionType.Pressed);
actionOnMovement = new BaseModuleAction<Vector2>(OnMovement, emitterOnJump, ModuleActionType.Held); actionOnMovement = new BaseModuleAction<Vector2>(OnMovement, emitterOnJump, ModuleActionType.Held);
} }
private void OnEnable() private void OnEnable()
{ {
//module.RegisterModuleAction(actionOnCrouch); module.RegisterModuleAction(actionOnCrouch);
module.RegisterModuleAction(actionOnJump); module.RegisterModuleAction(actionOnJump);
//module.RegisterModuleAction(actionOnMovement); //module.RegisterModuleAction(actionOnMovement);
} }
private void OnDisable() private void OnDisable()
{ {
//module.UnregisterModuleAction(actionOnCrouch); module.UnregisterModuleAction(actionOnCrouch);
module.UnregisterModuleAction(actionOnJump); module.UnregisterModuleAction(actionOnJump);
//module.UnregisterModuleAction(actionOnMovement); //module.UnregisterModuleAction(actionOnMovement);
} }
private void OnCrouch(bool pressed) private void OnCrouch()
{ {
Debug.Log($"Crouch was {(pressed ? "pressed" : "released")}"); Debug.Log($"Crouched");
} }
private void OnJump() private void OnJump()