Files
esengine/source/src/ECS/Utils/EntityList.ts
T

457 lines
16 KiB
TypeScript
Raw Normal View History

2020-07-22 20:07:14 +08:00
module es {
2020-07-28 16:25:20 +08:00
export class EntityList {
2023-03-13 23:32:24 +08:00
/**
* 场景引用
*/
2020-07-22 20:07:14 +08:00
public scene: Scene;
2023-03-13 23:32:24 +08:00
2020-07-24 15:29:07 +08:00
/**
2023-03-13 23:32:24 +08:00
* 实体列表
2020-07-24 15:29:07 +08:00
*/
public _entities: Entity[] = [];
2023-03-13 23:32:24 +08:00
2020-07-24 15:29:07 +08:00
/**
2023-03-13 23:32:24 +08:00
* 待添加的实体字典
2020-07-24 15:29:07 +08:00
*/
2023-03-13 23:32:24 +08:00
public _entitiesToAdded: { [index: number]: Entity } = {};
2020-07-24 15:29:07 +08:00
/**
2023-03-13 23:32:24 +08:00
* 待移除的实体字典
*/
public _entitiesToRemove: { [index: number]: Entity } = {};
/**
* 待添加的实体列表
2020-07-24 15:29:07 +08:00
*/
2021-05-12 13:08:47 +08:00
public _entitiesToAddedList: Entity[] = [];
2023-03-13 23:32:24 +08:00
/**
* 待移除的实体列表
*/
2021-05-12 13:08:47 +08:00
public _entitiesToRemoveList: Entity[] = [];
2023-03-13 23:32:24 +08:00
2020-07-24 15:29:07 +08:00
/**
2023-03-13 23:32:24 +08:00
* 实体列表是否已排序
2020-07-24 15:29:07 +08:00
*/
public _isEntityListUnsorted: boolean;
2023-03-13 23:32:24 +08:00
2020-07-24 15:29:07 +08:00
/**
2023-03-13 23:32:24 +08:00
* 实体字典,以实体标签为键
2020-07-24 15:29:07 +08:00
*/
2021-02-01 11:11:53 +08:00
public _entityDict: Map<number, Set<Entity>> = new Map<number, Set<Entity>>();
2023-03-13 23:32:24 +08:00
/**
* 未排序的标签集合
*/
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) {
this._entitiesToAdded[entity.id] = entity;
2021-05-12 13:08:47 +08:00
this._entitiesToAddedList.push(entity);
2020-06-08 20:11:58 +08:00
}
2020-07-24 15:29:07 +08:00
/**
2023-03-13 23:32:24 +08:00
* 从场景中移除实体。
* @param entity 要从场景中移除的实体。
2020-07-24 15:29:07 +08:00
*/
2020-07-28 16:25:20 +08:00
public remove(entity: Entity) {
2023-03-13 23:32:24 +08:00
// 如果实体在添加列表中,则将其从添加列表中移除
if (this._entitiesToAdded[entity.id]) {
2023-03-13 23:32:24 +08:00
const index = this._entitiesToAddedList.findIndex((e) => e.id === entity.id);
if (index !== -1) {
2021-05-12 13:08:47 +08:00
this._entitiesToAddedList.splice(index, 1);
2023-03-13 23:32:24 +08:00
}
delete this._entitiesToAdded[entity.id];
2020-07-22 20:07:14 +08:00
return;
}
2020-06-08 20:11:58 +08:00
2023-03-13 23:32:24 +08:00
// 如果实体不在添加列表中,则将其添加到移除列表中并将其添加到移除字典中
2021-05-12 13:08:47 +08:00
this._entitiesToRemoveList.push(entity);
2023-03-13 23:32:24 +08:00
if (!this._entitiesToRemove[entity.id]) {
this._entitiesToRemove[entity.id] = entity;
2023-03-13 23:32:24 +08:00
}
2020-06-08 20:11:58 +08:00
}
2020-07-22 20:07:14 +08:00
/**
2023-03-13 23:32:24 +08:00
* 从场景中移除所有实体。
2020-07-22 20:07:14 +08:00
*/
2020-07-28 16:25:20 +08:00
public removeAllEntities() {
2023-03-13 23:32:24 +08:00
// 清除字典和列表,以及是否已排序的标志
2020-10-27 18:08:49 +08:00
this._unsortedTags.clear();
this._entitiesToAdded = {};
2021-05-12 13:08:47 +08:00
this._entitiesToAddedList.length = 0;
2020-07-24 15:29:07 +08:00
this._isEntityListUnsorted = false;
2020-07-22 20:07:14 +08:00
2023-03-13 23:32:24 +08:00
// 调用updateLists方法,以处理要移除的实体
2020-07-24 15:29:07 +08:00
this.updateLists();
2020-06-08 20:11:58 +08:00
2023-03-13 23:32:24 +08:00
// 标记并移除所有实体
for (const entity of this._entities) {
entity._isDestroyed = true;
entity.onRemovedFromScene();
entity.scene = null;
2020-07-22 20:07:14 +08:00
}
2020-06-08 20:11:58 +08:00
2023-03-13 23:32:24 +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
/**
2023-03-13 23:32:24 +08:00
* 检查实体是否已经被添加到场景中。
* @param entity 要检查的实体
* @returns 如果实体已经被添加到场景中,则返回true;否则返回false
2020-07-22 20:07:14 +08:00
*/
2020-07-24 15:29:07 +08:00
public contains(entity: Entity): boolean {
2023-03-13 23:32:24 +08:00
// 检查实体是否存在于_entitiesToAdded字典中
return !!this._entitiesToAdded[entity.id];
2020-06-08 20:11:58 +08:00
}
2023-03-13 23:32:24 +08:00
/**
* 获取具有指定标签的实体列表。
* 如果列表不存在,则创建一个新列表并返回。
* @param tag 实体标签
* @returns 具有指定标签的实体列表
*/
public getTagList(tag: number): Set<Entity> {
// 尝试从_entityDict中获取具有指定标签的实体列表
2020-07-22 20:07:14 +08:00
let list = this._entityDict.get(tag);
2023-03-13 23:32:24 +08:00
// 如果列表不存在,则创建一个新的Set实例,并添加到_entityDict中
2020-07-28 16:25:20 +08:00
if (!list) {
2023-03-13 23:32:24 +08:00
list = new Set<Entity>();
2020-07-22 20:07:14 +08:00
this._entityDict.set(tag, list);
}
2020-06-08 20:11:58 +08:00
return list;
2020-07-22 20:07:14 +08:00
}
2020-06-08 20:11:58 +08:00
2023-03-13 23:32:24 +08:00
/**
* 添加实体到标签列表中。
* @param entity 实体
*/
2020-07-28 16:25:20 +08:00
public addToTagList(entity: Entity) {
2023-03-13 23:32:24 +08:00
// 获取标签列表
const list = this.getTagList(entity.tag);
// 将实体添加到标签列表中
list.add(entity);
// 添加未排序标志
2021-02-01 11:11:53 +08:00
this._unsortedTags.add(entity.tag);
2020-07-22 20:07:14 +08:00
}
2020-06-08 20:11:58 +08:00
2023-03-13 23:32:24 +08:00
/**
* 从标签列表中移除实体。
* @param entity 实体
*/
2020-07-28 16:25:20 +08:00
public removeFromTagList(entity: Entity) {
2023-03-13 23:32:24 +08:00
// 获取实体的标签列表
const list = this._entityDict.get(entity.tag);
// 如果标签列表存在,则从中移除实体
if (list) {
2021-02-01 14:23:13 +08:00
list.delete(entity);
2023-03-13 23:32:24 +08:00
}
2020-06-08 20:11:58 +08:00
}
2023-03-13 23:32:24 +08:00
/**
* 更新场景中所有启用的实体的Update方法
* 如果实体的UpdateInterval为1或Time.frameCount模除UpdateInterval为0,则每帧调用Update
*/
2020-07-28 16:25:20 +08:00
public update() {
2023-03-13 23:32:24 +08:00
for (let i = 0; i < this._entities.length; i++) {
const entity = this._entities[i];
if (entity.enabled && (entity.updateInterval === 1 || Time.frameCount % entity.updateInterval === 0)) {
2020-07-22 20:07:14 +08:00
entity.update();
2023-03-13 23:32:24 +08:00
}
2020-07-22 20:07:14 +08:00
}
}
2020-06-08 20:11:58 +08:00
2023-03-13 23:32:24 +08:00
/**
* 更新场景中实体的列表。
*/
2020-07-28 16:25:20 +08:00
public updateLists() {
2023-03-13 23:32:24 +08:00
// 处理要移除的实体
2021-05-12 13:08:47 +08:00
if (this._entitiesToRemoveList.length > 0) {
2023-03-13 23:32:24 +08:00
for (const entity of this._entitiesToRemoveList) {
// 从标签列表中删除实体
2021-05-12 13:08:47 +08:00
this.removeFromTagList(entity);
2023-03-13 23:32:24 +08:00
// 从场景实体列表中删除实体
const index = this._entities.findIndex((e) => e.id === entity.id);
if (index !== -1) {
2021-05-12 13:08:47 +08:00
this._entities.splice(index, 1);
2023-03-13 23:32:24 +08:00
}
// 调用实体的onRemovedFromScene方法,并将其scene属性设置为null
2021-05-12 13:08:47 +08:00
entity.onRemovedFromScene();
entity.scene = null;
2023-03-13 23:32:24 +08:00
// 通知场景实体处理器,一个实体已被移除
2021-05-12 13:08:47 +08:00
this.scene.entityProcessors.onEntityRemoved(entity);
}
2023-03-13 23:32:24 +08:00
// 清空要移除的实体列表和字典
2021-05-12 13:08:47 +08:00
this._entitiesToRemove = {};
this._entitiesToRemoveList.length = 0;
}
2020-11-26 20:02:53 +08:00
2023-03-13 23:32:24 +08:00
// 处理要添加的实体
2021-05-12 13:08:47 +08:00
if (this._entitiesToAddedList.length > 0) {
2023-03-13 23:32:24 +08:00
// 添加实体到场景实体列表和标签列表中
for (const entity of this._entitiesToAddedList) {
2021-05-12 13:08:47 +08:00
this._entities.push(entity);
entity.scene = this.scene;
this.addToTagList(entity);
2023-03-13 23:32:24 +08:00
}
// 通知场景实体处理器,有新的实体已添加
for (const entity of this._entitiesToAddedList) {
2021-05-12 13:08:47 +08:00
this.scene.entityProcessors.onEntityAdded(entity);
}
2023-03-13 23:32:24 +08:00
// 调用实体的onAddedToScene方法,以允许它们执行任何场景相关的操作
for (const entity of this._entitiesToAddedList) {
2021-05-12 13:08:47 +08:00
entity.onAddedToScene();
}
2020-07-24 15:29:07 +08:00
2023-03-13 23:32:24 +08:00
// 清空要添加的实体列表和字典
2021-05-12 13:08:47 +08:00
this._entitiesToAdded = {};
this._entitiesToAddedList.length = 0;
2020-07-22 20:07:14 +08:00
}
2020-06-09 22:32:18 +08:00
}
2020-07-24 15:29:07 +08:00
2023-03-13 23:32:24 +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) {
if (this._entities.length > 0) {
2023-03-13 23:32:24 +08:00
for (let i = 0, s = this._entities.length; i < s; ++i) {
let entity = this._entities[i];
if (entity.name == name)
return entity;
}
2020-07-24 15:29:07 +08:00
}
2023-03-13 23:32:24 +08:00
if (this._entitiesToAddedList.length > 0) {
2023-03-13 23:32:24 +08:00
for (let i = 0, s = this._entitiesToAddedList.length; i < s; ++i) {
let entity = this._entitiesToAddedList[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
}
2021-04-26 15:23:16 +08:00
/**
2023-03-13 23:32:24 +08:00
* 通过实体ID在场景中查找对应实体
* @param id 实体ID
* @returns 返回找到的实体,如果没有找到则返回 null
2021-04-26 15:23:16 +08:00
*/
public findEntityById(id: number) {
2023-03-13 23:32:24 +08:00
// 遍历场景中所有实体
if (this._entities.length > 0) {
2023-03-13 23:32:24 +08:00
for (let i = 0, s = this._entities.length; i < s; ++i) {
let entity = this._entities[i];
2023-03-13 23:32:24 +08:00
// 如果实体的ID匹配,返回该实体
if (entity.id == id)
return entity;
}
2021-04-26 15:23:16 +08:00
}
2023-03-13 23:32:24 +08:00
// 在未添加的实体列表中查找
2021-04-26 15:23:16 +08:00
return this._entitiesToAdded[id];
}
2020-07-24 15:29:07 +08:00
/**
2023-03-13 23:32:24 +08:00
* 获取标签对应的实体列表
* @param tag 实体的标签
* @returns 返回所有拥有该标签的实体列表
2020-07-24 15:29:07 +08:00
*/
2023-03-13 23:32:24 +08:00
public entitiesWithTag(tag: number): Entity[] {
// 从字典中获取对应标签的实体列表
const list = this.getTagList(tag);
// 从对象池中获取 Entity 类型的数组
const returnList = ListPool.obtain<Entity>(Entity);
2020-07-24 15:29:07 +08:00
if (list.size > 0) {
2023-03-13 23:32:24 +08:00
// 将实体列表中的实体添加到返回列表中
for (const entity of list) {
returnList.push(entity);
}
2021-02-01 11:11:53 +08:00
}
2023-03-13 23:32:24 +08:00
// 返回已填充好实体的返回列表
2020-07-24 15:29:07 +08:00
return returnList;
}
2021-03-30 18:02:27 +08:00
/**
* 返回第一个找到该tag的实体
2023-03-13 23:32:24 +08:00
* @param tag
* @returns
2021-03-30 18:02:27 +08:00
*/
public entityWithTag(tag: number) {
let list = this.getTagList(tag);
if (list.size > 0) {
for (let entity of list) {
return entity;
}
2021-03-30 18:02:27 +08:00
}
return null;
}
2020-07-24 15:29:07 +08:00
/**
2023-03-13 23:32:24 +08:00
* 在场景中查找具有给定类型的组件。
* @param type 要查找的组件类型。
* @returns 如果找到,则返回该组件;否则返回null。
2020-07-24 15:29:07 +08:00
*/
2023-03-13 23:32:24 +08:00
public findComponentOfType<T extends Component>(type: new (...args: any[]) => T): T | null {
// 遍历场景中的所有实体,查找具有给定类型的组件
for (const entity of this._entities) {
if (entity.enabled) {
const comp = entity.getComponent(type);
if (comp) {
return comp;
}
2020-07-24 15:29:07 +08:00
}
}
2023-03-13 23:32:24 +08:00
// 遍历待添加的实体列表中的所有实体,查找具有给定类型的组件
for (const entity of this._entitiesToAddedList) {
if (entity.enabled) {
const comp = entity.getComponent(type);
if (comp) {
return comp;
}
2020-07-24 15:29:07 +08:00
}
}
2023-03-13 23:32:24 +08:00
// 如果找不到具有给定类型的组件,则返回null
2020-07-24 15:29:07 +08:00
return null;
}
2023-03-13 23:32:24 +08:00
2020-07-24 15:29:07 +08:00
/**
2023-03-13 23:32:24 +08:00
* 在场景中查找具有给定类型的所有组件。
* @param type 要查找的组件类型。
* @returns 具有给定类型的所有组件的列表。
2020-07-24 15:29:07 +08:00
*/
2023-03-13 23:32:24 +08:00
public findComponentsOfType<T extends Component>(type: new (...args: any[]) => T): T[] {
// 从池中获取一个可重用的组件列表
const comps = ListPool.obtain<T>(type);
// 遍历场景中的所有实体,查找具有给定类型的组件并添加到组件列表中
for (const entity of this._entities) {
if (entity.enabled) {
entity.getComponents(type, comps);
}
2020-07-24 15:29:07 +08:00
}
2023-03-13 23:32:24 +08:00
// 遍历待添加的实体列表中的所有实体,查找具有给定类型的组件并添加到组件列表中
for (const entity of this._entitiesToAddedList) {
if (entity.enabled) {
entity.getComponents(type, comps);
}
2020-07-24 15:29:07 +08:00
}
2023-03-13 23:32:24 +08:00
// 返回具有给定类型的所有组件的列表
2020-07-24 15:29:07 +08:00
return comps;
}
2023-03-13 23:32:24 +08:00
/**
2023-03-13 23:32:24 +08:00
* 返回拥有指定类型组件的所有实体
* @param types 要查询的组件类型列表
* @returns 返回拥有指定类型组件的所有实体
*/
2021-10-04 16:53:00 +08:00
public findEntitiesOfComponent(...types: any[]): Entity[] {
2023-03-13 23:32:24 +08:00
const entities = [];
// 遍历所有已存在的实体
for (const entity of this._entities) {
// 只有启用的实体才会被考虑
if (entity.enabled) {
// 如果types数组为空,直接将实体添加到结果数组中
if (types.length === 0) {
entities.push(entity);
continue;
}
// 对于每个指定的组件类型,检查实体是否具有该组件
let meet = true;
for (const type of types) {
const hasComp = entity.hasComponent(type);
if (!hasComp) {
meet = false;
break;
}
}
2023-03-13 23:32:24 +08:00
// 如果实体满足要求,将其添加到结果数组中
if (meet) {
entities.push(entity);
}
}
}
2023-03-13 23:32:24 +08:00
// 遍历所有等待添加的实体,和上面的操作类似
for (const entity of this._entitiesToAddedList) {
if (entity.enabled) {
if (types.length === 0) {
entities.push(entity);
continue;
}
let meet = true;
for (const type of types) {
const hasComp = entity.hasComponent(type);
if (!hasComp) {
meet = false;
break;
}
}
2023-03-13 23:32:24 +08:00
if (meet) {
entities.push(entity);
}
}
}
return entities;
}
2023-03-13 23:32:24 +08:00
2020-06-08 20:11:58 +08:00
}
2020-07-22 20:07:14 +08:00
}