1 | "use strict";
|
2 | var __extends = (this && this.__extends) || (function () {
|
3 | var extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return function (d, b) {
|
7 | extendStatics(d, b);
|
8 | function __() { this.constructor = d; }
|
9 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
10 | };
|
11 | })();
|
12 | Object.defineProperty(exports, "__esModule", { value: true });
|
13 | var SideralObject_1 = require("./../SideralObject");
|
14 | var Module_1 = require("./../Module");
|
15 | var index_1 = require("./../index");
|
16 | var Tool_1 = require("./../Tool");
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | var Game = (function (_super) {
|
23 | __extends(Game, _super);
|
24 |
|
25 | |
26 |
|
27 |
|
28 | function Game() {
|
29 | var _this = _super.call(this) || this;
|
30 | |
31 |
|
32 |
|
33 | _this.props = {
|
34 | width: 10,
|
35 | height: 10,
|
36 | fullSize: false,
|
37 | backgroundColor: Tool_1.Color.transparent
|
38 | };
|
39 | |
40 |
|
41 |
|
42 | _this.container = document.getElementById("sideral");
|
43 | |
44 |
|
45 |
|
46 |
|
47 | _this.fps = 60;
|
48 | |
49 |
|
50 |
|
51 |
|
52 | _this.latency = 0;
|
53 | |
54 |
|
55 |
|
56 |
|
57 | _this.tick = 1;
|
58 | |
59 |
|
60 |
|
61 |
|
62 | _this.currentUpdate = 0;
|
63 | |
64 |
|
65 |
|
66 |
|
67 | _this.lastUpdate = 0;
|
68 | |
69 |
|
70 |
|
71 | _this.stopped = true;
|
72 | |
73 |
|
74 |
|
75 |
|
76 | _this.loaded = false;
|
77 | |
78 |
|
79 |
|
80 |
|
81 | _this._addQueue = [];
|
82 | |
83 |
|
84 |
|
85 | _this._swapScenes = [];
|
86 | _this._emptyDisplayObject = new index_1.PIXI.DisplayObject();
|
87 | _this.setProps({
|
88 | backgroundColor: Tool_1.Color.black
|
89 | });
|
90 | _this.context.game = _this;
|
91 | _this.setLoadingScene(new Module_1.SceneLoading());
|
92 | _this.signals.propChange.bind(["width", "height", "fullSize"], _this._resizeGame.bind(_this));
|
93 | _this.signals.propChange.bind("backgroundColor", _this._onBackgroundColorChange.bind(_this));
|
94 | return _this;
|
95 | }
|
96 | |
97 |
|
98 |
|
99 | Game.prototype.kill = function () {
|
100 | _super.prototype.kill.call(this);
|
101 | this.removeRenderer();
|
102 | if (this._debounceResize) {
|
103 | window.removeEventListener("resize", this._debounceResize);
|
104 | this._debounceResize = null;
|
105 | }
|
106 | };
|
107 | |
108 |
|
109 |
|
110 |
|
111 | Game.prototype.update = function (performance) {
|
112 | var _this = this;
|
113 | if (this.stopped) {
|
114 | return null;
|
115 | }
|
116 | performance = performance || window.performance.now();
|
117 | requestAnimationFrame(this.update.bind(this));
|
118 |
|
119 | this.currentUpdate = performance;
|
120 | this.latency = Tool_1.Util.limit(performance - this.lastUpdate, 8, 100);
|
121 | this.fps = Math.floor(1000 / this.latency);
|
122 | this.tick = 1000 / (this.fps * 1000);
|
123 | this.tick = this.tick < 0 ? 0 : this.tick;
|
124 | this.children.forEach(function (child) { return child.update(_this.tick); });
|
125 | this.nextCycle();
|
126 | var scenes = this.getScenes();
|
127 | if (this.renderer) {
|
128 | scenes.forEach(function (scene) { return _this.renderer.render(scene.container); });
|
129 | if (!scenes.length) {
|
130 | this.renderer.render(this._emptyDisplayObject);
|
131 | }
|
132 | }
|
133 | this.lastUpdate = window.performance.now();
|
134 | };
|
135 | Game.prototype.nextCycle = function () {
|
136 | var _this = this;
|
137 | _super.prototype.nextCycle.call(this);
|
138 | if (this.loaded) {
|
139 | var queue = this._addQueue.slice(0);
|
140 | this._addQueue = [];
|
141 | queue.forEach(function (q) { return _super.prototype.add.call(_this, q.item, q.props); });
|
142 | this._addQueue.forEach(function (queue) { return _super.prototype.add.call(_this, queue.item, queue.props); });
|
143 | this._swapScenes.forEach(function (swap) { return _this._swapScene(swap); });
|
144 | this._swapScenes = [];
|
145 | if (this._loadingScene) {
|
146 | this._loadingScene.kill();
|
147 | this._loadingScene = null;
|
148 | }
|
149 | }
|
150 | };
|
151 |
|
152 | |
153 |
|
154 |
|
155 | Game.prototype.add = function (item, props) {
|
156 | if (props === void 0) { props = {}; }
|
157 | this._addQueue.push({ item: item, props: props });
|
158 | return item;
|
159 | };
|
160 | |
161 |
|
162 |
|
163 |
|
164 |
|
165 | Game.prototype.setLoadingScene = function (loadingScene) {
|
166 | if (loadingScene && !(loadingScene instanceof Module_1.Scene)) {
|
167 | throw new Error("Game.setLoadingScene: The loadingScene must be an instance of Scene.");
|
168 | }
|
169 | if (this._loadingScene) {
|
170 | this._loadingScene.kill();
|
171 | }
|
172 | if (!loadingScene) {
|
173 | this._loadingScene = null;
|
174 | return null;
|
175 | }
|
176 | this._loadingScene = _super.prototype.add.call(this, loadingScene);
|
177 | return this._loadingScene;
|
178 | };
|
179 | |
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | Game.prototype.loadAssets = function (env, loadingScene, onLoad) {
|
186 | var _this = this;
|
187 | Tool_1.Assets.load(env, function () {
|
188 | var done = function () {
|
189 | _this.loaded = true;
|
190 | if (onLoad) {
|
191 | onLoad();
|
192 | }
|
193 | };
|
194 | if (_this._loadingScene) {
|
195 | _this._loadingScene.onAssetsLoaded(done);
|
196 | }
|
197 | else {
|
198 | done();
|
199 | }
|
200 | }, function (progress) { return _this._loadingScene && _this._loadingScene.signals.progress.dispatch(progress); });
|
201 | };
|
202 | |
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 | Game.prototype.start = function (width, height, container, onLoad) {
|
212 | this.setProps({
|
213 | width: width || this.props.width,
|
214 | height: height || this.props.height
|
215 | });
|
216 | if (!this.props.fullSize && (!this.props.width || !this.props.height)) {
|
217 | throw new Error("Engine.start: You must set 'width', 'height' and a 'container' container");
|
218 | }
|
219 | this.container = container || this.container;
|
220 | this._swapRenderer();
|
221 | this._resizeGame();
|
222 | this._onBackgroundColorChange();
|
223 | this.loadAssets(null, null, onLoad);
|
224 | this.update();
|
225 | return this;
|
226 | };
|
227 | |
228 |
|
229 |
|
230 | Game.prototype.stop = function () {
|
231 | this.stopped = true;
|
232 | this.removeRenderer();
|
233 | };
|
234 | |
235 |
|
236 |
|
237 | Game.prototype.resize = function () {
|
238 | if (!this.renderer) {
|
239 | return null;
|
240 | }
|
241 | this.renderer.resize(this.props.width, this.props.height);
|
242 | };
|
243 | |
244 |
|
245 |
|
246 |
|
247 | Game.prototype.removeRenderer = function (killCanvas) {
|
248 | if (killCanvas === void 0) { killCanvas = true; }
|
249 | if (!this.renderer) {
|
250 | return null;
|
251 | }
|
252 | this.renderer.destroy(killCanvas);
|
253 | this.renderer = null;
|
254 | };
|
255 | |
256 |
|
257 |
|
258 |
|
259 |
|
260 | Game.prototype.enableKeyboard = function (preventInputPropagation) {
|
261 | this.keyboard = this.add(new SideralObject_1.Keyboard());
|
262 | this.keyboard.preventInputPropagation = preventInputPropagation;
|
263 | return this.keyboard;
|
264 | };
|
265 | |
266 |
|
267 |
|
268 | Game.prototype.disableKeyboard = function () {
|
269 | if (this.keyboard) {
|
270 | this.keyboard.kill();
|
271 | }
|
272 | this.keyboard = null;
|
273 | };
|
274 | |
275 |
|
276 |
|
277 |
|
278 | Game.prototype.getScenes = function () {
|
279 | return this.children.filter(function (child) { return child instanceof Module_1.Scene; }).filter(function (child) { return !child.killed; });
|
280 | };
|
281 | |
282 |
|
283 |
|
284 | Game.prototype.attach = function (container) {
|
285 | this.container = container || this.container;
|
286 | if (!this.container) {
|
287 | throw new Error("Game: before start your game, you must set a dom container.");
|
288 | }
|
289 | this.container.appendChild(this.renderer.view);
|
290 | };
|
291 | |
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 | Game.prototype.swapScene = function (currentScene, nextScene, color, duration, onComplete) {
|
298 | if (duration === void 0) { duration = 500; }
|
299 | if (currentScene && nextScene) {
|
300 | this._swapScenes.push({ currentScene: currentScene, nextScene: nextScene, color: color, duration: duration, onComplete: onComplete });
|
301 | }
|
302 | return nextScene;
|
303 | };
|
304 |
|
305 | Game.prototype._swapScene = function (_a) {
|
306 | var _this = this;
|
307 | var currentScene = _a.currentScene, nextScene = _a.nextScene, color = _a.color, duration = _a.duration, onComplete = _a.onComplete;
|
308 | if (!color) {
|
309 | currentScene.kill();
|
310 | _super.prototype.add.call(this, nextScene);
|
311 | if (onComplete) {
|
312 | onComplete(nextScene);
|
313 | }
|
314 | }
|
315 | else {
|
316 | currentScene.fade("out", color, duration, function () {
|
317 | currentScene.kill();
|
318 | _super.prototype.add.call(_this, nextScene);
|
319 | nextScene.fade("in", color, duration, function () {
|
320 | if (onComplete) {
|
321 | onComplete(nextScene);
|
322 | }
|
323 | });
|
324 | });
|
325 | }
|
326 | };
|
327 | |
328 |
|
329 |
|
330 |
|
331 | Game.prototype._resizeGame = function () {
|
332 | var _this = this;
|
333 | if (!this.renderer) {
|
334 | return null;
|
335 | }
|
336 | if (this.props.fullSize) {
|
337 | this.setProps({
|
338 | width: this.renderer.view.parentElement.clientWidth,
|
339 | height: this.renderer.view.parentElement.clientHeight
|
340 | });
|
341 | }
|
342 | this.getScenes().filter(function (scene) { return scene.props.sizeAuto; }).forEach(function (scene) {
|
343 | scene.props.width = _this.props.width;
|
344 | scene.props.height = _this.props.height;
|
345 | });
|
346 | if (!this.props.fullSize && this._debounceResize) {
|
347 | window.removeEventListener("resize", this._debounceResize);
|
348 | this._debounceResize = null;
|
349 | }
|
350 | if (this.props.fullSize && !this._debounceResize) {
|
351 | window.addEventListener("resize", this._debounceResize = Tool_1.Util.debounce(this._resizeGame.bind(this), 250));
|
352 | }
|
353 | this.renderer.resize(this.props.width, this.props.height);
|
354 | };
|
355 | |
356 |
|
357 |
|
358 |
|
359 | Game.prototype._onBackgroundColorChange = function () {
|
360 | var color = Tool_1.Util.colorToDecimal(this.props.backgroundColor), transparent = Boolean(isNaN(color));
|
361 | if (!transparent) {
|
362 | this.renderer.backgroundColor = color;
|
363 | }
|
364 | if (transparent !== this.renderer.transparent) {
|
365 | this._swapRenderer();
|
366 | }
|
367 | };
|
368 | Game.prototype._swapRenderer = function () {
|
369 | var transparent = !this.props.backgroundColor || this.props.backgroundColor === Tool_1.Color.transparent;
|
370 | var view = null;
|
371 | this.removeRenderer();
|
372 | this.renderer = index_1.PIXI.autoDetectRenderer(this.props.width, this.props.height, {
|
373 | autoResize: true,
|
374 | transparent: transparent,
|
375 | backgroundColor: transparent ? 0 : Tool_1.Util.colorToDecimal(this.props.backgroundColor),
|
376 | roundPixels: false,
|
377 | antialias: true
|
378 | });
|
379 | this.attach();
|
380 | this.stopped = false;
|
381 | };
|
382 | return Game;
|
383 | }(SideralObject_1.SideralObject));
|
384 | exports.Game = Game;
|
385 | index_1.PIXI.utils.skipHello();
|