优化component add/remove效率

This commit is contained in:
yhh
2021-04-20 15:46:18 +08:00
parent 17b36f01b0
commit b5d0fc6d0d
7 changed files with 209 additions and 220 deletions

View File

@@ -7,10 +7,7 @@ declare module es {
* 核心发射器。只发出核心级别的事件 * 核心发射器。只发出核心级别的事件
*/ */
static emitter: Emitter<CoreEvents>; static emitter: Emitter<CoreEvents>;
/** static paused: boolean;
* 启用/禁用焦点丢失时的暂停。如果为真,则不调用更新或渲染方法
*/
static pauseOnFocusLost: boolean;
/** /**
* 是否启用调试渲染 * 是否启用调试渲染
*/ */
@@ -153,10 +150,16 @@ declare module es {
* - onRemovedFromEntity * - onRemovedFromEntity
*/ */
abstract class Component { abstract class Component {
static _idGenerator: number;
/**
* 此组件的唯一标识
*/
readonly id: number;
/** /**
* 此组件附加的实体 * 此组件附加的实体
*/ */
entity: Entity; entity: Entity;
constructor();
/** /**
* 快速访问 this.entity.transform * 快速访问 this.entity.transform
*/ */
@@ -1717,11 +1720,15 @@ declare module es {
/** /**
* 添加到此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工 * 添加到此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工
*/ */
_componentsToAdd: Component[]; _componentsToAdd: {
[index: number]: Component;
};
/** /**
* 标记要删除此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工 * 标记要删除此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工
*/ */
_componentsToRemove: Component[]; _componentsToRemove: {
[index: number]: Component;
};
_tempBufferList: Component[]; _tempBufferList: Component[];
/** /**
* 用于确定是否需要对该框架中的组件进行排序的标志 * 用于确定是否需要对该框架中的组件进行排序的标志

View File

@@ -225,6 +225,9 @@ var es;
return __awaiter(this, void 0, void 0, function () { return __awaiter(this, void 0, void 0, function () {
var i; var i;
return __generator(this, function (_a) { return __generator(this, function (_a) {
if (Core.paused) {
return [2 /*return*/];
}
es.Time.update(currentTime); es.Time.update(currentTime);
if (this._scene != null) { if (this._scene != null) {
for (i = this._globalManagers.length - 1; i >= 0; i--) { for (i = this._globalManagers.length - 1; i >= 0; i--) {
@@ -245,10 +248,7 @@ var es;
}); });
}); });
}; };
/** Core.paused = false;
* 启用/禁用焦点丢失时的暂停。如果为真,则不调用更新或渲染方法
*/
Core.pauseOnFocusLost = true;
/** /**
* 是否启用调试渲染 * 是否启用调试渲染
*/ */
@@ -449,6 +449,7 @@ var es;
function Component() { function Component() {
this._enabled = true; this._enabled = true;
this._updateOrder = 0; this._updateOrder = 0;
this.id = Component._idGenerator++;
} }
Object.defineProperty(Component.prototype, "transform", { Object.defineProperty(Component.prototype, "transform", {
/** /**
@@ -538,6 +539,7 @@ var es;
} }
return this; return this;
}; };
Component._idGenerator = 0;
return Component; return Component;
}()); }());
es.Component = Component; es.Component = Component;
@@ -2357,10 +2359,10 @@ var es;
function TriggerListenerHelper() { function TriggerListenerHelper() {
} }
TriggerListenerHelper.getITriggerListener = function (entity, components) { TriggerListenerHelper.getITriggerListener = function (entity, components) {
var e_2, _a, e_3, _b; var e_2, _a;
try { try {
for (var _c = __values(entity.components._components), _d = _c.next(); !_d.done; _d = _c.next()) { for (var _b = __values(entity.components._components), _c = _b.next(); !_c.done; _c = _b.next()) {
var component = _d.value; var component = _c.value;
if (es.isITriggerListener(component)) { if (es.isITriggerListener(component)) {
components.push(component); components.push(component);
} }
@@ -2369,25 +2371,16 @@ var es;
catch (e_2_1) { e_2 = { error: e_2_1 }; } catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally { finally {
try { try {
if (_d && !_d.done && (_a = _c.return)) _a.call(_c); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_2) throw e_2.error; } finally { if (e_2) throw e_2.error; }
} }
try { for (var i in entity.components._componentsToAdd) {
for (var _e = __values(entity.components._componentsToAdd), _f = _e.next(); !_f.done; _f = _e.next()) { var component = entity.components._componentsToAdd[i];
var component = _f.value; if (es.isITriggerListener(component)) {
if (es.isITriggerListener(component)) { components.push(component);
components.push(component);
}
} }
} }
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; }
}
return components; return components;
}; };
return TriggerListenerHelper; return TriggerListenerHelper;
@@ -2502,7 +2495,7 @@ var es;
* @param motion * @param motion
*/ */
ProjectileMover.prototype.move = function (motion) { ProjectileMover.prototype.move = function (motion) {
var e_4, _a; var e_3, _a;
if (this._collider == null) if (this._collider == null)
return false; return false;
var didCollide = false; var didCollide = false;
@@ -2519,12 +2512,12 @@ var es;
} }
} }
} }
catch (e_4_1) { e_4 = { error: e_4_1 }; } catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally { finally {
try { try {
if (neighbors_2_1 && !neighbors_2_1.done && (_a = neighbors_2.return)) _a.call(neighbors_2); if (neighbors_2_1 && !neighbors_2_1.done && (_a = neighbors_2.return)) _a.call(neighbors_2);
} }
finally { if (e_4) throw e_4.error; } finally { if (e_3) throw e_3.error; }
} }
return didCollide; return didCollide;
}; };
@@ -2749,7 +2742,7 @@ var es;
* @param result * @param result
*/ */
Collider.prototype.collidesWithAny = function (motion, result) { Collider.prototype.collidesWithAny = function (motion, result) {
var e_5, _a; var e_4, _a;
// 在我们的新位置上获取我们可能会碰到的任何东西 // 在我们的新位置上获取我们可能会碰到的任何东西
var colliderBounds = this.bounds.clone(); var colliderBounds = this.bounds.clone();
colliderBounds.x += motion.x; colliderBounds.x += motion.x;
@@ -2771,12 +2764,12 @@ var es;
} }
} }
} }
catch (e_5_1) { e_5 = { error: e_5_1 }; } catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally { finally {
try { try {
if (neighbors_3_1 && !neighbors_3_1.done && (_a = neighbors_3.return)) _a.call(neighbors_3); if (neighbors_3_1 && !neighbors_3_1.done && (_a = neighbors_3.return)) _a.call(neighbors_3);
} }
finally { if (e_5) throw e_5.error; } finally { if (e_4) throw e_4.error; }
} }
// 将形状位置返回到检查之前的位置 // 将形状位置返回到检查之前的位置
this.shape.position = oldPosition; this.shape.position = oldPosition;
@@ -2788,7 +2781,7 @@ var es;
*/ */
Collider.prototype.collidesWithAnyNonMotion = function (result) { Collider.prototype.collidesWithAnyNonMotion = function (result) {
if (result === void 0) { result = new es.CollisionResult(); } if (result === void 0) { result = new es.CollisionResult(); }
var e_6, _a; var e_5, _a;
// 在我们的新位置上获取我们可能会碰到的任何东西 // 在我们的新位置上获取我们可能会碰到的任何东西
var neighbors = es.Physics.boxcastBroadphaseExcludingSelfNonRect(this, this.collidesWithLayers.value); var neighbors = es.Physics.boxcastBroadphaseExcludingSelfNonRect(this, this.collidesWithLayers.value);
try { try {
@@ -2800,12 +2793,12 @@ var es;
return true; return true;
} }
} }
catch (e_6_1) { e_6 = { error: e_6_1 }; } catch (e_5_1) { e_5 = { error: e_5_1 }; }
finally { finally {
try { try {
if (neighbors_4_1 && !neighbors_4_1.done && (_a = neighbors_4.return)) _a.call(neighbors_4); if (neighbors_4_1 && !neighbors_4_1.done && (_a = neighbors_4.return)) _a.call(neighbors_4);
} }
finally { if (e_6) throw e_6.error; } finally { if (e_5) throw e_5.error; }
} }
return false; return false;
}; };
@@ -4024,11 +4017,11 @@ var es;
/** /**
* 添加到此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工 * 添加到此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工
*/ */
this._componentsToAdd = []; this._componentsToAdd = {};
/** /**
* 标记要删除此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工 * 标记要删除此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工
*/ */
this._componentsToRemove = []; this._componentsToRemove = {};
this._tempBufferList = []; this._tempBufferList = [];
this._entity = entity; this._entity = entity;
} }
@@ -4050,18 +4043,17 @@ var es;
this._isComponentListUnsorted = true; this._isComponentListUnsorted = true;
}; };
ComponentList.prototype.add = function (component) { ComponentList.prototype.add = function (component) {
this._componentsToAdd.push(component); this._componentsToAdd[component.id] = component;
}; };
ComponentList.prototype.remove = function (component) { ComponentList.prototype.remove = function (component) {
var componentToRemove = new es.List(this._componentsToRemove); es.Debug.warnIf(!!this._componentsToRemove[component.id], "\u60A8\u6B63\u5728\u5C1D\u8BD5\u5220\u9664\u4E00\u4E2A\u60A8\u5DF2\u7ECF\u5220\u9664\u7684\u7EC4\u4EF6(" + component + ")");
var componentToAdd = new es.List(this._componentsToAdd); //
es.Debug.warnIf(componentToRemove.contains(component), "\u60A8\u6B63\u5728\u5C1D\u8BD5\u5220\u9664\u4E00\u4E2A\u60A8\u5DF2\u7ECF\u5220\u9664\u7684\u7EC4\u4EF6(" + component + ")");
// 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除 // 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除
if (componentToAdd.contains(component)) { if (this._componentsToAdd[component.id]) {
componentToAdd.remove(component); delete this._componentsToAdd[component.id];
return; return;
} }
componentToRemove.add(component); this._componentsToRemove[component.id] = component;
}; };
/** /**
* 立即从组件列表中删除所有组件 * 立即从组件列表中删除所有组件
@@ -4072,11 +4064,11 @@ var es;
} }
this._components.length = 0; this._components.length = 0;
this._updatableComponents.length = 0; this._updatableComponents.length = 0;
this._componentsToAdd.length = 0; this._componentsToAdd = {};
this._componentsToRemove.length = 0; this._componentsToRemove = {};
}; };
ComponentList.prototype.deregisterAllComponents = function () { ComponentList.prototype.deregisterAllComponents = function () {
var e_7, _a; var e_6, _a;
try { try {
for (var _b = __values(this._components), _c = _b.next(); !_c.done; _c = _b.next()) { for (var _b = __values(this._components), _c = _b.next(); !_c.done; _c = _b.next()) {
var component = _c.value; var component = _c.value;
@@ -4089,16 +4081,16 @@ var es;
this._entity.scene.entityProcessors.onComponentRemoved(this._entity); this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
} }
} }
catch (e_7_1) { e_7 = { error: e_7_1 }; } catch (e_6_1) { e_6 = { error: e_6_1 }; }
finally { finally {
try { try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_7) throw e_7.error; } finally { if (e_6) throw e_6.error; }
} }
}; };
ComponentList.prototype.registerAllComponents = function () { ComponentList.prototype.registerAllComponents = function () {
var e_8, _a; var e_7, _a;
try { try {
for (var _b = __values(this._components), _c = _b.next(); !_c.done; _c = _b.next()) { for (var _b = __values(this._components), _c = _b.next(); !_c.done; _c = _b.next()) {
var component = _c.value; var component = _c.value;
@@ -4108,53 +4100,51 @@ var es;
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
} }
} }
catch (e_8_1) { e_8 = { error: e_8_1 }; } catch (e_7_1) { e_7 = { error: e_7_1 }; }
finally { finally {
try { try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_8) throw e_8.error; } finally { if (e_7) throw e_7.error; }
} }
}; };
/** /**
* 处理任何需要删除或添加的组件 * 处理任何需要删除或添加的组件
*/ */
ComponentList.prototype.updateLists = function () { ComponentList.prototype.updateLists = function () {
if (this._componentsToRemove.length > 0) { for (var i in this._componentsToRemove) {
for (var i = 0; i < this._componentsToRemove.length; i++) { var component = this._componentsToRemove[i];
this.handleRemove(this._componentsToRemove[i]); this.handleRemove(component);
new es.List(this._components).remove(this._componentsToRemove[i]); for (var index = 0; index < this._components.length; index++) {
} if (this._components[index].id == component.id) {
this._componentsToRemove.length = 0; this._components.splice(index, 1);
} break;
if (this._componentsToAdd.length > 0) {
for (var i = 0, count = this._componentsToAdd.length; i < count; i++) {
var component = this._componentsToAdd[i];
if (es.isIUpdatable(component))
this._updatableComponents.push(component);
this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(es.TypeUtils.getType(component)));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.push(component);
this._tempBufferList.push(component);
}
// 在调用onAddedToEntity之前清除以防添加更多组件
this._componentsToAdd.length = 0;
this._isComponentListUnsorted = true;
// 现在所有的组件都添加到了场景中我们再次循环并调用onAddedToEntity/onEnabled
for (var i = 0; i < this._tempBufferList.length; i++) {
var component = this._tempBufferList[i];
component.onAddedToEntity();
// enabled检查实体和组件
if (component.enabled) {
component.onEnabled();
} }
} }
this._tempBufferList.length = 0;
} }
// if (this._isComponentListUnsorted) { this._componentsToRemove = {};
// this._updatableComponents.sort(ComponentList.compareUpdatableOrder.compare); for (var i in this._componentsToAdd) {
// this._isComponentListUnsorted = false; var component = this._componentsToAdd[i];
// } if (es.isIUpdatable(component))
this._updatableComponents.push(component);
this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(es.TypeUtils.getType(component)));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.push(component);
this._tempBufferList.push(component);
}
// 在调用onAddedToEntity之前清除以防添加更多组件
this._componentsToAdd = {};
this._isComponentListUnsorted = true;
// 现在所有的组件都添加到了场景中我们再次循环并调用onAddedToEntity/onEnabled
for (var i = 0; i < this._tempBufferList.length; i++) {
var component = this._tempBufferList[i];
component.onAddedToEntity();
// enabled检查实体和组件
if (component.enabled) {
component.onEnabled();
}
}
this._tempBufferList.length = 0;
}; };
ComponentList.prototype.handleRemove = function (component) { ComponentList.prototype.handleRemove = function (component) {
if (es.isIUpdatable(component)) if (es.isIUpdatable(component))
@@ -4172,36 +4162,27 @@ var es;
* @param onlyReturnInitializedComponents * @param onlyReturnInitializedComponents
*/ */
ComponentList.prototype.getComponent = function (type, onlyReturnInitializedComponents) { ComponentList.prototype.getComponent = function (type, onlyReturnInitializedComponents) {
var e_9, _a, e_10, _b; var e_8, _a;
try { try {
for (var _c = __values(this._components), _d = _c.next(); !_d.done; _d = _c.next()) { for (var _b = __values(this._components), _c = _b.next(); !_c.done; _c = _b.next()) {
var component = _d.value; var component = _c.value;
if (component instanceof type) if (component instanceof type)
return component; return component;
} }
} }
catch (e_9_1) { e_9 = { error: e_9_1 }; } catch (e_8_1) { e_8 = { error: e_8_1 }; }
finally { finally {
try { try {
if (_d && !_d.done && (_a = _c.return)) _a.call(_c); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_9) throw e_9.error; } finally { if (e_8) throw e_8.error; }
} }
// 我们可以选择检查挂起的组件以防addComponent和getComponent在同一个框架中被调用 // 我们可以选择检查挂起的组件以防addComponent和getComponent在同一个框架中被调用
if (!onlyReturnInitializedComponents) { if (!onlyReturnInitializedComponents) {
try { for (var i in this._componentsToAdd) {
for (var _e = __values(this._componentsToAdd), _f = _e.next(); !_f.done; _f = _e.next()) { var component = this._componentsToAdd[i];
var component = _f.value; if (component instanceof type)
if (component instanceof type) return component;
return component;
}
}
catch (e_10_1) { e_10 = { error: e_10_1 }; }
finally {
try {
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
}
finally { if (e_10) throw e_10.error; }
} }
} }
return null; return null;
@@ -4212,40 +4193,31 @@ var es;
* @param components * @param components
*/ */
ComponentList.prototype.getComponents = function (typeName, components) { ComponentList.prototype.getComponents = function (typeName, components) {
var e_11, _a, e_12, _b; var e_9, _a;
if (!components) if (!components)
components = []; components = [];
try { try {
for (var _c = __values(this._components), _d = _c.next(); !_d.done; _d = _c.next()) { for (var _b = __values(this._components), _c = _b.next(); !_c.done; _c = _b.next()) {
var component = _d.value; var component = _c.value;
if (component instanceof typeName) { if (component instanceof typeName) {
components.push(component); components.push(component);
} }
} }
} }
catch (e_11_1) { e_11 = { error: e_11_1 }; } catch (e_9_1) { e_9 = { error: e_9_1 }; }
finally { finally {
try { try {
if (_d && !_d.done && (_a = _c.return)) _a.call(_c); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_11) throw e_11.error; } finally { if (e_9) throw e_9.error; }
} }
try { // 我们还检查了待处理的组件以防在同一帧中调用addComponent和getComponent
// 我们还检查了待处理的组件以防在同一帧中调用addComponent和getComponent for (var i in this._componentsToAdd) {
for (var _e = __values(this._componentsToAdd), _f = _e.next(); !_f.done; _f = _e.next()) { var component = this._componentsToAdd[i];
var component = _f.value; if (component instanceof typeName) {
if (component instanceof typeName) { components.push(component);
components.push(component);
}
} }
} }
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; }
}
return components; return components;
}; };
ComponentList.prototype.update = function () { ComponentList.prototype.update = function () {
@@ -4260,9 +4232,10 @@ var es;
if (this._components[i].enabled) if (this._components[i].enabled)
this._components[i].onEntityTransformChanged(comp); this._components[i].onEntityTransformChanged(comp);
} }
for (var i = 0; i < this._componentsToAdd.length; i++) { for (var i in this._componentsToAdd) {
if (this._componentsToAdd[i].enabled) var component = this._componentsToAdd[i];
this._componentsToAdd[i].onEntityTransformChanged(comp); if (component.enabled)
component.onEntityTransformChanged(comp);
} }
}; };
ComponentList.prototype.onEntityEnabled = function () { ComponentList.prototype.onEntityEnabled = function () {
@@ -4413,7 +4386,7 @@ var es;
list.delete(entity); list.delete(entity);
}; };
EntityList.prototype.update = function () { EntityList.prototype.update = function () {
var e_13, _a; var e_10, _a;
try { try {
for (var _b = __values(this._entities), _c = _b.next(); !_c.done; _c = _b.next()) { for (var _b = __values(this._entities), _c = _b.next(); !_c.done; _c = _b.next()) {
var entity = _c.value; var entity = _c.value;
@@ -4421,12 +4394,12 @@ var es;
entity.update(); entity.update();
} }
} }
catch (e_13_1) { e_13 = { error: e_13_1 }; } catch (e_10_1) { e_10 = { error: e_10_1 }; }
finally { finally {
try { try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_13) throw e_13.error; } finally { if (e_10) throw e_10.error; }
} }
}; };
EntityList.prototype.updateLists = function () { EntityList.prototype.updateLists = function () {
@@ -4484,7 +4457,7 @@ var es;
* @param tag * @param tag
*/ */
EntityList.prototype.entitiesWithTag = function (tag) { EntityList.prototype.entitiesWithTag = function (tag) {
var e_14, _a; var e_11, _a;
var list = this.getTagList(tag); var list = this.getTagList(tag);
var returnList = es.ListPool.obtain(); var returnList = es.ListPool.obtain();
try { try {
@@ -4493,12 +4466,12 @@ var es;
returnList.push(entity); returnList.push(entity);
} }
} }
catch (e_14_1) { e_14 = { error: e_14_1 }; } catch (e_11_1) { e_11 = { error: e_11_1 }; }
finally { finally {
try { try {
if (list_1_1 && !list_1_1.done && (_a = list_1.return)) _a.call(list_1); if (list_1_1 && !list_1_1.done && (_a = list_1.return)) _a.call(list_1);
} }
finally { if (e_14) throw e_14.error; } finally { if (e_11) throw e_11.error; }
} }
return returnList; return returnList;
}; };
@@ -4508,7 +4481,7 @@ var es;
* @returns * @returns
*/ */
EntityList.prototype.entityWithTag = function (tag) { EntityList.prototype.entityWithTag = function (tag) {
var e_15, _a; var e_12, _a;
var list = this.getTagList(tag); var list = this.getTagList(tag);
try { try {
for (var list_2 = __values(list), list_2_1 = list_2.next(); !list_2_1.done; list_2_1 = list_2.next()) { for (var list_2 = __values(list), list_2_1 = list_2.next(); !list_2_1.done; list_2_1 = list_2.next()) {
@@ -4516,12 +4489,12 @@ var es;
return entity; return entity;
} }
} }
catch (e_15_1) { e_15 = { error: e_15_1 }; } catch (e_12_1) { e_12 = { error: e_12_1 }; }
finally { finally {
try { try {
if (list_2_1 && !list_2_1.done && (_a = list_2.return)) _a.call(list_2); if (list_2_1 && !list_2_1.done && (_a = list_2.return)) _a.call(list_2);
} }
finally { if (e_15) throw e_15.error; } finally { if (e_12) throw e_12.error; }
} }
return null; return null;
}; };
@@ -4588,7 +4561,7 @@ var es;
for (var _i = 0; _i < arguments.length; _i++) { for (var _i = 0; _i < arguments.length; _i++) {
types[_i] = arguments[_i]; types[_i] = arguments[_i];
} }
var e_16, _a, e_17, _b; var e_13, _a, e_14, _b;
var entities = []; var entities = [];
for (var i = 0; i < this._entities.length; i++) { for (var i = 0; i < this._entities.length; i++) {
if (this._entities[i].enabled) { if (this._entities[i].enabled) {
@@ -4603,12 +4576,12 @@ var es;
} }
} }
} }
catch (e_16_1) { e_16 = { error: e_16_1 }; } catch (e_13_1) { e_13 = { error: e_13_1 }; }
finally { finally {
try { try {
if (types_1_1 && !types_1_1.done && (_a = types_1.return)) _a.call(types_1); if (types_1_1 && !types_1_1.done && (_a = types_1.return)) _a.call(types_1);
} }
finally { if (e_16) throw e_16.error; } finally { if (e_13) throw e_13.error; }
} }
if (meet) { if (meet) {
entities.push(this._entities[i]); entities.push(this._entities[i]);
@@ -4645,12 +4618,12 @@ var es;
} }
} }
} }
catch (e_17_1) { e_17 = { error: e_17_1 }; } catch (e_14_1) { e_14 = { error: e_14_1 }; }
finally { finally {
try { try {
if (types_2_1 && !types_2_1.done && (_b = types_2.return)) _b.call(types_2); if (types_2_1 && !types_2_1.done && (_b = types_2.return)) _b.call(types_2);
} }
finally { if (e_17) throw e_17.error; } finally { if (e_14) throw e_14.error; }
} }
if (meet) { if (meet) {
entities.push(entity); entities.push(entity);
@@ -7568,7 +7541,7 @@ var es;
* @param layerMask * @param layerMask
*/ */
SpatialHash.prototype.overlapRectangle = function (rect, results, layerMask) { SpatialHash.prototype.overlapRectangle = function (rect, results, layerMask) {
var e_18, _a; var e_15, _a;
this._overlapTestBox.updateBox(rect.width, rect.height); this._overlapTestBox.updateBox(rect.width, rect.height);
this._overlapTestBox.position = rect.location; this._overlapTestBox.position = rect.location;
var resultCounter = 0; var resultCounter = 0;
@@ -7599,12 +7572,12 @@ var es;
return resultCounter; return resultCounter;
} }
} }
catch (e_18_1) { e_18 = { error: e_18_1 }; } catch (e_15_1) { e_15 = { error: e_15_1 }; }
finally { finally {
try { try {
if (potentials_1_1 && !potentials_1_1.done && (_a = potentials_1.return)) _a.call(potentials_1); if (potentials_1_1 && !potentials_1_1.done && (_a = potentials_1.return)) _a.call(potentials_1);
} }
finally { if (e_18) throw e_18.error; } finally { if (e_15) throw e_15.error; }
} }
return resultCounter; return resultCounter;
}; };
@@ -7616,7 +7589,7 @@ var es;
* @param layerMask * @param layerMask
*/ */
SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) { SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) {
var e_19, _a; var e_16, _a;
var bounds = new es.Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2); var bounds = new es.Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
this._overlapTestCircle.radius = radius; this._overlapTestCircle.radius = radius;
this._overlapTestCircle.position = circleCenter; this._overlapTestCircle.position = circleCenter;
@@ -7649,12 +7622,12 @@ var es;
return resultCounter; return resultCounter;
} }
} }
catch (e_19_1) { e_19 = { error: e_19_1 }; } catch (e_16_1) { e_16 = { error: e_16_1 }; }
finally { finally {
try { try {
if (potentials_2_1 && !potentials_2_1.done && (_a = potentials_2.return)) _a.call(potentials_2); if (potentials_2_1 && !potentials_2_1.done && (_a = potentials_2.return)) _a.call(potentials_2);
} }
finally { if (e_19) throw e_19.error; } finally { if (e_16) throw e_16.error; }
} }
return resultCounter; return resultCounter;
}; };
@@ -12235,7 +12208,7 @@ var es;
* 创建一个Set从一个Enumerable.List< T>。 * 创建一个Set从一个Enumerable.List< T>。
*/ */
List.prototype.toSet = function () { List.prototype.toSet = function () {
var e_20, _a; var e_17, _a;
var result = new Set(); var result = new Set();
try { try {
for (var _b = __values(this._elements), _c = _b.next(); !_c.done; _c = _b.next()) { for (var _b = __values(this._elements), _c = _b.next(); !_c.done; _c = _b.next()) {
@@ -12243,12 +12216,12 @@ var es;
result.add(x); result.add(x);
} }
} }
catch (e_20_1) { e_20 = { error: e_20_1 }; } catch (e_17_1) { e_17 = { error: e_17_1 }; }
finally { finally {
try { try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_20) throw e_20.error; } finally { if (e_17) throw e_17.error; }
} }
return result; return result;
}; };
@@ -12497,7 +12470,7 @@ var es;
* 计算可见性多边形并返回三角形扇形的顶点减去中心顶点。返回的数组来自ListPool * 计算可见性多边形并返回三角形扇形的顶点减去中心顶点。返回的数组来自ListPool
*/ */
VisibilityComputer.prototype.end = function () { VisibilityComputer.prototype.end = function () {
var e_21, _a; var e_18, _a;
var output = es.ListPool.obtain(); var output = es.ListPool.obtain();
this.updateSegments(); this.updateSegments();
this._endPoints.sort(this._radialComparer.compare); this._endPoints.sort(this._radialComparer.compare);
@@ -12536,12 +12509,12 @@ var es;
} }
} }
} }
catch (e_21_1) { e_21 = { error: e_21_1 }; } catch (e_18_1) { e_18 = { error: e_18_1 }; }
finally { finally {
try { try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_21) throw e_21.error; } finally { if (e_18) throw e_18.error; }
} }
} }
VisibilityComputer._openSegments.clear(); VisibilityComputer._openSegments.clear();
@@ -12657,7 +12630,7 @@ var es;
* 处理片段,以便我们稍后对它们进行分类 * 处理片段,以便我们稍后对它们进行分类
*/ */
VisibilityComputer.prototype.updateSegments = function () { VisibilityComputer.prototype.updateSegments = function () {
var e_22, _a; var e_19, _a;
try { try {
for (var _b = __values(this._segments), _c = _b.next(); !_c.done; _c = _b.next()) { for (var _b = __values(this._segments), _c = _b.next(); !_c.done; _c = _b.next()) {
var segment = _c.value; var segment = _c.value;
@@ -12675,12 +12648,12 @@ var es;
segment.p2.begin = !segment.p1.begin; segment.p2.begin = !segment.p1.begin;
} }
} }
catch (e_22_1) { e_22 = { error: e_22_1 }; } catch (e_19_1) { e_19 = { error: e_19_1 }; }
finally { finally {
try { try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b); if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
} }
finally { if (e_22) throw e_22.error; } finally { if (e_19) throw e_19.error; }
} }
// 如果我们有一个聚光灯,我们需要存储前两个段的角度。 // 如果我们有一个聚光灯,我们需要存储前两个段的角度。
// 这些是光斑的边界,我们将用它们来过滤它们之外的任何顶点。 // 这些是光斑的边界,我们将用它们来过滤它们之外的任何顶点。

File diff suppressed because one or more lines are too long

View File

@@ -7,10 +7,7 @@ module es {
* 核心发射器。只发出核心级别的事件 * 核心发射器。只发出核心级别的事件
*/ */
public static emitter: Emitter<CoreEvents>; public static emitter: Emitter<CoreEvents>;
/** public static paused = false;
* 启用/禁用焦点丢失时的暂停。如果为真,则不调用更新或渲染方法
*/
public static pauseOnFocusLost = true;
/** /**
* 是否启用调试渲染 * 是否启用调试渲染
*/ */
@@ -179,6 +176,10 @@ module es {
} }
protected async update(currentTime: number = -1) { protected async update(currentTime: number = -1) {
if (Core.paused) {
return;
}
Time.update(currentTime); Time.update(currentTime);
if (this._scene != null) { if (this._scene != null) {
for (let i = this._globalManagers.length - 1; i >= 0; i--) { for (let i = this._globalManagers.length - 1; i >= 0; i--) {

View File

@@ -8,11 +8,20 @@ module es {
* - onRemovedFromEntity * - onRemovedFromEntity
*/ */
export abstract class Component { export abstract class Component {
public static _idGenerator: number = 0;
/**
* 此组件的唯一标识
*/
public readonly id: number;
/** /**
* 此组件附加的实体 * 此组件附加的实体
*/ */
public entity: Entity; public entity: Entity;
constructor() {
this.id = Component._idGenerator++;
}
/** /**
* 快速访问 this.entity.transform * 快速访问 this.entity.transform
*/ */

View File

@@ -29,7 +29,8 @@ module es {
} }
} }
for (let component of entity.components._componentsToAdd) { for (let i in entity.components._componentsToAdd) {
let component = entity.components._componentsToAdd[i];
if (isITriggerListener(component)) { if (isITriggerListener(component)) {
components.push(component); components.push(component);
} }

View File

@@ -18,11 +18,11 @@ module es {
/** /**
* 添加到此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工 * 添加到此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工
*/ */
public _componentsToAdd: Component[] = []; public _componentsToAdd: {[index: number]: Component} = {};
/** /**
* 标记要删除此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工 * 标记要删除此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工
*/ */
public _componentsToRemove: Component[] = []; public _componentsToRemove: {[index: number]: Component} = {};
public _tempBufferList: Component[] = []; public _tempBufferList: Component[] = [];
/** /**
* 用于确定是否需要对该框架中的组件进行排序的标志 * 用于确定是否需要对该框架中的组件进行排序的标志
@@ -46,21 +46,19 @@ module es {
} }
public add(component: Component) { public add(component: Component) {
this._componentsToAdd.push(component); this._componentsToAdd[component.id] = component;
} }
public remove(component: Component) { public remove(component: Component) {
let componentToRemove = new es.List(this._componentsToRemove); Debug.warnIf(!!this._componentsToRemove[component.id], `您正在尝试删除一个您已经删除的组件(${component})`);
let componentToAdd = new es.List(this._componentsToAdd); //
Debug.warnIf(componentToRemove.contains(component), `您正在尝试删除一个您已经删除的组件(${component})`);
// 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除 // 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除
if (componentToAdd.contains(component)) { if (this._componentsToAdd[component.id]) {
componentToAdd.remove(component); delete this._componentsToAdd[component.id];
return; return;
} }
componentToRemove.add(component); this._componentsToRemove[component.id] = component;
} }
/** /**
@@ -73,8 +71,8 @@ module es {
this._components.length = 0; this._components.length = 0;
this._updatableComponents.length = 0; this._updatableComponents.length = 0;
this._componentsToAdd.length = 0; this._componentsToAdd = {};
this._componentsToRemove.length = 0; this._componentsToRemove = {};
} }
public deregisterAllComponents() { public deregisterAllComponents() {
@@ -104,51 +102,48 @@ module es {
* 处理任何需要删除或添加的组件 * 处理任何需要删除或添加的组件
*/ */
public updateLists() { public updateLists() {
if (this._componentsToRemove.length > 0) { for (let i in this._componentsToRemove) {
for (let i = 0; i < this._componentsToRemove.length; i++) { let component = this._componentsToRemove[i];
this.handleRemove(this._componentsToRemove[i]); this.handleRemove(component);
new es.List(this._components).remove(this._componentsToRemove[i]); for (let index = 0; index < this._components.length; index ++) {
} if (this._components[index].id == component.id) {
this._components.splice(index, 1);
this._componentsToRemove.length = 0; break;
}
if (this._componentsToAdd.length > 0) {
for (let i = 0, count = this._componentsToAdd.length; i < count; i++) {
let component = this._componentsToAdd[i];
if (isIUpdatable(component))
this._updatableComponents.push(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.push(component);
this._tempBufferList.push(component);
}
// 在调用onAddedToEntity之前清除以防添加更多组件
this._componentsToAdd.length = 0;
this._isComponentListUnsorted = true;
// 现在所有的组件都添加到了场景中我们再次循环并调用onAddedToEntity/onEnabled
for (let i = 0; i < this._tempBufferList.length; i++) {
let component = this._tempBufferList[i];
component.onAddedToEntity();
// enabled检查实体和组件
if (component.enabled) {
component.onEnabled();
} }
} }
this._tempBufferList.length = 0;
} }
// if (this._isComponentListUnsorted) { this._componentsToRemove = {};
// this._updatableComponents.sort(ComponentList.compareUpdatableOrder.compare);
// this._isComponentListUnsorted = false; for (let i in this._componentsToAdd) {
// } let component = this._componentsToAdd[i];
if (isIUpdatable(component))
this._updatableComponents.push(component);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.push(component);
this._tempBufferList.push(component);
}
// 在调用onAddedToEntity之前清除以防添加更多组件
this._componentsToAdd = {};
this._isComponentListUnsorted = true;
// 现在所有的组件都添加到了场景中我们再次循环并调用onAddedToEntity/onEnabled
for (let i = 0; i < this._tempBufferList.length; i++) {
let component = this._tempBufferList[i];
component.onAddedToEntity();
// enabled检查实体和组件
if (component.enabled) {
component.onEnabled();
}
}
this._tempBufferList.length = 0;
} }
public handleRemove(component: Component) { public handleRemove(component: Component) {
@@ -178,7 +173,8 @@ module es {
// 我们可以选择检查挂起的组件以防addComponent和getComponent在同一个框架中被调用 // 我们可以选择检查挂起的组件以防addComponent和getComponent在同一个框架中被调用
if (!onlyReturnInitializedComponents) { if (!onlyReturnInitializedComponents) {
for (let component of this._componentsToAdd) { for (let i in this._componentsToAdd) {
let component = this._componentsToAdd[i];
if (component instanceof type) if (component instanceof type)
return component as T; return component as T;
} }
@@ -203,7 +199,8 @@ module es {
} }
// 我们还检查了待处理的组件以防在同一帧中调用addComponent和getComponent // 我们还检查了待处理的组件以防在同一帧中调用addComponent和getComponent
for (let component of this._componentsToAdd) { for (let i in this._componentsToAdd) {
let component = this._componentsToAdd[i];
if (component instanceof typeName) { if (component instanceof typeName) {
components.push(component); components.push(component);
} }
@@ -226,9 +223,10 @@ module es {
this._components[i].onEntityTransformChanged(comp); this._components[i].onEntityTransformChanged(comp);
} }
for (let i = 0; i < this._componentsToAdd.length; i++) { for (let i in this._componentsToAdd) {
if (this._componentsToAdd[i].enabled) let component = this._componentsToAdd[i];
this._componentsToAdd[i].onEntityTransformChanged(comp); if (component.enabled)
component.onEntityTransformChanged(comp);
} }
} }