Newer
Older
lostmynuts / shared / js / Engine / Utils / TimerGroup.js
Engine.TimerGroup = class
{

	constructor()
	{
		this.timerPool = new Engine.ObjectPool(Engine.SimpleTimer);
		this.activeTimers = [];
	}

	clear()
	{
		for(var timer of this.activeTimers)
		{
			timer.stop();
			timer.onFinish = null;
			this.timerPool.returnObject(timer);
		}
		this.activeTimers.clear();
	}

	addNew(time /*float*/, callback /*SimpleTimerTick*/, loop /*bool*/)
	{
		var timer = this.timerPool.getNextObject();
		this.activeTimers.push(timer);
		
		timer.onStop = () =>
		{
			timer.onFinish = null;
			this.activeTimers.remove(timer);
			this.timerPool.returnObject(timer);
		};

		timer.start(time, () =>
            {
                if (!loop)
                {
                    timer.onFinish = null;
                    this.activeTimers.remove(timer);
                    this.timerPool.returnObject(timer);
                }
                if (callback != null) callback();
            }, loop);
		
		return timer;
	}

}