移除fastlist(经测试比Array并没有快)

This commit is contained in:
yhh
2020-12-14 11:34:23 +08:00
parent 59c8d456cb
commit c60fe13d71
16 changed files with 325 additions and 520 deletions

View File

@@ -23,15 +23,13 @@ module es {
export class TriggerListenerHelper {
public static getITriggerListener(entity: Entity, components: ITriggerListener[]){
for (let i = 0; i < entity.components._components.length; i++) {
let component = entity.components._components.buffer[i];
for (let component of entity.components._components) {
if (isITriggerListener(component)) {
components.push(component);
}
}
for (let i = 0; i < entity.components._componentsToAdd.length; i++) {
let component = entity.components._componentsToAdd[i];
for (let component of entity.components._componentsToAdd) {
if (isITriggerListener(component)) {
components.push(component);
}

View File

@@ -76,7 +76,7 @@ module es {
public setUpdateOrder(updateOrder: number){
if (this.updateOrder != updateOrder){
this.updateOrder = updateOrder;
Core.scene._sceneComponents.sort(this);
Core.scene._sceneComponents.sort(this.compare);
}
return this;

View File

@@ -32,7 +32,7 @@ module es {
/**
* 全局访问系统
*/
public _globalManagers: FastList<GlobalManager> = new FastList<GlobalManager>();
public _globalManagers: GlobalManager[] = [];
public _timerManager: TimerManager = new TimerManager();
public width: number;
public height: number;
@@ -98,7 +98,7 @@ module es {
* @param manager
*/
public static registerGlobalManager(manager: es.GlobalManager) {
this._instance._globalManagers.add(manager);
this._instance._globalManagers.push(manager);
manager.enabled = true;
}
@@ -107,7 +107,7 @@ module es {
* @param manager
*/
public static unregisterGlobalManager(manager: es.GlobalManager) {
this._instance._globalManagers.remove(manager);
new linq.List(this._instance._globalManagers).remove(manager);
manager.enabled = false;
}
@@ -117,8 +117,8 @@ module es {
*/
public static getGlobalManager<T extends es.GlobalManager>(type): T {
for (let i = 0; i < this._instance._globalManagers.length; i++) {
if (this._instance._globalManagers.buffer[i] instanceof type)
return this._instance._globalManagers.buffer[i] as T;
if (this._instance._globalManagers[i] instanceof type)
return this._instance._globalManagers[i] as T;
}
return null;
}
@@ -183,8 +183,8 @@ module es {
if (currentTime != null) Time.update(currentTime);
if (this._scene != null) {
for (let i = this._globalManagers.length - 1; i >= 0; i--) {
if (this._globalManagers.buffer[i].enabled)
this._globalManagers.buffer[i].update();
if (this._globalManagers[i].enabled)
this._globalManagers[i].update();
}
this._scene.update();

View File

@@ -35,6 +35,9 @@ module es {
* 指定应该调用这个entity update方法的频率。1表示每一帧2表示每一帧以此类推
*/
public updateInterval: number = 1;
/**
* 返回一个BitSet实例包含实体拥有的组件的位
*/
public componentBits: BitSet;
constructor(name: string) {

View File

@@ -10,7 +10,7 @@ module es {
*/
public readonly entityProcessors: EntityProcessorList;
public readonly _sceneComponents: FastList<SceneComponent> = new FastList<SceneComponent>();
public readonly _sceneComponents: SceneComponent[] = [];
public _didSceneBegin;
constructor() {
@@ -66,9 +66,9 @@ module es {
this.entities.removeAllEntities();
for (let i = 0; i < this._sceneComponents.length; i++) {
this._sceneComponents.buffer[i].onRemovedFromScene();
this._sceneComponents[i].onRemovedFromScene();
}
this._sceneComponents.clear();
this._sceneComponents.length = 0;
Physics.clear();
@@ -87,8 +87,8 @@ module es {
this.entities.updateLists();
for (let i = this._sceneComponents.length - 1; i >= 0; i--) {
if (this._sceneComponents.buffer[i].enabled)
this._sceneComponents.buffer[i].update();
if (this._sceneComponents[i].enabled)
this._sceneComponents[i].update();
}
// 更新我们的实体解析器
@@ -109,8 +109,8 @@ module es {
public addSceneComponent<T extends SceneComponent>(component: T): T {
component.scene = this;
component.onEnabled();
this._sceneComponents.add(component);
this._sceneComponents.sort(component);
this._sceneComponents.push(component);
this._sceneComponents.sort(component.compare);
return component;
}
@@ -120,7 +120,7 @@ module es {
*/
public getSceneComponent<T extends SceneComponent>(type) {
for (let i = 0; i < this._sceneComponents.length; i++) {
let component = this._sceneComponents.buffer[i];
let component = this._sceneComponents[i];
if (component instanceof type)
return component as T;
}
@@ -145,12 +145,12 @@ module es {
* @param component
*/
public removeSceneComponent(component: SceneComponent) {
if (!this._sceneComponents.contains(component)) {
if (!new linq.List(this._sceneComponents).contains(component)) {
console.warn(`SceneComponent${component}不在SceneComponents列表中!`);
return;
}
this._sceneComponents.remove(component);
new linq.List(this._sceneComponents).remove(component);
component.onRemovedFromScene();
}
@@ -168,7 +168,7 @@ module es {
* @param entity
*/
public addEntity(entity: Entity) {
if (this.entities.buffer.contains(entity))
if (new linq.List(this.entities.buffer).contains(entity))
console.warn(`您试图将同一实体添加到场景两次: ${entity}`);
this.entities.add(entity);
entity.scene = this;

View File

@@ -2,6 +2,9 @@
module es {
/**
* 基本实体处理系统。将其用作处理具有特定组件的许多实体的基础
*
* 按实体引用遍历实体订阅成员实体的系统
* 当你需要处理与Matcher相匹配的实体并且你更喜欢使用Entity的时候可以使用这个功能。
*/
export abstract class EntityProcessingSystem extends EntitySystem {
constructor(matcher: Matcher) {

View File

@@ -1,5 +1,8 @@
module es {
export class EntitySystem {
/**
* 追踪实体的子集,但不实现任何排序或迭代。
*/
export abstract class EntitySystem {
private _entities: Entity[] = [];
constructor(matcher?: Matcher) {

View File

@@ -10,11 +10,11 @@ module es {
/**
* 添加到实体的组件列表
*/
public _components: FastList<Component> = new FastList<Component>();
public _components: Component[] = [];
/**
* 所有需要更新的组件列表
*/
public _updatableComponents: FastList<IUpdatable> = new FastList<IUpdatable>();
public _updatableComponents: IUpdatable[] = [];
/**
* 添加到此框架的组件列表。用来对组件进行分组,这样我们就可以同时进行加工
*/
@@ -38,7 +38,7 @@ module es {
}
public get buffer() {
return this._components.buffer;
return this._components;
}
public markEntityListUnsorted() {
@@ -72,21 +72,19 @@ module es {
this.handleRemove(this._components[i]);
}
this._components.clear();
this._updatableComponents.clear();
this._components.length = 0;
this._updatableComponents.length = 0;
this._componentsToAdd.length = 0;
this._componentsToRemove.length = 0;
}
public deregisterAllComponents() {
for (let i = 0; i < this._components.length; i++) {
let component = this._components.buffer[i];
for (let component of this._components) {
if (!component) continue;
// 处理IUpdatable
if (isIUpdatable(component))
this._updatableComponents.remove(component);
new linq.List(this._updatableComponents).remove(component);
if (Core.entitySystemsEnabled) {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)), false);
@@ -96,11 +94,9 @@ module es {
}
public registerAllComponents() {
for (let i = 0; i < this._components.length; i++) {
let component = this._components.buffer[i];
for (let component of this._components) {
if (isIUpdatable(component))
this._updatableComponents.add(component);
this._updatableComponents.push(component);
if (Core.entitySystemsEnabled) {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
@@ -116,7 +112,7 @@ module es {
if (this._componentsToRemove.length > 0) {
for (let i = 0; i < this._componentsToRemove.length; i++) {
this.handleRemove(this._componentsToRemove[i]);
this._components.remove(this._componentsToRemove[i]);
new linq.List(this._components).remove(this._componentsToRemove[i]);
}
this._componentsToRemove.length = 0;
@@ -127,14 +123,14 @@ module es {
let component = this._componentsToAdd[i];
if (isIUpdatable(component))
this._updatableComponents.add(component);
this._updatableComponents.push(component);
if (Core.entitySystemsEnabled) {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
}
this._components.add(component);
this._components.push(component);
this._tempBufferList.push(component);
}
@@ -157,7 +153,7 @@ module es {
}
if (this._isComponentListUnsorted) {
this._updatableComponents.sort(ComponentList.compareUpdatableOrder);
this._updatableComponents.sort(ComponentList.compareUpdatableOrder.compare);
this._isComponentListUnsorted = false;
}
}
@@ -166,7 +162,7 @@ module es {
if (!component) return;
if (isIUpdatable(component))
this._updatableComponents.remove(component);
new linq.List(this._updatableComponents).remove(component);
if (Core.entitySystemsEnabled) {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)), false);
@@ -186,16 +182,14 @@ module es {
* @param onlyReturnInitializedComponents
*/
public getComponent<T extends Component>(type, onlyReturnInitializedComponents: boolean): T {
for (let i = 0; i < this._components.length; i++) {
let component = this._components.buffer[i];
for (let component of this._components) {
if (component instanceof type)
return component as T;
}
// 我们可以选择检查挂起的组件以防addComponent和getComponent在同一个框架中被调用
if (!onlyReturnInitializedComponents) {
for (let i = 0; i < this._componentsToAdd.length; i++) {
let component = this._componentsToAdd[i];
for (let component of this._componentsToAdd) {
if (component instanceof type)
return component as T;
}
@@ -213,16 +207,14 @@ module es {
if (!components)
components = [];
for (let i = 0; i < this._components.length; i++) {
let component = this._components.buffer[i];
for (let component of this._components) {
if (component instanceof typeName) {
components.push(component);
}
}
// 我们还检查了待处理的组件以防在同一帧中调用addComponent和getComponent
for (let i = 0; i < this._componentsToAdd.length; i++) {
let component = this._componentsToAdd[i];
for (let component of this._componentsToAdd) {
if (component instanceof typeName) {
components.push(component);
}
@@ -234,15 +226,15 @@ module es {
public update() {
this.updateLists();
for (let i = 0; i < this._updatableComponents.length; i++) {
if (this._updatableComponents.buffer[i].enabled)
this._updatableComponents.buffer[i].update();
if (this._updatableComponents[i].enabled)
this._updatableComponents[i].update();
}
}
public onEntityTransformChanged(comp: transform.Component) {
for (let i = 0; i < this._components.length; i++) {
if (this._components.buffer[i].enabled)
this._components.buffer[i].onEntityTransformChanged(comp);
if (this._components[i].enabled)
this._components[i].onEntityTransformChanged(comp);
}
for (let i = 0; i < this._componentsToAdd.length; i++) {
@@ -253,12 +245,12 @@ module es {
public onEntityEnabled() {
for (let i = 0; i < this._components.length; i++)
this._components.buffer[i].onEnabled();
this._components[i].onEnabled();
}
public onEntityDisabled() {
for (let i = 0; i < this._components.length; i++)
this._components.buffer[i].onDisabled();
this._components[i].onDisabled();
}
}
}

View File

@@ -4,7 +4,7 @@ module es {
/**
* 场景中添加的实体列表
*/
public _entities: FastList<Entity> = new FastList<Entity>();
public _entities: Entity[] = [];
/**
* 本帧添加的实体列表。用于对实体进行分组,以便我们可以同时处理它们
*/
@@ -84,12 +84,12 @@ module es {
this.updateLists();
for (let i = 0; i < this._entities.length; i++) {
this._entities.buffer[i]._isDestroyed = true;
this._entities.buffer[i].onRemovedFromScene();
this._entities.buffer[i].scene = null;
this._entities[i]._isDestroyed = true;
this._entities[i].onRemovedFromScene();
this._entities[i].scene = null;
}
this._entities.clear();
this._entities.length = 0;
this._entityDict.clear();
}
@@ -98,7 +98,7 @@ module es {
* @param entity
*/
public contains(entity: Entity): boolean {
return this._entities.contains(entity) || this._entitiesToAdded.contains(entity);
return new linq.List(this._entities).contains(entity) || this._entitiesToAdded.contains(entity);
}
public getTagList(tag: number) {
@@ -127,8 +127,7 @@ module es {
}
public update() {
for (let i = 0; i < this._entities.length; i++) {
let entity = this._entities.buffer[i];
for (let entity of this._entities) {
if (entity.enabled && (entity.updateInterval == 1 || Time.frameCount % entity.updateInterval == 0))
entity.update();
}
@@ -141,7 +140,7 @@ module es {
this.removeFromTagList(entity);
// 处理常规实体列表
this._entities.remove(entity);
new linq.List(this._entities).remove(entity);
entity.onRemovedFromScene();
entity.scene = null;
@@ -153,7 +152,7 @@ module es {
if (this._entitiesToAdded.getCount() > 0) {
this._entitiesToAdded.toArray().forEach(entity => {
this._entities.add(entity);
this._entities.push(entity);
entity.scene = this.scene;
this.addToTagList(entity);
@@ -171,7 +170,7 @@ module es {
}
if (this._isEntityListUnsorted) {
this._entities.sort(Entity.entityComparer);
this._entities.sort(Entity.entityComparer.compare);
this._isEntityListUnsorted = false;
}
@@ -189,8 +188,8 @@ module es {
*/
public findEntity(name: string) {
for (let i = 0; i < this._entities.length; i++) {
if (this._entities.buffer[i].name == name)
return this._entities.buffer[i];
if (this._entities[i].name == name)
return this._entities[i];
}
for (let i = 0; i < this._entitiesToAdded.getCount(); i ++){
@@ -226,8 +225,8 @@ module es {
public entitiesOfType<T extends Entity>(type): T[] {
let list = ListPool.obtain<T>();
for (let i = 0; i < this._entities.length; i++) {
if (this._entities.buffer[i] instanceof type)
list.push(this._entities.buffer[i] as T);
if (this._entities[i] instanceof type)
list.push(this._entities[i] as T);
}
for (let i = 0; i < this._entitiesToAdded.getCount(); i ++){
@@ -246,8 +245,8 @@ module es {
*/
public findComponentOfType<T extends Component>(type): T {
for (let i = 0; i < this._entities.length; i++) {
if (this._entities.buffer[i].enabled) {
let comp = this._entities.buffer[i].getComponent<T>(type);
if (this._entities[i].enabled) {
let comp = this._entities[i].getComponent<T>(type);
if (comp)
return comp;
}
@@ -273,8 +272,8 @@ module es {
public findComponentsOfType<T extends Component>(type): T[] {
let comps = ListPool.obtain<T>();
for (let i = 0; i < this._entities.length; i++) {
if (this._entities.buffer[i].enabled)
this._entities.buffer[i].getComponents(type, comps);
if (this._entities[i].enabled)
this._entities[i].getComponents(type, comps);
}
for (let i = 0; i < this._entitiesToAdded.getCount(); i++) {