@eva/eva.js 1.2.7-fix.3 → 1.2.7-fix.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3015 @@
1
+ function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
2
+ import { __extends as __extends$1, __spread, __values, __decorate, __awaiter, __generator } from 'tslib';
3
+ import EventEmitter from 'eventemitter3';
4
+ import { isEqual, isObject } from 'lodash-es';
5
+ import { type, step } from '@eva/inspector-decorator';
6
+ import { requestAnimationFrame, windowAlias, navigator, documentAlias, XMLHttpRequestAlias } from '@eva/miniprogram-adapter';
7
+ function getComponentName(component) {
8
+ if (component instanceof Component) {
9
+ return component.name;
10
+ } else if (component instanceof Function) {
11
+ return component.componentName;
12
+ }
13
+ }
14
+ var Component = function (_super) {
15
+ __extends$1(Component, _super);
16
+ function Component(params) {
17
+ var _this = _super.call(this) || this;
18
+ _this.started = false;
19
+ _this.name = _this.constructor.componentName;
20
+ _this.__componentDefaultParams = params;
21
+ return _this;
22
+ }
23
+ return Component;
24
+ }(EventEmitter);
25
+ var Component$1 = Component;
26
+ var ObserverType;
27
+ (function (ObserverType) {
28
+ ObserverType["ADD"] = "ADD";
29
+ ObserverType["REMOVE"] = "REMOVE";
30
+ ObserverType["CHANGE"] = "CHANGE";
31
+ })(ObserverType || (ObserverType = {}));
32
+ var objectCache = {};
33
+ var systemInstance = {};
34
+ var observerInfos = {};
35
+ var componentProps = {};
36
+ function getObjectCache(component, keys) {
37
+ if (!objectCache[component.gameObject.id]) {
38
+ objectCache[component.gameObject.id] = {};
39
+ }
40
+ var cache = objectCache[component.gameObject.id];
41
+ var key = component.name + '_' + keys.join(',');
42
+ if (cache[key]) {
43
+ return cache[key];
44
+ }
45
+ var keyIndex = keys.length - 1;
46
+ var property = component;
47
+ for (var i = 0; i < keyIndex; i++) {
48
+ property = property[keys[i]];
49
+ }
50
+ cache[key] = {
51
+ property: property,
52
+ key: keys[keyIndex]
53
+ };
54
+ return cache[key];
55
+ }
56
+ function removeObjectCache(component) {
57
+ if (component.gameObject) {
58
+ delete objectCache[component.gameObject.id];
59
+ }
60
+ }
61
+ function addObserver(_a) {
62
+ var _b, _c;
63
+ var systemName = _a.systemName,
64
+ componentName = _a.componentName,
65
+ component = _a.component,
66
+ prop = _a.prop,
67
+ type = _a.type;
68
+ (_c = (_b = systemInstance[systemName]) === null || _b === void 0 ? void 0 : _b.componentObserver) === null || _c === void 0 ? void 0 : _c.add({
69
+ component: component,
70
+ prop: prop,
71
+ type: type,
72
+ componentName: componentName
73
+ });
74
+ }
75
+ function pushToQueue(_a) {
76
+ var prop = _a.prop,
77
+ component = _a.component,
78
+ componentName = _a.componentName;
79
+ for (var systemName in observerInfos) {
80
+ var observerInfo = observerInfos[systemName] || {};
81
+ var info = observerInfo[componentName];
82
+ if (!info) continue;
83
+ var index = info.findIndex(function (p) {
84
+ return isEqual(p, prop);
85
+ });
86
+ if (index > -1) {
87
+ addObserver({
88
+ systemName: systemName,
89
+ componentName: componentName,
90
+ component: component,
91
+ prop: prop,
92
+ type: ObserverType.CHANGE
93
+ });
94
+ }
95
+ }
96
+ }
97
+ function defineProperty(_a) {
98
+ var e_1, _b;
99
+ var obj = _a.obj,
100
+ key = _a.key,
101
+ prop = _a.prop,
102
+ component = _a.component,
103
+ componentName = _a.componentName;
104
+ if (obj === undefined) {
105
+ return;
106
+ }
107
+ if (!(key in obj)) {
108
+ console.error("prop " + key + " not in component: " + componentName + ", Can not observer");
109
+ return;
110
+ }
111
+ Object.defineProperty(obj, "_" + key, {
112
+ enumerable: false,
113
+ writable: true,
114
+ value: obj[key]
115
+ });
116
+ if (prop.deep && isObject(obj[key])) {
117
+ try {
118
+ for (var _c = __values(Object.keys(obj[key])), _d = _c.next(); !_d.done; _d = _c.next()) {
119
+ var childKey = _d.value;
120
+ defineProperty({
121
+ obj: obj[key],
122
+ key: childKey,
123
+ prop: prop,
124
+ component: component,
125
+ componentName: componentName
126
+ });
127
+ }
128
+ } catch (e_1_1) {
129
+ e_1 = {
130
+ error: e_1_1
131
+ };
132
+ } finally {
133
+ try {
134
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
135
+ } finally {
136
+ if (e_1) throw e_1.error;
137
+ }
138
+ }
139
+ }
140
+ Object.defineProperty(obj, key, {
141
+ enumerable: true,
142
+ set: function set(val) {
143
+ if (obj["_" + key] === val) return;
144
+ obj["_" + key] = val;
145
+ pushToQueue({
146
+ prop: prop,
147
+ component: component,
148
+ componentName: componentName
149
+ });
150
+ },
151
+ get: function get() {
152
+ return obj["_" + key];
153
+ }
154
+ });
155
+ }
156
+ function isComponent(comp) {
157
+ return comp && comp.constructor && 'componentName' in comp.constructor;
158
+ }
159
+ function initObserver(Systems) {
160
+ var e_2, _a, e_3, _b;
161
+ var Ss = [];
162
+ if (Systems instanceof Array) {
163
+ Ss.push.apply(Ss, __spread(Systems));
164
+ } else {
165
+ Ss.push(Systems);
166
+ }
167
+ try {
168
+ for (var Ss_1 = __values(Ss), Ss_1_1 = Ss_1.next(); !Ss_1_1.done; Ss_1_1 = Ss_1.next()) {
169
+ var S = Ss_1_1.value;
170
+ for (var componentName in S.observerInfo) {
171
+ componentProps[componentName] = componentProps[componentName] || [];
172
+ var props = componentProps[componentName];
173
+ var _loop_1 = function _loop_1(prop) {
174
+ var index = props.findIndex(function (p) {
175
+ return isEqual(p, prop);
176
+ });
177
+ if (index === -1) {
178
+ componentProps[componentName].push(prop);
179
+ }
180
+ };
181
+ try {
182
+ for (var _c = (e_3 = void 0, __values(S.observerInfo[componentName])), _d = _c.next(); !_d.done; _d = _c.next()) {
183
+ var prop = _d.value;
184
+ _loop_1(prop);
185
+ }
186
+ } catch (e_3_1) {
187
+ e_3 = {
188
+ error: e_3_1
189
+ };
190
+ } finally {
191
+ try {
192
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
193
+ } finally {
194
+ if (e_3) throw e_3.error;
195
+ }
196
+ }
197
+ }
198
+ }
199
+ } catch (e_2_1) {
200
+ e_2 = {
201
+ error: e_2_1
202
+ };
203
+ } finally {
204
+ try {
205
+ if (Ss_1_1 && !Ss_1_1.done && (_a = Ss_1.return)) _a.call(Ss_1);
206
+ } finally {
207
+ if (e_2) throw e_2.error;
208
+ }
209
+ }
210
+ }
211
+ function observer(component, componentName) {
212
+ var e_4, _a;
213
+ if (componentName === void 0) {
214
+ componentName = component.name;
215
+ }
216
+ if (!componentName || !componentProps[componentName]) {
217
+ return;
218
+ }
219
+ if (!component || !isComponent(component)) {
220
+ throw new Error('component param must be an instance of Component');
221
+ }
222
+ if (!component.gameObject || !component.gameObject.id) {
223
+ throw new Error('component should be add to a gameObject');
224
+ }
225
+ try {
226
+ for (var _b = __values(componentProps[componentName]), _c = _b.next(); !_c.done; _c = _b.next()) {
227
+ var item = _c.value;
228
+ var _d = getObjectCache(component, item.prop),
229
+ property = _d.property,
230
+ key = _d.key;
231
+ defineProperty({
232
+ obj: property,
233
+ key: key,
234
+ prop: item,
235
+ component: component,
236
+ componentName: componentName
237
+ });
238
+ }
239
+ } catch (e_4_1) {
240
+ e_4 = {
241
+ error: e_4_1
242
+ };
243
+ } finally {
244
+ try {
245
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
246
+ } finally {
247
+ if (e_4) throw e_4.error;
248
+ }
249
+ }
250
+ }
251
+ function observerAdded(component, componentName) {
252
+ var _a, _b;
253
+ if (componentName === void 0) {
254
+ componentName = component.name;
255
+ }
256
+ for (var systemName in observerInfos) {
257
+ var observerInfo = observerInfos[systemName] || {};
258
+ var info = observerInfo[componentName];
259
+ if (info) {
260
+ (_b = (_a = systemInstance[systemName]) === null || _a === void 0 ? void 0 : _a.componentObserver) === null || _b === void 0 ? void 0 : _b.add({
261
+ component: component,
262
+ type: ObserverType.ADD,
263
+ componentName: componentName
264
+ });
265
+ }
266
+ }
267
+ }
268
+ function observerRemoved(component, componentName) {
269
+ var _a, _b;
270
+ if (componentName === void 0) {
271
+ componentName = component.name;
272
+ }
273
+ for (var systemName in observerInfos) {
274
+ var observerInfo = observerInfos[systemName] || {};
275
+ var info = observerInfo[componentName];
276
+ if (info) {
277
+ (_b = (_a = systemInstance[systemName]) === null || _a === void 0 ? void 0 : _a.componentObserver) === null || _b === void 0 ? void 0 : _b.add({
278
+ component: component,
279
+ type: ObserverType.REMOVE,
280
+ componentName: componentName
281
+ });
282
+ }
283
+ }
284
+ removeObjectCache(component);
285
+ }
286
+ function setSystemObserver(system, S) {
287
+ observerInfos[S.systemName] = S.observerInfo;
288
+ systemInstance[S.systemName] = system;
289
+ }
290
+ var Transform = function (_super) {
291
+ __extends$1(Transform, _super);
292
+ function Transform() {
293
+ var _this = _super !== null && _super.apply(this, arguments) || this;
294
+ _this.name = 'Transform';
295
+ _this._parent = null;
296
+ _this.inScene = false;
297
+ _this.children = [];
298
+ _this.position = {
299
+ x: 0,
300
+ y: 0
301
+ };
302
+ _this.size = {
303
+ width: 0,
304
+ height: 0
305
+ };
306
+ _this.origin = {
307
+ x: 0,
308
+ y: 0
309
+ };
310
+ _this.anchor = {
311
+ x: 0,
312
+ y: 0
313
+ };
314
+ _this.scale = {
315
+ x: 1,
316
+ y: 1
317
+ };
318
+ _this.skew = {
319
+ x: 0,
320
+ y: 0
321
+ };
322
+ _this.rotation = 0;
323
+ return _this;
324
+ }
325
+ Transform.prototype.init = function (params) {
326
+ var e_1, _a;
327
+ if (params === void 0) {
328
+ params = {};
329
+ }
330
+ var props = ['position', 'size', 'origin', 'anchor', 'scale', 'skew'];
331
+ try {
332
+ for (var props_1 = __values(props), props_1_1 = props_1.next(); !props_1_1.done; props_1_1 = props_1.next()) {
333
+ var key = props_1_1.value;
334
+ _extends(this[key], params[key]);
335
+ }
336
+ } catch (e_1_1) {
337
+ e_1 = {
338
+ error: e_1_1
339
+ };
340
+ } finally {
341
+ try {
342
+ if (props_1_1 && !props_1_1.done && (_a = props_1.return)) _a.call(props_1);
343
+ } finally {
344
+ if (e_1) throw e_1.error;
345
+ }
346
+ }
347
+ this.rotation = params.rotation || this.rotation;
348
+ };
349
+ Object.defineProperty(Transform.prototype, "parent", {
350
+ get: function get() {
351
+ return this._parent;
352
+ },
353
+ set: function set(val) {
354
+ if (val) {
355
+ val.addChild(this);
356
+ } else if (this.parent) {
357
+ this.parent.removeChild(this);
358
+ }
359
+ },
360
+ enumerable: false,
361
+ configurable: true
362
+ });
363
+ Transform.prototype.addChild = function (child) {
364
+ if (child.parent === this) {
365
+ var index = this.children.findIndex(function (item) {
366
+ return item === child;
367
+ });
368
+ this.children.splice(index, 1);
369
+ } else if (child.parent) {
370
+ child.parent.removeChild(child);
371
+ }
372
+ child._parent = this;
373
+ this.children.push(child);
374
+ };
375
+ Transform.prototype.removeChild = function (child) {
376
+ var index = this.children.findIndex(function (item) {
377
+ return item === child;
378
+ });
379
+ if (index > -1) {
380
+ this.children.splice(index, 1);
381
+ child._parent = null;
382
+ }
383
+ };
384
+ Transform.prototype.clearChildren = function () {
385
+ this.children.length = 0;
386
+ };
387
+ Transform.componentName = 'Transform';
388
+ __decorate([type('vector2'), step(1)], Transform.prototype, "position", void 0);
389
+ __decorate([type('size'), step(1)], Transform.prototype, "size", void 0);
390
+ __decorate([type('vector2'), step(0.1)], Transform.prototype, "origin", void 0);
391
+ __decorate([type('vector2'), step(0.1)], Transform.prototype, "anchor", void 0);
392
+ __decorate([type('vector2'), step(0.1)], Transform.prototype, "scale", void 0);
393
+ __decorate([type('vector2'), step(0.1)], Transform.prototype, "skew", void 0);
394
+ __decorate([type('number'), step(0.1)], Transform.prototype, "rotation", void 0);
395
+ return Transform;
396
+ }(Component$1);
397
+ var Transform$1 = Transform;
398
+ var _id = 0;
399
+ function getId() {
400
+ return ++_id;
401
+ }
402
+ var GameObject = function () {
403
+ function GameObject(name, obj) {
404
+ this._componentCache = {};
405
+ this.components = [];
406
+ this.destroyed = false;
407
+ this._name = name;
408
+ this.id = getId();
409
+ this.addComponent(Transform$1, obj);
410
+ }
411
+ Object.defineProperty(GameObject.prototype, "transform", {
412
+ get: function get() {
413
+ return this.getComponent(Transform$1);
414
+ },
415
+ enumerable: false,
416
+ configurable: true
417
+ });
418
+ Object.defineProperty(GameObject.prototype, "parent", {
419
+ get: function get() {
420
+ return this.transform && this.transform.parent && this.transform.parent.gameObject;
421
+ },
422
+ enumerable: false,
423
+ configurable: true
424
+ });
425
+ Object.defineProperty(GameObject.prototype, "name", {
426
+ get: function get() {
427
+ return this._name;
428
+ },
429
+ enumerable: false,
430
+ configurable: true
431
+ });
432
+ Object.defineProperty(GameObject.prototype, "scene", {
433
+ get: function get() {
434
+ return this._scene;
435
+ },
436
+ set: function set(val) {
437
+ var e_1, _a;
438
+ if (this._scene === val) return;
439
+ var scene = this._scene;
440
+ this._scene = val;
441
+ if (this.transform && this.transform.children) {
442
+ try {
443
+ for (var _b = __values(this.transform.children), _c = _b.next(); !_c.done; _c = _b.next()) {
444
+ var child = _c.value;
445
+ child.gameObject.scene = val;
446
+ }
447
+ } catch (e_1_1) {
448
+ e_1 = {
449
+ error: e_1_1
450
+ };
451
+ } finally {
452
+ try {
453
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
454
+ } finally {
455
+ if (e_1) throw e_1.error;
456
+ }
457
+ }
458
+ }
459
+ if (val) {
460
+ val.addGameObject(this);
461
+ } else {
462
+ scene && scene.removeGameObject(this);
463
+ }
464
+ },
465
+ enumerable: false,
466
+ configurable: true
467
+ });
468
+ GameObject.prototype.addChild = function (gameObject) {
469
+ if (!gameObject || !gameObject.transform || gameObject === this) return;
470
+ if (!(gameObject instanceof GameObject)) {
471
+ throw new Error('addChild only receive GameObject');
472
+ }
473
+ if (!this.transform) {
474
+ throw new Error("gameObject '" + this.name + "' has been destroy");
475
+ }
476
+ gameObject.transform.parent = this.transform;
477
+ gameObject.scene = this.scene;
478
+ };
479
+ GameObject.prototype.removeChild = function (gameObject) {
480
+ if (!(gameObject instanceof GameObject) || !gameObject.parent || gameObject.parent !== this) {
481
+ return gameObject;
482
+ }
483
+ gameObject.transform.parent = null;
484
+ gameObject.scene = null;
485
+ return gameObject;
486
+ };
487
+ GameObject.prototype.addComponent = function (C, obj) {
488
+ if (this.destroyed) return;
489
+ var componentName = getComponentName(C);
490
+ if (this._componentCache[componentName]) return;
491
+ var component;
492
+ if (C instanceof Function) {
493
+ component = new C(obj);
494
+ } else if (C instanceof Component$1) {
495
+ component = C;
496
+ } else {
497
+ throw new Error('addComponent recieve Component and Component Constructor');
498
+ }
499
+ if (component.gameObject) {
500
+ throw new Error("component has been added on gameObject " + component.gameObject.name);
501
+ }
502
+ component.gameObject = this;
503
+ component.init && component.init(component.__componentDefaultParams);
504
+ observerAdded(component, component.name);
505
+ observer(component, component.name);
506
+ this.components.push(component);
507
+ this._componentCache[componentName] = component;
508
+ component.awake && component.awake();
509
+ return component;
510
+ };
511
+ GameObject.prototype.removeComponent = function (c) {
512
+ var componentName;
513
+ if (typeof c === 'string') {
514
+ componentName = c;
515
+ } else if (c instanceof Component$1) {
516
+ componentName = c.name;
517
+ } else if (c.componentName) {
518
+ componentName = c.componentName;
519
+ }
520
+ if (componentName === 'Transform') {
521
+ throw new Error("Transform can't be removed");
522
+ }
523
+ return this._removeComponent(componentName);
524
+ };
525
+ GameObject.prototype._removeComponent = function (componentName) {
526
+ var index = this.components.findIndex(function (_a) {
527
+ var name = _a.name;
528
+ return name === componentName;
529
+ });
530
+ if (index === -1) return;
531
+ var component = this.components.splice(index, 1)[0];
532
+ delete this._componentCache[componentName];
533
+ delete component.__componentDefaultParams;
534
+ component.onDestroy && component.onDestroy();
535
+ observerRemoved(component, componentName);
536
+ component.gameObject = undefined;
537
+ return component;
538
+ };
539
+ GameObject.prototype.getComponent = function (c) {
540
+ var componentName;
541
+ if (typeof c === 'string') {
542
+ componentName = c;
543
+ } else if (c instanceof Component$1) {
544
+ componentName = c.name;
545
+ } else if (c.componentName) {
546
+ componentName = c.componentName;
547
+ }
548
+ if (typeof this._componentCache[componentName] !== 'undefined') {
549
+ return this._componentCache[componentName];
550
+ } else {
551
+ return;
552
+ }
553
+ };
554
+ GameObject.prototype.remove = function () {
555
+ if (this.parent) return this.parent.removeChild(this);
556
+ };
557
+ GameObject.prototype.destroy = function () {
558
+ if (!this.transform) {
559
+ console.error('Cannot destroy gameObject that have already been destroyed.');
560
+ return;
561
+ }
562
+ Array.from(this.transform.children).forEach(function (_a) {
563
+ var gameObject = _a.gameObject;
564
+ gameObject.destroy();
565
+ });
566
+ this.remove();
567
+ this.transform.clearChildren();
568
+ for (var key in this._componentCache) {
569
+ this._removeComponent(key);
570
+ }
571
+ this.components.length = 0;
572
+ this.destroyed = true;
573
+ };
574
+ return GameObject;
575
+ }();
576
+ var GameObject$1 = GameObject;
577
+ var ComponentObserver = function () {
578
+ function ComponentObserver() {
579
+ this.events = [];
580
+ }
581
+ ComponentObserver.prototype.add = function (_a) {
582
+ var component = _a.component,
583
+ prop = _a.prop,
584
+ type = _a.type,
585
+ componentName = _a.componentName;
586
+ if (type === ObserverType.REMOVE) {
587
+ if (this.events.find(function (changed) {
588
+ return changed.component === component && changed.type === ObserverType.ADD;
589
+ })) {
590
+ this.events = this.events.filter(function (changed) {
591
+ return changed.component !== component;
592
+ });
593
+ return;
594
+ }
595
+ this.events = this.events.filter(function (changed) {
596
+ return changed.component !== component;
597
+ });
598
+ }
599
+ var index = this.events.findIndex(function (changed) {
600
+ return changed.component === component && isEqual(changed.prop, prop) && changed.type === type;
601
+ });
602
+ if (index > -1) {
603
+ this.events.splice(index, 1);
604
+ }
605
+ this.events.push({
606
+ gameObject: component.gameObject,
607
+ component: component,
608
+ prop: prop,
609
+ type: type,
610
+ componentName: componentName
611
+ });
612
+ };
613
+ ComponentObserver.prototype.getChanged = function () {
614
+ return this.events;
615
+ };
616
+ Object.defineProperty(ComponentObserver.prototype, "changed", {
617
+ get: function get() {
618
+ return this.events;
619
+ },
620
+ enumerable: false,
621
+ configurable: true
622
+ });
623
+ ComponentObserver.prototype.clear = function () {
624
+ var events = this.events;
625
+ this.events = [];
626
+ return events;
627
+ };
628
+ return ComponentObserver;
629
+ }();
630
+ var ComponentObserver$1 = ComponentObserver;
631
+ var System = function () {
632
+ function System(params) {
633
+ this.started = false;
634
+ this.componentObserver = new ComponentObserver$1();
635
+ this.__systemDefaultParams = params;
636
+ this.name = this.constructor.systemName;
637
+ }
638
+ System.prototype.destroy = function () {
639
+ var _a;
640
+ this.componentObserver = null;
641
+ this.__systemDefaultParams = null;
642
+ (_a = this.onDestroy) === null || _a === void 0 ? void 0 : _a.call(this);
643
+ };
644
+ return System;
645
+ }();
646
+ var System$1 = System;
647
+ function createNowTime() {
648
+ var nowtime = null;
649
+ if (Date.now) {
650
+ nowtime = Date.now;
651
+ } else {
652
+ nowtime = function nowtime() {
653
+ return new Date().getTime();
654
+ };
655
+ }
656
+ return nowtime;
657
+ }
658
+ var _nowtime = createNowTime();
659
+ var defaultOptions$1 = {
660
+ originTime: 0,
661
+ playbackRate: 1.0
662
+ };
663
+ var Timeline = function () {
664
+ function Timeline(options, parent) {
665
+ if (options instanceof Timeline) {
666
+ parent = options;
667
+ options = {};
668
+ }
669
+ options = _extends({}, defaultOptions$1, options);
670
+ if (parent) {
671
+ this._parent = parent;
672
+ }
673
+ this._createTime = _nowtime();
674
+ this._timeMark = [{
675
+ globalTime: this.globalTime,
676
+ localTime: -options.originTime,
677
+ entropy: -options.originTime,
678
+ playbackRate: options.playbackRate,
679
+ globalEntropy: 0
680
+ }];
681
+ if (this._parent) {
682
+ this._timeMark[0].globalEntropy = this._parent.entropy;
683
+ }
684
+ this._playbackRate = options.playbackRate;
685
+ }
686
+ Object.defineProperty(Timeline.prototype, "globalTime", {
687
+ get: function get() {
688
+ return this.parent ? this.parent.currentTime : _nowtime() - this._createTime;
689
+ },
690
+ enumerable: false,
691
+ configurable: true
692
+ });
693
+ Object.defineProperty(Timeline.prototype, "parent", {
694
+ get: function get() {
695
+ return this._parent;
696
+ },
697
+ enumerable: false,
698
+ configurable: true
699
+ });
700
+ Object.defineProperty(Timeline.prototype, "lastTimeMark", {
701
+ get: function get() {
702
+ return this._timeMark[this._timeMark.length - 1];
703
+ },
704
+ enumerable: false,
705
+ configurable: true
706
+ });
707
+ Timeline.prototype.markTime = function (_a) {
708
+ var _b = _a === void 0 ? {} : _a,
709
+ _c = _b.time,
710
+ time = _c === void 0 ? this.currentTime : _c,
711
+ _d = _b.entropy,
712
+ entropy = _d === void 0 ? this.entropy : _d,
713
+ _e = _b.playbackRate,
714
+ playbackRate = _e === void 0 ? this.playbackRate : _e;
715
+ var timeMark = {
716
+ globalTime: this.globalTime,
717
+ localTime: time,
718
+ entropy: entropy,
719
+ playbackRate: playbackRate,
720
+ globalEntropy: this.globalEntropy
721
+ };
722
+ this._timeMark.push(timeMark);
723
+ };
724
+ Object.defineProperty(Timeline.prototype, "currentTime", {
725
+ get: function get() {
726
+ var _a = this.lastTimeMark,
727
+ localTime = _a.localTime,
728
+ globalTime = _a.globalTime;
729
+ return localTime + (this.globalTime - globalTime) * this.playbackRate;
730
+ },
731
+ set: function set(time) {
732
+ this.markTime({
733
+ time: time
734
+ });
735
+ },
736
+ enumerable: false,
737
+ configurable: true
738
+ });
739
+ Object.defineProperty(Timeline.prototype, "globalEntropy", {
740
+ get: function get() {
741
+ return this._parent ? this._parent.entropy : this.globalTime;
742
+ },
743
+ enumerable: false,
744
+ configurable: true
745
+ });
746
+ Object.defineProperty(Timeline.prototype, "entropy", {
747
+ get: function get() {
748
+ var _a = this.lastTimeMark,
749
+ entropy = _a.entropy,
750
+ globalEntropy = _a.globalEntropy;
751
+ return entropy + Math.abs((this.globalEntropy - globalEntropy) * this.playbackRate);
752
+ },
753
+ set: function set(entropy) {
754
+ if (this.entropy > entropy) {
755
+ var idx = this.seekTimeMark(entropy);
756
+ this._timeMark.length = idx + 1;
757
+ }
758
+ this.markTime({
759
+ entropy: entropy
760
+ });
761
+ },
762
+ enumerable: false,
763
+ configurable: true
764
+ });
765
+ Timeline.prototype.fork = function (options) {
766
+ return new Timeline(options, this);
767
+ };
768
+ Timeline.prototype.seekGlobalTime = function (seekEntropy) {
769
+ var idx = this.seekTimeMark(seekEntropy),
770
+ timeMark = this._timeMark[idx];
771
+ var entropy = timeMark.entropy,
772
+ playbackRate = timeMark.playbackRate,
773
+ globalTime = timeMark.globalTime;
774
+ return globalTime + (seekEntropy - entropy) / Math.abs(playbackRate);
775
+ };
776
+ Timeline.prototype.seekLocalTime = function (seekEntropy) {
777
+ var idx = this.seekTimeMark(seekEntropy),
778
+ timeMark = this._timeMark[idx];
779
+ var localTime = timeMark.localTime,
780
+ entropy = timeMark.entropy,
781
+ playbackRate = timeMark.playbackRate;
782
+ if (playbackRate > 0) {
783
+ return localTime + (seekEntropy - entropy);
784
+ }
785
+ return localTime - (seekEntropy - entropy);
786
+ };
787
+ Timeline.prototype.seekTimeMark = function (entropy) {
788
+ var timeMark = this._timeMark;
789
+ var l = 0,
790
+ r = timeMark.length - 1;
791
+ if (entropy <= timeMark[l].entropy) {
792
+ return l;
793
+ }
794
+ if (entropy >= timeMark[r].entropy) {
795
+ return r;
796
+ }
797
+ var m = Math.floor((l + r) / 2);
798
+ while (m > l && m < r) {
799
+ if (entropy === timeMark[m].entropy) {
800
+ return m;
801
+ }
802
+ if (entropy < timeMark[m].entropy) {
803
+ r = m;
804
+ } else if (entropy > timeMark[m].entropy) {
805
+ l = m;
806
+ }
807
+ m = Math.floor((l + r) / 2);
808
+ }
809
+ return l;
810
+ };
811
+ Object.defineProperty(Timeline.prototype, "playbackRate", {
812
+ get: function get() {
813
+ return this._playbackRate;
814
+ },
815
+ set: function set(rate) {
816
+ if (rate !== this.playbackRate) {
817
+ this.markTime({
818
+ playbackRate: rate
819
+ });
820
+ this._playbackRate = rate;
821
+ }
822
+ },
823
+ enumerable: false,
824
+ configurable: true
825
+ });
826
+ Object.defineProperty(Timeline.prototype, "paused", {
827
+ get: function get() {
828
+ if (this.playbackRate === 0) return true;
829
+ var parent = this.parent;
830
+ while (parent) {
831
+ if (parent.playbackRate === 0) return true;
832
+ parent = parent.parent;
833
+ }
834
+ return false;
835
+ },
836
+ enumerable: false,
837
+ configurable: true
838
+ });
839
+ return Timeline;
840
+ }();
841
+ var Timeline$1 = Timeline;
842
+ var defaultOptions = {
843
+ autoStart: true,
844
+ frameRate: 60
845
+ };
846
+ var Ticker = function () {
847
+ function Ticker(options) {
848
+ var _this = this;
849
+ options = _extends({}, defaultOptions, options);
850
+ this._frameCount = 0;
851
+ this._frameDuration = 1000 / options.frameRate;
852
+ this.autoStart = options.autoStart;
853
+ this.frameRate = options.frameRate;
854
+ this.timeline = new Timeline$1({
855
+ originTime: 0,
856
+ playbackRate: 1.0
857
+ });
858
+ this._lastFrameTime = this.timeline.currentTime;
859
+ this._tickers = new Set();
860
+ this._requestId = null;
861
+ this._ticker = function () {
862
+ if (_this._started) {
863
+ _this._requestId = requestAnimationFrame(_this._ticker);
864
+ _this.update();
865
+ }
866
+ };
867
+ if (this.autoStart) {
868
+ this.start();
869
+ }
870
+ }
871
+ Ticker.prototype.update = function () {
872
+ var e_1, _a;
873
+ var currentTime = this.timeline.currentTime;
874
+ var durationTime = currentTime - this._lastFrameTime;
875
+ if (durationTime >= this._frameDuration) {
876
+ var frameTime = currentTime - durationTime % this._frameDuration;
877
+ var deltaTime = frameTime - this._lastFrameTime;
878
+ this._lastFrameTime = frameTime;
879
+ var options = {
880
+ deltaTime: deltaTime,
881
+ time: frameTime,
882
+ currentTime: frameTime,
883
+ frameCount: ++this._frameCount,
884
+ fps: Math.round(1000 / deltaTime)
885
+ };
886
+ try {
887
+ for (var _b = __values(this._tickers), _c = _b.next(); !_c.done; _c = _b.next()) {
888
+ var func = _c.value;
889
+ if (typeof func === 'function') {
890
+ func(options);
891
+ }
892
+ }
893
+ } catch (e_1_1) {
894
+ e_1 = {
895
+ error: e_1_1
896
+ };
897
+ } finally {
898
+ try {
899
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
900
+ } finally {
901
+ if (e_1) throw e_1.error;
902
+ }
903
+ }
904
+ }
905
+ };
906
+ Ticker.prototype.add = function (fn) {
907
+ this._tickers.add(fn);
908
+ };
909
+ Ticker.prototype.remove = function (fn) {
910
+ this._tickers.delete(fn);
911
+ };
912
+ Ticker.prototype.start = function () {
913
+ if (this._started) return;
914
+ this._started = true;
915
+ this.timeline.playbackRate = 1.0;
916
+ this._requestId = requestAnimationFrame(this._ticker);
917
+ };
918
+ Ticker.prototype.pause = function () {
919
+ this._started = false;
920
+ this.timeline.playbackRate = 0;
921
+ };
922
+ Ticker.prototype.setPlaybackRate = function (rate) {
923
+ this.timeline.playbackRate = rate;
924
+ };
925
+ return Ticker;
926
+ }();
927
+ var Ticker$1 = Ticker;
928
+ var Scene = function (_super) {
929
+ __extends$1(Scene, _super);
930
+ function Scene(name, obj) {
931
+ var _this = _super.call(this, name, obj) || this;
932
+ _this.gameObjects = [];
933
+ _this.scene = _this;
934
+ return _this;
935
+ }
936
+ Scene.prototype.addGameObject = function (gameObject) {
937
+ this.gameObjects.push(gameObject);
938
+ if (gameObject.transform) {
939
+ gameObject.transform.inScene = true;
940
+ }
941
+ };
942
+ Scene.prototype.removeGameObject = function (gameObject) {
943
+ var index = this.gameObjects.indexOf(gameObject);
944
+ if (index === -1) return;
945
+ if (gameObject.transform) {
946
+ gameObject.transform.inScene = false;
947
+ }
948
+ this.gameObjects.splice(index, 1);
949
+ };
950
+ Scene.prototype.destroy = function () {
951
+ this.scene = null;
952
+ _super.prototype.destroy.call(this);
953
+ this.gameObjects = null;
954
+ this.canvas = null;
955
+ };
956
+ return Scene;
957
+ }(GameObject$1);
958
+ var Scene$1 = Scene;
959
+ var LOAD_SCENE_MODE;
960
+ (function (LOAD_SCENE_MODE) {
961
+ LOAD_SCENE_MODE["SINGLE"] = "SINGLE";
962
+ LOAD_SCENE_MODE["MULTI_CANVAS"] = "MULTI_CANVAS";
963
+ })(LOAD_SCENE_MODE || (LOAD_SCENE_MODE = {}));
964
+ var triggerStart = function triggerStart(obj) {
965
+ if (!(obj instanceof System$1) && !(obj instanceof Component$1)) return;
966
+ if (obj.started) return;
967
+ obj.started = true;
968
+ try {
969
+ obj.start && obj.start();
970
+ } catch (e) {
971
+ if (obj instanceof Component$1) {
972
+ console.error(obj.constructor.componentName + " start error", e);
973
+ } else {
974
+ console.error(obj.constructor.systemName + " start error", e);
975
+ }
976
+ }
977
+ };
978
+ var getAllGameObjects = function getAllGameObjects(game) {
979
+ var e_1, _a;
980
+ var _b;
981
+ var mainSceneGameObjects = ((_b = game === null || game === void 0 ? void 0 : game.scene) === null || _b === void 0 ? void 0 : _b.gameObjects) || [];
982
+ var gameObjectsArray = game === null || game === void 0 ? void 0 : game.multiScenes.map(function (_a) {
983
+ var gameObjects = _a.gameObjects;
984
+ return gameObjects;
985
+ });
986
+ var otherSceneGameObjects = [];
987
+ try {
988
+ for (var gameObjectsArray_1 = __values(gameObjectsArray), gameObjectsArray_1_1 = gameObjectsArray_1.next(); !gameObjectsArray_1_1.done; gameObjectsArray_1_1 = gameObjectsArray_1.next()) {
989
+ var gameObjects = gameObjectsArray_1_1.value;
990
+ otherSceneGameObjects = __spread(otherSceneGameObjects, gameObjects);
991
+ }
992
+ } catch (e_1_1) {
993
+ e_1 = {
994
+ error: e_1_1
995
+ };
996
+ } finally {
997
+ try {
998
+ if (gameObjectsArray_1_1 && !gameObjectsArray_1_1.done && (_a = gameObjectsArray_1.return)) _a.call(gameObjectsArray_1);
999
+ } finally {
1000
+ if (e_1) throw e_1.error;
1001
+ }
1002
+ }
1003
+ return __spread(mainSceneGameObjects, otherSceneGameObjects);
1004
+ };
1005
+ var gameObjectLoop = function gameObjectLoop(e, gameObjects) {
1006
+ var e_2, _a, e_3, _b, e_4, _c, e_5, _d;
1007
+ if (gameObjects === void 0) {
1008
+ gameObjects = [];
1009
+ }
1010
+ try {
1011
+ for (var gameObjects_1 = __values(gameObjects), gameObjects_1_1 = gameObjects_1.next(); !gameObjects_1_1.done; gameObjects_1_1 = gameObjects_1.next()) {
1012
+ var gameObject = gameObjects_1_1.value;
1013
+ try {
1014
+ for (var _e = (e_3 = void 0, __values(gameObject.components)), _f = _e.next(); !_f.done; _f = _e.next()) {
1015
+ var component = _f.value;
1016
+ try {
1017
+ triggerStart(component);
1018
+ component.update && component.update(e);
1019
+ } catch (e) {
1020
+ console.error("gameObject: " + gameObject.name + " " + component.name + " update error", e);
1021
+ }
1022
+ }
1023
+ } catch (e_3_1) {
1024
+ e_3 = {
1025
+ error: e_3_1
1026
+ };
1027
+ } finally {
1028
+ try {
1029
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
1030
+ } finally {
1031
+ if (e_3) throw e_3.error;
1032
+ }
1033
+ }
1034
+ }
1035
+ } catch (e_2_1) {
1036
+ e_2 = {
1037
+ error: e_2_1
1038
+ };
1039
+ } finally {
1040
+ try {
1041
+ if (gameObjects_1_1 && !gameObjects_1_1.done && (_a = gameObjects_1.return)) _a.call(gameObjects_1);
1042
+ } finally {
1043
+ if (e_2) throw e_2.error;
1044
+ }
1045
+ }
1046
+ try {
1047
+ for (var gameObjects_2 = __values(gameObjects), gameObjects_2_1 = gameObjects_2.next(); !gameObjects_2_1.done; gameObjects_2_1 = gameObjects_2.next()) {
1048
+ var gameObject = gameObjects_2_1.value;
1049
+ try {
1050
+ for (var _g = (e_5 = void 0, __values(gameObject.components)), _h = _g.next(); !_h.done; _h = _g.next()) {
1051
+ var component = _h.value;
1052
+ try {
1053
+ component.lateUpdate && component.lateUpdate(e);
1054
+ } catch (e) {
1055
+ console.error("gameObject: " + gameObject.name + " " + component.name + " lateUpdate error", e);
1056
+ }
1057
+ }
1058
+ } catch (e_5_1) {
1059
+ e_5 = {
1060
+ error: e_5_1
1061
+ };
1062
+ } finally {
1063
+ try {
1064
+ if (_h && !_h.done && (_d = _g.return)) _d.call(_g);
1065
+ } finally {
1066
+ if (e_5) throw e_5.error;
1067
+ }
1068
+ }
1069
+ }
1070
+ } catch (e_4_1) {
1071
+ e_4 = {
1072
+ error: e_4_1
1073
+ };
1074
+ } finally {
1075
+ try {
1076
+ if (gameObjects_2_1 && !gameObjects_2_1.done && (_c = gameObjects_2.return)) _c.call(gameObjects_2);
1077
+ } finally {
1078
+ if (e_4) throw e_4.error;
1079
+ }
1080
+ }
1081
+ };
1082
+ var gameObjectResume = function gameObjectResume(gameObjects) {
1083
+ var e_6, _a, e_7, _b;
1084
+ try {
1085
+ for (var gameObjects_3 = __values(gameObjects), gameObjects_3_1 = gameObjects_3.next(); !gameObjects_3_1.done; gameObjects_3_1 = gameObjects_3.next()) {
1086
+ var gameObject = gameObjects_3_1.value;
1087
+ try {
1088
+ for (var _c = (e_7 = void 0, __values(gameObject.components)), _d = _c.next(); !_d.done; _d = _c.next()) {
1089
+ var component = _d.value;
1090
+ try {
1091
+ component.onResume && component.onResume();
1092
+ } catch (e) {
1093
+ console.error("gameObject: " + gameObject.name + ", " + component.name + ", onResume error", e);
1094
+ }
1095
+ }
1096
+ } catch (e_7_1) {
1097
+ e_7 = {
1098
+ error: e_7_1
1099
+ };
1100
+ } finally {
1101
+ try {
1102
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
1103
+ } finally {
1104
+ if (e_7) throw e_7.error;
1105
+ }
1106
+ }
1107
+ }
1108
+ } catch (e_6_1) {
1109
+ e_6 = {
1110
+ error: e_6_1
1111
+ };
1112
+ } finally {
1113
+ try {
1114
+ if (gameObjects_3_1 && !gameObjects_3_1.done && (_a = gameObjects_3.return)) _a.call(gameObjects_3);
1115
+ } finally {
1116
+ if (e_6) throw e_6.error;
1117
+ }
1118
+ }
1119
+ };
1120
+ var gameObjectPause = function gameObjectPause(gameObjects) {
1121
+ var e_8, _a, e_9, _b;
1122
+ try {
1123
+ for (var gameObjects_4 = __values(gameObjects), gameObjects_4_1 = gameObjects_4.next(); !gameObjects_4_1.done; gameObjects_4_1 = gameObjects_4.next()) {
1124
+ var gameObject = gameObjects_4_1.value;
1125
+ try {
1126
+ for (var _c = (e_9 = void 0, __values(gameObject.components)), _d = _c.next(); !_d.done; _d = _c.next()) {
1127
+ var component = _d.value;
1128
+ try {
1129
+ component.onPause && component.onPause();
1130
+ } catch (e) {
1131
+ console.error("gameObject: " + gameObject.name + ", " + component.name + ", onResume error", e);
1132
+ }
1133
+ }
1134
+ } catch (e_9_1) {
1135
+ e_9 = {
1136
+ error: e_9_1
1137
+ };
1138
+ } finally {
1139
+ try {
1140
+ if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
1141
+ } finally {
1142
+ if (e_9) throw e_9.error;
1143
+ }
1144
+ }
1145
+ }
1146
+ } catch (e_8_1) {
1147
+ e_8 = {
1148
+ error: e_8_1
1149
+ };
1150
+ } finally {
1151
+ try {
1152
+ if (gameObjects_4_1 && !gameObjects_4_1.done && (_a = gameObjects_4.return)) _a.call(gameObjects_4);
1153
+ } finally {
1154
+ if (e_8) throw e_8.error;
1155
+ }
1156
+ }
1157
+ };
1158
+ var Game = function (_super) {
1159
+ __extends$1(Game, _super);
1160
+ function Game(_a) {
1161
+ var e_10, _b;
1162
+ var _c = _a === void 0 ? {} : _a,
1163
+ systems = _c.systems,
1164
+ _d = _c.frameRate,
1165
+ frameRate = _d === void 0 ? 60 : _d,
1166
+ _e = _c.autoStart,
1167
+ autoStart = _e === void 0 ? true : _e,
1168
+ _f = _c.needScene,
1169
+ needScene = _f === void 0 ? true : _f;
1170
+ var _this = _super.call(this) || this;
1171
+ _this.playing = false;
1172
+ _this.started = false;
1173
+ _this.multiScenes = [];
1174
+ _this.systems = [];
1175
+ if (windowAlias.__EVA_INSPECTOR_ENV__) {
1176
+ windowAlias.__EVA_GAME_INSTANCE__ = _this;
1177
+ }
1178
+ _this.ticker = new Ticker$1({
1179
+ autoStart: false,
1180
+ frameRate: frameRate
1181
+ });
1182
+ _this.initTicker();
1183
+ if (systems && systems.length) {
1184
+ try {
1185
+ for (var systems_1 = __values(systems), systems_1_1 = systems_1.next(); !systems_1_1.done; systems_1_1 = systems_1.next()) {
1186
+ var system = systems_1_1.value;
1187
+ _this.addSystem(system);
1188
+ }
1189
+ } catch (e_10_1) {
1190
+ e_10 = {
1191
+ error: e_10_1
1192
+ };
1193
+ } finally {
1194
+ try {
1195
+ if (systems_1_1 && !systems_1_1.done && (_b = systems_1.return)) _b.call(systems_1);
1196
+ } finally {
1197
+ if (e_10) throw e_10.error;
1198
+ }
1199
+ }
1200
+ }
1201
+ if (needScene) {
1202
+ _this.loadScene(new Scene$1('scene'));
1203
+ }
1204
+ if (autoStart) {
1205
+ _this.start();
1206
+ }
1207
+ return _this;
1208
+ }
1209
+ Object.defineProperty(Game.prototype, "scene", {
1210
+ get: function get() {
1211
+ return this._scene;
1212
+ },
1213
+ set: function set(scene) {
1214
+ this._scene = scene;
1215
+ },
1216
+ enumerable: false,
1217
+ configurable: true
1218
+ });
1219
+ Object.defineProperty(Game.prototype, "gameObjects", {
1220
+ get: function get() {
1221
+ return getAllGameObjects(this);
1222
+ },
1223
+ enumerable: false,
1224
+ configurable: true
1225
+ });
1226
+ Game.prototype.addSystem = function (S, obj) {
1227
+ var system;
1228
+ if (S instanceof Function) {
1229
+ system = new S(obj);
1230
+ } else if (S instanceof System$1) {
1231
+ system = S;
1232
+ } else {
1233
+ console.warn('can only add System');
1234
+ return;
1235
+ }
1236
+ var hasTheSystem = this.systems.find(function (item) {
1237
+ return item.constructor === system.constructor;
1238
+ });
1239
+ if (hasTheSystem) {
1240
+ console.warn(system.constructor.systemName + " System has been added");
1241
+ return;
1242
+ }
1243
+ system.game = this;
1244
+ system.init && system.init(system.__systemDefaultParams);
1245
+ setSystemObserver(system, system.constructor);
1246
+ initObserver(system.constructor);
1247
+ try {
1248
+ system.awake && system.awake();
1249
+ } catch (e) {
1250
+ console.error(system.constructor.systemName + " awake error", e);
1251
+ }
1252
+ this.systems.push(system);
1253
+ return system;
1254
+ };
1255
+ Game.prototype.removeSystem = function (system) {
1256
+ if (!system) return;
1257
+ var index = -1;
1258
+ if (typeof system === 'string') {
1259
+ index = this.systems.findIndex(function (s) {
1260
+ return s.name === system;
1261
+ });
1262
+ } else if (system instanceof Function) {
1263
+ index = this.systems.findIndex(function (s) {
1264
+ return s.constructor === system;
1265
+ });
1266
+ } else if (system instanceof System$1) {
1267
+ index = this.systems.findIndex(function (s) {
1268
+ return s === system;
1269
+ });
1270
+ }
1271
+ if (index > -1) {
1272
+ this.systems[index].destroy && this.systems[index].destroy();
1273
+ this.systems.splice(index, 1);
1274
+ }
1275
+ };
1276
+ Game.prototype.getSystem = function (S) {
1277
+ return this.systems.find(function (system) {
1278
+ if (typeof S === 'string') {
1279
+ return system.name === S;
1280
+ } else {
1281
+ return system instanceof S;
1282
+ }
1283
+ });
1284
+ };
1285
+ Game.prototype.pause = function () {
1286
+ if (!this.playing) return;
1287
+ this.playing = false;
1288
+ this.ticker.pause();
1289
+ this.triggerPause();
1290
+ };
1291
+ Game.prototype.start = function () {
1292
+ if (this.playing) return;
1293
+ this.playing = true;
1294
+ this.started = true;
1295
+ this.ticker.start();
1296
+ };
1297
+ Game.prototype.resume = function () {
1298
+ if (this.playing) return;
1299
+ this.playing = true;
1300
+ this.ticker.start();
1301
+ this.triggerResume();
1302
+ };
1303
+ Game.prototype.initTicker = function () {
1304
+ var _this = this;
1305
+ this.ticker.add(function (e) {
1306
+ var e_11, _a, e_12, _b;
1307
+ _this.scene && gameObjectLoop(e, _this.gameObjects);
1308
+ try {
1309
+ for (var _c = __values(_this.systems), _d = _c.next(); !_d.done; _d = _c.next()) {
1310
+ var system = _d.value;
1311
+ try {
1312
+ triggerStart(system);
1313
+ system.update && system.update(e);
1314
+ } catch (e) {
1315
+ console.error(system.constructor.systemName + " update error", e);
1316
+ }
1317
+ }
1318
+ } catch (e_11_1) {
1319
+ e_11 = {
1320
+ error: e_11_1
1321
+ };
1322
+ } finally {
1323
+ try {
1324
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
1325
+ } finally {
1326
+ if (e_11) throw e_11.error;
1327
+ }
1328
+ }
1329
+ try {
1330
+ for (var _e = __values(_this.systems), _f = _e.next(); !_f.done; _f = _e.next()) {
1331
+ var system = _f.value;
1332
+ try {
1333
+ system.lateUpdate && system.lateUpdate(e);
1334
+ } catch (e) {
1335
+ console.error(system.constructor.systemName + " lateUpdate error", e);
1336
+ }
1337
+ }
1338
+ } catch (e_12_1) {
1339
+ e_12 = {
1340
+ error: e_12_1
1341
+ };
1342
+ } finally {
1343
+ try {
1344
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
1345
+ } finally {
1346
+ if (e_12) throw e_12.error;
1347
+ }
1348
+ }
1349
+ });
1350
+ };
1351
+ Game.prototype.triggerResume = function () {
1352
+ var e_13, _a;
1353
+ gameObjectResume(this.gameObjects);
1354
+ try {
1355
+ for (var _b = __values(this.systems), _c = _b.next(); !_c.done; _c = _b.next()) {
1356
+ var system = _c.value;
1357
+ try {
1358
+ system.onResume && system.onResume();
1359
+ } catch (e) {
1360
+ console.error(system.constructor.systemName + ", onResume error", e);
1361
+ }
1362
+ }
1363
+ } catch (e_13_1) {
1364
+ e_13 = {
1365
+ error: e_13_1
1366
+ };
1367
+ } finally {
1368
+ try {
1369
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1370
+ } finally {
1371
+ if (e_13) throw e_13.error;
1372
+ }
1373
+ }
1374
+ };
1375
+ Game.prototype.triggerPause = function () {
1376
+ var e_14, _a;
1377
+ gameObjectPause(this.gameObjects);
1378
+ try {
1379
+ for (var _b = __values(this.systems), _c = _b.next(); !_c.done; _c = _b.next()) {
1380
+ var system = _c.value;
1381
+ try {
1382
+ system.onPause && system.onPause();
1383
+ } catch (e) {
1384
+ console.error(system.constructor.systemName + ", onPause error", e);
1385
+ }
1386
+ }
1387
+ } catch (e_14_1) {
1388
+ e_14 = {
1389
+ error: e_14_1
1390
+ };
1391
+ } finally {
1392
+ try {
1393
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1394
+ } finally {
1395
+ if (e_14) throw e_14.error;
1396
+ }
1397
+ }
1398
+ };
1399
+ Game.prototype.destroySystems = function () {
1400
+ var e_15, _a;
1401
+ try {
1402
+ for (var _b = __values(__spread(this.systems)), _c = _b.next(); !_c.done; _c = _b.next()) {
1403
+ var system = _c.value;
1404
+ this.removeSystem(system);
1405
+ }
1406
+ } catch (e_15_1) {
1407
+ e_15 = {
1408
+ error: e_15_1
1409
+ };
1410
+ } finally {
1411
+ try {
1412
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
1413
+ } finally {
1414
+ if (e_15) throw e_15.error;
1415
+ }
1416
+ }
1417
+ this.systems.length = 0;
1418
+ };
1419
+ Game.prototype.destroy = function () {
1420
+ this.removeAllListeners();
1421
+ this.pause();
1422
+ this.scene.destroy();
1423
+ this.destroySystems();
1424
+ this.ticker = null;
1425
+ this.scene = null;
1426
+ this.canvas = null;
1427
+ this.multiScenes = null;
1428
+ };
1429
+ Game.prototype.loadScene = function (_a) {
1430
+ var scene = _a.scene,
1431
+ _b = _a.mode,
1432
+ mode = _b === void 0 ? LOAD_SCENE_MODE.SINGLE : _b,
1433
+ _c = _a.params,
1434
+ params = _c === void 0 ? {} : _c;
1435
+ if (!scene) {
1436
+ return;
1437
+ }
1438
+ switch (mode) {
1439
+ case LOAD_SCENE_MODE.SINGLE:
1440
+ this.scene = scene;
1441
+ break;
1442
+ case LOAD_SCENE_MODE.MULTI_CANVAS:
1443
+ this.multiScenes.push(scene);
1444
+ break;
1445
+ }
1446
+ this.emit('sceneChanged', {
1447
+ scene: scene,
1448
+ mode: mode,
1449
+ params: params
1450
+ });
1451
+ };
1452
+ return Game;
1453
+ }(EventEmitter);
1454
+ var Game$1 = Game;
1455
+ function IDEProp(target, propertyKey) {
1456
+ if (!target.constructor.IDEProps) {
1457
+ target.constructor.IDEProps = [];
1458
+ }
1459
+ target.constructor.IDEProps.push(propertyKey);
1460
+ }
1461
+ function componentObserver(observerInfo) {
1462
+ if (observerInfo === void 0) {
1463
+ observerInfo = {};
1464
+ }
1465
+ return function (constructor) {
1466
+ if (!constructor.observerInfo) {
1467
+ for (var key in observerInfo) {
1468
+ for (var index in observerInfo[key]) {
1469
+ if (typeof observerInfo[key][index] === 'string') {
1470
+ observerInfo[key][index] = [observerInfo[key][index]];
1471
+ }
1472
+ var observerProp = void 0;
1473
+ if (Array.isArray(observerInfo[key][index])) {
1474
+ observerProp = {
1475
+ prop: observerInfo[key][index],
1476
+ deep: false
1477
+ };
1478
+ observerInfo[key][index] = observerProp;
1479
+ }
1480
+ observerProp = observerInfo[key][index];
1481
+ if (typeof observerProp.prop === 'string') {
1482
+ observerProp.prop = [observerProp.prop];
1483
+ }
1484
+ }
1485
+ }
1486
+ constructor.observerInfo = observerInfo;
1487
+ }
1488
+ };
1489
+ }
1490
+
1491
+ var SignalBindingImpl = function () {
1492
+ function SignalBindingImpl(fn, once, thisArg) {
1493
+ if (once === void 0) {
1494
+ once = false;
1495
+ }
1496
+ this.next = null;
1497
+ this.prev = null;
1498
+ this.owner = null;
1499
+ this.fn = fn;
1500
+ this.once = once;
1501
+ this.thisArg = thisArg;
1502
+ }
1503
+ SignalBindingImpl.prototype.detach = function () {
1504
+ if (this.owner === null) return false;
1505
+ this.owner.detach(this);
1506
+ return true;
1507
+ };
1508
+ SignalBindingImpl.prototype.dispose = function () {
1509
+ this.detach();
1510
+ };
1511
+ return SignalBindingImpl;
1512
+ }();
1513
+ var Signal = function () {
1514
+ function Signal() {
1515
+ this._head = null;
1516
+ this._tail = null;
1517
+ this._filter = null;
1518
+ }
1519
+ Signal.prototype.handlers = function () {
1520
+ var node = this._head;
1521
+ var handlers = [];
1522
+ while (node) {
1523
+ handlers.push(node);
1524
+ node = node.next;
1525
+ }
1526
+ return handlers;
1527
+ };
1528
+ Signal.prototype.hasAny = function () {
1529
+ return !!this._head;
1530
+ };
1531
+ Signal.prototype.has = function (node) {
1532
+ return node.owner === this;
1533
+ };
1534
+ Signal.prototype.dispatch = function () {
1535
+ var args = [];
1536
+ for (var _i = 0; _i < arguments.length; _i++) {
1537
+ args[_i] = arguments[_i];
1538
+ }
1539
+ var node = this._head;
1540
+ if (!node) return false;
1541
+ if (this._filter && !this._filter.apply(this, args)) return false;
1542
+ while (node) {
1543
+ if (node.once) this.detach(node);
1544
+ node.fn.apply(node.thisArg, args);
1545
+ node = node.next;
1546
+ }
1547
+ return true;
1548
+ };
1549
+ Signal.prototype.add = function (fn, thisArg) {
1550
+ if (thisArg === void 0) {
1551
+ thisArg = null;
1552
+ }
1553
+ return this._addSignalBinding(new SignalBindingImpl(fn, false, thisArg));
1554
+ };
1555
+ Signal.prototype.once = function (fn, thisArg) {
1556
+ if (thisArg === void 0) {
1557
+ thisArg = null;
1558
+ }
1559
+ return this._addSignalBinding(new SignalBindingImpl(fn, true, thisArg));
1560
+ };
1561
+ Signal.prototype.detach = function (node_) {
1562
+ var node = node_;
1563
+ if (node.owner !== this) return this;
1564
+ if (node.prev) node.prev.next = node.next;
1565
+ if (node.next) node.next.prev = node.prev;
1566
+ if (node === this._head) {
1567
+ this._head = node.next;
1568
+ if (node.next === null) {
1569
+ this._tail = null;
1570
+ }
1571
+ } else if (node === this._tail) {
1572
+ this._tail = node.prev;
1573
+ if (this._tail) this._tail.next = null;
1574
+ }
1575
+ node.owner = null;
1576
+ return this;
1577
+ };
1578
+ Signal.prototype.detachAll = function () {
1579
+ var node = this._head;
1580
+ if (!node) return this;
1581
+ this._head = null;
1582
+ this._tail = null;
1583
+ while (node) {
1584
+ node.owner = null;
1585
+ node = node.next;
1586
+ }
1587
+ return this;
1588
+ };
1589
+ Signal.prototype.filter = function (filter) {
1590
+ this._filter = filter;
1591
+ };
1592
+ Signal.prototype.proxy = function () {
1593
+ var _this = this;
1594
+ var signals = [];
1595
+ for (var _i = 0; _i < arguments.length; _i++) {
1596
+ signals[_i] = arguments[_i];
1597
+ }
1598
+ var fn = function fn() {
1599
+ var args = [];
1600
+ for (var _i = 0; _i < arguments.length; _i++) {
1601
+ args[_i] = arguments[_i];
1602
+ }
1603
+ return _this.dispatch.apply(_this, args);
1604
+ };
1605
+ for (var i = 0; i < signals.length; ++i) {
1606
+ signals[i].add(fn);
1607
+ }
1608
+ return this;
1609
+ };
1610
+ Signal.prototype._addSignalBinding = function (node_) {
1611
+ var node = node_;
1612
+ if (!this._head) {
1613
+ this._head = node;
1614
+ this._tail = node;
1615
+ } else {
1616
+ if (this._tail) this._tail.next = node;
1617
+ node.prev = this._tail;
1618
+ this._tail = node;
1619
+ }
1620
+ node.owner = this;
1621
+ return node;
1622
+ };
1623
+ return Signal;
1624
+ }();
1625
+ var src = function src(str, opts) {
1626
+ if (opts === void 0) {
1627
+ opts = {};
1628
+ }
1629
+ if (!str) return undefined;
1630
+ var o = {
1631
+ key: ['source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor'],
1632
+ q: {
1633
+ name: 'queryKey',
1634
+ parser: /(?:^|&)([^&=]*)=?([^&]*)/g
1635
+ },
1636
+ parser: {
1637
+ strict: /^(?:([^:/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:/?#]*)(?::(\d*))?))?((((?:[^?#/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
1638
+ loose: /^(?:(?![^:@]+:[^:@/]*@)([^:/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#/]*\.[^?#/.]+(?:[?#]|$)))*\/?)?([^?#/]*))(?:\?([^#]*))?(?:#(.*))?)/
1639
+ }
1640
+ };
1641
+ var m = o.parser[opts.strictMode ? 'strict' : 'loose'].exec(str);
1642
+ var uri = {};
1643
+ var i = 14;
1644
+ while (i--) {
1645
+ uri[o.key[i]] = m[i] || '';
1646
+ }
1647
+ uri[o.q.name] = {};
1648
+ uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
1649
+ if ($1) uri[o.q.name][$1] = $2;
1650
+ });
1651
+ return uri;
1652
+ };
1653
+ var parseUri = src;
1654
+ var AbstractLoadStrategy = function () {
1655
+ function AbstractLoadStrategy(config) {
1656
+ this.config = config;
1657
+ this.onError = new Signal();
1658
+ this.onComplete = new Signal();
1659
+ this.onProgress = new Signal();
1660
+ }
1661
+ return AbstractLoadStrategy;
1662
+ }();
1663
+
1664
+ var _extendStatics = function extendStatics(d, b) {
1665
+ _extendStatics = Object.setPrototypeOf || {
1666
+ __proto__: []
1667
+ } instanceof Array && function (d, b) {
1668
+ d.__proto__ = b;
1669
+ } || function (d, b) {
1670
+ for (var p in b) {
1671
+ if (b.hasOwnProperty(p)) d[p] = b[p];
1672
+ }
1673
+ };
1674
+ return _extendStatics(d, b);
1675
+ };
1676
+ function __extends(d, b) {
1677
+ _extendStatics(d, b);
1678
+ function __() {
1679
+ this.constructor = d;
1680
+ }
1681
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
1682
+ }
1683
+ function __spreadArrays() {
1684
+ for (var s = 0, i = 0, il = arguments.length; i < il; i++) {
1685
+ s += arguments[i].length;
1686
+ }
1687
+ for (var r = Array(s), k = 0, i = 0; i < il; i++) {
1688
+ for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) {
1689
+ r[k] = a[j];
1690
+ }
1691
+ }
1692
+ return r;
1693
+ }
1694
+ function getExtension(url) {
1695
+ var isDataUrl = url.indexOf('data:') === 0;
1696
+ var ext = '';
1697
+ if (isDataUrl) {
1698
+ var slashIndex = url.indexOf('/');
1699
+ ext = url.substring(slashIndex + 1, url.indexOf(';', slashIndex));
1700
+ } else {
1701
+ var queryStart = url.indexOf('?');
1702
+ var hashStart = url.indexOf('#');
1703
+ var index = Math.min(queryStart > -1 ? queryStart : url.length, hashStart > -1 ? hashStart : url.length);
1704
+ url = url.substring(0, index);
1705
+ ext = url.substring(url.lastIndexOf('.') + 1);
1706
+ }
1707
+ return ext.toLowerCase();
1708
+ }
1709
+ function assertNever(x) {
1710
+ throw new Error('Unexpected value. Should have been never.');
1711
+ }
1712
+ var ResourceType;
1713
+ (function (ResourceType) {
1714
+ ResourceType[ResourceType["Unknown"] = 0] = "Unknown";
1715
+ ResourceType[ResourceType["Buffer"] = 1] = "Buffer";
1716
+ ResourceType[ResourceType["Blob"] = 2] = "Blob";
1717
+ ResourceType[ResourceType["Json"] = 3] = "Json";
1718
+ ResourceType[ResourceType["Xml"] = 4] = "Xml";
1719
+ ResourceType[ResourceType["Image"] = 5] = "Image";
1720
+ ResourceType[ResourceType["Audio"] = 6] = "Audio";
1721
+ ResourceType[ResourceType["Video"] = 7] = "Video";
1722
+ ResourceType[ResourceType["Text"] = 8] = "Text";
1723
+ })(ResourceType || (ResourceType = {}));
1724
+ var ResourceState;
1725
+ (function (ResourceState) {
1726
+ ResourceState[ResourceState["NotStarted"] = 0] = "NotStarted";
1727
+ ResourceState[ResourceState["Loading"] = 1] = "Loading";
1728
+ ResourceState[ResourceState["Complete"] = 2] = "Complete";
1729
+ })(ResourceState || (ResourceState = {}));
1730
+ var MediaElementLoadStrategy = function (_super) {
1731
+ __extends(MediaElementLoadStrategy, _super);
1732
+ function MediaElementLoadStrategy(config, elementType) {
1733
+ var _this = _super.call(this, config) || this;
1734
+ _this.elementType = elementType;
1735
+ _this._boundOnLoad = _this._onLoad.bind(_this);
1736
+ _this._boundOnError = _this._onError.bind(_this);
1737
+ _this._boundOnTimeout = _this._onTimeout.bind(_this);
1738
+ _this._element = _this._createElement();
1739
+ _this._elementTimer = 0;
1740
+ return _this;
1741
+ }
1742
+ MediaElementLoadStrategy.prototype.load = function () {
1743
+ var config = this.config;
1744
+ if (config.crossOrigin) this._element.crossOrigin = config.crossOrigin;
1745
+ var urls = config.sourceSet || [config.url];
1746
+ if (navigator.isCocoonJS) {
1747
+ this._element.src = urls[0];
1748
+ } else {
1749
+ for (var i = 0; i < urls.length; ++i) {
1750
+ var url = urls[i];
1751
+ var mimeType = config.mimeTypes ? config.mimeTypes[i] : undefined;
1752
+ if (!mimeType) mimeType = this.elementType + "/" + getExtension(url);
1753
+ var source = documentAlias.createElement('source');
1754
+ source.src = url;
1755
+ source.type = mimeType;
1756
+ this._element.appendChild(source);
1757
+ }
1758
+ }
1759
+ this._element.addEventListener('load', this._boundOnLoad, false);
1760
+ this._element.addEventListener('canplaythrough', this._boundOnLoad, false);
1761
+ this._element.addEventListener('error', this._boundOnError, false);
1762
+ this._element.load();
1763
+ if (config.timeout) this._elementTimer = windowAlias.setTimeout(this._boundOnTimeout, config.timeout);
1764
+ };
1765
+ MediaElementLoadStrategy.prototype.abort = function () {
1766
+ this._clearEvents();
1767
+ while (this._element.firstChild) {
1768
+ this._element.removeChild(this._element.firstChild);
1769
+ }
1770
+ this._error(this.elementType + " load aborted by the user.");
1771
+ };
1772
+ MediaElementLoadStrategy.prototype._createElement = function () {
1773
+ if (this.config.loadElement) return this.config.loadElement;else return documentAlias.createElement(this.elementType);
1774
+ };
1775
+ MediaElementLoadStrategy.prototype._clearEvents = function () {
1776
+ clearTimeout(this._elementTimer);
1777
+ this._element.removeEventListener('load', this._boundOnLoad, false);
1778
+ this._element.removeEventListener('canplaythrough', this._boundOnLoad, false);
1779
+ this._element.removeEventListener('error', this._boundOnError, false);
1780
+ };
1781
+ MediaElementLoadStrategy.prototype._error = function (errMessage) {
1782
+ this._clearEvents();
1783
+ this.onError.dispatch(errMessage);
1784
+ };
1785
+ MediaElementLoadStrategy.prototype._complete = function () {
1786
+ this._clearEvents();
1787
+ var resourceType = ResourceType.Unknown;
1788
+ switch (this.elementType) {
1789
+ case 'audio':
1790
+ resourceType = ResourceType.Audio;
1791
+ break;
1792
+ case 'video':
1793
+ resourceType = ResourceType.Video;
1794
+ break;
1795
+ default:
1796
+ assertNever(this.elementType);
1797
+ }
1798
+ this.onComplete.dispatch(resourceType, this._element);
1799
+ };
1800
+ MediaElementLoadStrategy.prototype._onLoad = function () {
1801
+ this._complete();
1802
+ };
1803
+ MediaElementLoadStrategy.prototype._onError = function () {
1804
+ this._error(this.elementType + " failed to load.");
1805
+ };
1806
+ MediaElementLoadStrategy.prototype._onTimeout = function () {
1807
+ this._error(this.elementType + " load timed out.");
1808
+ };
1809
+ return MediaElementLoadStrategy;
1810
+ }(AbstractLoadStrategy);
1811
+ var AudioLoadStrategy = function (_super) {
1812
+ __extends(AudioLoadStrategy, _super);
1813
+ function AudioLoadStrategy(config) {
1814
+ return _super.call(this, config, 'audio') || this;
1815
+ }
1816
+ return AudioLoadStrategy;
1817
+ }(MediaElementLoadStrategy);
1818
+ var EMPTY_GIF = 'data:image/gif;base64,R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==';
1819
+ var ImageLoadStrategy = function (_super) {
1820
+ __extends(ImageLoadStrategy, _super);
1821
+ function ImageLoadStrategy() {
1822
+ var _this = _super !== null && _super.apply(this, arguments) || this;
1823
+ _this._boundOnLoad = _this._onLoad.bind(_this);
1824
+ _this._boundOnError = _this._onError.bind(_this);
1825
+ _this._boundOnTimeout = _this._onTimeout.bind(_this);
1826
+ _this._element = _this._createElement();
1827
+ _this._elementTimer = 0;
1828
+ return _this;
1829
+ }
1830
+ ImageLoadStrategy.prototype.load = function () {
1831
+ var config = this.config;
1832
+ if (config.crossOrigin) this._element.crossOrigin = config.crossOrigin;
1833
+ this._element.src = config.url;
1834
+ this._element.addEventListener('load', this._boundOnLoad, false);
1835
+ this._element.addEventListener('error', this._boundOnError, false);
1836
+ if (config.timeout) this._elementTimer = windowAlias.setTimeout(this._boundOnTimeout, config.timeout);
1837
+ };
1838
+ ImageLoadStrategy.prototype.abort = function () {
1839
+ this._clearEvents();
1840
+ this._element.src = EMPTY_GIF;
1841
+ this._error('Image load aborted by the user.');
1842
+ };
1843
+ ImageLoadStrategy.prototype._createElement = function () {
1844
+ if (this.config.loadElement) return this.config.loadElement;else return documentAlias.createElement('img');
1845
+ };
1846
+ ImageLoadStrategy.prototype._clearEvents = function () {
1847
+ clearTimeout(this._elementTimer);
1848
+ this._element.removeEventListener('load', this._boundOnLoad, false);
1849
+ this._element.removeEventListener('error', this._boundOnError, false);
1850
+ };
1851
+ ImageLoadStrategy.prototype._error = function (errMessage) {
1852
+ this._clearEvents();
1853
+ this.onError.dispatch(errMessage);
1854
+ };
1855
+ ImageLoadStrategy.prototype._complete = function () {
1856
+ this._clearEvents();
1857
+ this.onComplete.dispatch(ResourceType.Image, this._element);
1858
+ };
1859
+ ImageLoadStrategy.prototype._onLoad = function () {
1860
+ this._complete();
1861
+ };
1862
+ ImageLoadStrategy.prototype._onError = function () {
1863
+ this._error('Image failed to load.');
1864
+ };
1865
+ ImageLoadStrategy.prototype._onTimeout = function () {
1866
+ this._error('Image load timed out.');
1867
+ };
1868
+ return ImageLoadStrategy;
1869
+ }(AbstractLoadStrategy);
1870
+ var VideoLoadStrategy = function (_super) {
1871
+ __extends(VideoLoadStrategy, _super);
1872
+ function VideoLoadStrategy(config) {
1873
+ return _super.call(this, config, 'video') || this;
1874
+ }
1875
+ return VideoLoadStrategy;
1876
+ }(MediaElementLoadStrategy);
1877
+ var useXdr = !!(windowAlias.XDomainRequest && !('withCredentials' in new XMLHttpRequestAlias()));
1878
+ var XhrResponseType;
1879
+ (function (XhrResponseType) {
1880
+ XhrResponseType["Default"] = "text";
1881
+ XhrResponseType["Buffer"] = "arraybuffer";
1882
+ XhrResponseType["Blob"] = "blob";
1883
+ XhrResponseType["Document"] = "document";
1884
+ XhrResponseType["Json"] = "json";
1885
+ XhrResponseType["Text"] = "text";
1886
+ })(XhrResponseType || (XhrResponseType = {}));
1887
+ function reqType(xhr) {
1888
+ return xhr.toString().replace('object ', '');
1889
+ }
1890
+ var XhrLoadStrategy = function (_super) {
1891
+ __extends(XhrLoadStrategy, _super);
1892
+ function XhrLoadStrategy() {
1893
+ var _this = _super !== null && _super.apply(this, arguments) || this;
1894
+ _this._boundOnLoad = _this._onLoad.bind(_this);
1895
+ _this._boundOnAbort = _this._onAbort.bind(_this);
1896
+ _this._boundOnError = _this._onError.bind(_this);
1897
+ _this._boundOnTimeout = _this._onTimeout.bind(_this);
1898
+ _this._boundOnProgress = _this._onProgress.bind(_this);
1899
+ _this._xhr = _this._createRequest();
1900
+ _this._xhrType = XhrResponseType.Default;
1901
+ return _this;
1902
+ }
1903
+ XhrLoadStrategy.prototype.load = function () {
1904
+ var config = this.config;
1905
+ var ext = getExtension(config.url);
1906
+ if (typeof config.xhrType !== 'string') {
1907
+ config.xhrType = this._determineXhrType(ext);
1908
+ }
1909
+ var xhr = this._xhr;
1910
+ this._xhrType = config.xhrType || XhrResponseType.Default;
1911
+ if (useXdr) {
1912
+ xhr.timeout = config.timeout || 5000;
1913
+ xhr.onload = this._boundOnLoad;
1914
+ xhr.onerror = this._boundOnError;
1915
+ xhr.ontimeout = this._boundOnTimeout;
1916
+ xhr.onprogress = this._boundOnProgress;
1917
+ xhr.open('GET', config.url, true);
1918
+ setTimeout(function () {
1919
+ xhr.send();
1920
+ }, 0);
1921
+ } else {
1922
+ xhr.open('GET', config.url, true);
1923
+ if (config.timeout) xhr.timeout = config.timeout;
1924
+ if (config.xhrType === XhrResponseType.Json || config.xhrType === XhrResponseType.Document) xhr.responseType = XhrResponseType.Text;else xhr.responseType = config.xhrType;
1925
+ xhr.addEventListener('load', this._boundOnLoad, false);
1926
+ xhr.addEventListener('abort', this._boundOnAbort, false);
1927
+ xhr.addEventListener('error', this._boundOnError, false);
1928
+ xhr.addEventListener('timeout', this._boundOnTimeout, false);
1929
+ xhr.addEventListener('progress', this._boundOnProgress, false);
1930
+ xhr.send();
1931
+ }
1932
+ };
1933
+ XhrLoadStrategy.prototype.abort = function () {
1934
+ if (useXdr) {
1935
+ this._clearEvents();
1936
+ this._xhr.abort();
1937
+ this._onAbort();
1938
+ } else {
1939
+ this._xhr.abort();
1940
+ }
1941
+ };
1942
+ XhrLoadStrategy.prototype._createRequest = function () {
1943
+ if (useXdr) return new windowAlias.XDomainRequest();else return new XMLHttpRequestAlias();
1944
+ };
1945
+ XhrLoadStrategy.prototype._determineXhrType = function (ext) {
1946
+ return XhrLoadStrategy._xhrTypeMap[ext] || XhrResponseType.Default;
1947
+ };
1948
+ XhrLoadStrategy.prototype._clearEvents = function () {
1949
+ if (useXdr) {
1950
+ this._xhr.onload = null;
1951
+ this._xhr.onerror = null;
1952
+ this._xhr.ontimeout = null;
1953
+ this._xhr.onprogress = null;
1954
+ } else {
1955
+ this._xhr.removeEventListener('load', this._boundOnLoad, false);
1956
+ this._xhr.removeEventListener('abort', this._boundOnAbort, false);
1957
+ this._xhr.removeEventListener('error', this._boundOnError, false);
1958
+ this._xhr.removeEventListener('timeout', this._boundOnTimeout, false);
1959
+ this._xhr.removeEventListener('progress', this._boundOnProgress, false);
1960
+ }
1961
+ };
1962
+ XhrLoadStrategy.prototype._error = function (errMessage) {
1963
+ this._clearEvents();
1964
+ this.onError.dispatch(errMessage);
1965
+ };
1966
+ XhrLoadStrategy.prototype._complete = function (type, data) {
1967
+ this._clearEvents();
1968
+ this.onComplete.dispatch(type, data);
1969
+ };
1970
+ XhrLoadStrategy.prototype._onLoad = function () {
1971
+ var xhr = this._xhr;
1972
+ var text = '';
1973
+ var status = typeof xhr.status === 'undefined' ? 200 : xhr.status;
1974
+ if (typeof xhr.responseType === 'undefined' || xhr.responseType === '' || xhr.responseType === 'text') {
1975
+ text = xhr.responseText;
1976
+ }
1977
+ if (status === 0 && (text.length > 0 || xhr.responseType === XhrResponseType.Buffer)) {
1978
+ status = 200;
1979
+ } else if (status === 1223) {
1980
+ status = 204;
1981
+ }
1982
+ var flattenedStatus = Math.floor(status / 100) * 100;
1983
+ if (flattenedStatus !== 200) {
1984
+ this._error("[" + xhr.status + "] " + xhr.statusText + ": " + xhr.responseURL);
1985
+ return;
1986
+ }
1987
+ switch (this._xhrType) {
1988
+ case XhrResponseType.Buffer:
1989
+ this._complete(ResourceType.Buffer, xhr.response);
1990
+ break;
1991
+ case XhrResponseType.Blob:
1992
+ this._complete(ResourceType.Blob, xhr.response);
1993
+ break;
1994
+ case XhrResponseType.Document:
1995
+ this._parseDocument(text);
1996
+ break;
1997
+ case XhrResponseType.Json:
1998
+ this._parseJson(text);
1999
+ break;
2000
+ case XhrResponseType.Default:
2001
+ case XhrResponseType.Text:
2002
+ this._complete(ResourceType.Text, text);
2003
+ break;
2004
+ default:
2005
+ assertNever(this._xhrType);
2006
+ }
2007
+ };
2008
+ XhrLoadStrategy.prototype._parseDocument = function (text) {
2009
+ try {
2010
+ if (windowAlias.DOMParser) {
2011
+ var parser = new DOMParser();
2012
+ var data = parser.parseFromString(text, 'text/xml');
2013
+ this._complete(ResourceType.Xml, data);
2014
+ } else {
2015
+ var div = documentAlias.createElement('div');
2016
+ div.innerHTML = text;
2017
+ this._complete(ResourceType.Xml, div);
2018
+ }
2019
+ } catch (e) {
2020
+ this._error("Error trying to parse loaded xml: " + e);
2021
+ }
2022
+ };
2023
+ XhrLoadStrategy.prototype._parseJson = function (text) {
2024
+ try {
2025
+ var data = JSON.parse(text);
2026
+ this._complete(ResourceType.Json, data);
2027
+ } catch (e) {
2028
+ this._error("Error trying to parse loaded json: " + e);
2029
+ }
2030
+ };
2031
+ XhrLoadStrategy.prototype._onAbort = function () {
2032
+ var xhr = this._xhr;
2033
+ this._error(reqType(xhr) + " Request was aborted by the user.");
2034
+ };
2035
+ XhrLoadStrategy.prototype._onError = function () {
2036
+ var xhr = this._xhr;
2037
+ this._error(reqType(xhr) + " Request failed. Status: " + xhr.status + ", text: \"" + xhr.statusText + "\"");
2038
+ };
2039
+ XhrLoadStrategy.prototype._onTimeout = function () {
2040
+ var xhr = this._xhr;
2041
+ this._error(reqType(xhr) + " Request timed out.");
2042
+ };
2043
+ XhrLoadStrategy.prototype._onProgress = function (event) {
2044
+ if (event && event.lengthComputable) {
2045
+ this.onProgress.dispatch(event.loaded / event.total);
2046
+ }
2047
+ };
2048
+ XhrLoadStrategy.setExtensionXhrType = function (extname, xhrType) {
2049
+ if (extname && extname.indexOf('.') === 0) extname = extname.substring(1);
2050
+ if (!extname) return;
2051
+ XhrLoadStrategy._xhrTypeMap[extname] = xhrType;
2052
+ };
2053
+ XhrLoadStrategy.ResponseType = XhrResponseType;
2054
+ XhrLoadStrategy._xhrTypeMap = {
2055
+ xhtml: XhrResponseType.Document,
2056
+ html: XhrResponseType.Document,
2057
+ htm: XhrResponseType.Document,
2058
+ xml: XhrResponseType.Document,
2059
+ tmx: XhrResponseType.Document,
2060
+ svg: XhrResponseType.Document,
2061
+ tsx: XhrResponseType.Document,
2062
+ gif: XhrResponseType.Blob,
2063
+ png: XhrResponseType.Blob,
2064
+ bmp: XhrResponseType.Blob,
2065
+ jpg: XhrResponseType.Blob,
2066
+ jpeg: XhrResponseType.Blob,
2067
+ tif: XhrResponseType.Blob,
2068
+ tiff: XhrResponseType.Blob,
2069
+ webp: XhrResponseType.Blob,
2070
+ tga: XhrResponseType.Blob,
2071
+ json: XhrResponseType.Json,
2072
+ text: XhrResponseType.Text,
2073
+ txt: XhrResponseType.Text,
2074
+ ttf: XhrResponseType.Buffer,
2075
+ otf: XhrResponseType.Buffer
2076
+ };
2077
+ return XhrLoadStrategy;
2078
+ }(AbstractLoadStrategy);
2079
+ function onlyOnce(func) {
2080
+ var fn = func;
2081
+ return function onceWrapper() {
2082
+ var args = [];
2083
+ for (var _i = 0; _i < arguments.length; _i++) {
2084
+ args[_i] = arguments[_i];
2085
+ }
2086
+ if (fn === null) throw new Error('Callback was already called.');
2087
+ var callFn = fn;
2088
+ fn = null;
2089
+ return callFn.apply(this, args);
2090
+ };
2091
+ }
2092
+ var AsyncQueue = function () {
2093
+ function AsyncQueue(worker, concurrency) {
2094
+ if (concurrency === void 0) {
2095
+ concurrency = 1;
2096
+ }
2097
+ this.worker = worker;
2098
+ this.concurrency = concurrency;
2099
+ this.workers = 0;
2100
+ this.buffer = 0;
2101
+ this.paused = false;
2102
+ this._started = false;
2103
+ this._tasks = [];
2104
+ this.onSaturated = new Signal();
2105
+ this.onUnsaturated = new Signal();
2106
+ this.onEmpty = new Signal();
2107
+ this.onDrain = new Signal();
2108
+ this.onError = new Signal();
2109
+ if (concurrency === 0) throw new Error('Concurrency must not be zero');
2110
+ this.buffer = concurrency / 4;
2111
+ }
2112
+ Object.defineProperty(AsyncQueue.prototype, "started", {
2113
+ get: function get() {
2114
+ return this._started;
2115
+ },
2116
+ enumerable: true,
2117
+ configurable: true
2118
+ });
2119
+ AsyncQueue.prototype.reset = function () {
2120
+ this.onDrain.detachAll();
2121
+ this.workers = 0;
2122
+ this._started = false;
2123
+ this._tasks = [];
2124
+ };
2125
+ AsyncQueue.prototype.push = function (data, callback) {
2126
+ this._insert(data, false, callback);
2127
+ };
2128
+ AsyncQueue.prototype.unshift = function (data, callback) {
2129
+ this._insert(data, true, callback);
2130
+ };
2131
+ AsyncQueue.prototype.process = function () {
2132
+ while (!this.paused && this.workers < this.concurrency && this._tasks.length) {
2133
+ var task = this._tasks.shift();
2134
+ if (this._tasks.length === 0) this.onEmpty.dispatch();
2135
+ this.workers += 1;
2136
+ if (this.workers === this.concurrency) this.onSaturated.dispatch();
2137
+ this.worker(task.data, onlyOnce(this._next(task)));
2138
+ }
2139
+ };
2140
+ AsyncQueue.prototype.length = function () {
2141
+ return this._tasks.length;
2142
+ };
2143
+ AsyncQueue.prototype.running = function () {
2144
+ return this.workers;
2145
+ };
2146
+ AsyncQueue.prototype.idle = function () {
2147
+ return this._tasks.length + this.workers === 0;
2148
+ };
2149
+ AsyncQueue.prototype.pause = function () {
2150
+ if (this.paused === true) return;
2151
+ this.paused = true;
2152
+ };
2153
+ AsyncQueue.prototype.resume = function () {
2154
+ if (this.paused === false) return;
2155
+ this.paused = false;
2156
+ for (var w = 1; w <= this.concurrency; w++) {
2157
+ this.process();
2158
+ }
2159
+ };
2160
+ AsyncQueue.prototype.getTask = function (index) {
2161
+ return this._tasks[index];
2162
+ };
2163
+ AsyncQueue.prototype._insert = function (data, insertAtFront, callback) {
2164
+ var _this = this;
2165
+ if (callback != null && typeof callback !== 'function') {
2166
+ throw new Error('task callback must be a function');
2167
+ }
2168
+ this._started = true;
2169
+ if (data == null && this.idle()) {
2170
+ setTimeout(function () {
2171
+ return _this.onDrain.dispatch();
2172
+ }, 1);
2173
+ return;
2174
+ }
2175
+ var task = {
2176
+ data: data,
2177
+ callback: callback
2178
+ };
2179
+ if (insertAtFront) this._tasks.unshift(task);else this._tasks.push(task);
2180
+ setTimeout(function () {
2181
+ return _this.process();
2182
+ }, 1);
2183
+ };
2184
+ AsyncQueue.prototype._next = function (task) {
2185
+ var _this = this;
2186
+ return function (err) {
2187
+ var args = [];
2188
+ for (var _i = 1; _i < arguments.length; _i++) {
2189
+ args[_i - 1] = arguments[_i];
2190
+ }
2191
+ _this.workers -= 1;
2192
+ if (task.callback) task.callback.apply(task, __spreadArrays([err], args));
2193
+ if (err) _this.onError.dispatch(err, task.data);
2194
+ if (_this.workers <= _this.concurrency - _this.buffer) _this.onUnsaturated.dispatch();
2195
+ if (_this.idle()) _this.onDrain.dispatch();
2196
+ _this.process();
2197
+ };
2198
+ };
2199
+ return AsyncQueue;
2200
+ }();
2201
+ var Resource$1 = function () {
2202
+ function Resource(name, options) {
2203
+ this.children = [];
2204
+ this.onStart = new Signal();
2205
+ this.onProgress = new Signal();
2206
+ this.onComplete = new Signal();
2207
+ this.onAfterMiddleware = new Signal();
2208
+ this.data = null;
2209
+ this.type = ResourceType.Unknown;
2210
+ this.error = '';
2211
+ this.progressChunk = 0;
2212
+ this._dequeue = function () {};
2213
+ this._onCompleteBinding = null;
2214
+ this._state = ResourceState.NotStarted;
2215
+ this.name = name;
2216
+ this.metadata = options.metadata;
2217
+ if (typeof options.crossOrigin !== 'string') options.crossOrigin = this._determineCrossOrigin(options.url);
2218
+ if (options.strategy && typeof options.strategy !== 'function') {
2219
+ this._strategy = options.strategy;
2220
+ this._strategy.config = options;
2221
+ } else {
2222
+ var StrategyCtor = options.strategy;
2223
+ if (!StrategyCtor) StrategyCtor = Resource._loadStrategyMap[getExtension(options.url)];
2224
+ if (!StrategyCtor) StrategyCtor = Resource._defaultLoadStrategy;
2225
+ this._strategy = new StrategyCtor(options);
2226
+ }
2227
+ this._strategy.onError.add(this._error, this);
2228
+ this._strategy.onComplete.add(this._complete, this);
2229
+ this._strategy.onProgress.add(this._progress, this);
2230
+ }
2231
+ Resource.setDefaultLoadStrategy = function (strategy) {
2232
+ Resource._defaultLoadStrategy = strategy;
2233
+ };
2234
+ Resource.setLoadStrategy = function (extname, strategy) {
2235
+ if (extname && extname.indexOf('.') === 0) extname = extname.substring(1);
2236
+ if (!extname) return;
2237
+ Resource._loadStrategyMap[extname] = strategy;
2238
+ };
2239
+ Object.defineProperty(Resource.prototype, "strategy", {
2240
+ get: function get() {
2241
+ return this._strategy;
2242
+ },
2243
+ enumerable: true,
2244
+ configurable: true
2245
+ });
2246
+ Object.defineProperty(Resource.prototype, "url", {
2247
+ get: function get() {
2248
+ return this._strategy.config.url;
2249
+ },
2250
+ enumerable: true,
2251
+ configurable: true
2252
+ });
2253
+ Object.defineProperty(Resource.prototype, "isLoading", {
2254
+ get: function get() {
2255
+ return this._state === ResourceState.Loading;
2256
+ },
2257
+ enumerable: true,
2258
+ configurable: true
2259
+ });
2260
+ Object.defineProperty(Resource.prototype, "isComplete", {
2261
+ get: function get() {
2262
+ return this._state === ResourceState.Complete;
2263
+ },
2264
+ enumerable: true,
2265
+ configurable: true
2266
+ });
2267
+ Resource.prototype.abort = function () {
2268
+ this._strategy.abort();
2269
+ };
2270
+ Resource.prototype.load = function () {
2271
+ this._state = ResourceState.Loading;
2272
+ this.onStart.dispatch(this);
2273
+ this._strategy.load();
2274
+ };
2275
+ Resource.prototype._error = function (errMessage) {
2276
+ this._state = ResourceState.Complete;
2277
+ this.error = errMessage;
2278
+ this.onComplete.dispatch(this);
2279
+ };
2280
+ Resource.prototype._complete = function (type, data) {
2281
+ this._state = ResourceState.Complete;
2282
+ this.type = type;
2283
+ this.data = data;
2284
+ this.onComplete.dispatch(this);
2285
+ };
2286
+ Resource.prototype._progress = function (percent) {
2287
+ this.onProgress.dispatch(this, percent);
2288
+ };
2289
+ Resource.prototype._determineCrossOrigin = function (url, loc) {
2290
+ if (loc === void 0) {
2291
+ loc = windowAlias.location;
2292
+ }
2293
+ if (url.indexOf('data:') === 0 || url.indexOf('javascript:') === 0) return '';
2294
+ if (windowAlias.origin !== windowAlias.location.origin) return 'anonymous';
2295
+ if (!Resource._tempAnchor) Resource._tempAnchor = documentAlias.createElement('a');
2296
+ Resource._tempAnchor.href = url;
2297
+ var parsed = parseUri(Resource._tempAnchor.href, {
2298
+ strictMode: true
2299
+ });
2300
+ var samePort = !parsed.port && loc.port === '' || parsed.port === loc.port;
2301
+ var protocol = parsed.protocol ? parsed.protocol + ":" : '';
2302
+ if (parsed.host !== loc.hostname || !samePort || protocol !== loc.protocol) return 'anonymous';
2303
+ return '';
2304
+ };
2305
+ Resource._tempAnchor = null;
2306
+ Resource._defaultLoadStrategy = XhrLoadStrategy;
2307
+ Resource._loadStrategyMap = {
2308
+ gif: ImageLoadStrategy,
2309
+ png: ImageLoadStrategy,
2310
+ bmp: ImageLoadStrategy,
2311
+ jpg: ImageLoadStrategy,
2312
+ jpeg: ImageLoadStrategy,
2313
+ tif: ImageLoadStrategy,
2314
+ tiff: ImageLoadStrategy,
2315
+ webp: ImageLoadStrategy,
2316
+ tga: ImageLoadStrategy,
2317
+ svg: ImageLoadStrategy,
2318
+ 'svg+xml': ImageLoadStrategy,
2319
+ mp3: AudioLoadStrategy,
2320
+ ogg: AudioLoadStrategy,
2321
+ wav: AudioLoadStrategy,
2322
+ mp4: VideoLoadStrategy,
2323
+ webm: VideoLoadStrategy,
2324
+ mov: VideoLoadStrategy
2325
+ };
2326
+ return Resource;
2327
+ }();
2328
+ function eachSeries(array, iterator, callback, deferNext) {
2329
+ if (deferNext === void 0) {
2330
+ deferNext = false;
2331
+ }
2332
+ var i = 0;
2333
+ var len = array.length;
2334
+ (function next(err) {
2335
+ if (err || i === len) {
2336
+ if (callback) callback(err);
2337
+ return;
2338
+ }
2339
+ if (deferNext) setTimeout(function () {
2340
+ return iterator(array[i++], next);
2341
+ }, 1);else iterator(array[i++], next);
2342
+ })();
2343
+ }
2344
+ var MAX_PROGRESS = 100;
2345
+ var rgxExtractUrlHash = /(#[\w-]+)?$/;
2346
+ var Loader = function () {
2347
+ function Loader(baseUrl, concurrency) {
2348
+ if (baseUrl === void 0) {
2349
+ baseUrl = '';
2350
+ }
2351
+ if (concurrency === void 0) {
2352
+ concurrency = 10;
2353
+ }
2354
+ this.progress = 0;
2355
+ this.loading = false;
2356
+ this.defaultQueryString = '';
2357
+ this.resources = {};
2358
+ this.onError = new Signal();
2359
+ this.onLoad = new Signal();
2360
+ this.onStart = new Signal();
2361
+ this.onComplete = new Signal();
2362
+ this.onProgress = new Signal();
2363
+ this._baseUrl = '';
2364
+ this._urlResolvers = [];
2365
+ this._middleware = [];
2366
+ this._resourcesParsing = [];
2367
+ this._boundLoadResource = this._loadResource.bind(this);
2368
+ this.baseUrl = baseUrl;
2369
+ this._queue = new AsyncQueue(this._boundLoadResource, concurrency);
2370
+ this._queue.pause();
2371
+ this._middleware = Loader._defaultMiddleware.slice();
2372
+ }
2373
+ Object.defineProperty(Loader.prototype, "baseUrl", {
2374
+ get: function get() {
2375
+ return this._baseUrl;
2376
+ },
2377
+ set: function set(url) {
2378
+ while (url.length && url.charAt(url.length - 1) === '/') {
2379
+ url = url.slice(0, -1);
2380
+ }
2381
+ this._baseUrl = url;
2382
+ },
2383
+ enumerable: true,
2384
+ configurable: true
2385
+ });
2386
+ Loader.prototype.add = function (options, url_) {
2387
+ if (Array.isArray(options)) {
2388
+ for (var i = 0; i < options.length; ++i) {
2389
+ this.add(options[i]);
2390
+ }
2391
+ return this;
2392
+ }
2393
+ var url = '';
2394
+ var name = '';
2395
+ var baseUrl = this._baseUrl;
2396
+ var resOptions = {
2397
+ url: ''
2398
+ };
2399
+ if (typeof options === 'object') {
2400
+ url = options.url;
2401
+ name = options.name || options.url;
2402
+ baseUrl = options.baseUrl || baseUrl;
2403
+ resOptions = options;
2404
+ } else {
2405
+ name = options;
2406
+ if (typeof url_ === 'string') url = url_;else url = name;
2407
+ }
2408
+ if (!url) throw new Error('You must specify the `url` property.');
2409
+ if (this.loading && !resOptions.parentResource) {
2410
+ throw new Error('Cannot add root resources while the loader is running.');
2411
+ }
2412
+ if (this.resources[name]) {
2413
+ throw new Error("Resource named \"" + name + "\" already exists.");
2414
+ }
2415
+ url = this._prepareUrl(url, baseUrl);
2416
+ resOptions.url = url;
2417
+ var resource = new Resource$1(name, resOptions);
2418
+ this.resources[name] = resource;
2419
+ if (typeof resOptions.onComplete === 'function') {
2420
+ resource.onAfterMiddleware.once(resOptions.onComplete);
2421
+ }
2422
+ if (this.loading) {
2423
+ var parent_1 = resOptions.parentResource;
2424
+ var incompleteChildren = [];
2425
+ for (var i = 0; i < parent_1.children.length; ++i) {
2426
+ if (!parent_1.children[i].isComplete) {
2427
+ incompleteChildren.push(parent_1.children[i]);
2428
+ }
2429
+ }
2430
+ var fullChunk = parent_1.progressChunk * (incompleteChildren.length + 1);
2431
+ var eachChunk = fullChunk / (incompleteChildren.length + 2);
2432
+ parent_1.children.push(resource);
2433
+ parent_1.progressChunk = eachChunk;
2434
+ for (var i = 0; i < incompleteChildren.length; ++i) {
2435
+ incompleteChildren[i].progressChunk = eachChunk;
2436
+ }
2437
+ resource.progressChunk = eachChunk;
2438
+ }
2439
+ this._queue.push(resource);
2440
+ return this;
2441
+ };
2442
+ Loader.prototype.use = function (fn, priority) {
2443
+ if (priority === void 0) {
2444
+ priority = Loader.DefaultMiddlewarePriority;
2445
+ }
2446
+ this._middleware.push({
2447
+ fn: fn,
2448
+ priority: priority
2449
+ });
2450
+ this._middleware.sort(function (a, b) {
2451
+ return a.priority - b.priority;
2452
+ });
2453
+ return this;
2454
+ };
2455
+ Loader.prototype.reset = function () {
2456
+ this.progress = 0;
2457
+ this.loading = false;
2458
+ this._queue.reset();
2459
+ this._queue.pause();
2460
+ for (var k in this.resources) {
2461
+ var res = this.resources[k];
2462
+ if (!res) continue;
2463
+ if (res._onCompleteBinding) res._onCompleteBinding.detach();
2464
+ if (res.isLoading) res.abort();
2465
+ }
2466
+ this.resources = {};
2467
+ return this;
2468
+ };
2469
+ Loader.prototype.load = function (cb) {
2470
+ if (typeof cb === 'function') this.onComplete.once(cb);
2471
+ if (this.loading) return this;
2472
+ if (this._queue.idle()) {
2473
+ this._onStart();
2474
+ this._onComplete();
2475
+ } else {
2476
+ var numTasks = this._queue.length();
2477
+ var chunk = MAX_PROGRESS / numTasks;
2478
+ for (var i = 0; i < this._queue.length(); ++i) {
2479
+ this._queue.getTask(i).data.progressChunk = chunk;
2480
+ }
2481
+ this._onStart();
2482
+ this._queue.resume();
2483
+ }
2484
+ return this;
2485
+ };
2486
+ Object.defineProperty(Loader.prototype, "concurrency", {
2487
+ get: function get() {
2488
+ return this._queue.concurrency;
2489
+ },
2490
+ set: function set(concurrency) {
2491
+ this._queue.concurrency = concurrency;
2492
+ },
2493
+ enumerable: true,
2494
+ configurable: true
2495
+ });
2496
+ Loader.prototype.addUrlResolver = function (func) {
2497
+ this._urlResolvers.push(func);
2498
+ return this;
2499
+ };
2500
+ Loader.prototype._prepareUrl = function (url, baseUrl) {
2501
+ var parsed = parseUri(url, {
2502
+ strictMode: true
2503
+ });
2504
+ this._urlResolvers.forEach(function (resolver) {
2505
+ url = resolver(url, parsed);
2506
+ parsed = parseUri(url, {
2507
+ strictMode: true
2508
+ });
2509
+ });
2510
+ if (!parsed.protocol && url.indexOf('//') !== 0) {
2511
+ if (baseUrl.length && url.charAt(0) !== '/') url = baseUrl + "/" + url;else url = baseUrl + url;
2512
+ }
2513
+ if (this.defaultQueryString) {
2514
+ var match = rgxExtractUrlHash.exec(url);
2515
+ if (match) {
2516
+ var hash = match[0];
2517
+ url = url.substr(0, url.length - hash.length);
2518
+ if (url.indexOf('?') !== -1) url += "&" + this.defaultQueryString;else url += "?" + this.defaultQueryString;
2519
+ url += hash;
2520
+ }
2521
+ }
2522
+ return url;
2523
+ };
2524
+ Loader.prototype._loadResource = function (resource, dequeue) {
2525
+ resource._dequeue = dequeue;
2526
+ resource._onCompleteBinding = resource.onComplete.once(this._onLoad, this);
2527
+ resource.load();
2528
+ };
2529
+ Loader.prototype._onStart = function () {
2530
+ this.progress = 0;
2531
+ this.loading = true;
2532
+ this.onStart.dispatch(this);
2533
+ };
2534
+ Loader.prototype._onComplete = function () {
2535
+ this.progress = MAX_PROGRESS;
2536
+ this.loading = false;
2537
+ this.onComplete.dispatch(this, this.resources);
2538
+ };
2539
+ Loader.prototype._onLoad = function (resource) {
2540
+ var _this = this;
2541
+ resource._onCompleteBinding = null;
2542
+ this._resourcesParsing.push(resource);
2543
+ resource._dequeue();
2544
+ eachSeries(this._middleware, function (middleware, next) {
2545
+ middleware.fn.call(_this, resource, next);
2546
+ }, function () {
2547
+ resource.onAfterMiddleware.dispatch(resource);
2548
+ _this.progress = Math.min(MAX_PROGRESS, _this.progress + resource.progressChunk);
2549
+ _this.onProgress.dispatch(_this, resource);
2550
+ if (resource.error) _this.onError.dispatch(resource.error, _this, resource);else _this.onLoad.dispatch(_this, resource);
2551
+ _this._resourcesParsing.splice(_this._resourcesParsing.indexOf(resource), 1);
2552
+ if (_this._queue.idle() && _this._resourcesParsing.length === 0) _this._onComplete();
2553
+ }, true);
2554
+ };
2555
+ Loader.use = function (fn, priority) {
2556
+ if (priority === void 0) {
2557
+ priority = Loader.DefaultMiddlewarePriority;
2558
+ }
2559
+ Loader._defaultMiddleware.push({
2560
+ fn: fn,
2561
+ priority: priority
2562
+ });
2563
+ Loader._defaultMiddleware.sort(function (a, b) {
2564
+ return a.priority - b.priority;
2565
+ });
2566
+ return Loader;
2567
+ };
2568
+ Loader.DefaultMiddlewarePriority = 50;
2569
+ Loader._defaultMiddleware = [];
2570
+ return Loader;
2571
+ }();
2572
+ var Progress = function (_super) {
2573
+ __extends$1(Progress, _super);
2574
+ function Progress(_a) {
2575
+ var resource = _a.resource,
2576
+ resourceTotal = _a.resourceTotal;
2577
+ var _this = _super.call(this) || this;
2578
+ _this.progress = 0;
2579
+ _this.resourceTotal = 0;
2580
+ _this.resourceLoadedCount = 0;
2581
+ _this.resource = resource;
2582
+ _this.resourceTotal = resourceTotal;
2583
+ if (resourceTotal === 0) {
2584
+ _this.resource.emit(LOAD_EVENT.COMPLETE, _this);
2585
+ }
2586
+ return _this;
2587
+ }
2588
+ Progress.prototype.onStart = function () {
2589
+ this.resource.emit(LOAD_EVENT.START, this);
2590
+ };
2591
+ Progress.prototype.onProgress = function (param) {
2592
+ this.resourceLoadedCount++;
2593
+ this.progress = Math.floor(this.resourceLoadedCount / this.resourceTotal * 100) / 100;
2594
+ if (param.success) {
2595
+ this.resource.emit(LOAD_EVENT.LOADED, this, param);
2596
+ } else {
2597
+ this.resource.emit(LOAD_EVENT.ERROR, this, param);
2598
+ }
2599
+ this.resource.emit(LOAD_EVENT.PROGRESS, this, param);
2600
+ if (this.resourceLoadedCount === this.resourceTotal) {
2601
+ this.resource.emit(LOAD_EVENT.COMPLETE, this);
2602
+ }
2603
+ };
2604
+ return Progress;
2605
+ }(EventEmitter);
2606
+ var Progress$1 = Progress;
2607
+ var resourceLoader = {
2608
+ AbstractLoadStrategy: AbstractLoadStrategy,
2609
+ AudioLoadStrategy: AudioLoadStrategy,
2610
+ ImageLoadStrategy: ImageLoadStrategy,
2611
+ XhrResponseType: XhrResponseType,
2612
+ MediaElementLoadStrategy: MediaElementLoadStrategy,
2613
+ VideoLoadStrategy: VideoLoadStrategy,
2614
+ XhrLoadStrategy: XhrLoadStrategy,
2615
+ Loader: Loader,
2616
+ Resource: Resource$1,
2617
+ ResourceType: ResourceType,
2618
+ ResourceState: ResourceState
2619
+ };
2620
+ var LOAD_EVENT;
2621
+ (function (LOAD_EVENT) {
2622
+ LOAD_EVENT["START"] = "start";
2623
+ LOAD_EVENT["PROGRESS"] = "progress";
2624
+ LOAD_EVENT["LOADED"] = "loaded";
2625
+ LOAD_EVENT["COMPLETE"] = "complete";
2626
+ LOAD_EVENT["ERROR"] = "error";
2627
+ })(LOAD_EVENT || (LOAD_EVENT = {}));
2628
+ var RESOURCE_TYPE;
2629
+ (function (RESOURCE_TYPE) {
2630
+ RESOURCE_TYPE["IMAGE"] = "IMAGE";
2631
+ RESOURCE_TYPE["SPRITE"] = "SPRITE";
2632
+ RESOURCE_TYPE["SPRITE_ANIMATION"] = "SPRITE_ANIMATION";
2633
+ RESOURCE_TYPE["AUDIO"] = "AUDIO";
2634
+ RESOURCE_TYPE["VIDEO"] = "VIDEO";
2635
+ })(RESOURCE_TYPE || (RESOURCE_TYPE = {}));
2636
+ XhrLoadStrategy.setExtensionXhrType('json', XhrResponseType.Json);
2637
+ XhrLoadStrategy.setExtensionXhrType('tex', XhrResponseType.Json);
2638
+ XhrLoadStrategy.setExtensionXhrType('ske', XhrResponseType.Json);
2639
+ XhrLoadStrategy.setExtensionXhrType('mp3', XhrResponseType.Buffer);
2640
+ XhrLoadStrategy.setExtensionXhrType('wav', XhrResponseType.Buffer);
2641
+ XhrLoadStrategy.setExtensionXhrType('aac', XhrResponseType.Buffer);
2642
+ XhrLoadStrategy.setExtensionXhrType('ogg', XhrResponseType.Buffer);
2643
+ var RESOURCE_TYPE_STRATEGY = {
2644
+ png: ImageLoadStrategy,
2645
+ jpg: ImageLoadStrategy,
2646
+ jpeg: ImageLoadStrategy,
2647
+ webp: ImageLoadStrategy,
2648
+ json: XhrLoadStrategy,
2649
+ tex: XhrLoadStrategy,
2650
+ ske: XhrLoadStrategy,
2651
+ audio: XhrLoadStrategy,
2652
+ video: VideoLoadStrategy
2653
+ };
2654
+ var Resource = function (_super) {
2655
+ __extends$1(Resource, _super);
2656
+ function Resource(options) {
2657
+ var _this = _super.call(this) || this;
2658
+ _this.timeout = 6000;
2659
+ _this.preProcessResourceHandlers = [];
2660
+ _this.resourcesMap = {};
2661
+ _this.makeInstanceFunctions = {};
2662
+ _this.destroyInstanceFunctions = {};
2663
+ _this.promiseMap = {};
2664
+ _this.loaders = [];
2665
+ if (options && typeof options.timeout === 'number') {
2666
+ _this.timeout = options.timeout;
2667
+ }
2668
+ return _this;
2669
+ }
2670
+ Resource.prototype.loadConfig = function (resources) {
2671
+ this.addResource(resources);
2672
+ this.preload();
2673
+ };
2674
+ Resource.prototype.loadSingle = function (resource) {
2675
+ this.addResource([resource]);
2676
+ return this.getResource(resource.name);
2677
+ };
2678
+ Resource.prototype.addResource = function (resources) {
2679
+ var e_1, _a;
2680
+ if (!resources || resources.length < 1) {
2681
+ console.warn('no resources');
2682
+ return;
2683
+ }
2684
+ try {
2685
+ for (var resources_1 = __values(resources), resources_1_1 = resources_1.next(); !resources_1_1.done; resources_1_1 = resources_1.next()) {
2686
+ var res = resources_1_1.value;
2687
+ if (this.resourcesMap[res.name]) {
2688
+ console.warn(res.name + ' was already added');
2689
+ continue;
2690
+ }
2691
+ this.resourcesMap[res.name] = res;
2692
+ this.resourcesMap[res.name].data = {};
2693
+ }
2694
+ } catch (e_1_1) {
2695
+ e_1 = {
2696
+ error: e_1_1
2697
+ };
2698
+ } finally {
2699
+ try {
2700
+ if (resources_1_1 && !resources_1_1.done && (_a = resources_1.return)) _a.call(resources_1);
2701
+ } finally {
2702
+ if (e_1) throw e_1.error;
2703
+ }
2704
+ }
2705
+ };
2706
+ Resource.prototype.addPreProcessResourceHandler = function (handler) {
2707
+ this.preProcessResourceHandlers.push(handler);
2708
+ };
2709
+ Resource.prototype.removePreProcessResourceHandler = function (handler) {
2710
+ this.preProcessResourceHandlers.splice(this.preProcessResourceHandlers.indexOf(handler), 1);
2711
+ };
2712
+ Resource.prototype.preload = function () {
2713
+ var names = [];
2714
+ for (var key in this.resourcesMap) {
2715
+ var resource_1 = this.resourcesMap[key];
2716
+ if (resource_1.preload && !resource_1.complete && !this.promiseMap[key]) {
2717
+ names.push(resource_1.name);
2718
+ }
2719
+ }
2720
+ this.progress = new Progress$1({
2721
+ resource: this,
2722
+ resourceTotal: names.length
2723
+ });
2724
+ this.loadResource({
2725
+ names: names,
2726
+ preload: true
2727
+ });
2728
+ };
2729
+ Resource.prototype.getResource = function (name) {
2730
+ return __awaiter(this, void 0, void 0, function () {
2731
+ return __generator(this, function (_a) {
2732
+ this.loadResource({
2733
+ names: [name]
2734
+ });
2735
+ return [2, this.promiseMap[name] || Promise.resolve({})];
2736
+ });
2737
+ });
2738
+ };
2739
+ Resource.prototype.instance = function (name) {
2740
+ return __awaiter(this, void 0, void 0, function () {
2741
+ var res, _a;
2742
+ return __generator(this, function (_b) {
2743
+ switch (_b.label) {
2744
+ case 0:
2745
+ res = this.resourcesMap[name];
2746
+ _a = this.makeInstanceFunctions[res.type];
2747
+ if (!_a) return [3, 2];
2748
+ return [4, this.makeInstanceFunctions[res.type](res)];
2749
+ case 1:
2750
+ _a = _b.sent();
2751
+ _b.label = 2;
2752
+ case 2:
2753
+ return [2, _a];
2754
+ }
2755
+ });
2756
+ });
2757
+ };
2758
+ Resource.prototype.destroy = function (name) {
2759
+ return __awaiter(this, void 0, void 0, function () {
2760
+ return __generator(this, function (_a) {
2761
+ switch (_a.label) {
2762
+ case 0:
2763
+ return [4, this._destroy(name)];
2764
+ case 1:
2765
+ _a.sent();
2766
+ return [2];
2767
+ }
2768
+ });
2769
+ });
2770
+ };
2771
+ Resource.prototype._destroy = function (name, loadError) {
2772
+ if (loadError === void 0) {
2773
+ loadError = false;
2774
+ }
2775
+ return __awaiter(this, void 0, void 0, function () {
2776
+ var resource, e_2;
2777
+ return __generator(this, function (_a) {
2778
+ switch (_a.label) {
2779
+ case 0:
2780
+ resource = this.resourcesMap[name];
2781
+ if (!resource) return [2];
2782
+ if (!!loadError) return [3, 5];
2783
+ _a.label = 1;
2784
+ case 1:
2785
+ _a.trys.push([1, 4,, 5]);
2786
+ if (!this.destroyInstanceFunctions[resource.type]) return [3, 3];
2787
+ return [4, this.destroyInstanceFunctions[resource.type](resource)];
2788
+ case 2:
2789
+ _a.sent();
2790
+ _a.label = 3;
2791
+ case 3:
2792
+ return [3, 5];
2793
+ case 4:
2794
+ e_2 = _a.sent();
2795
+ console.warn("destroy resource " + resource.name + " error with '" + e_2.message + "'");
2796
+ return [3, 5];
2797
+ case 5:
2798
+ delete this.promiseMap[name];
2799
+ resource.data = {};
2800
+ resource.complete = false;
2801
+ resource.instance = undefined;
2802
+ return [2];
2803
+ }
2804
+ });
2805
+ });
2806
+ };
2807
+ Resource.prototype.registerResourceType = function (type, value) {
2808
+ if (value === void 0) {
2809
+ value = type;
2810
+ }
2811
+ if (RESOURCE_TYPE[type]) {
2812
+ throw new Error("The type " + type + " already exists in RESOURCE_TYPE");
2813
+ }
2814
+ RESOURCE_TYPE[type] = value;
2815
+ };
2816
+ Resource.prototype.registerInstance = function (type, callback) {
2817
+ this.makeInstanceFunctions[type] = callback;
2818
+ };
2819
+ Resource.prototype.registerDestroy = function (type, callback) {
2820
+ this.destroyInstanceFunctions[type] = callback;
2821
+ };
2822
+ Resource.prototype.loadResource = function (_a) {
2823
+ var _this = this;
2824
+ var _b = _a.names,
2825
+ names = _b === void 0 ? [] : _b,
2826
+ _c = _a.preload,
2827
+ preload = _c === void 0 ? false : _c;
2828
+ var unLoadNames = names.filter(function (name) {
2829
+ return !_this.promiseMap[name] && _this.resourcesMap[name];
2830
+ });
2831
+ if (!unLoadNames.length) return;
2832
+ var resolves = {};
2833
+ var loader = this.getLoader(preload);
2834
+ unLoadNames.forEach(function (name) {
2835
+ var e_3, _a;
2836
+ _this.promiseMap[name] = new Promise(function (r) {
2837
+ return resolves[name] = r;
2838
+ });
2839
+ var res = _this.resourcesMap[name];
2840
+ try {
2841
+ for (var _b = __values(_this.preProcessResourceHandlers), _c = _b.next(); !_c.done; _c = _b.next()) {
2842
+ var handler = _c.value;
2843
+ handler(res);
2844
+ }
2845
+ } catch (e_3_1) {
2846
+ e_3 = {
2847
+ error: e_3_1
2848
+ };
2849
+ } finally {
2850
+ try {
2851
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
2852
+ } finally {
2853
+ if (e_3) throw e_3.error;
2854
+ }
2855
+ }
2856
+ for (var key in res.src) {
2857
+ var resourceType = res.src[key].type;
2858
+ if (resourceType === 'data') {
2859
+ res.data[key] = res.src[key].data;
2860
+ _this.doComplete(name, resolves[name], preload);
2861
+ } else {
2862
+ loader.add({
2863
+ url: res.src[key].url,
2864
+ name: res.name + "_" + key,
2865
+ strategy: RESOURCE_TYPE_STRATEGY[resourceType],
2866
+ metadata: {
2867
+ key: key,
2868
+ name: res.name,
2869
+ resolves: resolves
2870
+ }
2871
+ });
2872
+ }
2873
+ }
2874
+ });
2875
+ loader.load();
2876
+ };
2877
+ Resource.prototype.doComplete = function (name, resolve, preload) {
2878
+ if (preload === void 0) {
2879
+ preload = false;
2880
+ }
2881
+ return __awaiter(this, void 0, void 0, function () {
2882
+ var res, param, _a, err_1;
2883
+ return __generator(this, function (_b) {
2884
+ switch (_b.label) {
2885
+ case 0:
2886
+ res = this.resourcesMap[name];
2887
+ param = {
2888
+ name: name,
2889
+ resource: this.resourcesMap[name],
2890
+ success: true
2891
+ };
2892
+ if (!this.checkAllLoaded(name)) return [3, 4];
2893
+ _b.label = 1;
2894
+ case 1:
2895
+ _b.trys.push([1, 3,, 4]);
2896
+ _a = res;
2897
+ return [4, this.instance(name)];
2898
+ case 2:
2899
+ _a.instance = _b.sent();
2900
+ res.complete = true;
2901
+ if (preload) {
2902
+ this.progress.onProgress(param);
2903
+ }
2904
+ resolve(res);
2905
+ return [3, 4];
2906
+ case 3:
2907
+ err_1 = _b.sent();
2908
+ console.error(err_1);
2909
+ res.complete = false;
2910
+ if (preload) {
2911
+ param.errMsg = err_1.message;
2912
+ param.success = false;
2913
+ this.progress.onProgress(param);
2914
+ }
2915
+ resolve({});
2916
+ return [3, 4];
2917
+ case 4:
2918
+ return [2];
2919
+ }
2920
+ });
2921
+ });
2922
+ };
2923
+ Resource.prototype.checkAllLoaded = function (name) {
2924
+ var res = this.resourcesMap[name];
2925
+ return Array.from(Object.keys(res.src)).every(function (resourceKey) {
2926
+ return res.data[resourceKey];
2927
+ });
2928
+ };
2929
+ Resource.prototype.getLoader = function (preload) {
2930
+ var _this = this;
2931
+ if (preload === void 0) {
2932
+ preload = false;
2933
+ }
2934
+ var loader = this.loaders.find(function (_a) {
2935
+ var loading = _a.loading;
2936
+ return !loading;
2937
+ });
2938
+ if (!loader) {
2939
+ loader = new Loader();
2940
+ this.loaders.push(loader);
2941
+ }
2942
+ if (preload) {
2943
+ loader.onStart.once(function () {
2944
+ _this.progress.onStart();
2945
+ });
2946
+ }
2947
+ loader.onLoad.add(function (_, resource) {
2948
+ _this.onLoad({
2949
+ preload: preload,
2950
+ resource: resource
2951
+ });
2952
+ });
2953
+ loader.onError.add(function (errMsg, _, resource) {
2954
+ _this.onError({
2955
+ errMsg: errMsg,
2956
+ resource: resource,
2957
+ preload: preload
2958
+ });
2959
+ });
2960
+ loader.onComplete.once(function () {
2961
+ loader.onLoad.detachAll();
2962
+ loader.onError.detachAll();
2963
+ loader.reset();
2964
+ });
2965
+ return loader;
2966
+ };
2967
+ Resource.prototype.onLoad = function (_a) {
2968
+ var _b = _a.preload,
2969
+ preload = _b === void 0 ? false : _b,
2970
+ resource = _a.resource;
2971
+ return __awaiter(this, void 0, void 0, function () {
2972
+ var _c, key, name, resolves, data, res;
2973
+ return __generator(this, function (_d) {
2974
+ _c = resource.metadata, key = _c.key, name = _c.name, resolves = _c.resolves, data = resource.data;
2975
+ res = this.resourcesMap[name];
2976
+ res.data[key] = data;
2977
+ this.doComplete(name, resolves[name], preload);
2978
+ return [2];
2979
+ });
2980
+ });
2981
+ };
2982
+ Resource.prototype.onError = function (_a) {
2983
+ var errMsg = _a.errMsg,
2984
+ _b = _a.preload,
2985
+ preload = _b === void 0 ? false : _b,
2986
+ resource = _a.resource;
2987
+ return __awaiter(this, void 0, void 0, function () {
2988
+ var _c, name, resolves, param;
2989
+ return __generator(this, function (_d) {
2990
+ _c = resource.metadata, name = _c.name, resolves = _c.resolves;
2991
+ this._destroy(name, true);
2992
+ resolves[name]({});
2993
+ if (preload) {
2994
+ param = {
2995
+ name: name,
2996
+ resource: this.resourcesMap[name],
2997
+ success: false,
2998
+ errMsg: errMsg
2999
+ };
3000
+ this.progress.onProgress(param);
3001
+ }
3002
+ return [2];
3003
+ });
3004
+ });
3005
+ };
3006
+ return Resource;
3007
+ }(EventEmitter);
3008
+ var resource = new Resource();
3009
+ var decorators = {
3010
+ IDEProp: IDEProp,
3011
+ componentObserver: componentObserver
3012
+ };
3013
+ var version = '1.2.7-fix.2';
3014
+ console.log("Eva.js version: " + version);
3015
+ export { Component$1 as Component, Game$1 as Game, GameObject$1 as GameObject, IDEProp, LOAD_EVENT, LOAD_SCENE_MODE, ObserverType as OBSERVER_TYPE, RESOURCE_TYPE, RESOURCE_TYPE_STRATEGY, Scene$1 as Scene, System$1 as System, Transform$1 as Transform, componentObserver, decorators, resource, resourceLoader, version };