@smoud/tiny
Version:
Fast and tiny JavaScript library for HTML5 game and playable ads creation.
111 lines (99 loc) • 3.47 kB
JavaScript
import { registerSystem } from './registrar';
var noop = function () {};
var Timer = function (autoStart, autoRemove, game, cb, ctx, delay, loop, n, oncomplete) {
this.game = game;
this.cb = cb || noop;
this.ctx = ctx || this;
this.delay = delay == undefined ? 1000 : delay;
this.loop = loop;
this.count = n || 0;
this.repeat = this.count > 0;
this.running = !!autoStart;
this._lastFrame = 0;
this.autoRemove = autoRemove;
this.onComplete = oncomplete || noop;
};
Timer.prototype = {
start: function () {
this.running = true;
},
pause: function () {
this.running = false;
},
stop: function () {
this.running = false;
this._lastFrame = 0;
},
update: function (deltaTime) {
if (this.running) {
this._lastFrame += deltaTime;
if (this._lastFrame >= this.delay) {
this.cb.call(this.ctx);
this._lastFrame = 0;
if (this.repeat) {
this.count--;
if (this.count === 0) {
this.running = false;
this.autoRemove && this.game.timer.remove(this);
this.onComplete();
}
} else if (!this.loop) {
this.running = false;
this.autoRemove && this.game.timer.remove(this);
}
}
}
}
};
var TimerCreator = function (game) {
this.game = game;
this.list = [];
this.autoStart = true;
this.autoRemove = true;
};
TimerCreator.prototype = {
update: function (delta) {
for (var i = 0; i < this.list.length; i++) {
this.list[i].update(delta);
}
},
removeAll: function () {
for (var i = 0; i < this.list.length; i++) {
this.list[i].stop();
}
this.list = [];
},
remove: function (tm) {
var indexOf = this.list.indexOf(tm);
if (indexOf > -1) {
tm.stop();
this.list.splice(indexOf, 1);
}
},
add: function (delay, cb, ctx, autostart, autoremove) {
autostart = autostart != undefined ? autostart : this.autoStart;
autoremove = autoremove != undefined ? autoremove : this.autoRemove;
var timer = new Timer(autostart, autoremove, this.game, cb, ctx, delay);
this.list.push(timer);
return timer;
},
loop: function (delay, cb, ctx, autostart, autoremove) {
autostart = autostart != undefined ? autostart : this.autoStart;
autoremove = autoremove != undefined ? autoremove : this.autoRemove;
var timer = new Timer(autostart, autoremove, this.game, cb, ctx, delay, true);
this.list.push(timer);
return timer;
},
repeat: function (delay, n, cb, ctx, autostart, autoremove, complete) {
autostart = autostart != undefined ? autostart : this.autoStart;
autoremove = autoremove != undefined ? autoremove : this.autoRemove;
var timer = new Timer(autostart, autoremove, this.game, cb, ctx, delay, false, n, complete);
this.list.push(timer);
return timer;
},
destroy: function () {
this.removeAll();
}
};
registerSystem('timer', TimerCreator);
export { TimerCreator, Timer };