// Copyright (c) Pixel Crushers. All rights reserved.
using UnityEngine;
namespace PixelCrushers.DialogueSystem
{
///
/// Basic slider-based timer for response menus.
///
[AddComponentMenu("")] // Use wrapper.
public class StandardUITimer : MonoBehaviour
{
private UnityEngine.UI.Slider slider = null;
private bool m_isCountingDown = false;
private float m_startTime; // When the timer started.
private float m_duration;
private System.Action m_timeoutHandler;
public virtual void Awake()
{
slider = GetComponent();
}
///
/// Called by the response menu. Starts the timer. Each tick, the UpdateTimeLeft
/// method is called.
///
/// Duration in seconds.
/// Handler to invoke if the timer reaches zero.
public virtual void StartCountdown(float duration, System.Action timeoutHandler)
{
m_isCountingDown = true;
m_startTime = DialogueTime.time;
m_duration = duration;
m_timeoutHandler = timeoutHandler;
}
protected virtual void Update()
{
if (m_isCountingDown)
{
float elapsed = DialogueTime.time - m_startTime;
UpdateTimeLeft(Mathf.Clamp01(1 - (elapsed / m_duration)));
if (elapsed >= m_duration)
{
m_isCountingDown = false;
if (m_timeoutHandler != null) m_timeoutHandler();
}
}
}
public virtual void StopCountdown()
{
m_isCountingDown = false;
m_timeoutHandler = null;
}
///
/// Adjusts the amount of time left.
///
/// Seconds to fast-forward the timer (or rewind the timer if negative).
public void SkipTime(float amountToSkip)
{
m_startTime -= amountToSkip;
}
///
/// Called each tick to update the timer display. The default method updates a UI slider.
///
/// 1 at the start, 0 when the timer times out.
public virtual void UpdateTimeLeft(float normalizedTimeLeft)
{
if (slider == null) return;
slider.value = normalizedTimeLeft;
}
}
}