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

307 lines
11 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 {
2020-07-22 20:07:14 +08:00
public scene: Scene;
/**
*
*/
public _entities: Entity[] = [];
/**
* 便
*/
public _entitiesToAdded: Entity[] = [];
/**
* 便
*/
public _entitiesToRemove: Entity[] = [];
/**
*
*/
public _isEntityListUnsorted: boolean;
/**
* 便
*/
public _entityDict: Map<number, Entity[]> = new Map<number, Entity[]>();
public _unsortedTags: number[] = [];
public _addToSceneEntityList: Entity[] = [];
/** 是否使用分帧处理 */
public frameAllocate: boolean = false;
/** 每帧最大处理数量 */
public maxAllocate: number = 10;
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() {
this._isEntityListUnsorted = true;
}
2020-07-28 16:25:20 +08:00
public markTagUnsorted(tag: number) {
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
}
/**
*
* @param entity
*/
2020-07-28 16:25:20 +08:00
public remove(entity: Entity) {
if (!this._entitiesToRemove.contains(entity)) {
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-22 20:07:14 +08:00
*/
2020-07-28 16:25:20 +08:00
public removeAllEntities() {
this._unsortedTags.length = 0;
this._entitiesToAdded.length = 0;
this._isEntityListUnsorted = false;
2020-07-22 20:07:14 +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++) {
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
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
/**
* EntityList管理
* @param entity
2020-07-22 20:07:14 +08:00
*/
public contains(entity: Entity): boolean {
return this._entities.findIndex(e => e.id == entity.id) != -1 ||
this._entitiesToAdded.findIndex(e => e.id == entity.id) != -1;
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
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);
if (list.findIndex(e => e.id == entity.id) == -1) {
2020-07-22 20:07:14 +08:00
list.push(entity);
if (!this._unsortedTags.contains(entity.tag))
this._unsortedTags.push(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-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];
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) {
for (const entity of this._entitiesToRemove) {
this.removeFromTagList(entity);
2020-07-22 20:07:14 +08:00
this._entities.remove(entity);
entity.onRemovedFromScene();
2020-07-22 20:07:14 +08:00
entity.scene = null;
this.scene.entityProcessors.onEntityRemoved(entity);
2020-10-09 15:39:06 +08:00
}
2020-07-22 20:07:14 +08:00
this._entitiesToRemove.length = 0;
2020-07-22 20:07:14 +08:00
}
// 现在所有实体都被添加到场景中我们再次循环并调用onAddedToScene
while (this._addToSceneEntityList.length > 0){
let entity = this._addToSceneEntityList.shift();
entity.onAddedToScene();
}
2020-07-22 20:07:14 +08:00
if (this._entitiesToAdded.length > 0) {
if (this.frameAllocate && this._entitiesToAdded.length > this.maxAllocate){
// 启用分帧处理
for (let i = 0; i < this.maxAllocate; i ++){
this.perEntityAddToScene();
}
if (this._entitiesToAdded.length == 0) this._isEntityListUnsorted = true;
}else{
while (this._entitiesToAdded.length > 0){
this.perEntityAddToScene();
2020-07-22 20:07:14 +08:00
}
this._isEntityListUnsorted = true;
2020-10-09 15:39:06 +08:00
}
}
2020-07-28 16:25:20 +08:00
if (this._isEntityListUnsorted) {
2020-10-09 15:39:06 +08:00
this._entities.sort((a, b)=>{
return a.compareTo(b);
});
this._isEntityListUnsorted = false;
2020-07-22 20:07:14 +08:00
}
if (this._addToSceneEntityList.length == 0 && this._unsortedTags.length > 0) {
2020-10-09 15:39:06 +08:00
for (const tag of this._unsortedTags) {
this._entityDict.get(tag).sort((a, b) => {
return a.compareTo(b);
});
}
2020-07-22 20:07:14 +08:00
this._unsortedTags.length = 0;
}
2020-06-09 22:32:18 +08:00
}
/** 每次添加一个实体到场景 */
private perEntityAddToScene(){
let entity = this._entitiesToAdded.shift();
this._addToSceneEntityList.push(entity);
if (this._entities.findIndex(e => e.id == entity.id) == -1) {
this._entities.push(entity);
entity.scene = this.scene;
this.addToTagList(entity);
this.scene.entityProcessors.onEntityAdded(entity);
}
}
/**
* null
* @param name
*/
2020-07-28 16:25:20 +08:00
public findEntity(name: string) {
for (let i = 0; i < this._entities.length; i++) {
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) {
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++)
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[] {
let list = ListPool.obtain<T>();
2020-07-28 16:25:20 +08:00
for (let i = 0; i < this._entities.length; i++) {
if (this._entities[i] instanceof type)
list.push(this._entities[i] as T);
}
2020-10-09 15:39:06 +08:00
for (const entity of this._entitiesToAdded) {
if (entity instanceof type)
list.push(entity as T);
2020-10-09 15:39:06 +08:00
}
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) {
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++) {
let entity = this._entitiesToAdded[i];
2020-07-28 16:25:20 +08:00
if (entity.enabled) {
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[] {
let comps = ListPool.obtain<T>();
2020-07-28 16:25:20 +08:00
for (let i = 0; i < this._entities.length; i++) {
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++) {
let entity = this._entitiesToAdded[i];
if (entity.enabled)
2020-07-28 16:25:20 +08:00
entity.getComponents(type, comps);
}
return comps;
}
2020-06-08 20:11:58 +08:00
}
2020-07-22 20:07:14 +08:00
}