新增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,13 +1,10 @@
module es {
export class CameraInset {
public left: number = 0;
public right: number = 0;
public top: number = 0;
public bottom: number = 0;
export interface CameraInset {
left: number, right: number, top: number, bottom: number
}
export class Camera extends Component {
public _inset: CameraInset = new CameraInset();
public _inset: CameraInset = {left: 0, right: 0, top: 0, bottom: 0};
public _areMatrixedDirty: boolean = true;
public _areBoundsDirty: boolean = true;
public _isProjectionMatrixDirty = true;
@@ -209,11 +206,7 @@ module es {
* @param bottom
*/
public setInset(left: number, right: number, top: number, bottom: number): Camera {
this._inset = new CameraInset();
this._inset.left = left;
this._inset.right = right;
this._inset.top = top;
this._inset.bottom = bottom;
this._inset = {left: left, right: right, top: top, bottom: bottom};
this._areBoundsDirty = true;
return this;
}

View File

@@ -1,8 +1,14 @@
module es {
export interface IUpdatable {
enabled: boolean;
updateOrder: number;
update();
}
/**
*
*/
export class IUpdatableComparer {
export class IUpdatableComparer implements IComparer<IUpdatable> {
public compare(a: Component, b: Component) {
return a.updateOrder - b.updateOrder;
}

View File

@@ -155,7 +155,7 @@ module es {
}
public async draw() {
this.startDebugDraw(Time.deltaTime);
// this.startDebugDraw(Time.deltaTime);
if (this._sceneTransition) {
this._sceneTransition.preRender();
@@ -182,7 +182,7 @@ module es {
this._scene.postRender();
}
this.endDebugDraw();
// this.endDebugDraw();
}
public startDebugUpdate() {

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);
}
}
}

View File

@@ -1,6 +1,6 @@
module es {
/** 2d 向量 */
export class Vector2 {
export class Vector2 implements IEquatable<Vector2> {
public x: number = 0;
public y: number = 0;
@@ -111,7 +111,7 @@ module es {
}
/**
*
* 将指定的值限制在一个范围内
* @param value1
* @param min
* @param max
@@ -122,17 +122,18 @@ module es {
}
/**
* 包含指定向量的线性插值
* 创建一个新的Vector2其中包含指定向量的线性插值
* @param value1 第一个向量
* @param value2 第二个向量
* @param amount 权值(0.01.0之间)
* @param amount 权值(0.0-1.0之间)
* @returns 指定向量的线性插值结果
*/
public static lerp(value1: Vector2, value2: Vector2, amount: number) {
return new Vector2(MathHelper.lerp(value1.x, value2.x, amount), MathHelper.lerp(value1.y, value2.y, amount));
}
/**
*
* 创建一个新的Vector2该Vector2包含了通过指定的Matrix进行的二维向量变换。
* @param position
* @param matrix
*/
@@ -145,8 +146,9 @@ module es {
* 返回两个向量之间的距离
* @param value1
* @param value2
* @returns 两个向量之间的距离
*/
public static distance(value1: Vector2, value2: Vector2) {
public static distance(value1: Vector2, value2: Vector2): number {
let v1 = value1.x - value2.x, v2 = value1.y - value2.y;
return Math.sqrt((v1 * v1) + (v2 * v2));
}
@@ -163,15 +165,15 @@ module es {
}
/**
* 矢量反演的结果
* 创建一个包含指定向量反转的新Vector2
* @param value
* @returns 矢量反演的结果
*/
public static negate(value: Vector2) {
let result: Vector2 = new Vector2();
result.x = -value.x;
result.y = -value.y;
value.x = -value.x;
value.y = -value.y;
return result;
return value;
}
/**
@@ -205,8 +207,9 @@ module es {
}
/**
*
* @param value
* 从当前Vector2减去一个Vector2
* @param value 要减去的Vector2
* @returns 当前Vector2
*/
public subtract(value: Vector2) {
this.x -= value.x;
@@ -214,7 +217,9 @@ module es {
return this;
}
/** 变成一个方向相同的单位向量 */
/**
* 将这个Vector2变成一个方向相同的单位向量
*/
public normalize() {
let val = 1 / Math.sqrt((this.x * this.x) + (this.y * this.y));
this.x *= val;
@@ -227,19 +232,31 @@ module es {
}
/**
* 返回其长度的平方
* 返回该Vector2的平方长度
* @returns 这个Vector2的平方长度
*/
public lengthSquared(): number {
return (this.x * this.x) + (this.y * this.y);
}
/** 对x和y值四舍五入 */
/**
* 四舍五入X和Y值
*/
public round(): Vector2 {
return new Vector2(Math.round(this.x), Math.round(this.y));
}
public equals(other: Vector2) {
return other.x == this.x && other.y == this.y;
/**
* 比较当前实例是否等于指定的对象
* @param other 要比较的对象
* @returns 如果实例相同true 否则false
*/
public equals(other: Vector2 | object): boolean {
if (other instanceof Vector2){
return other.x == this.x && other.y == this.y;
}
return false;
}
}
}

View File

@@ -0,0 +1,17 @@
module es {
export class EqualityComparer<T> implements IEqualityComparer {
public static default<T>(){
return new EqualityComparer<T>();
}
protected constructor(){ }
public equals(x: T, y: T): boolean{
if (typeof x["equals"] == 'function'){
return x["equals"](y);
} else {
return x === y;
}
}
}
}

View File

@@ -0,0 +1,5 @@
module es {
export interface IComparer<T>{
compare(x: T, y: T): number;
}
}

View File

@@ -0,0 +1,5 @@
module es {
export interface IEqualityComparer {
equals(x: any, y: any): boolean;
}
}

View File

@@ -0,0 +1,8 @@
module es {
/**
* 实现该接口用于判定两个对象是否相等的快速接口
*/
export interface IEquatable<T> {
equals(other: T): boolean;
}
}

View File

@@ -2,7 +2,7 @@ module es {
/**
* 用于管理一对对象的简单DTO
*/
export class Pair<T> {
export class Pair<T> implements IEquatable<Pair<T>> {
public first: T;
public second: T;
@@ -15,7 +15,8 @@ module es {
this.first = this.second = null;
}
public equals(other: Pair<T>) {
public equals(other: Pair<T>): boolean {
// 这两种方法在功能上应该是等价的
return this.first == other.first && this.second == other.second;
}
}