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
|
|
|
|
/**
|
2020-10-27 18:08:49 +08:00
|
|
|
|
* 场景中添加的实体列表
|
2020-07-24 15:29:07 +08:00
|
|
|
|
*/
|
2020-12-14 11:34:23 +08:00
|
|
|
|
public _entities: Entity[] = [];
|
2020-07-24 15:29:07 +08:00
|
|
|
|
/**
|
2020-10-27 18:08:49 +08:00
|
|
|
|
* 本帧添加的实体列表。用于对实体进行分组,以便我们可以同时处理它们
|
2020-07-24 15:29:07 +08:00
|
|
|
|
*/
|
2020-11-27 11:07:43 +08:00
|
|
|
|
public _entitiesToAdded: HashSet<Entity> = new HashSet<Entity>();
|
2020-07-24 15:29:07 +08:00
|
|
|
|
/**
|
2020-10-27 18:08:49 +08:00
|
|
|
|
* 本帧被标记为删除的实体列表。用于对实体进行分组,以便我们可以同时处理它们
|
2020-07-24 15:29:07 +08:00
|
|
|
|
*/
|
2020-11-27 11:07:43 +08:00
|
|
|
|
public _entitiesToRemove: HashSet<Entity> = new HashSet<Entity>();
|
2020-07-24 15:29:07 +08:00
|
|
|
|
/**
|
2020-10-27 18:08:49 +08:00
|
|
|
|
* 标志,用于确定我们是否需要在这一帧中对实体进行排序
|
2020-07-24 15:29:07 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public _isEntityListUnsorted: boolean;
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 通过标签跟踪实体,便于检索
|
|
|
|
|
|
*/
|
|
|
|
|
|
public _entityDict: Map<number, Entity[]> = new Map<number, Entity[]>();
|
2020-10-27 18:08:49 +08:00
|
|
|
|
public _unsortedTags: Set<number> = new Set<number>();
|
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-10-27 18:08:49 +08:00
|
|
|
|
this._unsortedTags.add(tag);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-10-27 18:08:49 +08:00
|
|
|
|
* 将一个实体添加到列表中。所有的生命周期方法将在下一帧中被调用
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* @param entity
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public add(entity: Entity) {
|
2020-11-26 20:02:53 +08:00
|
|
|
|
this._entitiesToAdded.add(entity);
|
2020-06-08 20:11:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-24 15:29:07 +08:00
|
|
|
|
/**
|
2020-10-27 18:08:49 +08:00
|
|
|
|
* 从列表中删除一个实体。所有的生命周期方法将在下一帧中被调用
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* @param entity
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public remove(entity: Entity) {
|
2021-01-18 19:54:41 +08:00
|
|
|
|
Debug.warnIf(this._entitiesToRemove.contains(entity), `您正在尝试删除已经删除的实体(${entity.name})`);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
|
|
|
|
|
|
// 防止在同一帧中添加或删除实体
|
2020-11-27 11:07:43 +08:00
|
|
|
|
if (this._entitiesToAdded.contains(entity)) {
|
|
|
|
|
|
this._entitiesToAdded.remove(entity);
|
2020-07-22 20:07:14 +08:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
|
2020-11-27 11:07:43 +08:00
|
|
|
|
if (!this._entitiesToRemove.contains(entity))
|
2020-11-26 20:02:53 +08:00
|
|
|
|
this._entitiesToRemove.add(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-10-27 18:08:49 +08:00
|
|
|
|
this._unsortedTags.clear();
|
2020-11-26 20:02:53 +08:00
|
|
|
|
this._entitiesToAdded.clear();
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this._isEntityListUnsorted = false;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
|
2020-10-27 18:08:49 +08:00
|
|
|
|
// 为什么我们要在这里更新列表?主要是为了处理在场景切换前被分离的实体。
|
|
|
|
|
|
// 它们仍然会在_entitiesToRemove列表中,这将由updateLists处理。
|
2020-07-24 15:29:07 +08:00
|
|
|
|
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-12-14 11:34:23 +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-12-14 11:34:23 +08:00
|
|
|
|
this._entities.length = 0;
|
2020-07-24 15:29:07 +08:00
|
|
|
|
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-10-27 18:08:49 +08:00
|
|
|
|
* 检查实体目前是否由这个EntityList管理
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* @param entity
|
2020-07-22 20:07:14 +08:00
|
|
|
|
*/
|
2020-07-24 15:29:07 +08:00
|
|
|
|
public contains(entity: Entity): boolean {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
return new linq.List(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-10-09 16:59:34 +08:00
|
|
|
|
return list;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
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-10-09 16:59:34 +08:00
|
|
|
|
if (list.findIndex(e => e.id == entity.id) == -1) {
|
2020-07-22 20:07:14 +08:00
|
|
|
|
list.push(entity);
|
2020-10-27 18:08:49 +08:00
|
|
|
|
this._unsortedTags.add(entity.tag);
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
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-11-30 13:50:18 +08:00
|
|
|
|
new linq.List(list).remove(entity);
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
2020-06-08 20:11:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public update() {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
for (let entity of this._entities) {
|
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() {
|
2020-11-27 11:07:43 +08:00
|
|
|
|
if (this._entitiesToRemove.getCount() > 0) {
|
2020-12-04 10:54:51 +08:00
|
|
|
|
this._entitiesToRemove.toArray().forEach(entity => {
|
2020-12-30 16:28:07 +08:00
|
|
|
|
// 处理标签列表
|
|
|
|
|
|
this.removeFromTagList(entity);
|
|
|
|
|
|
|
|
|
|
|
|
// 处理常规实体列表
|
|
|
|
|
|
new linq.List(this._entities).remove(entity);
|
|
|
|
|
|
entity.onRemovedFromScene();
|
|
|
|
|
|
entity.scene = null;
|
|
|
|
|
|
|
|
|
|
|
|
this.scene.entityProcessors.onEntityRemoved(entity);
|
2020-12-04 10:54:51 +08:00
|
|
|
|
});
|
2020-11-26 20:02:53 +08:00
|
|
|
|
this._entitiesToRemove.clear();
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-27 11:07:43 +08:00
|
|
|
|
if (this._entitiesToAdded.getCount() > 0) {
|
2020-12-04 10:54:51 +08:00
|
|
|
|
this._entitiesToAdded.toArray().forEach(entity => {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
this._entities.push(entity);
|
2020-11-26 20:02:53 +08:00
|
|
|
|
entity.scene = this.scene;
|
2020-07-24 15:29:07 +08:00
|
|
|
|
|
2020-11-26 20:02:53 +08:00
|
|
|
|
this.addToTagList(entity);
|
2020-07-22 20:07:14 +08:00
|
|
|
|
|
2020-12-30 16:28:07 +08:00
|
|
|
|
this.scene.entityProcessors.onEntityAdded(entity);
|
2020-12-04 10:54:51 +08:00
|
|
|
|
});
|
2020-11-26 20:02:53 +08:00
|
|
|
|
|
2020-12-04 10:54:51 +08:00
|
|
|
|
this._entitiesToAdded.toArray().forEach(entity => {
|
|
|
|
|
|
entity.onAddedToScene();
|
|
|
|
|
|
})
|
2020-11-26 20:02:53 +08:00
|
|
|
|
|
|
|
|
|
|
this._entitiesToAdded.clear();
|
|
|
|
|
|
this._isEntityListUnsorted = true;
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-07-28 16:25:20 +08:00
|
|
|
|
if (this._isEntityListUnsorted) {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
this._entities.sort(Entity.entityComparer.compare);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
this._isEntityListUnsorted = false;
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-10-27 18:08:49 +08:00
|
|
|
|
// 根据需要对标签列表进行排序
|
2020-12-04 10:54:51 +08:00
|
|
|
|
if (this._unsortedTags.size > 0) {
|
2020-11-27 11:07:43 +08:00
|
|
|
|
this._unsortedTags.forEach(value => this._entityDict.get(value).sort((a, b) => a.compareTo(b)));
|
2020-12-30 16:28:07 +08:00
|
|
|
|
|
2020-10-27 18:08:49 +08:00
|
|
|
|
this._unsortedTags.clear();
|
2020-07-22 20:07:14 +08:00
|
|
|
|
}
|
2020-06-09 22:32:18 +08:00
|
|
|
|
}
|
2020-07-24 15:29:07 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
2020-10-27 18:08:49 +08:00
|
|
|
|
* 返回第一个找到的名字为name的实体。如果没有找到则返回null
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* @param name
|
|
|
|
|
|
*/
|
2020-07-28 16:25:20 +08:00
|
|
|
|
public findEntity(name: string) {
|
|
|
|
|
|
for (let i = 0; i < this._entities.length; i++) {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
if (this._entities[i].name == name)
|
|
|
|
|
|
return this._entities[i];
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-30 16:28:07 +08:00
|
|
|
|
for (let i = 0; i < this._entitiesToAdded.getCount(); i++) {
|
2020-11-27 11:07:43 +08:00
|
|
|
|
let entity = this._entitiesToAdded.toArray()[i];
|
|
|
|
|
|
if (entity.name == name)
|
|
|
|
|
|
return entity;
|
2020-11-26 20:02:53 +08:00
|
|
|
|
}
|
2020-12-30 16:28:07 +08:00
|
|
|
|
|
2020-11-26 20:02:53 +08:00
|
|
|
|
return null;
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-10-27 18:08:49 +08:00
|
|
|
|
* 返回带有标签的所有实体的列表。如果没有实体有标签,则返回一个空列表。
|
|
|
|
|
|
* 返回的List可以通过ListPool.free放回池中
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* @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-10-27 18:08:49 +08:00
|
|
|
|
returnList.length = this._entities.length;
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-10-27 18:08:49 +08:00
|
|
|
|
* 返回一个T类型的所有实体的列表。
|
|
|
|
|
|
* 返回的List可以通过ListPool.free放回池中。
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* @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-12-14 11:34:23 +08:00
|
|
|
|
if (this._entities[i] instanceof type)
|
|
|
|
|
|
list.push(this._entities[i] as T);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
2020-11-26 20:02:53 +08:00
|
|
|
|
|
2020-12-30 16:28:07 +08:00
|
|
|
|
for (let i = 0; i < this._entitiesToAdded.getCount(); i++) {
|
2020-11-27 11:07:43 +08:00
|
|
|
|
let entity = this._entitiesToAdded.toArray()[i];
|
|
|
|
|
|
if (TypeUtils.getType(entity) instanceof type) {
|
|
|
|
|
|
list.push(entity as T);
|
2020-11-26 20:02:53 +08:00
|
|
|
|
}
|
2020-10-09 15:39:06 +08:00
|
|
|
|
}
|
2020-07-24 15:29:07 +08:00
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-10-27 18:08:49 +08:00
|
|
|
|
* 返回在场景中找到的第一个T类型的组件。
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* @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++) {
|
2020-12-14 11:34:23 +08:00
|
|
|
|
if (this._entities[i].enabled) {
|
|
|
|
|
|
let comp = this._entities[i].getComponent<T>(type);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
if (comp)
|
|
|
|
|
|
return comp;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-27 11:07:43 +08:00
|
|
|
|
for (let i = 0; i < this._entitiesToAdded.getCount(); i++) {
|
|
|
|
|
|
let entity: Entity = this._entitiesToAdded.toArray()[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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-10-27 18:08:49 +08:00
|
|
|
|
* 返回在场景中找到的所有T类型的组件。
|
|
|
|
|
|
* 返回的List可以通过ListPool.free放回池中。
|
2020-07-24 15:29:07 +08:00
|
|
|
|
* @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-12-14 11:34:23 +08:00
|
|
|
|
if (this._entities[i].enabled)
|
|
|
|
|
|
this._entities[i].getComponents(type, comps);
|
2020-07-24 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-11-27 11:07:43 +08:00
|
|
|
|
for (let i = 0; i < this._entitiesToAdded.getCount(); i++) {
|
|
|
|
|
|
let entity = this._entitiesToAdded.toArray()[i];
|
2020-07-24 15:29:07 +08:00
|
|
|
|
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
|
|
|
|
}
|