// Copyright (c) 2024 Synty Studios Limited. All rights reserved.
//
// Use of this software is subject to the terms and conditions of the Synty Studios End User Licence Agreement (EULA)
// available at: https://syntystore.com/pages/end-user-licence-agreement
//
// Sample scripts are included only as examples and are not intended as production-ready.
using System;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Serialization;
namespace Synty.AnimationBaseLocomotion.Samples.InputSystem
{
public class InputReader : MonoBehaviour, Controls.IPlayerActions
{
public Vector2 _mouseDelta;
public Vector2 _moveComposite;
public float _movementInputDuration;
public bool _movementInputDetected;
private Controls _controls;
public Action onAimActivated;
public Action onAimDeactivated;
public Action onCrouchActivated;
public Action onCrouchDeactivated;
public Action onJumpPerformed;
public Action onLockOnToggled;
public Action onSprintActivated;
public Action onSprintDeactivated;
public Action onWalkToggled;
///
private void OnEnable()
{
if (_controls == null)
{
_controls = new Controls();
_controls.Player.SetCallbacks(this);
}
_controls.Player.Enable();
}
///
public void OnDisable()
{
_controls.Player.Disable();
}
///
/// Defines the action to perform when the OnLook callback is called.
///
/// The context of the callback.
public void OnLook(InputAction.CallbackContext context)
{
_mouseDelta = context.ReadValue();
}
///
/// Defines the action to perform when the OnMove callback is called.
///
/// The context of the callback.
public void OnMove(InputAction.CallbackContext context)
{
_moveComposite = context.ReadValue();
_movementInputDetected = _moveComposite.magnitude > 0;
}
///
/// Defines the action to perform when the OnJump callback is called.
///
/// The context of the callback.
public void OnJump(InputAction.CallbackContext context)
{
if (!context.performed)
{
return;
}
onJumpPerformed?.Invoke();
}
///
/// Defines the action to perform when the OnToggleWalk callback is called.
///
/// The context of the callback.
public void OnToggleWalk(InputAction.CallbackContext context)
{
if (!context.performed)
{
return;
}
onWalkToggled?.Invoke();
}
///
/// Defines the action to perform when the OnSprint callback is called.
///
/// The context of the callback.
public void OnSprint(InputAction.CallbackContext context)
{
if (context.started)
{
onSprintActivated?.Invoke();
}
else if (context.canceled)
{
onSprintDeactivated?.Invoke();
}
}
///
/// Defines the action to perform when the OnCrouch callback is called.
///
/// The context of the callback.
public void OnCrouch(InputAction.CallbackContext context)
{
if (context.started)
{
onCrouchActivated?.Invoke();
}
else if (context.canceled)
{
onCrouchDeactivated?.Invoke();
}
}
///
/// Defines the action to perform when the OnAim callback is called.
///
/// The context of the callback.
public void OnAim(InputAction.CallbackContext context)
{
if (context.started)
{
onAimActivated?.Invoke();
}
if (context.canceled)
{
onAimDeactivated?.Invoke();
}
}
///
/// Defines the action to perform when the OnLockOn callback is called.
///
/// The context of the callback.
public void OnLockOn(InputAction.CallbackContext context)
{
if (!context.performed)
{
return;
}
onLockOnToggled?.Invoke();
onSprintDeactivated?.Invoke();
}
}
}