优化transform结构

新增实体列表排序
This commit is contained in:
yhh
2020-07-24 15:29:07 +08:00
parent d4c244daf5
commit 6be43fc9ac
11 changed files with 1595 additions and 378 deletions

View File

@@ -1,12 +1,31 @@
module es {
export class EntityList{
public scene: Scene;
private _entitiesToRemove: Entity[] = [];
private _entitiesToAdded: Entity[] = [];
private _tempEntityList: Entity[] = [];
private _entities: Entity[] = [];
private _entityDict: Map<number, Entity[]> = new Map<number, Entity[]>();
private _unsortedTags: number[] = [];
/**
* 添加到场景中的实体列表
*/
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[] = [];
constructor(scene: Scene){
this.scene = scene;
@@ -20,12 +39,34 @@ module es {
return this._entities;
}
public markEntityListUnsorted(){
this._isEntityListUnsorted = true;
}
public markTagUnsorted(tag: number){
this._unsortedTags.push(tag);
}
/**
* 将实体添加到列表中。所有生命周期方法将在下一帧中被调用。
* @param entity
*/
public add(entity: Entity){
if (this._entitiesToAdded.indexOf(entity) == -1)
this._entitiesToAdded.push(entity);
}
/**
* 从列表中删除一个实体。所有生命周期方法将在下一帧中被调用。
* @param entity
*/
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;
}
// 防止在同一帧中添加或删除实体
if (this._entitiesToAdded.contains(entity)){
this._entitiesToAdded.remove(entity);
return;
@@ -35,6 +76,126 @@ module es {
this._entitiesToRemove.push(entity);
}
/**
* 从实体列表中删除所有实体
*/
public removeAllEntities(){
this._unsortedTags.length = 0;
this._entitiesToAdded.length = 0;
this._isEntityListUnsorted = false;
// 为什么我们要在这里更新列表?主要用于处理场景切换前分离的实体。
// 它们仍然在_entitiesToRemove列表中该列表将由更新列表处理。
this.updateLists();
for (let i = 0; i < this._entities.length; i ++){
this._entities[i]._isDestroyed = true;
this._entities[i].onRemovedFromScene();
this._entities[i].scene = null;
}
this._entities.length = 0;
this._entityDict.clear();
}
/**
* 检查该实体当前是否由此EntityList管理
* @param entity
*/
public contains(entity: Entity): boolean {
return this._entities.contains(entity) || this._entitiesToAdded.contains(entity);
}
public getTagList(tag: number){
let list = this._entityDict.get(tag);
if (!list){
list = [];
this._entityDict.set(tag, list);
}
return this._entityDict.get(tag);
}
public addToTagList(entity: Entity){
let list = this.getTagList(entity.tag);
if (!list.contains(entity)){
list.push(entity);
this._unsortedTags.push(entity.tag);
}
}
public removeFromTagList(entity: Entity){
let list = this._entityDict.get(entity.tag);
if (list){
list.remove(entity);
}
}
public update(){
for (let i = 0; i < this._entities.length; i++){
let entity = this._entities[i];
if (entity.enabled && (entity.updateInterval == 1 || Time.frameCount % entity.updateInterval == 0))
entity.update();
}
}
public updateLists(){
if (this._entitiesToRemove.length > 0){
let temp = this._entitiesToRemove;
this._entitiesToRemove = this._tempEntityList;
this._tempEntityList = temp;
this._tempEntityList.forEach(entity => {
this.removeFromTagList(entity);
this._entities.remove(entity);
entity.onRemovedFromScene();
entity.scene = null;
this.scene.entityProcessors.onEntityRemoved(entity);
});
this._tempEntityList.length = 0;
}
if (this._entitiesToAdded.length > 0){
let temp = this._entitiesToAdded;
this._entitiesToAdded = this._tempEntityList;
this._tempEntityList = temp;
this._tempEntityList.forEach(entity => {
if (!this._entities.contains(entity)){
this._entities.push(entity);
entity.scene = this.scene;
this.addToTagList(entity);
this.scene.entityProcessors.onEntityAdded(entity)
}
});
// 现在所有实体都被添加到场景中我们再次循环并调用onAddedToScene
this._tempEntityList.forEach(entity => entity.onAddedToScene());
this._tempEntityList.length = 0;
this._isEntityListUnsorted = true;
}
if (this._isEntityListUnsorted){
this._entities.sort();
this._isEntityListUnsorted = false;
}
if (this._unsortedTags.length > 0){
this._unsortedTags.forEach(tag => {
this._entityDict.get(tag).sort();
});
this._unsortedTags.length = 0;
}
}
/**
* 返回找到的第一个实体的名称。如果没有找到则返回null。
* @param name
*/
public findEntity(name: string){
for (let i = 0; i < this._entities.length; i ++){
if (this._entities[i].name == name)
@@ -120,94 +281,5 @@ module es {
return comps;
}
public getTagList(tag: number){
let list = this._entityDict.get(tag);
if (!list){
list = [];
this._entityDict.set(tag, list);
}
return this._entityDict.get(tag);
}
public addToTagList(entity: Entity){
let list = this.getTagList(entity.tag);
if (!list.contains(entity)){
list.push(entity);
this._unsortedTags.push(entity.tag);
}
}
public removeFromTagList(entity: Entity){
let list = this._entityDict.get(entity.tag);
if (list){
list.remove(entity);
}
}
public update(){
for (let i = 0; i < this._entities.length; i++){
let entity = this._entities[i];
if (entity.enabled)
entity.update();
}
}
public removeAllEntities(){
this._entitiesToAdded.length = 0;
this.updateLists();
for (let i = 0; i < this._entities.length; i ++){
this._entities[i]._isDestroyed = true;
this._entities[i].onRemovedFromScene();
this._entities[i].scene = null;
}
this._entities.length = 0;
this._entityDict.clear();
}
public updateLists(){
if (this._entitiesToRemove.length > 0){
let temp = this._entitiesToRemove;
this._entitiesToRemove = this._tempEntityList;
this._tempEntityList = temp;
this._tempEntityList.forEach(entity => {
this._entities.remove(entity);
entity.scene = null;
this.scene.entityProcessors.onEntityRemoved(entity);
});
this._tempEntityList.length = 0;
}
if (this._entitiesToAdded.length > 0){
let temp = this._entitiesToAdded;
this._entitiesToAdded = this._tempEntityList;
this._tempEntityList = temp;
this._tempEntityList.forEach(entity => {
if (!this._entities.contains(entity)){
this._entities.push(entity);
entity.scene = this.scene;
this.scene.entityProcessors.onEntityAdded(entity)
}
});
this._tempEntityList.forEach(entity => entity.onAddedToScene());
this._tempEntityList.length = 0;
}
if (this._unsortedTags.length > 0){
this._unsortedTags.forEach(tag => {
this._entityDict.get(tag).sort();
});
this._unsortedTags.length = 0;
}
}
}
}