新增fastList、注释完善

This commit is contained in:
yhh
2020-10-27 18:08:49 +08:00
parent 0e7b0bc45c
commit fc6a8a0803
19 changed files with 595 additions and 205 deletions

View File

@@ -1,4 +1,4 @@
///<reference path="../Components/IUpdatableComparer.ts" />
///<reference path="../Components/IUpdatable.ts" />
module es {
export class ComponentList {
/**
@@ -10,7 +10,7 @@ module es {
/**
* 添加到实体的组件列表
*/
public _components: Component[] = [];
public _components: FastList<Component> = new FastList<Component>();
/**
* 添加到此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工
*/
@@ -34,7 +34,7 @@ module es {
}
public get buffer() {
return this._components;
return this._components.buffer;
}
public markEntityListUnsorted() {
@@ -47,7 +47,7 @@ module es {
public remove(component: Component) {
if (this._componentsToRemove.contains(component))
console.warn(`You are trying to remove a Component (${component}) that you already removed`);
console.warn(`您正在尝试删除一个您已经删除的组件(${component})`);
// 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除
if (this._componentsToAdd.contains(component)) {
@@ -66,14 +66,14 @@ module es {
this.handleRemove(this._components[i]);
}
this._components.length = 0;
this._components.clear();
this._componentsToAdd.length = 0;
this._componentsToRemove.length = 0;
}
public deregisterAllComponents() {
for (let i = 0; i < this._components.length; i++) {
let component = this._components[i];
let component = this._components.buffer[i];
// 处理渲染层列表
if (component instanceof RenderableComponent) {
@@ -92,7 +92,7 @@ module es {
public registerAllComponents() {
for (let i = 0; i < this._components.length; i++) {
let component = this._components[i];
let component = this._components.buffer[i];
if (component instanceof RenderableComponent) {
if (!this._entity.scene.dynamicBatch) this._entity.scene.addChild(component.displayObject);
@@ -131,7 +131,7 @@ module es {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.push(component);
this._components.add(component);
this._tempBufferList.push(component);
}
if (this._entity.scene.dynamicBatch) this._entity.scene.dynamicInBatch();
@@ -155,7 +155,7 @@ module es {
}
if (this._isComponentListUnsorted) {
this._components.sort(ComponentList.compareUpdatableOrder.compare);
this._components.sort(ComponentList.compareUpdatableOrder);
this._isComponentListUnsorted = false;
}
}
@@ -187,7 +187,7 @@ module es {
*/
public getComponent<T extends Component>(type, onlyReturnInitializedComponents: boolean): T {
for (let i = 0; i < this._components.length; i++) {
let component = this._components[i];
let component = this._components.buffer[i];
if (component instanceof type)
return component as T;
}
@@ -214,7 +214,7 @@ module es {
components = [];
for (let i = 0; i < this._components.length; i++) {
let component = this._components[i];
let component = this._components.buffer[i];
if (typeof (typeName) == "string") {
if (egret.is(component, typeName)) {
components.push(component);
@@ -245,7 +245,7 @@ module es {
public update() {
this.updateLists();
for (let i = 0; i < this._components.length; i++) {
let updatableComponent = this._components[i];
let updatableComponent = this._components.buffer[i];
if (updatableComponent.enabled &&
(updatableComponent.updateInterval == 1 ||
@@ -256,8 +256,8 @@ module es {
public onEntityTransformChanged(comp: transform.Component) {
for (let i = 0; i < this._components.length; i++) {
if (this._components[i].enabled)
this._components[i].onEntityTransformChanged(comp);
if (this._components.buffer[i].enabled)
this._components.buffer[i].onEntityTransformChanged(comp);
}
for (let i = 0; i < this._componentsToAdd.length; i++) {
@@ -268,18 +268,18 @@ module es {
public onEntityEnabled() {
for (let i = 0; i < this._components.length; i++)
this._components[i].onEnabled();
this._components.buffer[i].onEnabled();
}
public onEntityDisabled() {
for (let i = 0; i < this._components.length; i++)
this._components[i].onDisabled();
this._components.buffer[i].onDisabled();
}
public debugRender(camera: Camera){
for (let i = 0; i < this._components.length; i ++){
if (this._components[i].enabled)
this._components[i].debugRender(camera);
if (this._components.buffer[i].enabled)
this._components.buffer[i].debugRender(camera);
}
}
}

View File

@@ -2,26 +2,26 @@ module es {
export class EntityList {
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 _unsortedTags: Set<number> = new Set<number>();
public _addToSceneEntityList: Entity[] = [];
/** 是否使用分帧处理 */
public frameAllocate: boolean = false;
@@ -45,11 +45,11 @@ module es {
}
public markTagUnsorted(tag: number) {
this._unsortedTags.push(tag);
this._unsortedTags.add(tag);
}
/**
* 将实体添加到列表中。所有生命周期方法将在下一帧中被调用
* 将一个实体添加到列表中。所有生命周期方法将在下一帧中被调用
* @param entity
*/
public add(entity: Entity) {
@@ -58,12 +58,12 @@ module es {
}
/**
* 从列表中删除一个实体。所有生命周期方法将在下一帧中被调用
* 从列表中删除一个实体。所有生命周期方法将在下一帧中被调用
* @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`);
console.warn(`您正在尝试删除已经删除的实体(${entity.name})`);
return;
}
@@ -81,12 +81,12 @@ module es {
* 从实体列表中删除所有实体
*/
public removeAllEntities() {
this._unsortedTags.length = 0;
this._unsortedTags.clear();
this._entitiesToAdded.length = 0;
this._isEntityListUnsorted = false;
// 为什么我们要在这里更新列表?主要用于处理场景切换前分离的实体。
// 它们仍然在_entitiesToRemove列表中该列表将由更新列表处理。
// 为什么我们要在这里更新列表主要是为了处理场景切换前分离的实体。
// 它们仍然在_entitiesToRemove列表中这将由updateLists处理。
this.updateLists();
for (let i = 0; i < this._entities.length; i++) {
@@ -100,7 +100,7 @@ module es {
}
/**
* 检查实体前是否由EntityList管理
* 检查实体前是否由这个EntityList管理
* @param entity
*/
public contains(entity: Entity): boolean {
@@ -122,8 +122,7 @@ module es {
let list = this.getTagList(entity.tag);
if (list.findIndex(e => e.id == entity.id) == -1) {
list.push(entity);
if (!this._unsortedTags.contains(entity.tag))
this._unsortedTags.push(entity.tag);
this._unsortedTags.add(entity.tag);
}
}
@@ -157,7 +156,7 @@ module es {
this._entitiesToRemove.length = 0;
}
// 现在所有实体都被添加到场景中我们再次循环并调用onAddedToScene
// 现在所有实体都被添加到场景中我们再次循环并调用onAddedToScene
while (this._addToSceneEntityList.length > 0){
let entity = this._addToSceneEntityList.shift();
entity.onAddedToScene();
@@ -187,14 +186,15 @@ module es {
this._isEntityListUnsorted = false;
}
if (this._addToSceneEntityList.length == 0 && this._unsortedTags.length > 0) {
for (const tag of this._unsortedTags) {
// 根据需要对标签列表进行排序
if (this._addToSceneEntityList.length == 0 && this._unsortedTags.size > 0) {
this._unsortedTags.forEach((tag)=>{
this._entityDict.get(tag).sort((a, b) => {
return a.compareTo(b);
});
}
this._unsortedTags.length = 0;
});
this._unsortedTags.clear();
}
}
@@ -214,7 +214,7 @@ module es {
}
/**
* 返回找到的第一个实体的名称。如果没有找到则返回null
* 返回第一个找到的名字为name的实体。如果没有找到则返回null
* @param name
*/
public findEntity(name: string) {
@@ -227,13 +227,15 @@ module es {
}
/**
* 返回带有标的所有实体的列表。如果没有实体有标,则返回一个空列表。可以通过ListPool.free将返回的列表放回池中。
* 返回带有标的所有实体的列表。如果没有实体有标,则返回一个空列表。
* 返回的List可以通过ListPool.free放回池中
* @param tag
*/
public entitiesWithTag(tag: number) {
let list = this.getTagList(tag);
let returnList = ListPool.obtain<Entity>();
returnList.length = this._entities.length;
for (let i = 0; i < list.length; i++)
returnList.push(list[i]);
@@ -241,7 +243,8 @@ module es {
}
/**
* 返回t类型的所有实体的列表。返回的列表可以通过ListPool.free放回池中。
* 返回一个T类型的所有实体的列表。
* 返回的List可以通过ListPool.free放回池中。
* @param type
*/
public entitiesOfType<T extends Entity>(type): T[] {
@@ -259,7 +262,7 @@ module es {
}
/**
* 返回在类型为T的场景中找到的第一个组件
* 返回在场景中找到的第一个T类型的组件
* @param type
*/
public findComponentOfType<T extends Component>(type): T {
@@ -284,7 +287,8 @@ module es {
}
/**
* 返回在类型t的场景中找到的所有组件。返回的列表可以通过ListPool.free放回池中
* 返回在场景中找到的所有T类型的组件。
* 返回的List可以通过ListPool.free放回池中。
* @param type
*/
public findComponentsOfType<T extends Component>(type): T[] {

View File

@@ -1,6 +1,6 @@
module es {
export class EntityProcessorList {
private _processors: EntitySystem[] = [];
protected _processors: EntitySystem[] = [];
public add(processor: EntitySystem) {
this._processors.push(processor);

View File

@@ -0,0 +1,116 @@
module es {
/**
* 围绕一个数组的非常基本的包装,当它达到容量时自动扩展。
* 注意在迭代时应该这样直接访问缓冲区但使用FastList.length字段。
*
* @tutorial
* for( var i = 0; i <= list.length; i++ )
* var item = list.buffer[i];
*/
export class FastList<T> {
/**
* 直接访问后备缓冲区。
* 不要使用buffer.Length! 使用FastList.length
*/
public buffer: T[];
/**
* 直接访问缓冲区内填充项的长度。不要改变。
*/
public length: number = 0;
constructor(size: number = 5) {
this.buffer = new Array(size);
}
/**
* 清空列表并清空缓冲区中的所有项目
*/
public clear() {
this.buffer.length = 0;
this.length = 0;
}
/**
* 和clear的工作原理一样只是它不会将缓冲区中的所有项目清空。
*/
public reset() {
this.length = 0;
}
/**
* 将该项目添加到列表中
* @param item
*/
public add(item: T) {
if (this.length == this.buffer.length)
this.buffer.length = Math.max(this.buffer.length << 1, 10);
this.buffer[this.length++] = item;
}
/**
* 从列表中删除该项目
* @param item
*/
public remove(item: T){
let comp = EqualityComparer.default<T>();
for (let i = 0; i < this.length; ++i){
if (comp.equals(this.buffer[i], item)){
this.removeAt(i);
return;
}
}
}
/**
* 从列表中删除给定索引的项目。
* @param index
*/
public removeAt(index: number){
if (index >= this.length)
throw new Error("index超出范围");
this.length --;
this.buffer.removeAt(index);
}
/**
* 检查项目是否在FastList中
* @param item
*/
public contains(item: T){
let comp = EqualityComparer.default<T>();
for (let i = 0; i < this.length; ++ i){
if (comp.equals(this.buffer[i], item))
return true;
}
return false;
}
/**
* 如果缓冲区达到最大将分配更多的空间来容纳额外的ItemCount。
* @param additionalItemCount
*/
public ensureCapacity(additionalItemCount: number = 1){
if (this.length + additionalItemCount >= this.buffer.length)
this.buffer.length = Math.max(this.buffer.length << 1, this.length + additionalItemCount);
}
/**
* 添加数组中的所有项目
* @param array
*/
public addRange(array: T[]){
for (let item of array)
this.add(item);
}
/**
* 对缓冲区中的所有项目进行排序,长度不限。
*/
public sort(comparer: IComparer<T>){
this.buffer.sort(comparer.compare);
}
}
}