优化getComponent与getComponents性能

This commit is contained in:
yhh
2021-04-26 15:23:16 +08:00
parent d576a95548
commit bc6920f829
7 changed files with 215 additions and 127 deletions

View File

@@ -408,7 +408,7 @@ declare module es {
* @param typeName * @param typeName
* @param componentList * @param componentList
*/ */
getComponents(typeName: any, componentList?: any): any; getComponents(typeName: any, componentList?: any): any[];
/** /**
* 从组件列表中删除组件 * 从组件列表中删除组件
* @param component * @param component
@@ -647,6 +647,7 @@ declare module es {
* @param name * @param name
*/ */
findEntity(name: string): Entity; findEntity(name: string): Entity;
findEntityById(id: number): Entity;
/** /**
* 返回具有给定标记的所有实体 * 返回具有给定标记的所有实体
* @param tag * @param tag
@@ -1713,6 +1714,9 @@ declare module es {
* 添加到实体的组件列表 * 添加到实体的组件列表
*/ */
_components: Component[]; _components: Component[];
/** 记录component的快速读取列表 */
fastComponentsMap: Map<new (...args: any[]) => Component, Component[]>;
fastComponentsToAddMap: Map<new (...args: any[]) => Component, Component[]>;
/** /**
* 所有需要更新的组件列表 * 所有需要更新的组件列表
*/ */
@@ -1751,6 +1755,10 @@ declare module es {
*/ */
updateLists(): void; updateLists(): void;
handleRemove(component: Component): void; handleRemove(component: Component): void;
private removeFastComponent;
private addFastComponent;
private removeFastComponentToAdd;
private addFastComponentToAdd;
/** /**
* 获取类型T的第一个组件并返回它 * 获取类型T的第一个组件并返回它
* 可以选择跳过检查未初始化的组件(尚未调用onAddedToEntity方法的组件) * 可以选择跳过检查未初始化的组件(尚未调用onAddedToEntity方法的组件)
@@ -1764,7 +1772,7 @@ declare module es {
* @param typeName * @param typeName
* @param components * @param components
*/ */
getComponents(typeName: any, components?: any): any; getComponents(typeName: any, components?: any[]): any[];
update(): void; update(): void;
onEntityTransformChanged(comp: transform.Component): void; onEntityTransformChanged(comp: transform.Component): void;
onEntityEnabled(): void; onEntityEnabled(): void;
@@ -1850,6 +1858,12 @@ declare module es {
* @param name * @param name
*/ */
findEntity(name: string): Entity; findEntity(name: string): Entity;
/**
*
* @param id
* @returns
*/
findEntityById(id: number): Entity;
/** /**
* 返回带有标签的所有实体的列表。如果没有实体有标签,则返回一个空列表。 * 返回带有标签的所有实体的列表。如果没有实体有标签,则返回一个空列表。
* 返回的List可以通过ListPool.free放回池中 * 返回的List可以通过ListPool.free放回池中

View File

@@ -1470,6 +1470,9 @@ var es;
Scene.prototype.findEntity = function (name) { Scene.prototype.findEntity = function (name) {
return this.entities.findEntity(name); return this.entities.findEntity(name);
}; };
Scene.prototype.findEntityById = function (id) {
return this.entities.findEntityById(id);
};
/** /**
* 返回具有给定标记的所有实体 * 返回具有给定标记的所有实体
* @param tag * @param tag
@@ -3078,14 +3081,14 @@ var es;
} }
SystemIndexManager.getIndexFor = function (es) { SystemIndexManager.getIndexFor = function (es) {
var index = SystemIndexManager.indices.get(es); var index = SystemIndexManager.indices.get(es);
if (index === undefined) { if (!index) {
index = SystemIndexManager.INDEX++; index = SystemIndexManager.INDEX++;
SystemIndexManager.indices.put(es, index); SystemIndexManager.indices.set(es, index);
} }
return index; return index;
}; };
SystemIndexManager.INDEX = 0; SystemIndexManager.INDEX = 0;
SystemIndexManager.indices = new es_1.HashMap(); SystemIndexManager.indices = new Map();
return SystemIndexManager; return SystemIndexManager;
}()); }());
es_1.SystemIndexManager = SystemIndexManager; es_1.SystemIndexManager = SystemIndexManager;
@@ -3134,7 +3137,7 @@ var es;
EntitySystem.prototype.initialize = function () { EntitySystem.prototype.initialize = function () {
}; };
EntitySystem.prototype.onChanged = function (entity) { EntitySystem.prototype.onChanged = function (entity) {
var contains = new es.List(this._entities).contains(entity); var contains = entity.getSystemBits().get(this.systemIndex_);
var interest = this._matcher.isInterestedEntity(entity); var interest = this._matcher.isInterestedEntity(entity);
if (interest && !contains) if (interest && !contains)
this.add(entity); this.add(entity);
@@ -3143,11 +3146,13 @@ var es;
}; };
EntitySystem.prototype.add = function (entity) { EntitySystem.prototype.add = function (entity) {
this._entities.push(entity); this._entities.push(entity);
entity.getSystemBits().set(this.systemIndex_);
this.onAdded(entity); this.onAdded(entity);
}; };
EntitySystem.prototype.onAdded = function (entity) { }; EntitySystem.prototype.onAdded = function (entity) { };
EntitySystem.prototype.remove = function (entity) { EntitySystem.prototype.remove = function (entity) {
new es.List(this._entities).remove(entity); new es.List(this._entities).remove(entity);
entity.getSystemBits().clear(this.systemIndex_);
this.onRemoved(entity); this.onRemoved(entity);
}; };
EntitySystem.prototype.onRemoved = function (entity) { }; EntitySystem.prototype.onRemoved = function (entity) { };
@@ -4008,6 +4013,9 @@ var es;
* 添加到实体的组件列表 * 添加到实体的组件列表
*/ */
this._components = []; this._components = [];
/** 记录component的快速读取列表 */
this.fastComponentsMap = new Map();
this.fastComponentsToAddMap = new Map();
/** /**
* 所有需要更新的组件列表 * 所有需要更新的组件列表
*/ */
@@ -4042,6 +4050,7 @@ var es;
}; };
ComponentList.prototype.add = function (component) { ComponentList.prototype.add = function (component) {
this._componentsToAdd[component.id] = component; this._componentsToAdd[component.id] = component;
this.addFastComponentToAdd(component);
}; };
ComponentList.prototype.remove = function (component) { ComponentList.prototype.remove = function (component) {
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 + ")"); 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 + ")");
@@ -4049,6 +4058,7 @@ var es;
// 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除 // 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除
if (this._componentsToAdd[component.id]) { if (this._componentsToAdd[component.id]) {
delete this._componentsToAdd[component.id]; delete this._componentsToAdd[component.id];
this.removeFastComponentToAdd(component);
return; return;
} }
this._componentsToRemove[component.id] = component; this._componentsToRemove[component.id] = component;
@@ -4060,6 +4070,8 @@ var es;
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
this.handleRemove(this._components[i]); this.handleRemove(this._components[i]);
} }
this.fastComponentsMap.clear();
this.fastComponentsToAddMap.clear();
this._components.length = 0; this._components.length = 0;
this._updatableComponents.length = 0; this._updatableComponents.length = 0;
this._componentsToAdd = {}; this._componentsToAdd = {};
@@ -4114,11 +4126,13 @@ var es;
var component = this._componentsToRemove[i]; var component = this._componentsToRemove[i];
this.handleRemove(component); this.handleRemove(component);
for (var index = 0; index < this._components.length; index++) { for (var index = 0; index < this._components.length; index++) {
if (this._components[index].id == component.id) { var searchComponent = this._components[index];
if (searchComponent.id == component.id) {
this._components.splice(index, 1); this._components.splice(index, 1);
break; break;
} }
} }
this.removeFastComponent(component);
} }
this._componentsToRemove = {}; this._componentsToRemove = {};
for (var i in this._componentsToAdd) { for (var i in this._componentsToAdd) {
@@ -4127,11 +4141,13 @@ var es;
this._updatableComponents.push(component); this._updatableComponents.push(component);
this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(es.TypeUtils.getType(component))); this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(es.TypeUtils.getType(component)));
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this.addFastComponent(component);
this._components.push(component); this._components.push(component);
this._tempBufferList.push(component); this._tempBufferList.push(component);
} }
// 在调用onAddedToEntity之前清除以防添加更多组件 // 在调用onAddedToEntity之前清除以防添加更多组件
this._componentsToAdd = {}; this._componentsToAdd = {};
this.fastComponentsToAddMap.clear();
this._isComponentListUnsorted = true; this._isComponentListUnsorted = true;
// 现在所有的组件都添加到了场景中我们再次循环并调用onAddedToEntity/onEnabled // 现在所有的组件都添加到了场景中我们再次循环并调用onAddedToEntity/onEnabled
for (var i = 0; i < this._tempBufferList.length; i++) { for (var i = 0; i < this._tempBufferList.length; i++) {
@@ -4152,6 +4168,32 @@ var es;
component.onRemovedFromEntity(); component.onRemovedFromEntity();
component.entity = null; component.entity = null;
}; };
ComponentList.prototype.removeFastComponent = function (component) {
var fastList = this.fastComponentsMap.get(es.TypeUtils.getType(component));
var fastIndex = fastList.findIndex(function (c) { return c.id == component.id; });
if (fastIndex != -1)
fastList.splice(fastIndex, 1);
};
ComponentList.prototype.addFastComponent = function (component) {
var fastList = this.fastComponentsMap.get(es.TypeUtils.getType(component));
if (!fastList)
fastList = [];
fastList.push(component);
this.fastComponentsMap.set(es.TypeUtils.getType(component), fastList);
};
ComponentList.prototype.removeFastComponentToAdd = function (component) {
var fastList = this.fastComponentsToAddMap.get(es.TypeUtils.getType(component));
var fastIndex = fastList.findIndex(function (c) { return c.id == component.id; });
if (fastIndex != -1)
fastList.splice(fastIndex, 1);
};
ComponentList.prototype.addFastComponentToAdd = function (component) {
var fastList = this.fastComponentsToAddMap.get(es.TypeUtils.getType(component));
if (!fastList)
fastList = [];
fastList.push(component);
this.fastComponentsToAddMap.set(es.TypeUtils.getType(component), fastList);
};
/** /**
* 获取类型T的第一个组件并返回它 * 获取类型T的第一个组件并返回它
* 可以选择跳过检查未初始化的组件(尚未调用onAddedToEntity方法的组件) * 可以选择跳过检查未初始化的组件(尚未调用onAddedToEntity方法的组件)
@@ -4160,28 +4202,23 @@ var es;
* @param onlyReturnInitializedComponents * @param onlyReturnInitializedComponents
*/ */
ComponentList.prototype.getComponent = function (type, onlyReturnInitializedComponents) { ComponentList.prototype.getComponent = function (type, onlyReturnInitializedComponents) {
var e_8, _a; // for (let component of this._components) {
try { // if (component instanceof type)
for (var _b = __values(this._components), _c = _b.next(); !_c.done; _c = _b.next()) { // return component as T;
var component = _c.value; // }
if (component instanceof type) var fastList = this.fastComponentsMap.get(type);
return component; if (fastList && fastList.length > 0)
} return fastList[0];
}
catch (e_8_1) { e_8 = { error: e_8_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_8) throw e_8.error; }
}
// 我们可以选择检查挂起的组件以防addComponent和getComponent在同一个框架中被调用 // 我们可以选择检查挂起的组件以防addComponent和getComponent在同一个框架中被调用
if (!onlyReturnInitializedComponents) { if (!onlyReturnInitializedComponents) {
for (var i in this._componentsToAdd) { // for (let i in this._componentsToAdd) {
var component = this._componentsToAdd[i]; // let component = this._componentsToAdd[i];
if (component instanceof type) // if (component instanceof type)
return component; // return component as T;
} // }
var fastToAddList = this.fastComponentsToAddMap.get(type);
if (fastToAddList && fastToAddList.length > 0)
return fastToAddList[0];
} }
return null; return null;
}; };
@@ -4191,31 +4228,26 @@ var es;
* @param components * @param components
*/ */
ComponentList.prototype.getComponents = function (typeName, components) { ComponentList.prototype.getComponents = function (typeName, components) {
var e_9, _a;
if (!components) if (!components)
components = []; components = [];
try { // for (let component of this._components) {
for (var _b = __values(this._components), _c = _b.next(); !_c.done; _c = _b.next()) { // if (component instanceof typeName) {
var component = _c.value; // components.push(component);
if (component instanceof typeName) { // }
components.push(component); // }
} var fastList = this.fastComponentsMap.get(typeName);
} if (fastList)
} components.concat(fastList);
catch (e_9_1) { e_9 = { error: e_9_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_9) throw e_9.error; }
}
// 我们还检查了待处理的组件以防在同一帧中调用addComponent和getComponent // 我们还检查了待处理的组件以防在同一帧中调用addComponent和getComponent
for (var i in this._componentsToAdd) { // for (let i in this._componentsToAdd) {
var component = this._componentsToAdd[i]; // let component = this._componentsToAdd[i];
if (component instanceof typeName) { // if (component instanceof typeName) {
components.push(component); // components.push(component);
} // }
} // }
var fastToAddList = this.fastComponentsToAddMap.get(typeName);
if (fastToAddList)
components.concat(fastToAddList);
return components; return components;
}; };
ComponentList.prototype.update = function () { ComponentList.prototype.update = function () {
@@ -4412,7 +4444,7 @@ var es;
list.delete(entity); list.delete(entity);
}; };
EntityList.prototype.update = function () { EntityList.prototype.update = function () {
var e_10, _a; var e_8, _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;
@@ -4420,12 +4452,12 @@ var es;
entity.update(); entity.update();
} }
} }
catch (e_10_1) { e_10 = { error: e_10_1 }; } catch (e_8_1) { e_8 = { error: e_8_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_10) throw e_10.error; } finally { if (e_8) throw e_8.error; }
} }
}; };
EntityList.prototype.updateLists = function () { EntityList.prototype.updateLists = function () {
@@ -4450,11 +4482,6 @@ var es;
this._entitiesToAdded[i].onAddedToScene(); this._entitiesToAdded[i].onAddedToScene();
} }
this._entitiesToAdded = {}; this._entitiesToAdded = {};
// this._isEntityListUnsorted = true;
// if (this._isEntityListUnsorted) {
// this._entities.sort(Entity.entityComparer.compare);
// this._isEntityListUnsorted = false;
// }
}; };
/** /**
* 返回第一个找到的名字为name的实体。如果没有找到则返回null * 返回第一个找到的名字为name的实体。如果没有找到则返回null
@@ -4465,11 +4492,6 @@ var es;
if (this._entities[i].name == name) if (this._entities[i].name == name)
return this._entities[i]; return this._entities[i];
} }
// for (let i = 0; i < this._entitiesToAdded.size; i++) {
// let entity = this._entitiesToAdded.values;
// if (entity.name == name)
// return entity;
// }
for (var i in this._entitiesToAdded) { for (var i in this._entitiesToAdded) {
var entity = this._entitiesToAdded[i]; var entity = this._entitiesToAdded[i];
if (entity.name == name) if (entity.name == name)
@@ -4477,13 +4499,25 @@ var es;
} }
return null; return null;
}; };
/**
*
* @param id
* @returns
*/
EntityList.prototype.findEntityById = function (id) {
for (var i = 0; i < this._entities.length; i++) {
if (this._entities[i].id == id)
return this._entities[i];
}
return this._entitiesToAdded[id];
};
/** /**
* 返回带有标签的所有实体的列表。如果没有实体有标签,则返回一个空列表。 * 返回带有标签的所有实体的列表。如果没有实体有标签,则返回一个空列表。
* 返回的List可以通过ListPool.free放回池中 * 返回的List可以通过ListPool.free放回池中
* @param tag * @param tag
*/ */
EntityList.prototype.entitiesWithTag = function (tag) { EntityList.prototype.entitiesWithTag = function (tag) {
var e_11, _a; var e_9, _a;
var list = this.getTagList(tag); var list = this.getTagList(tag);
var returnList = es.ListPool.obtain(); var returnList = es.ListPool.obtain();
try { try {
@@ -4492,12 +4526,12 @@ var es;
returnList.push(entity); returnList.push(entity);
} }
} }
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 (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_11) throw e_11.error; } finally { if (e_9) throw e_9.error; }
} }
return returnList; return returnList;
}; };
@@ -4507,7 +4541,7 @@ var es;
* @returns * @returns
*/ */
EntityList.prototype.entityWithTag = function (tag) { EntityList.prototype.entityWithTag = function (tag) {
var e_12, _a; var e_10, _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()) {
@@ -4515,12 +4549,12 @@ var es;
return entity; return entity;
} }
} }
catch (e_12_1) { e_12 = { error: e_12_1 }; } catch (e_10_1) { e_10 = { error: e_10_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_12) throw e_12.error; } finally { if (e_10) throw e_10.error; }
} }
return null; return null;
}; };
@@ -4587,7 +4621,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_13, _a, e_14, _b; var e_11, _a, e_12, _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) {
@@ -4602,12 +4636,12 @@ var es;
} }
} }
} }
catch (e_13_1) { e_13 = { error: e_13_1 }; } catch (e_11_1) { e_11 = { error: e_11_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_13) throw e_13.error; } finally { if (e_11) throw e_11.error; }
} }
if (meet) { if (meet) {
entities.push(this._entities[i]); entities.push(this._entities[i]);
@@ -4644,12 +4678,12 @@ var es;
} }
} }
} }
catch (e_14_1) { e_14 = { error: e_14_1 }; } catch (e_12_1) { e_12 = { error: e_12_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_14) throw e_14.error; } finally { if (e_12) throw e_12.error; }
} }
if (meet) { if (meet) {
entities.push(entity); entities.push(entity);
@@ -7567,7 +7601,7 @@ var es;
* @param layerMask * @param layerMask
*/ */
SpatialHash.prototype.overlapRectangle = function (rect, results, layerMask) { SpatialHash.prototype.overlapRectangle = function (rect, results, layerMask) {
var e_15, _a; var e_13, _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;
@@ -7598,12 +7632,12 @@ var es;
return resultCounter; return resultCounter;
} }
} }
catch (e_15_1) { e_15 = { error: e_15_1 }; } catch (e_13_1) { e_13 = { error: e_13_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_15) throw e_15.error; } finally { if (e_13) throw e_13.error; }
} }
return resultCounter; return resultCounter;
}; };
@@ -7615,7 +7649,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_16, _a; var e_14, _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;
@@ -7648,12 +7682,12 @@ var es;
return resultCounter; return resultCounter;
} }
} }
catch (e_16_1) { e_16 = { error: e_16_1 }; } catch (e_14_1) { e_14 = { error: e_14_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_16) throw e_16.error; } finally { if (e_14) throw e_14.error; }
} }
return resultCounter; return resultCounter;
}; };
@@ -12234,7 +12268,7 @@ var es;
* 创建一个Set从一个Enumerable.List< T>。 * 创建一个Set从一个Enumerable.List< T>。
*/ */
List.prototype.toSet = function () { List.prototype.toSet = function () {
var e_17, _a; var e_15, _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()) {
@@ -12242,12 +12276,12 @@ var es;
result.add(x); result.add(x);
} }
} }
catch (e_17_1) { e_17 = { error: e_17_1 }; } catch (e_15_1) { e_15 = { error: e_15_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_17) throw e_17.error; } finally { if (e_15) throw e_15.error; }
} }
return result; return result;
}; };
@@ -12496,7 +12530,7 @@ var es;
* 计算可见性多边形并返回三角形扇形的顶点减去中心顶点。返回的数组来自ListPool * 计算可见性多边形并返回三角形扇形的顶点减去中心顶点。返回的数组来自ListPool
*/ */
VisibilityComputer.prototype.end = function () { VisibilityComputer.prototype.end = function () {
var e_18, _a; var e_16, _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);
@@ -12535,12 +12569,12 @@ var es;
} }
} }
} }
catch (e_18_1) { e_18 = { error: e_18_1 }; } catch (e_16_1) { e_16 = { error: e_16_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_18) throw e_18.error; } finally { if (e_16) throw e_16.error; }
} }
} }
VisibilityComputer._openSegments.clear(); VisibilityComputer._openSegments.clear();
@@ -12656,7 +12690,7 @@ var es;
* 处理片段,以便我们稍后对它们进行分类 * 处理片段,以便我们稍后对它们进行分类
*/ */
VisibilityComputer.prototype.updateSegments = function () { VisibilityComputer.prototype.updateSegments = function () {
var e_19, _a; var e_17, _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;
@@ -12674,12 +12708,12 @@ var es;
segment.p2.begin = !segment.p1.begin; segment.p2.begin = !segment.p1.begin;
} }
} }
catch (e_19_1) { e_19 = { error: e_19_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_19) throw e_19.error; } finally { if (e_17) throw e_17.error; }
} }
// 如果我们有一个聚光灯,我们需要存储前两个段的角度。 // 如果我们有一个聚光灯,我们需要存储前两个段的角度。
// 这些是光斑的边界,我们将用它们来过滤它们之外的任何顶点。 // 这些是光斑的边界,我们将用它们来过滤它们之外的任何顶点。

File diff suppressed because one or more lines are too long

View File

@@ -181,6 +181,10 @@ module es {
return this.entities.findEntity(name); return this.entities.findEntity(name);
} }
public findEntityById(id: number): Entity {
return this.entities.findEntityById(id);
}
/** /**
* 返回具有给定标记的所有实体 * 返回具有给定标记的所有实体
* @param tag * @param tag

View File

@@ -2,13 +2,13 @@
module es { module es {
export class SystemIndexManager { export class SystemIndexManager {
public static INDEX = 0; public static INDEX = 0;
private static indices: HashMap<Function, number> = new HashMap<Function, number>(); private static indices: Map<Function, number> = new Map<Function, number>();
public static getIndexFor(es: Class): number { public static getIndexFor(es: Class): number {
let index: number = SystemIndexManager.indices.get(es); let index: number = SystemIndexManager.indices.get(es);
if (index === undefined) { if (!index) {
index = SystemIndexManager.INDEX++; index = SystemIndexManager.INDEX++;
SystemIndexManager.indices.put(es, index); SystemIndexManager.indices.set(es, index);
} }
return index; return index;
} }
@@ -60,7 +60,7 @@ module es {
} }
public onChanged(entity: Entity) { public onChanged(entity: Entity) {
let contains = new es.List(this._entities).contains(entity); let contains = entity.getSystemBits().get(this.systemIndex_);
let interest = this._matcher.isInterestedEntity(entity); let interest = this._matcher.isInterestedEntity(entity);
if (interest && !contains) if (interest && !contains)
@@ -71,6 +71,7 @@ module es {
public add(entity: Entity) { public add(entity: Entity) {
this._entities.push(entity); this._entities.push(entity);
entity.getSystemBits().set(this.systemIndex_);
this.onAdded(entity); this.onAdded(entity);
} }
@@ -78,6 +79,7 @@ module es {
public remove(entity: Entity) { public remove(entity: Entity) {
new es.List(this._entities).remove(entity); new es.List(this._entities).remove(entity);
entity.getSystemBits().clear(this.systemIndex_);
this.onRemoved(entity); this.onRemoved(entity);
} }

View File

@@ -11,6 +11,9 @@ module es {
* 添加到实体的组件列表 * 添加到实体的组件列表
*/ */
public _components: Component[] = []; public _components: Component[] = [];
/** 记录component的快速读取列表 */
public fastComponentsMap = new Map<new (...args: any[]) => Component, es.Component[]>();
public fastComponentsToAddMap = new Map<new (...args: any[]) => Component, es.Component[]>();
/** /**
* 所有需要更新的组件列表 * 所有需要更新的组件列表
*/ */
@@ -47,6 +50,7 @@ module es {
public add(component: Component) { public add(component: Component) {
this._componentsToAdd[component.id] = component; this._componentsToAdd[component.id] = component;
this.addFastComponentToAdd(component);
} }
public remove(component: Component) { public remove(component: Component) {
@@ -55,6 +59,7 @@ module es {
// 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除 // 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除
if (this._componentsToAdd[component.id]) { if (this._componentsToAdd[component.id]) {
delete this._componentsToAdd[component.id]; delete this._componentsToAdd[component.id];
this.removeFastComponentToAdd(component);
return; return;
} }
@@ -69,6 +74,8 @@ module es {
this.handleRemove(this._components[i]); this.handleRemove(this._components[i]);
} }
this.fastComponentsMap.clear();
this.fastComponentsToAddMap.clear();
this._components.length = 0; this._components.length = 0;
this._updatableComponents.length = 0; this._updatableComponents.length = 0;
this._componentsToAdd = {}; this._componentsToAdd = {};
@@ -106,11 +113,13 @@ module es {
let component = this._componentsToRemove[i]; let component = this._componentsToRemove[i];
this.handleRemove(component); this.handleRemove(component);
for (let index = 0; index < this._components.length; index ++) { for (let index = 0; index < this._components.length; index ++) {
if (this._components[index].id == component.id) { let searchComponent = this._components[index];
if (searchComponent.id == component.id) {
this._components.splice(index, 1); this._components.splice(index, 1);
break; break;
} }
} }
this.removeFastComponent(component);
} }
this._componentsToRemove = {}; this._componentsToRemove = {};
@@ -123,6 +132,7 @@ module es {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component))); this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this.addFastComponent(component);
this._components.push(component); this._components.push(component);
this._tempBufferList.push(component); this._tempBufferList.push(component);
@@ -130,6 +140,7 @@ module es {
// 在调用onAddedToEntity之前清除以防添加更多组件 // 在调用onAddedToEntity之前清除以防添加更多组件
this._componentsToAdd = {}; this._componentsToAdd = {};
this.fastComponentsToAddMap.clear();
this._isComponentListUnsorted = true; this._isComponentListUnsorted = true;
// 现在所有的组件都添加到了场景中我们再次循环并调用onAddedToEntity/onEnabled // 现在所有的组件都添加到了场景中我们再次循环并调用onAddedToEntity/onEnabled
@@ -157,6 +168,35 @@ module es {
component.entity = null; component.entity = null;
} }
private removeFastComponent(component: Component) {
let fastList = this.fastComponentsMap.get(TypeUtils.getType(component));
let fastIndex = fastList.findIndex(c => c.id == component.id);
if (fastIndex != -1)
fastList.splice(fastIndex, 1);
}
private addFastComponent(component: Component) {
let fastList = this.fastComponentsMap.get(TypeUtils.getType(component));
if (!fastList)
fastList = [];
fastList.push(component);
this.fastComponentsMap.set(TypeUtils.getType(component), fastList);
}
private removeFastComponentToAdd(component: Component) {
let fastList = this.fastComponentsToAddMap.get(TypeUtils.getType(component));
let fastIndex = fastList.findIndex(c => c.id == component.id);
if (fastIndex != -1)
fastList.splice(fastIndex, 1);
}
private addFastComponentToAdd(component: Component) {
let fastList = this.fastComponentsToAddMap.get(TypeUtils.getType(component));
if (!fastList)
fastList = [];
fastList.push(component);
this.fastComponentsToAddMap.set(TypeUtils.getType(component), fastList);
}
/** /**
* 获取类型T的第一个组件并返回它 * 获取类型T的第一个组件并返回它
@@ -166,18 +206,15 @@ module es {
* @param onlyReturnInitializedComponents * @param onlyReturnInitializedComponents
*/ */
public getComponent<T extends Component>(type, onlyReturnInitializedComponents: boolean): T { public getComponent<T extends Component>(type, onlyReturnInitializedComponents: boolean): T {
for (let component of this._components) { let fastList = this.fastComponentsMap.get(type);
if (component instanceof type) if (fastList && fastList.length > 0)
return component as T; return fastList[0] as T;
}
// 我们可以选择检查挂起的组件以防addComponent和getComponent在同一个框架中被调用 // 我们可以选择检查挂起的组件以防addComponent和getComponent在同一个框架中被调用
if (!onlyReturnInitializedComponents) { if (!onlyReturnInitializedComponents) {
for (let i in this._componentsToAdd) { let fastToAddList = this.fastComponentsToAddMap.get(type);
let component = this._componentsToAdd[i]; if (fastToAddList && fastToAddList.length > 0)
if (component instanceof type) return fastToAddList[0] as T;
return component as T;
}
} }
return null; return null;
@@ -188,23 +225,17 @@ module es {
* @param typeName * @param typeName
* @param components * @param components
*/ */
public getComponents(typeName: any, components?) { public getComponents(typeName: any, components?: any[]) {
if (!components) if (!components)
components = []; components = [];
for (let component of this._components) { let fastList = this.fastComponentsMap.get(typeName);
if (component instanceof typeName) { if (fastList)
components.push(component); components.concat(fastList);
}
}
// 我们还检查了待处理的组件以防在同一帧中调用addComponent和getComponent let fastToAddList = this.fastComponentsToAddMap.get(typeName);
for (let i in this._componentsToAdd) { if (fastToAddList)
let component = this._componentsToAdd[i]; components.concat(fastToAddList);
if (component instanceof typeName) {
components.push(component);
}
}
return components; return components;
} }

View File

@@ -154,12 +154,6 @@ module es {
} }
this._entitiesToAdded = {}; this._entitiesToAdded = {};
// this._isEntityListUnsorted = true;
// if (this._isEntityListUnsorted) {
// this._entities.sort(Entity.entityComparer.compare);
// this._isEntityListUnsorted = false;
// }
} }
/** /**
@@ -172,11 +166,6 @@ module es {
return this._entities[i]; return this._entities[i];
} }
// for (let i = 0; i < this._entitiesToAdded.size; i++) {
// let entity = this._entitiesToAdded.values;
// if (entity.name == name)
// return entity;
// }
for (let i in this._entitiesToAdded) { for (let i in this._entitiesToAdded) {
let entity = this._entitiesToAdded[i]; let entity = this._entitiesToAdded[i];
if (entity.name == name) if (entity.name == name)
@@ -186,6 +175,20 @@ module es {
return null; return null;
} }
/**
*
* @param id
* @returns
*/
public findEntityById(id: number) {
for (let i = 0; i < this._entities.length; i ++) {
if (this._entities[i].id == id)
return this._entities[i];
}
return this._entitiesToAdded[id];
}
/** /**
* 返回带有标签的所有实体的列表。如果没有实体有标签,则返回一个空列表。 * 返回带有标签的所有实体的列表。如果没有实体有标签,则返回一个空列表。
* 返回的List可以通过ListPool.free放回池中 * 返回的List可以通过ListPool.free放回池中