using System;
namespace Funique
{
///
/// Counter
/// ------------------------------------------------
/// 計時器
///
public sealed class Counter
{
// 欄位
#region Field
float timer;
float pass;
bool called;
bool loop;
#endregion
// 事件
#region Event
///
/// Called when times up
/// ------------------------------------------------
/// 當時間到時 會呼叫
///
public event Action OnTimeUp;
#endregion
///
/// Timer is the end point, second as unit
/// If loop is on, it will auto reset
/// ------------------------------------------------
/// timer 是計時器的終點, 以秒為單位
/// 如果 loop 為真, 計時器會自動重置
///
/// Second
/// Auto reset
public Counter(float timer, bool loop = false)
{
this.timer = timer;
this.pass = 0;
this.called = false;
this.loop = loop;
}
// 功能
#region Utility
///
/// Reset the timer
/// ------------------------------------------------
/// 重置計時器
///
public void Reset()
{
pass = 0;
called = false;
}
///
/// Put this method in update function
/// ------------------------------------------------
/// 把這個函式放在更新事件中
///
///
public void Update(float delta)
{
pass += delta;
if (pass >= timer && !called)
{
if (OnTimeUp != null) OnTimeUp.Invoke();
called = true;
// Automatically reset
if (loop) Reset();
}
}
#endregion
}
}