@eva/eva.js
Version:
@eva/eva.js
2,034 lines (1,697 loc) • 95.2 kB
JavaScript
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); }
import { __extends as __extends$1, __spread, __values, __decorate, __awaiter, __generator } from 'tslib';
import EventEmitter from 'eventemitter3';
import { isEqual, isObject } from 'lodash-es';
import { type, step } from '@eva/inspector-decorator';
import { requestAnimationFrame, windowAlias, navigator, documentAlias, XMLHttpRequestAlias } from '@eva/miniprogram-adapter';
function getComponentName(component) {
if (component instanceof Component) {
return component.name;
} else if (component instanceof Function) {
return component.componentName;
}
}
var Component = function (_super) {
__extends$1(Component, _super);
function Component(params) {
var _this = _super.call(this) || this;
_this.started = false;
_this.name = _this.constructor.componentName;
_this.__componentDefaultParams = params;
return _this;
}
return Component;
}(EventEmitter);
var Component$1 = Component;
var ObserverType;
(function (ObserverType) {
ObserverType["ADD"] = "ADD";
ObserverType["REMOVE"] = "REMOVE";
ObserverType["CHANGE"] = "CHANGE";
})(ObserverType || (ObserverType = {}));
var objectCache = {};
var systemInstance = {};
var observerInfos = {};
var componentProps = {};
function getObjectCache(component, keys) {
if (!objectCache[component.gameObject.id]) {
objectCache[component.gameObject.id] = {};
}
var cache = objectCache[component.gameObject.id];
var key = component.name + '_' + keys.join(',');
if (cache[key]) {
return cache[key];
}
var keyIndex = keys.length - 1;
var property = component;
for (var i = 0; i < keyIndex; i++) {
property = property[keys[i]];
}
cache[key] = {
property: property,
key: keys[keyIndex]
};
return cache[key];
}
function removeObjectCache(component) {
if (component.gameObject) {
delete objectCache[component.gameObject.id];
}
}
function addObserver(_a) {
var _b, _c;
var systemName = _a.systemName,
componentName = _a.componentName,
component = _a.component,
prop = _a.prop,
type = _a.type;
(_c = (_b = systemInstance[systemName]) === null || _b === void 0 ? void 0 : _b.componentObserver) === null || _c === void 0 ? void 0 : _c.add({
component: component,
prop: prop,
type: type,
componentName: componentName
});
}
function pushToQueue(_a) {
var prop = _a.prop,
component = _a.component,
componentName = _a.componentName;
for (var systemName in observerInfos) {
var observerInfo = observerInfos[systemName] || {};
var info = observerInfo[componentName];
if (!info) continue;
var index = info.findIndex(function (p) {
return isEqual(p, prop);
});
if (index > -1) {
addObserver({
systemName: systemName,
componentName: componentName,
component: component,
prop: prop,
type: ObserverType.CHANGE
});
}
}
}
function defineProperty(_a) {
var e_1, _b;
var obj = _a.obj,
key = _a.key,
prop = _a.prop,
component = _a.component,
componentName = _a.componentName;
if (obj === undefined) {
return;
}
if (!(key in obj)) {
console.error("prop " + key + " not in component: " + componentName + ", Can not observer");
return;
}
Object.defineProperty(obj, "_" + key, {
enumerable: false,
writable: true,
value: obj[key]
});
if (prop.deep && isObject(obj[key])) {
try {
for (var _c = __values(Object.keys(obj[key])), _d = _c.next(); !_d.done; _d = _c.next()) {
var childKey = _d.value;
defineProperty({
obj: obj[key],
key: childKey,
prop: prop,
component: component,
componentName: componentName
});
}
} catch (e_1_1) {
e_1 = {
error: e_1_1
};
} finally {
try {
if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
} finally {
if (e_1) throw e_1.error;
}
}
}
Object.defineProperty(obj, key, {
enumerable: true,
set: function set(val) {
if (obj["_" + key] === val) return;
obj["_" + key] = val;
pushToQueue({
prop: prop,
component: component,
componentName: componentName
});
},
get: function get() {
return obj["_" + key];
}
});
}
function isComponent(comp) {
return comp && comp.constructor && 'componentName' in comp.constructor;
}
function initObserver(Systems) {
var e_2, _a, e_3, _b;
var Ss = [];
if (Systems instanceof Array) {
Ss.push.apply(Ss, __spread(Systems));
} else {
Ss.push(Systems);
}
try {
for (var Ss_1 = __values(Ss), Ss_1_1 = Ss_1.next(); !Ss_1_1.done; Ss_1_1 = Ss_1.next()) {
var S = Ss_1_1.value;
for (var componentName in S.observerInfo) {
componentProps[componentName] = componentProps[componentName] || [];
var props = componentProps[componentName];
var _loop_1 = function _loop_1(prop) {
var index = props.findIndex(function (p) {
return isEqual(p, prop);
});
if (index === -1) {
componentProps[componentName].push(prop);
}
};
try {
for (var _c = (e_3 = void 0, __values(S.observerInfo[componentName])), _d = _c.next(); !_d.done; _d = _c.next()) {
var prop = _d.value;
_loop_1(prop);
}
} catch (e_3_1) {
e_3 = {
error: e_3_1
};
} finally {
try {
if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
} finally {
if (e_3) throw e_3.error;
}
}
}
}
} catch (e_2_1) {
e_2 = {
error: e_2_1
};
} finally {
try {
if (Ss_1_1 && !Ss_1_1.done && (_a = Ss_1.return)) _a.call(Ss_1);
} finally {
if (e_2) throw e_2.error;
}
}
}
function observer(component, componentName) {
var e_4, _a;
if (componentName === void 0) {
componentName = component.name;
}
if (!componentName || !componentProps[componentName]) {
return;
}
if (!component || !isComponent(component)) {
throw new Error('component param must be an instance of Component');
}
if (!component.gameObject || !component.gameObject.id) {
throw new Error('component should be add to a gameObject');
}
try {
for (var _b = __values(componentProps[componentName]), _c = _b.next(); !_c.done; _c = _b.next()) {
var item = _c.value;
var _d = getObjectCache(component, item.prop),
property = _d.property,
key = _d.key;
defineProperty({
obj: property,
key: key,
prop: item,
component: component,
componentName: componentName
});
}
} catch (e_4_1) {
e_4 = {
error: e_4_1
};
} finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} finally {
if (e_4) throw e_4.error;
}
}
}
function observerAdded(component, componentName) {
var _a, _b;
if (componentName === void 0) {
componentName = component.name;
}
for (var systemName in observerInfos) {
var observerInfo = observerInfos[systemName] || {};
var info = observerInfo[componentName];
if (info) {
(_b = (_a = systemInstance[systemName]) === null || _a === void 0 ? void 0 : _a.componentObserver) === null || _b === void 0 ? void 0 : _b.add({
component: component,
type: ObserverType.ADD,
componentName: componentName
});
}
}
}
function observerRemoved(component, componentName) {
var _a, _b;
if (componentName === void 0) {
componentName = component.name;
}
for (var systemName in observerInfos) {
var observerInfo = observerInfos[systemName] || {};
var info = observerInfo[componentName];
if (info) {
(_b = (_a = systemInstance[systemName]) === null || _a === void 0 ? void 0 : _a.componentObserver) === null || _b === void 0 ? void 0 : _b.add({
component: component,
type: ObserverType.REMOVE,
componentName: componentName
});
}
}
removeObjectCache(component);
}
function setSystemObserver(system, S) {
observerInfos[S.systemName] = S.observerInfo;
systemInstance[S.systemName] = system;
}
var Transform = function (_super) {
__extends$1(Transform, _super);
function Transform() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.name = 'Transform';
_this._parent = null;
_this.inScene = false;
_this.children = [];
_this.position = {
x: 0,
y: 0
};
_this.size = {
width: 0,
height: 0
};
_this.origin = {
x: 0,
y: 0
};
_this.anchor = {
x: 0,
y: 0
};
_this.scale = {
x: 1,
y: 1
};
_this.skew = {
x: 0,
y: 0
};
_this.rotation = 0;
return _this;
}
Transform.prototype.init = function (params) {
var e_1, _a;
if (params === void 0) {
params = {};
}
var props = ['position', 'size', 'origin', 'anchor', 'scale', 'skew'];
try {
for (var props_1 = __values(props), props_1_1 = props_1.next(); !props_1_1.done; props_1_1 = props_1.next()) {
var key = props_1_1.value;
_extends(this[key], params[key]);
}
} catch (e_1_1) {
e_1 = {
error: e_1_1
};
} finally {
try {
if (props_1_1 && !props_1_1.done && (_a = props_1.return)) _a.call(props_1);
} finally {
if (e_1) throw e_1.error;
}
}
this.rotation = params.rotation || this.rotation;
};
Object.defineProperty(Transform.prototype, "parent", {
get: function get() {
return this._parent;
},
set: function set(val) {
if (val) {
val.addChild(this);
} else if (this.parent) {
this.parent.removeChild(this);
}
},
enumerable: false,
configurable: true
});
Transform.prototype.addChild = function (child) {
if (child.parent === this) {
var index = this.children.findIndex(function (item) {
return item === child;
});
this.children.splice(index, 1);
} else if (child.parent) {
child.parent.removeChild(child);
}
child._parent = this;
this.children.push(child);
};
Transform.prototype.removeChild = function (child) {
var index = this.children.findIndex(function (item) {
return item === child;
});
if (index > -1) {
this.children.splice(index, 1);
child._parent = null;
}
};
Transform.prototype.clearChildren = function () {
this.children.length = 0;
};
Transform.componentName = 'Transform';
__decorate([type('vector2'), step(1)], Transform.prototype, "position", void 0);
__decorate([type('size'), step(1)], Transform.prototype, "size", void 0);
__decorate([type('vector2'), step(0.1)], Transform.prototype, "origin", void 0);
__decorate([type('vector2'), step(0.1)], Transform.prototype, "anchor", void 0);
__decorate([type('vector2'), step(0.1)], Transform.prototype, "scale", void 0);
__decorate([type('vector2'), step(0.1)], Transform.prototype, "skew", void 0);
__decorate([type('number'), step(0.1)], Transform.prototype, "rotation", void 0);
return Transform;
}(Component$1);
var Transform$1 = Transform;
var _id = 0;
function getId() {
return ++_id;
}
var GameObject = function () {
function GameObject(name, obj) {
this._componentCache = {};
this.components = [];
this.destroyed = false;
this._name = name;
this.id = getId();
this.addComponent(Transform$1, obj);
}
Object.defineProperty(GameObject.prototype, "transform", {
get: function get() {
return this.getComponent(Transform$1);
},
enumerable: false,
configurable: true
});
Object.defineProperty(GameObject.prototype, "parent", {
get: function get() {
return this.transform && this.transform.parent && this.transform.parent.gameObject;
},
enumerable: false,
configurable: true
});
Object.defineProperty(GameObject.prototype, "name", {
get: function get() {
return this._name;
},
enumerable: false,
configurable: true
});
Object.defineProperty(GameObject.prototype, "scene", {
get: function get() {
return this._scene;
},
set: function set(val) {
var e_1, _a;
if (this._scene === val) return;
var scene = this._scene;
this._scene = val;
if (this.transform && this.transform.children) {
try {
for (var _b = __values(this.transform.children), _c = _b.next(); !_c.done; _c = _b.next()) {
var child = _c.value;
child.gameObject.scene = val;
}
} catch (e_1_1) {
e_1 = {
error: e_1_1
};
} finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} finally {
if (e_1) throw e_1.error;
}
}
}
if (val) {
val.addGameObject(this);
} else {
scene && scene.removeGameObject(this);
}
},
enumerable: false,
configurable: true
});
GameObject.prototype.addChild = function (gameObject) {
if (!gameObject || !gameObject.transform || gameObject === this) return;
if (!(gameObject instanceof GameObject)) {
throw new Error('addChild only receive GameObject');
}
if (!this.transform) {
throw new Error("gameObject '" + this.name + "' has been destroy");
}
gameObject.transform.parent = this.transform;
gameObject.scene = this.scene;
};
GameObject.prototype.removeChild = function (gameObject) {
if (!(gameObject instanceof GameObject) || !gameObject.parent || gameObject.parent !== this) {
return gameObject;
}
gameObject.transform.parent = null;
gameObject.scene = null;
return gameObject;
};
GameObject.prototype.addComponent = function (C, obj) {
if (this.destroyed) return;
var componentName = getComponentName(C);
if (this._componentCache[componentName]) return;
var component;
if (C instanceof Function) {
component = new C(obj);
} else if (C instanceof Component$1) {
component = C;
} else {
throw new Error('addComponent recieve Component and Component Constructor');
}
if (component.gameObject) {
throw new Error("component has been added on gameObject " + component.gameObject.name);
}
component.gameObject = this;
component.init && component.init(component.__componentDefaultParams);
observerAdded(component, component.name);
observer(component, component.name);
this.components.push(component);
this._componentCache[componentName] = component;
component.awake && component.awake();
return component;
};
GameObject.prototype.removeComponent = function (c) {
var componentName;
if (typeof c === 'string') {
componentName = c;
} else if (c instanceof Component$1) {
componentName = c.name;
} else if (c.componentName) {
componentName = c.componentName;
}
if (componentName === 'Transform') {
throw new Error("Transform can't be removed");
}
return this._removeComponent(componentName);
};
GameObject.prototype._removeComponent = function (componentName) {
var index = this.components.findIndex(function (_a) {
var name = _a.name;
return name === componentName;
});
if (index === -1) return;
var component = this.components.splice(index, 1)[0];
delete this._componentCache[componentName];
delete component.__componentDefaultParams;
component.onDestroy && component.onDestroy();
observerRemoved(component, componentName);
component.gameObject = undefined;
return component;
};
GameObject.prototype.getComponent = function (c) {
var componentName;
if (typeof c === 'string') {
componentName = c;
} else if (c instanceof Component$1) {
componentName = c.name;
} else if (c.componentName) {
componentName = c.componentName;
}
if (typeof this._componentCache[componentName] !== 'undefined') {
return this._componentCache[componentName];
} else {
return;
}
};
GameObject.prototype.remove = function () {
if (this.parent) return this.parent.removeChild(this);
};
GameObject.prototype.destroy = function () {
if (!this.transform) {
console.error('Cannot destroy gameObject that have already been destroyed.');
return;
}
Array.from(this.transform.children).forEach(function (_a) {
var gameObject = _a.gameObject;
gameObject.destroy();
});
this.remove();
this.transform.clearChildren();
for (var key in this._componentCache) {
this._removeComponent(key);
}
this.components.length = 0;
this.destroyed = true;
};
return GameObject;
}();
var GameObject$1 = GameObject;
var ComponentObserver = function () {
function ComponentObserver() {
this.events = [];
}
ComponentObserver.prototype.add = function (_a) {
var component = _a.component,
prop = _a.prop,
type = _a.type,
componentName = _a.componentName;
if (type === ObserverType.REMOVE) {
if (this.events.find(function (changed) {
return changed.component === component && changed.type === ObserverType.ADD;
})) {
this.events = this.events.filter(function (changed) {
return changed.component !== component;
});
return;
}
this.events = this.events.filter(function (changed) {
return changed.component !== component;
});
}
var index = this.events.findIndex(function (changed) {
return changed.component === component && isEqual(changed.prop, prop) && changed.type === type;
});
if (index > -1) {
this.events.splice(index, 1);
}
this.events.push({
gameObject: component.gameObject,
component: component,
prop: prop,
type: type,
componentName: componentName
});
};
ComponentObserver.prototype.getChanged = function () {
return this.events;
};
Object.defineProperty(ComponentObserver.prototype, "changed", {
get: function get() {
return this.events;
},
enumerable: false,
configurable: true
});
ComponentObserver.prototype.clear = function () {
var events = this.events;
this.events = [];
return events;
};
return ComponentObserver;
}();
var ComponentObserver$1 = ComponentObserver;
var System = function () {
function System(params) {
this.started = false;
this.componentObserver = new ComponentObserver$1();
this.__systemDefaultParams = params;
this.name = this.constructor.systemName;
}
System.prototype.destroy = function () {
var _a;
this.componentObserver = null;
this.__systemDefaultParams = null;
(_a = this.onDestroy) === null || _a === void 0 ? void 0 : _a.call(this);
};
return System;
}();
var System$1 = System;
function createNowTime() {
var nowtime = null;
if (Date.now) {
nowtime = Date.now;
} else {
nowtime = function nowtime() {
return new Date().getTime();
};
}
return nowtime;
}
var _nowtime = createNowTime();
var defaultOptions$1 = {
originTime: 0,
playbackRate: 1.0
};
var Timeline = function () {
function Timeline(options, parent) {
if (options instanceof Timeline) {
parent = options;
options = {};
}
options = _extends({}, defaultOptions$1, options);
if (parent) {
this._parent = parent;
}
this._createTime = _nowtime();
this._timeMark = [{
globalTime: this.globalTime,
localTime: -options.originTime,
entropy: -options.originTime,
playbackRate: options.playbackRate,
globalEntropy: 0
}];
if (this._parent) {
this._timeMark[0].globalEntropy = this._parent.entropy;
}
this._playbackRate = options.playbackRate;
}
Object.defineProperty(Timeline.prototype, "globalTime", {
get: function get() {
return this.parent ? this.parent.currentTime : _nowtime() - this._createTime;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Timeline.prototype, "parent", {
get: function get() {
return this._parent;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Timeline.prototype, "lastTimeMark", {
get: function get() {
return this._timeMark[this._timeMark.length - 1];
},
enumerable: false,
configurable: true
});
Timeline.prototype.markTime = function (_a) {
var _b = _a === void 0 ? {} : _a,
_c = _b.time,
time = _c === void 0 ? this.currentTime : _c,
_d = _b.entropy,
entropy = _d === void 0 ? this.entropy : _d,
_e = _b.playbackRate,
playbackRate = _e === void 0 ? this.playbackRate : _e;
var timeMark = {
globalTime: this.globalTime,
localTime: time,
entropy: entropy,
playbackRate: playbackRate,
globalEntropy: this.globalEntropy
};
this._timeMark.push(timeMark);
};
Object.defineProperty(Timeline.prototype, "currentTime", {
get: function get() {
var _a = this.lastTimeMark,
localTime = _a.localTime,
globalTime = _a.globalTime;
return localTime + (this.globalTime - globalTime) * this.playbackRate;
},
set: function set(time) {
this.markTime({
time: time
});
},
enumerable: false,
configurable: true
});
Object.defineProperty(Timeline.prototype, "globalEntropy", {
get: function get() {
return this._parent ? this._parent.entropy : this.globalTime;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Timeline.prototype, "entropy", {
get: function get() {
var _a = this.lastTimeMark,
entropy = _a.entropy,
globalEntropy = _a.globalEntropy;
return entropy + Math.abs((this.globalEntropy - globalEntropy) * this.playbackRate);
},
set: function set(entropy) {
if (this.entropy > entropy) {
var idx = this.seekTimeMark(entropy);
this._timeMark.length = idx + 1;
}
this.markTime({
entropy: entropy
});
},
enumerable: false,
configurable: true
});
Timeline.prototype.fork = function (options) {
return new Timeline(options, this);
};
Timeline.prototype.seekGlobalTime = function (seekEntropy) {
var idx = this.seekTimeMark(seekEntropy),
timeMark = this._timeMark[idx];
var entropy = timeMark.entropy,
playbackRate = timeMark.playbackRate,
globalTime = timeMark.globalTime;
return globalTime + (seekEntropy - entropy) / Math.abs(playbackRate);
};
Timeline.prototype.seekLocalTime = function (seekEntropy) {
var idx = this.seekTimeMark(seekEntropy),
timeMark = this._timeMark[idx];
var localTime = timeMark.localTime,
entropy = timeMark.entropy,
playbackRate = timeMark.playbackRate;
if (playbackRate > 0) {
return localTime + (seekEntropy - entropy);
}
return localTime - (seekEntropy - entropy);
};
Timeline.prototype.seekTimeMark = function (entropy) {
var timeMark = this._timeMark;
var l = 0,
r = timeMark.length - 1;
if (entropy <= timeMark[l].entropy) {
return l;
}
if (entropy >= timeMark[r].entropy) {
return r;
}
var m = Math.floor((l + r) / 2);
while (m > l && m < r) {
if (entropy === timeMark[m].entropy) {
return m;
}
if (entropy < timeMark[m].entropy) {
r = m;
} else if (entropy > timeMark[m].entropy) {
l = m;
}
m = Math.floor((l + r) / 2);
}
return l;
};
Object.defineProperty(Timeline.prototype, "playbackRate", {
get: function get() {
return this._playbackRate;
},
set: function set(rate) {
if (rate !== this.playbackRate) {
this.markTime({
playbackRate: rate
});
this._playbackRate = rate;
}
},
enumerable: false,
configurable: true
});
Object.defineProperty(Timeline.prototype, "paused", {
get: function get() {
if (this.playbackRate === 0) return true;
var parent = this.parent;
while (parent) {
if (parent.playbackRate === 0) return true;
parent = parent.parent;
}
return false;
},
enumerable: false,
configurable: true
});
return Timeline;
}();
var Timeline$1 = Timeline;
var defaultOptions = {
autoStart: true,
frameRate: 60
};
var Ticker = function () {
function Ticker(options) {
var _this = this;
options = _extends({}, defaultOptions, options);
this._frameCount = 0;
this._frameDuration = 1000 / options.frameRate;
this.autoStart = options.autoStart;
this.frameRate = options.frameRate;
this.timeline = new Timeline$1({
originTime: 0,
playbackRate: 1.0
});
this._lastFrameTime = this.timeline.currentTime;
this._tickers = new Set();
this._requestId = null;
this._ticker = function () {
if (_this._started) {
_this._requestId = requestAnimationFrame(_this._ticker);
_this.update();
}
};
if (this.autoStart) {
this.start();
}
}
Ticker.prototype.update = function () {
var e_1, _a;
var currentTime = this.timeline.currentTime;
var durationTime = currentTime - this._lastFrameTime;
if (durationTime >= this._frameDuration) {
var frameTime = currentTime - durationTime % this._frameDuration;
var deltaTime = frameTime - this._lastFrameTime;
this._lastFrameTime = frameTime;
var options = {
deltaTime: deltaTime,
time: frameTime,
currentTime: frameTime,
frameCount: ++this._frameCount,
fps: Math.round(1000 / deltaTime)
};
try {
for (var _b = __values(this._tickers), _c = _b.next(); !_c.done; _c = _b.next()) {
var func = _c.value;
if (typeof func === 'function') {
func(options);
}
}
} catch (e_1_1) {
e_1 = {
error: e_1_1
};
} finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} finally {
if (e_1) throw e_1.error;
}
}
}
};
Ticker.prototype.add = function (fn) {
this._tickers.add(fn);
};
Ticker.prototype.remove = function (fn) {
this._tickers.delete(fn);
};
Ticker.prototype.start = function () {
if (this._started) return;
this._started = true;
this.timeline.playbackRate = 1.0;
this._requestId = requestAnimationFrame(this._ticker);
};
Ticker.prototype.pause = function () {
this._started = false;
this.timeline.playbackRate = 0;
};
Ticker.prototype.setPlaybackRate = function (rate) {
this.timeline.playbackRate = rate;
};
return Ticker;
}();
var Ticker$1 = Ticker;
var Scene = function (_super) {
__extends$1(Scene, _super);
function Scene(name, obj) {
var _this = _super.call(this, name, obj) || this;
_this.gameObjects = [];
_this.scene = _this;
return _this;
}
Scene.prototype.addGameObject = function (gameObject) {
this.gameObjects.push(gameObject);
if (gameObject.transform) {
gameObject.transform.inScene = true;
}
};
Scene.prototype.removeGameObject = function (gameObject) {
var index = this.gameObjects.indexOf(gameObject);
if (index === -1) return;
if (gameObject.transform) {
gameObject.transform.inScene = false;
}
this.gameObjects.splice(index, 1);
};
Scene.prototype.destroy = function () {
this.scene = null;
_super.prototype.destroy.call(this);
this.gameObjects = null;
this.canvas = null;
};
return Scene;
}(GameObject$1);
var Scene$1 = Scene;
var LOAD_SCENE_MODE;
(function (LOAD_SCENE_MODE) {
LOAD_SCENE_MODE["SINGLE"] = "SINGLE";
LOAD_SCENE_MODE["MULTI_CANVAS"] = "MULTI_CANVAS";
})(LOAD_SCENE_MODE || (LOAD_SCENE_MODE = {}));
var triggerStart = function triggerStart(obj) {
if (!(obj instanceof System$1) && !(obj instanceof Component$1)) return;
if (obj.started) return;
obj.started = true;
try {
obj.start && obj.start();
} catch (e) {
if (obj instanceof Component$1) {
console.error(obj.constructor.componentName + " start error", e);
} else {
console.error(obj.constructor.systemName + " start error", e);
}
}
};
var getAllGameObjects = function getAllGameObjects(game) {
var e_1, _a;
var _b;
var mainSceneGameObjects = ((_b = game === null || game === void 0 ? void 0 : game.scene) === null || _b === void 0 ? void 0 : _b.gameObjects) || [];
var gameObjectsArray = game === null || game === void 0 ? void 0 : game.multiScenes.map(function (_a) {
var gameObjects = _a.gameObjects;
return gameObjects;
});
var otherSceneGameObjects = [];
try {
for (var gameObjectsArray_1 = __values(gameObjectsArray), gameObjectsArray_1_1 = gameObjectsArray_1.next(); !gameObjectsArray_1_1.done; gameObjectsArray_1_1 = gameObjectsArray_1.next()) {
var gameObjects = gameObjectsArray_1_1.value;
otherSceneGameObjects = __spread(otherSceneGameObjects, gameObjects);
}
} catch (e_1_1) {
e_1 = {
error: e_1_1
};
} finally {
try {
if (gameObjectsArray_1_1 && !gameObjectsArray_1_1.done && (_a = gameObjectsArray_1.return)) _a.call(gameObjectsArray_1);
} finally {
if (e_1) throw e_1.error;
}
}
return __spread(mainSceneGameObjects, otherSceneGameObjects);
};
var gameObjectLoop = function gameObjectLoop(e, gameObjects) {
var e_2, _a, e_3, _b, e_4, _c, e_5, _d;
if (gameObjects === void 0) {
gameObjects = [];
}
try {
for (var gameObjects_1 = __values(gameObjects), gameObjects_1_1 = gameObjects_1.next(); !gameObjects_1_1.done; gameObjects_1_1 = gameObjects_1.next()) {
var gameObject = gameObjects_1_1.value;
try {
for (var _e = (e_3 = void 0, __values(gameObject.components)), _f = _e.next(); !_f.done; _f = _e.next()) {
var component = _f.value;
try {
triggerStart(component);
component.update && component.update(e);
} catch (e) {
console.error("gameObject: " + gameObject.name + " " + component.name + " update error", e);
}
}
} catch (e_3_1) {
e_3 = {
error: e_3_1
};
} finally {
try {
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
} finally {
if (e_3) throw e_3.error;
}
}
}
} catch (e_2_1) {
e_2 = {
error: e_2_1
};
} finally {
try {
if (gameObjects_1_1 && !gameObjects_1_1.done && (_a = gameObjects_1.return)) _a.call(gameObjects_1);
} finally {
if (e_2) throw e_2.error;
}
}
try {
for (var gameObjects_2 = __values(gameObjects), gameObjects_2_1 = gameObjects_2.next(); !gameObjects_2_1.done; gameObjects_2_1 = gameObjects_2.next()) {
var gameObject = gameObjects_2_1.value;
try {
for (var _g = (e_5 = void 0, __values(gameObject.components)), _h = _g.next(); !_h.done; _h = _g.next()) {
var component = _h.value;
try {
component.lateUpdate && component.lateUpdate(e);
} catch (e) {
console.error("gameObject: " + gameObject.name + " " + component.name + " lateUpdate error", e);
}
}
} catch (e_5_1) {
e_5 = {
error: e_5_1
};
} finally {
try {
if (_h && !_h.done && (_d = _g.return)) _d.call(_g);
} finally {
if (e_5) throw e_5.error;
}
}
}
} catch (e_4_1) {
e_4 = {
error: e_4_1
};
} finally {
try {
if (gameObjects_2_1 && !gameObjects_2_1.done && (_c = gameObjects_2.return)) _c.call(gameObjects_2);
} finally {
if (e_4) throw e_4.error;
}
}
};
var gameObjectResume = function gameObjectResume(gameObjects) {
var e_6, _a, e_7, _b;
try {
for (var gameObjects_3 = __values(gameObjects), gameObjects_3_1 = gameObjects_3.next(); !gameObjects_3_1.done; gameObjects_3_1 = gameObjects_3.next()) {
var gameObject = gameObjects_3_1.value;
try {
for (var _c = (e_7 = void 0, __values(gameObject.components)), _d = _c.next(); !_d.done; _d = _c.next()) {
var component = _d.value;
try {
component.onResume && component.onResume();
} catch (e) {
console.error("gameObject: " + gameObject.name + ", " + component.name + ", onResume error", e);
}
}
} catch (e_7_1) {
e_7 = {
error: e_7_1
};
} finally {
try {
if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
} finally {
if (e_7) throw e_7.error;
}
}
}
} catch (e_6_1) {
e_6 = {
error: e_6_1
};
} finally {
try {
if (gameObjects_3_1 && !gameObjects_3_1.done && (_a = gameObjects_3.return)) _a.call(gameObjects_3);
} finally {
if (e_6) throw e_6.error;
}
}
};
var gameObjectPause = function gameObjectPause(gameObjects) {
var e_8, _a, e_9, _b;
try {
for (var gameObjects_4 = __values(gameObjects), gameObjects_4_1 = gameObjects_4.next(); !gameObjects_4_1.done; gameObjects_4_1 = gameObjects_4.next()) {
var gameObject = gameObjects_4_1.value;
try {
for (var _c = (e_9 = void 0, __values(gameObject.components)), _d = _c.next(); !_d.done; _d = _c.next()) {
var component = _d.value;
try {
component.onPause && component.onPause();
} catch (e) {
console.error("gameObject: " + gameObject.name + ", " + component.name + ", onResume error", e);
}
}
} catch (e_9_1) {
e_9 = {
error: e_9_1
};
} finally {
try {
if (_d && !_d.done && (_b = _c.return)) _b.call(_c);
} finally {
if (e_9) throw e_9.error;
}
}
}
} catch (e_8_1) {
e_8 = {
error: e_8_1
};
} finally {
try {
if (gameObjects_4_1 && !gameObjects_4_1.done && (_a = gameObjects_4.return)) _a.call(gameObjects_4);
} finally {
if (e_8) throw e_8.error;
}
}
};
var Game = function (_super) {
__extends$1(Game, _super);
function Game(_a) {
var e_10, _b;
var _c = _a === void 0 ? {} : _a,
systems = _c.systems,
_d = _c.frameRate,
frameRate = _d === void 0 ? 60 : _d,
_e = _c.autoStart,
autoStart = _e === void 0 ? true : _e,
_f = _c.needScene,
needScene = _f === void 0 ? true : _f;
var _this = _super.call(this) || this;
_this.playing = false;
_this.started = false;
_this.multiScenes = [];
_this.systems = [];
if (windowAlias.__EVA_INSPECTOR_ENV__) {
windowAlias.__EVA_GAME_INSTANCE__ = _this;
}
_this.ticker = new Ticker$1({
autoStart: false,
frameRate: frameRate
});
_this.initTicker();
if (systems && systems.length) {
try {
for (var systems_1 = __values(systems), systems_1_1 = systems_1.next(); !systems_1_1.done; systems_1_1 = systems_1.next()) {
var system = systems_1_1.value;
_this.addSystem(system);
}
} catch (e_10_1) {
e_10 = {
error: e_10_1
};
} finally {
try {
if (systems_1_1 && !systems_1_1.done && (_b = systems_1.return)) _b.call(systems_1);
} finally {
if (e_10) throw e_10.error;
}
}
}
if (needScene) {
_this.loadScene(new Scene$1('scene'));
}
if (autoStart) {
_this.start();
}
return _this;
}
Object.defineProperty(Game.prototype, "scene", {
get: function get() {
return this._scene;
},
set: function set(scene) {
this._scene = scene;
},
enumerable: false,
configurable: true
});
Object.defineProperty(Game.prototype, "gameObjects", {
get: function get() {
return getAllGameObjects(this);
},
enumerable: false,
configurable: true
});
Game.prototype.addSystem = function (S, obj) {
var system;
if (S instanceof Function) {
system = new S(obj);
} else if (S instanceof System$1) {
system = S;
} else {
console.warn('can only add System');
return;
}
var hasTheSystem = this.systems.find(function (item) {
return item.constructor === system.constructor;
});
if (hasTheSystem) {
console.warn(system.constructor.systemName + " System has been added");
return;
}
system.game = this;
system.init && system.init(system.__systemDefaultParams);
setSystemObserver(system, system.constructor);
initObserver(system.constructor);
try {
system.awake && system.awake();
} catch (e) {
console.error(system.constructor.systemName + " awake error", e);
}
this.systems.push(system);
return system;
};
Game.prototype.removeSystem = function (system) {
if (!system) return;
var index = -1;
if (typeof system === 'string') {
index = this.systems.findIndex(function (s) {
return s.name === system;
});
} else if (system instanceof Function) {
index = this.systems.findIndex(function (s) {
return s.constructor === system;
});
} else if (system instanceof System$1) {
index = this.systems.findIndex(function (s) {
return s === system;
});
}
if (index > -1) {
this.systems[index].destroy && this.systems[index].destroy();
this.systems.splice(index, 1);
}
};
Game.prototype.getSystem = function (S) {
return this.systems.find(function (system) {
if (typeof S === 'string') {
return system.name === S;
} else {
return system instanceof S;
}
});
};
Game.prototype.pause = function () {
if (!this.playing) return;
this.playing = false;
this.ticker.pause();
this.triggerPause();
};
Game.prototype.start = function () {
if (this.playing) return;
this.playing = true;
this.started = true;
this.ticker.start();
};
Game.prototype.resume = function () {
if (this.playing) return;
this.playing = true;
this.ticker.start();
this.triggerResume();
};
Game.prototype.initTicker = function () {
var _this = this;
this.ticker.add(function (e) {
var e_11, _a, e_12, _b;
_this.scene && gameObjectLoop(e, _this.gameObjects);
try {
for (var _c = __values(_this.systems), _d = _c.next(); !_d.done; _d = _c.next()) {
var system = _d.value;
try {
triggerStart(system);
system.update && system.update(e);
} catch (e) {
console.error(system.constructor.systemName + " update error", e);
}
}
} catch (e_11_1) {
e_11 = {
error: e_11_1
};
} finally {
try {
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
} finally {
if (e_11) throw e_11.error;
}
}
try {
for (var _e = __values(_this.systems), _f = _e.next(); !_f.done; _f = _e.next()) {
var system = _f.value;
try {
system.lateUpdate && system.lateUpdate(e);
} catch (e) {
console.error(system.constructor.systemName + " lateUpdate error", e);
}
}
} catch (e_12_1) {
e_12 = {
error: e_12_1
};
} finally {
try {
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
} finally {
if (e_12) throw e_12.error;
}
}
});
};
Game.prototype.triggerResume = function () {
var e_13, _a;
gameObjectResume(this.gameObjects);
try {
for (var _b = __values(this.systems), _c = _b.next(); !_c.done; _c = _b.next()) {
var system = _c.value;
try {
system.onResume && system.onResume();
} catch (e) {
console.error(system.constructor.systemName + ", onResume error", e);
}
}
} catch (e_13_1) {
e_13 = {
error: e_13_1
};
} finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} finally {
if (e_13) throw e_13.error;
}
}
};
Game.prototype.triggerPause = function () {
var e_14, _a;
gameObjectPause(this.gameObjects);
try {
for (var _b = __values(this.systems), _c = _b.next(); !_c.done; _c = _b.next()) {
var system = _c.value;
try {
system.onPause && system.onPause();
} catch (e) {
console.error(system.constructor.systemName + ", onPause error", e);
}
}
} catch (e_14_1) {
e_14 = {
error: e_14_1
};
} finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} finally {
if (e_14) throw e_14.error;
}
}
};
Game.prototype.destroySystems = function () {
var e_15, _a;
try {
for (var _b = __values(__spread(this.systems)), _c = _b.next(); !_c.done; _c = _b.next()) {
var system = _c.value;
this.removeSystem(system);
}
} catch (e_15_1) {
e_15 = {
error: e_15_1
};
} finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} finally {
if (e_15) throw e_15.error;
}
}
this.systems.length = 0;
};
Game.prototype.destroy = function () {
this.removeAllListeners();
this.pause();
this.scene.destroy();
this.destroySystems();
this.ticker = null;
this.scene = null;
this.canvas = null;
this.multiScenes = null;
};
Game.prototype.loadScene = function (_a) {
var scene = _a.scene,
_b = _a.mode,
mode = _b === void 0 ? LOAD_SCENE_MODE.SINGLE : _b,
_c = _a.params,
params = _c === void 0 ? {} : _c;
if (!scene) {
return;
}
switch (mode) {
case LOAD_SCENE_MODE.SINGLE:
this.scene = scene;
break;
case LOAD_SCENE_MODE.MULTI_CANVAS:
this.multiScenes.push(scene);
break;
}
this.emit('sceneChanged', {
scene: scene,
mode: mode,
params: params
});
};
return Game;
}(EventEmitter);
var Game$1 = Game;
function IDEProp(target, propertyKey) {
if (!target.constructor.IDEProps) {
target.constructor.IDEProps = [];
}
target.constructor.IDEProps.push(propertyKey);
}
function componentObserver(observerInfo) {
if (observerInfo === void 0) {
observerInfo = {};
}
return function (constructor) {
if (!constructor.observerInfo) {
for (var key in observerInfo) {
for (var index in observerInfo[key]) {
if (typeof observerInfo[key][index] === 'string') {
observerInfo[key][index] = [observerInfo[key][index]];
}
var observerProp = void 0;
if (Array.isArray(observerInfo[key][index])) {
observerProp = {
prop: observerInfo[key][index],
deep: false
};
observerInfo[key][index] = observerProp;
}
observerProp = observerInfo[key][index];
if (typeof observerProp.prop === 'string') {
observerProp.prop = [observerProp.prop];
}
}
}
constructor.observerInfo = observerInfo;
}
};
}
var SignalBindingImpl = function () {
function SignalBindingImpl(fn, once, thisArg) {
if (once === void 0) {
once = false;
}
this.next = null;
this.prev = null;
this.owner = null;
this.fn = fn;
this.once = once;
this.thisArg = thisArg;
}
SignalBindingImpl.prototype.detach = function () {
if (this.owner === null) return false;
this.owner.detach(this);
return true;
};
SignalBindingImpl.prototype.dispose = function () {
this.detach();
};
return SignalBindingImpl;
}();
var Signal = function () {
function Signal() {
this._head = null;
this._tail = null;
this._filter = null;
}
Signal.prototype.handlers = function () {
var node = this._head;
var handlers = [];
while (node) {
handlers.push(node);
node = node.next;
}
return handlers;
};
Signal.prototype.hasAny = function () {
return !!this._head;
};
Signal.prototype.has = function (node) {
return node.owner === this;
};
Signal.prototype.dispatch = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var node = this._head;
if (!node) return false;
if (this._filter && !this._filter.apply(this, args)) return false;
while (node) {
if (node.once) this.detach(node);
node.fn.apply(node.thisArg, args);
node = node.next;
}
return true;
};
Signal.prototype.add = function (fn, thisArg) {
if (thisArg === void 0) {
thisArg = null;
}
return this._addSignalBinding(new SignalBindingImpl(fn, false, thisArg));
};
Signal.prototype.once = function (fn, thisArg) {
if (thisArg === void 0) {
thisArg = null;
}
return this._addSignalBinding(new SignalBindingImpl(fn, true, thisArg));
};
Signal.prototype.detach = function (node_) {
var node = node_;
if (node.owner !== this) return this;
if (node.prev) node.prev.next = node.next;
if (node.next) node.next.prev = node.prev;
if (node === this._head) {
this._head = node.next;
if (node.next === null) {
this._tail = null;
}
} else if (node === this._tail) {
this._tail = node.prev;
if (this._tail) this._tail.next = null;
}
node.owner = null;
return this;
};
Signal.prototype.detachAll = function () {
var node = this._head;
if (!node) return this;
this._head = null;
this._tail = null;
while (node) {
node.owner = null;
node = node.next;
}
return this;
};
Signal.prototype.filter = function (filter) {
this._filter = filter;
};
Signal.prototype.proxy = function () {
var _this = this;
var signals = [];
for (var _i = 0; _i < arguments.length; _i++) {
signals[_i] = arguments[_i];
}
var fn = function fn() {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return _this.dispatch.apply(_this, args);
};
for (var i = 0; i < signals.length; ++i) {
signals[i].add(fn);
}
return this;
};
Signal.prototype._addSignalBinding = function (node_) {
var node = node_;
if (!this._head) {
this._head = node;
this._tail = node;
} else {
if (this._tail) this._tail.next = node;
node.prev = this._tail;
this._tail = node;
}
node.owner = this;
return node;
};
return Signal;
}();
var src = function src(str, opts) {
if (opts === void 0) {
opts = {};
}
if (!str) return undefined;
var o = {
key: ['source', 'protocol', 'authority', 'userInfo', 'user', 'password', 'host', 'port', 'relative', 'path', 'directory', 'file', 'query', 'anchor'],
q: {
name: 'queryKey',
parser: /(?:^|&)([^&=]*)=?([^&]*)/g
},
parser: {
strict: /^(?:([^:/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:/?#]*)(?::(\d*))?))?((((?:[^?#/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
loose: /^(?:(?![^:@]+:[^:@/]*@)([^:/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#/]*\.[^?#/.]+(?:[?#]|$)))*\/?)?([^?#/]*))(?:\?([^#]*))?(?:#(.*))?)/
}
};
var m = o.parser[opts.strictMode ? 'strict' : 'loose'].exec(str);
var uri = {};
var i = 14;
while (i--) {
uri[o.key[i]] = m[i] || '';
}
uri[o.q.name] = {};
uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
if ($1) uri[o.q.name][$1] = $2;
});
return uri;
};
var parseUri = src;
var AbstractLoadStrategy = function () {
function AbstractLoadStrategy(config) {
this.config = config;
this.onError = new Signal();
this.onComplete = new Signal();
this.onProgress = new Signal();
}
return AbstractLoadStrategy;
}();
var _extendStatics = function extendStatics(d, b) {
_extendStatics = Object.setPrototypeOf || {
__proto__: []
} instanceof Array && function (d, b) {
d.__proto__ = b;
} || function (d, b) {
for (var p in b) {
if (b.hasOwnProperty(p)) d[p] = b[p];
}
};
return _extendStatics(d, b);
};
function __extends(d, b) {
_extendStatics(d, b);
function __() {
this.constructor = d;
}
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
}
function __spreadArrays() {
for (var s = 0, i = 0, il = arguments.length; i < il; i++) {
s += arguments[i].length;
}
for (var r = Array(s), k = 0, i = 0; i < il; i++) {
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) {
r[k] = a[j];
}
}
return r;
}
function getExtension(url) {
var isDataUrl = url.indexOf('data:') === 0;
var ext = '';
if (isDataUrl) {
var slashIndex = url.indexOf('/');
ext = url.subst