2020-07-22 20:07:14 +08:00
|
|
|
|
module es {
|
2020-07-28 16:25:20 +08:00
|
|
|
|
export class EntityList {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
public scene: Scene;
|
2020-07-24 15:29:07 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 添加到场景中的实体列表
|
|
|
|
|
|
*/
|
|
|
|
|
|
public _entities: Entity[] = [];
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 添加到此框架的实体列表。用于对实体进行分组,以便我们可以同时处理它们
|
|
|
|
|
|
*/
|
|
|
|
|
|
public _entitiesToAdded: Entity[] = [];
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 标记要删除此框架的实体列表。用于对实体进行分组,以便我们可以同时处理它们
|
|
|
|
|
|
*/
|
|
|
|
|
|
public _entitiesToRemove: Entity[] = [];
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 用于确定是否需要在此框架中对实体进行排序的标志
|
|
|
|
|
|
*/
|
|
|
|
|
|
public _isEntityListUnsorted: boolean;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 通过标签跟踪实体,便于检索
|
|
|
|
|
|
*/
|
|
|
|
|
|
public _entityDict: Map<number, Entity[]> = new Map<number, Entity[]>();
|
|
|
|
|
|
public _unsortedTags: number[] = [];
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 在updateLists中用于双缓冲区,以便可以在其他地方修改原始列表
|
|
|
|
|
|
*/
|
|
|
|
|
|
public _tempEntityList: Entity[] = [];
|
2020-07-22 20:07:14 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
constructor(scene: Scene) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this.scene = scene;
|
|
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public get count() {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return this._entities.length;
|
|
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public get buffer() {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return this._entities;
|
|
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public markEntityListUnsorted() {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this._isEntityListUnsorted = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public markTagUnsorted(tag: number) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this._unsortedTags.push(tag);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 将实体添加到列表中。所有生命周期方法将在下一帧中被调用。
|
|
|
|
|
|
* @param entity
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public add(entity: Entity) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
if (this._entitiesToAdded.indexOf(entity) == -1)
|
|
|
|
|
|
this._entitiesToAdded.push(entity);
|
2020-06-08 20:11:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-24 15:29:07 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 从列表中删除一个实体。所有生命周期方法将在下一帧中被调用。
|
|
|
|
|
|
* @param entity
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public remove(entity: Entity) {
|
|
|
|
|
|
if (!this._entitiesToRemove.contains(entity)) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
console.warn(`You are trying to remove an entity (${entity.name}) that you already removed`);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 防止在同一帧中添加或删除实体
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (this._entitiesToAdded.contains(entity)) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._entitiesToAdded.remove(entity);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
if (!this._entitiesToRemove.contains(entity))
|
|
|
|
|
|
this._entitiesToRemove.push(entity);
|
2020-06-08 20:11:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
/**
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* 从实体列表中删除所有实体
|
2020-07-22 20:07:14 +08:00
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public removeAllEntities() {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this._unsortedTags.length = 0;
|
|
|
|
|
|
this._entitiesToAdded.length = 0;
|
|
|
|
|
|
this._isEntityListUnsorted = false;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
|
2020-07-24 15:29:07 +08:00
|
|
|
|
// 为什么我们要在这里更新列表?主要用于处理场景切换前分离的实体。
|
|
|
|
|
|
// 它们仍然在_entitiesToRemove列表中,该列表将由更新列表处理。
|
|
|
|
|
|
this.updateLists();
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
for (let i = 0; i < this._entities.length; i++) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this._entities[i]._isDestroyed = true;
|
|
|
|
|
|
this._entities[i].onRemovedFromScene();
|
|
|
|
|
|
this._entities[i].scene = null;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this._entities.length = 0;
|
|
|
|
|
|
this._entityDict.clear();
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
/**
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* 检查该实体当前是否由此EntityList管理
|
|
|
|
|
|
* @param entity
|
2020-07-22 20:07:14 +08:00
|
|
|
|
*/
|
2020-07-24 15:29:07 +08:00
|
|
|
|
public contains(entity: Entity): boolean {
|
|
|
|
|
|
return this._entities.contains(entity) || this._entitiesToAdded.contains(entity);
|
2020-06-08 20:11:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public getTagList(tag: number) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
let list = this._entityDict.get(tag);
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (!list) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
list = [];
|
|
|
|
|
|
this._entityDict.set(tag, list);
|
|
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return this._entityDict.get(tag);
|
|
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public addToTagList(entity: Entity) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
let list = this.getTagList(entity.tag);
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (!list.contains(entity)) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
list.push(entity);
|
|
|
|
|
|
this._unsortedTags.push(entity.tag);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public removeFromTagList(entity: Entity) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
let list = this._entityDict.get(entity.tag);
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (list) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
list.remove(entity);
|
|
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public update() {
|
|
|
|
|
|
for (let i = 0; i < this._entities.length; i++) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
let entity = this._entities[i];
|
2020-07-24 15:29:07 +08:00
|
|
|
|
if (entity.enabled && (entity.updateInterval == 1 || Time.frameCount % entity.updateInterval == 0))
|
2020-07-22 20:07:14 +08:00
|
|
|
|
entity.update();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public updateLists() {
|
|
|
|
|
|
if (this._entitiesToRemove.length > 0) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
let temp = this._entitiesToRemove;
|
|
|
|
|
|
this._entitiesToRemove = this._tempEntityList;
|
|
|
|
|
|
this._tempEntityList = temp;
|
|
|
|
|
|
this._tempEntityList.forEach(entity => {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this.removeFromTagList(entity);
|
|
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._entities.remove(entity);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
entity.onRemovedFromScene();
|
2020-07-22 20:07:14 +08:00
|
|
|
|
entity.scene = null;
|
|
|
|
|
|
|
|
|
|
|
|
this.scene.entityProcessors.onEntityRemoved(entity);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this._tempEntityList.length = 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (this._entitiesToAdded.length > 0) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
let temp = this._entitiesToAdded;
|
|
|
|
|
|
this._entitiesToAdded = this._tempEntityList;
|
|
|
|
|
|
this._tempEntityList = temp;
|
|
|
|
|
|
this._tempEntityList.forEach(entity => {
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (!this._entities.contains(entity)) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._entities.push(entity);
|
|
|
|
|
|
entity.scene = this.scene;
|
|
|
|
|
|
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this.addToTagList(entity);
|
|
|
|
|
|
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this.scene.entityProcessors.onEntityAdded(entity)
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2020-07-24 15:29:07 +08:00
|
|
|
|
// 现在所有实体都被添加到场景中,我们再次循环并调用onAddedToScene
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._tempEntityList.forEach(entity => entity.onAddedToScene());
|
|
|
|
|
|
this._tempEntityList.length = 0;
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this._isEntityListUnsorted = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (this._isEntityListUnsorted) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this._entities.sort();
|
|
|
|
|
|
this._isEntityListUnsorted = false;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (this._unsortedTags.length > 0) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
this._unsortedTags.forEach(tag => {
|
|
|
|
|
|
this._entityDict.get(tag).sort();
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
this._unsortedTags.length = 0;
|
|
|
|
|
|
}
|
2020-06-09 22:32:18 +08:00
|
|
|
|
}
|
2020-07-24 15:29:07 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 返回找到的第一个实体的名称。如果没有找到,则返回null。
|
|
|
|
|
|
* @param name
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public findEntity(name: string) {
|
|
|
|
|
|
for (let i = 0; i < this._entities.length; i++) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
if (this._entities[i].name == name)
|
|
|
|
|
|
return this._entities[i];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return this._entitiesToAdded.firstOrDefault(entity => entity.name == name);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 返回带有标记的所有实体的列表。如果没有实体具有标记,则返回一个空列表。可以通过ListPool.free将返回的列表放回池中。
|
|
|
|
|
|
* @param tag
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public entitiesWithTag(tag: number) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
let list = this.getTagList(tag);
|
|
|
|
|
|
|
|
|
|
|
|
let returnList = ListPool.obtain<Entity>();
|
2020-07-28 16:25:20 +08:00
|
|
|
|
for (let i = 0; i < list.length; i++)
|
2020-07-24 15:29:07 +08:00
|
|
|
|
returnList.push(list[i]);
|
|
|
|
|
|
|
|
|
|
|
|
return returnList;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 返回t类型的所有实体的列表。返回的列表可以通过ListPool.free放回池中。
|
|
|
|
|
|
* @param type
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public entitiesOfType<T extends Entity>(type): T[] {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
let list = ListPool.obtain<T>();
|
2020-07-28 16:25:20 +08:00
|
|
|
|
for (let i = 0; i < this._entities.length; i++) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
if (this._entities[i] instanceof type)
|
|
|
|
|
|
list.push(this._entities[i] as T);
|
|
|
|
|
|
}
|
|
|
|
|
|
this._entitiesToAdded.forEach(entity => {
|
|
|
|
|
|
if (entity instanceof type)
|
|
|
|
|
|
list.push(entity as T);
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 返回在类型为T的场景中找到的第一个组件
|
|
|
|
|
|
* @param type
|
|
|
|
|
|
*/
|
|
|
|
|
|
public findComponentOfType<T extends Component>(type): T {
|
2020-07-28 16:25:20 +08:00
|
|
|
|
for (let i = 0; i < this._entities.length; i++) {
|
|
|
|
|
|
if (this._entities[i].enabled) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
let comp = this._entities[i].getComponent<T>(type);
|
|
|
|
|
|
if (comp)
|
|
|
|
|
|
return comp;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
for (let i = 0; i < this._entitiesToAdded.length; i++) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
let entity = this._entitiesToAdded[i];
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (entity.enabled) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
let comp = entity.getComponent<T>(type);
|
|
|
|
|
|
if (comp)
|
|
|
|
|
|
return comp;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 返回在类型t的场景中找到的所有组件。返回的列表可以通过ListPool.free放回池中。
|
|
|
|
|
|
* @param type
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public findComponentsOfType<T extends Component>(type): T[] {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
let comps = ListPool.obtain<T>();
|
2020-07-28 16:25:20 +08:00
|
|
|
|
for (let i = 0; i < this._entities.length; i++) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
if (this._entities[i].enabled)
|
|
|
|
|
|
this._entities[i].getComponents(type, comps);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
for (let i = 0; i < this._entitiesToAdded.length; i++) {
|
2020-07-24 15:29:07 +08:00
|
|
|
|
let entity = this._entitiesToAdded[i];
|
|
|
|
|
|
if (entity.enabled)
|
2020-07-28 16:25:20 +08:00
|
|
|
|
entity.getComponents(type, comps);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return comps;
|
|
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
}
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|