Newer
Older
lostmynuts / shared / js / Engine / Utils / SimpleTimer.js
Engine.SimpleTimer = class
{
    constructor(time, callback, loop /* = false*/, useRealTime /* = false*/)
    {
		if (loop === undefined) loop = false;
		if (useRealTime === undefined) useRealTime = false;
		
		this.useRealTime = useRealTime;
		
        this.hasTime = false;
        this.t = 0;
        this.tSeconds = 0;
        this.active = false;
        this.onUpdate = null;
        this.onFinish = null;
        this.onSecondsChanged = null;
        this.timeScale = 1;
        this.ignoreFirstUpdate = true;
        this.duration = 0;
        this.loop = loop ? loop : false;
		
        this.pauseCount = 0;

		if (time !== undefined && callback !== undefined)
		{			
			this.start(time, callback, loop, this.useRealTime);
		}
    }

    get timeLeft() { return this.t; }
    get paused() { return this.pauseCount > 0;}

    start(time, callback, loop /* = false*/, useRealTime /* = false*/)
    {
        if (loop === undefined) loop = false;
		if (useRealTime === undefined) useRealTime = false;
		
		this.useRealTime = useRealTime;

        this.hasTime = true;
        this.loop = loop;
        this.onFinish = callback;
        this.duration = time;
        this.t = time;
        this.setTSeconds();

        Engine.EnterFrameManagerInstance.add(this, this.update, this.useRealTime);
        this.active = true;
        this.ignoreFirstUpdate = true;
    }

    setTSeconds()
    {
        var oldTSeconds = this.tSeconds;
        this.tSeconds = Math.max(0, Math.ceil(this.t));
        if (oldTSeconds != this.tSeconds && this.onSecondsChanged != null)
        {
            this.onSecondsChanged(this.tSeconds);
        }
    }

    stop()
    {
        if (!this.active) return;
        Engine.EnterFrameManagerInstance.remove(this);
        this.active = false;
        this.onFinish = null;
        this.onUpdate = null;
    }

    pause()
    {
        if (!this.active) return;
        this.pauseCount++;
        if (this.pauseCount == 1)
        {
            Engine.EnterFrameManagerInstance.remove(this);
        }
    }

    resume()
    {
        if (this.pauseCount == 0 || !this.hasTime) return;
        this.pauseCount--;
        if (this.pauseCount == 0)
        {
            Engine.EnterFrameManagerInstance.add(this, this.update, this.useRealTime);
        }
    }

    setTime(t)
    {
        this.t = t;
        this.setTSeconds();
    }

    addTime(a)
    {
        this.t += a;
        this.setTSeconds();
    }

    update(timeElapsed)
    {
        if (this.ignoreFirstUpdate)
        {
            this.ignoreFirstUpdate = false;
            return;
        }

        if (!this.active) return;
        if (this.paused) return;

        this.t -= (timeElapsed * this.timeScale);
        this.setTSeconds();

        if(this.onUpdate != null) this.onUpdate(this.duration - this.t);

        if (this.t <= 0)
        {
            var actualTime = this.duration - this.t;
            if (this.loop)
            {
                this.t += this.duration;
                this.setTSeconds();
            }
            else
            {
                this.t = 0;
                this.setTSeconds();

                this.active = false;
                Engine.EnterFrameManagerInstance.remove(this);
            }

            if (this.onFinish) this.onFinish();
        }
    }

}