移动部分类模块至es

优化框架
This commit is contained in:
YHH
2020-07-22 23:30:31 +08:00
parent 5b8f414a45
commit 15c0844e29
18 changed files with 1245 additions and 674 deletions

View File

@@ -1,5 +1,10 @@
///<reference path="../Components/IUpdatableComparer.ts" />
module es {
export class ComponentList {
/**
* 组件列表的全局updateOrder排序
*/
public static compareUpdatableOrder: IUpdatableComparer = new IUpdatableComparer();
public _entity: Entity;
/**
@@ -15,6 +20,10 @@ module es {
*/
public _componentsToRemove: Component[] = [];
public _tempBufferList: Component[] = [];
/**
* 用于确定是否需要对该框架中的组件进行排序的标志
*/
public _isComponentListUnsorted: boolean;
constructor(entity: Entity) {
this._entity = entity;
@@ -28,13 +37,17 @@ module es {
return this._components;
}
public markEntityListUnsorted(){
this._isComponentListUnsorted = true;
}
public add(component: Component) {
this._componentsToAdd.push(component);
}
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(`You are trying to remove a Component (${component}) that you already removed`);
// 这可能不是一个活动的组件,所以我们必须注意它是否还没有被处理,它可能正在同一帧中被删除
if (this._componentsToAdd.contains(component)) {
@@ -111,6 +124,7 @@ module es {
// 在调用onAddedToEntity之前清除以防添加更多组件
this._componentsToAdd.length = 0;
this._isComponentListUnsorted = true;
// 现在所有的组件都添加到了场景中我们再次循环并调用onAddedToEntity/onEnabled
for (let i = 0; i < this._tempBufferList.length; i++) {
@@ -125,6 +139,11 @@ module es {
this._tempBufferList.length = 0;
}
if (this._isComponentListUnsorted){
this._components.sort(ComponentList.compareUpdatableOrder.compare);
this._isComponentListUnsorted = false;
}
}
public handleRemove(component: Component) {
@@ -216,7 +235,7 @@ module es {
}
}
public onEntityTransformChanged(comp: Transform.Component) {
public onEntityTransformChanged(comp: transform.Component) {
for (let i = 0; i < this._components.length; i++) {
if (this._components[i].enabled)
this._components[i].onEntityTransformChanged(comp);

View File

@@ -1,22 +1,101 @@
class RenderableComponentList {
private _components: IRenderable[] = [];
public get count(){
return this._components.length;
}
///<reference path="../../Graphics/Renderers/IRenderable.ts" />
module es {
export class RenderableComponentList {
/**
* IRenderable列表的全局updatePrder排序
*/
public static compareUpdatableOrder = new RenderableComparer();
/**
* 添加到实体的组件列表
*/
public _components: IRenderable[] = [];
/**
* 通过渲染层跟踪组件,便于检索
*/
public _componentsByRenderLayer: Map<number, IRenderable[]> = new Map<number, IRenderable[]>();
public _unsortedRenderLayers: number[] = [];
public _componentsNeedSort: boolean = true;
public get buffer(){
return this._components;
}
public get count() {
return this._components.length;
}
public add(component: IRenderable){
this._components.push(component);
}
public get buffer() {
return this._components;
}
public remove(component: IRenderable){
this._components.remove(component);
}
public add(component: IRenderable) {
this._components.push(component);
this.addToRenderLayerList(component, component.renderLayer);
}
public updateList(){
public remove(component: IRenderable) {
this._components.remove(component);
this._componentsByRenderLayer.get(component.renderLayer).remove(component);
}
public updateRenderableRenderLayer(component: IRenderable, oldRenderLayer: number, newRenderLayer: number) {
// 需要注意的是如果渲染层在组件update之前发生了改变
if (this._componentsByRenderLayer.has(oldRenderLayer) && this._componentsByRenderLayer.get(oldRenderLayer).contains(component)) {
this._componentsByRenderLayer.get(oldRenderLayer).remove(component);
this.addToRenderLayerList(component, newRenderLayer);
}
}
/**
* 将渲染层排序标志弄脏,让所有组件重新排序
* @param renderLayer
*/
public setRenderLayerNeedsComponentSort(renderLayer: number) {
if (!this._unsortedRenderLayers.contains(renderLayer))
this._unsortedRenderLayers.push(renderLayer);
this._componentsNeedSort = true;
}
public setNeedsComponentSort() {
this._componentsNeedSort = true;
}
public addToRenderLayerList(component: IRenderable, renderLayer: number) {
let list = this.componentsWithRenderLayer(renderLayer);
if (!list.contains(component)) {
console.warn("Component renderLayer list already contains this component");
return;
}
list.push(component);
if (!this._unsortedRenderLayers.contains(renderLayer))
this._unsortedRenderLayers.push(renderLayer);
this._componentsNeedSort = true;
}
/**
* 使用给定的渲染层获取所有组件。组件列表是预先排序的
* @param renderLayer
*/
public componentsWithRenderLayer(renderLayer: number): IRenderable[] {
if (!this._componentsByRenderLayer.get(renderLayer)) {
this._componentsByRenderLayer.set(renderLayer, []);
}
return this._componentsByRenderLayer.get(renderLayer);
}
public updateList() {
if (this._componentsNeedSort) {
this._components.sort(RenderableComponentList.compareUpdatableOrder.compare);
this._componentsNeedSort = false;
}
if (this._unsortedRenderLayers.length > 0) {
for (let i = 0, count = this._unsortedRenderLayers.length; i < count; i++) {
let renderLayerComponents = this._componentsByRenderLayer.get(this._unsortedRenderLayers[i]);
if (renderLayerComponents) {
renderLayerComponents.sort(RenderableComponentList.compareUpdatableOrder.compare);
}
}
this._unsortedRenderLayers.length = 0;
}
}
}
}
}