// Copyright (C) 2023 Nicholas Maltbie
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
// associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute,
// sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
using System;
using nickmaltbie.TestUtilsUnity;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Serialization;
namespace nickmaltbie.OpenKCC.Input
{
///
/// Buffered input that supports a delta of time between when the button
/// is pressed and when the action is considered performed. Also supports
/// a cooldown between action resets.
///
[Serializable]
public class BufferedInput
{
///
/// Unity service for getting unity inputs in testable manner.
///
public IUnityService unityService = UnityService.Instance;
///
/// Action associated with this input.
///
[SerializeField]
[FormerlySerializedAs("inputAction")]
[Tooltip("Input action to control this buffered input.")]
public InputActionReference inputActionReference;
///
/// Override input action for testing.
///
private InputAction overrideInputAction;
///
/// Gets the input action associated with this buffered input.
///
public InputAction InputAction
{
get => overrideInputAction ?? inputActionReference;
set => overrideInputAction = value;
}
///
/// Buffer time for configuring the player input.
///
[SerializeField]
[Tooltip("Total buffer time for player input.")]
public float bufferTime;
///
/// Cooldown time between input actions.
///
[SerializeField]
[Tooltip("Total buffer time for player input.")]
public float cooldown = 0.1f;
///
/// Amount of time elapsed since the player pressed the input.
///
private float elapsedSincePressed = Mathf.Infinity;
///
/// Amount fo elapsed time since the input was reset.
///
private float elapsedSinceReset = Mathf.Infinity;
///
/// Is the input currently active.
///
public bool Pressed => elapsedSinceReset > cooldown && elapsedSincePressed < bufferTime;
///
/// Add delta time to update the buffered input state.
///
/// Delta time to add in seconds.
public void Update()
{
elapsedSincePressed += unityService.deltaTime;
elapsedSinceReset += unityService.deltaTime;
if (InputAction?.IsPressed() ?? false)
{
elapsedSincePressed = 0.0f;
}
}
///
/// Reset the cooldown for this button.
///
public void ResetCooldown()
{
if (cooldown > 0)
{
elapsedSinceReset = 0;
}
}
///
/// Reset the active state for this button.
///
public void Reset()
{
elapsedSincePressed = Mathf.Infinity;
elapsedSinceReset = 0;
}
}
}