revival/game/Assets/Scripts/Input/BufferedInput.cs

71 lines
1.4 KiB
C#
Raw Normal View History

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public struct BufferedInput
{
public BufferedInput( float bufferTime )
{
_bufferTime = bufferTime;
_value = false;
2021-02-15 19:09:32 +01:00
_timeSincePress = Mathf.Infinity;
_timeSinceRelease = Mathf.Infinity;
}
private float _bufferTime;
private bool _value;
private float _timeSinceRelease;
private float _timeSincePress;
public void Set( bool newValue )
{
if ( newValue && !_value )
{
_timeSincePress = 0f;
2021-02-15 19:09:32 +01:00
}
else if ( !newValue && _value )
{
_timeSinceRelease = 0f;
}
_value = newValue;
}
public void Update( float deltaTime )
{
_timeSincePress += deltaTime;
2021-02-15 19:09:32 +01:00
_timeSinceRelease += deltaTime;
}
public bool GetValue()
{
return _value || _timeSinceRelease <= _bufferTime;
}
2021-02-15 19:09:32 +01:00
public bool GetRawValue()
{
return _value;
}
public bool GetDown( bool consume = true )
{
2021-02-15 19:09:32 +01:00
bool value = _timeSincePress <= _bufferTime;
if ( consume && value )
_timeSincePress = Mathf.Infinity;
return value;
}
2021-02-15 19:09:32 +01:00
public bool GetRelease( bool consume = true )
{
2021-02-15 19:09:32 +01:00
bool value = _timeSinceRelease <= _bufferTime;
if ( consume && value )
_timeSinceRelease = Mathf.Infinity;
return value;
}
}