UNPKG

@smoud/tiny

Version:

Fast and tiny JavaScript library for HTML5 game and playable ads creation.

159 lines (123 loc) 3.82 kB
import { EventEmitter } from '../utils/EventEmitter'; import { systems } from './registrar'; var noop = function () {}; var App = function (states) { this.callbackContext = this; this.state = 0; this.timeScale = 1; this.width = 0; this.height = 0; this.systems = []; this.updatable = []; this.paused = false; this.pauseDuration = 0; this.inputView = document.body; if (!Tiny.app) Tiny.app = this; EventEmitter.mixin(this); states = states || {}; this.boot = states.boot || this.boot || noop; this.preload = states.preload || this.preload || noop; this.create = states.create || this.create || noop; this.update = states.update || this.update || noop; this.render = states.render || this.render || noop; this._resize_cb = states.resize || noop; this._destroy_cb = states.destroy || noop; var self = this; setTimeout(function () { self._boot(); }, 0); }; App.prototype._boot = function () { for (var i = 0; i < systems.length; i++) { var system = systems[i]; var _sys_ = new system._class_(this); this.systems.push(_sys_); if (_sys_.update) this.updatable.push(_sys_); if (system.name) this[system.name] = _sys_; } if (Tiny.RAF) { this.raf = new Tiny.RAF(this); } this.boot.call(this.callbackContext); var self = this; setTimeout(function () { if (self.load) self._preload(); else self._create(); }, 0); }; App.prototype._preload = function () { this.preload.call(this.callbackContext); this.state = 1; this.load.start(this._create); }; App.prototype._create = function () { this.emit('load'); this.create.call(this.callbackContext); if (this.raf) { this.raf.start(); } this.state = 2; }; App.prototype.pause = function () { if (this.raf) { this.raf.reset(); } if (!this.paused) { for (var i = 0; i < this.systems.length; i++) { if (this.systems[i].pause) this.systems[i].pause(); } this.paused = true; } }; App.prototype.resume = function () { if (this.raf) { this.raf.reset(); } if (this.paused) { for (var i = 0; i < this.systems.length; i++) { if (this.systems[i].resume) this.systems[i].resume(); } this.paused = false; } }; App.prototype._update = function (time, delta) { if (!this.paused) { delta *= this.timeScale; this.update.call(this.callbackContext, time, delta); this.emit('update', delta); for (var i = 0; i < this.updatable.length; i++) { this.updatable[i].update(delta); } } else { this.pauseDuration += delta; } this.render(); this.emit('postrender'); }; App.prototype.resize = function (width, height) { this.width = width || this.width; this.height = height || this.height; if (this.state > 0) { this._resize_cb.call(this.callbackContext, this.width, this.height); this.emit('resize', width, height); } var self = this; setTimeout(function () { if (self.input) self.input.updateBounds(); }, 0); }; App.prototype.destroy = function (clearCache) { for (var i = 0; i < this.systems.length; i++) { if (this.systems[i].destroy) this.systems[i].destroy(clearCache); } this.paused = true; if (clearCache) { this.load.clearCache(); } if (this.raf) { this.raf.stop(); } this._destroy_cb.call(this.callbackContext); if (Tiny.app === this) Tiny.app = null; }; export { App };