UNPKG

@eva/eva.js

Version:

@eva/eva.js

1,344 lines (1,317 loc) 73.4 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var EventEmitter = require('eventemitter3'); var lodashEs = require('lodash-es'); var inspectorDecorator = require('@eva/inspector-decorator'); var resourceLoader$1 = require('resource-loader'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e["default"] : e; } var EventEmitter__default = /*#__PURE__*/_interopDefaultLegacy(EventEmitter); /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ /* global Reflect, Promise */ var extendStatics = function(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 __decorate(decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; } function __awaiter(thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function __generator(thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } } function __values(o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; } function __read(o, n) { var m = typeof Symbol === "function" && o[Symbol.iterator]; if (!m) return o; var i = m.call(o), r, ar = [], e; try { while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); } catch (error) { e = { error: error }; } finally { try { if (r && !r.done && (m = i["return"])) m.call(i); } finally { if (e) throw e.error; } } return ar; } function __spread() { for (var ar = [], i = 0; i < arguments.length; i++) ar = ar.concat(__read(arguments[i])); return ar; } function getComponentName(component) { if (component instanceof Component) { return component.name; } else if (component instanceof Function) { return component.componentName; } } var Component = (function (_super) { __extends(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__default)); var Component$1 = Component; exports.OBSERVER_TYPE = void 0; (function (ObserverType) { ObserverType["ADD"] = "ADD"; ObserverType["REMOVE"] = "REMOVE"; ObserverType["CHANGE"] = "CHANGE"; })(exports.OBSERVER_TYPE || (exports.OBSERVER_TYPE = {})); 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 lodashEs.isEqual(p, prop); }); if (index > -1) { addObserver({ systemName: systemName, componentName: componentName, component: component, prop: prop, type: exports.OBSERVER_TYPE.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 && lodashEs.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 (val) { if (obj["_" + key] === val) return; obj["_" + key] = val; pushToQueue({ prop: prop, component: component, componentName: componentName }); }, get: function () { 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 (prop) { var index = props.findIndex(function (p) { return lodashEs.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: exports.OBSERVER_TYPE.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: exports.OBSERVER_TYPE.REMOVE, componentName: componentName, }); } } removeObjectCache(component); } function setSystemObserver(system, S) { observerInfos[S.systemName] = S.observerInfo; systemInstance[S.systemName] = system; } var Transform = (function (_super) { __extends(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; Object.assign(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 () { return this._parent; }, set: function (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([ inspectorDecorator.type('vector2'), inspectorDecorator.step(1) ], Transform.prototype, "position", void 0); __decorate([ inspectorDecorator.type('size'), inspectorDecorator.step(1) ], Transform.prototype, "size", void 0); __decorate([ inspectorDecorator.type('vector2'), inspectorDecorator.step(0.1) ], Transform.prototype, "origin", void 0); __decorate([ inspectorDecorator.type('vector2'), inspectorDecorator.step(0.1) ], Transform.prototype, "anchor", void 0); __decorate([ inspectorDecorator.type('vector2'), inspectorDecorator.step(0.1) ], Transform.prototype, "scale", void 0); __decorate([ inspectorDecorator.type('vector2'), inspectorDecorator.step(0.1) ], Transform.prototype, "skew", void 0); __decorate([ inspectorDecorator.type('number'), inspectorDecorator.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 () { return this.getComponent(Transform$1); }, enumerable: false, configurable: true }); Object.defineProperty(GameObject.prototype, "parent", { get: function () { return this.transform && this.transform.parent && this.transform.parent.gameObject; }, enumerable: false, configurable: true }); Object.defineProperty(GameObject.prototype, "name", { get: function () { return this._name; }, enumerable: false, configurable: true }); Object.defineProperty(GameObject.prototype, "scene", { get: function () { return this._scene; }, set: function (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 === exports.OBSERVER_TYPE.REMOVE) { if (this.events.find(function (changed) { return changed.component === component && changed.type === exports.OBSERVER_TYPE.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 && lodashEs.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 () { 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 () { 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 = Object.assign({}, 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 () { return this.parent ? this.parent.currentTime : _nowtime() - this._createTime; }, enumerable: false, configurable: true }); Object.defineProperty(Timeline.prototype, "parent", { get: function () { return this._parent; }, enumerable: false, configurable: true }); Object.defineProperty(Timeline.prototype, "lastTimeMark", { get: function () { 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 () { var _a = this.lastTimeMark, localTime = _a.localTime, globalTime = _a.globalTime; return localTime + (this.globalTime - globalTime) * this.playbackRate; }, set: function (time) { this.markTime({ time: time }); }, enumerable: false, configurable: true }); Object.defineProperty(Timeline.prototype, "globalEntropy", { get: function () { return this._parent ? this._parent.entropy : this.globalTime; }, enumerable: false, configurable: true }); Object.defineProperty(Timeline.prototype, "entropy", { get: function () { var _a = this.lastTimeMark, entropy = _a.entropy, globalEntropy = _a.globalEntropy; return entropy + Math.abs((this.globalEntropy - globalEntropy) * this.playbackRate); }, set: function (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 () { return this._playbackRate; }, set: function (rate) { if (rate !== this.playbackRate) { this.markTime({ playbackRate: rate }); this._playbackRate = rate; } }, enumerable: false, configurable: true }); Object.defineProperty(Timeline.prototype, "paused", { get: function () { 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 = Object.assign({}, 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(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; exports.LOAD_SCENE_MODE = void 0; (function (LOAD_SCENE_MODE) { LOAD_SCENE_MODE["SINGLE"] = "SINGLE"; LOAD_SCENE_MODE["MULTI_CANVAS"] = "MULTI_CANVAS"; })(exports.LOAD_SCENE_MODE || (exports.LOAD_SCENE_MODE = {})); var triggerStart = function (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 (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 (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 (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 (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(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 (window.__EVA_INSPECTOR_ENV__) { window.__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 () { return this._scene; }, set: function (scene) { this._scene = scene; }, enumerable: false, configurable: true }); Object.defineProperty(Game.prototype, "gameObjects", { get: function () { 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()) {