优化框架性能,Time支持传入引擎dt
This commit is contained in:
@@ -73,40 +73,48 @@ module es {
|
||||
* 立即从组件列表中删除所有组件
|
||||
*/
|
||||
public removeAllComponents() {
|
||||
for (let component of this._components) {
|
||||
this.handleRemove(component);
|
||||
}
|
||||
if (this._components.length > 0) {
|
||||
for (let i = 0, s = this._components.length; i < s; ++ i) {
|
||||
this.handleRemove(this._components[i]);
|
||||
}
|
||||
|
||||
this.componentsByType.clear();
|
||||
this.componentsToAddByType.clear();
|
||||
this._components.length = 0;
|
||||
this._updatableComponents.length = 0;
|
||||
this._componentsToAdd = {};
|
||||
this._componentsToRemove = {};
|
||||
this._componentsToAddList.length = 0;
|
||||
this._componentsToRemoveList.length = 0;
|
||||
this.componentsByType.clear();
|
||||
this.componentsToAddByType.clear();
|
||||
this._components.length = 0;
|
||||
this._updatableComponents.length = 0;
|
||||
this._componentsToAdd = {};
|
||||
this._componentsToRemove = {};
|
||||
this._componentsToAddList.length = 0;
|
||||
this._componentsToRemoveList.length = 0;
|
||||
}
|
||||
}
|
||||
|
||||
public deregisterAllComponents() {
|
||||
for (let component of this._components) {
|
||||
if (!component) continue;
|
||||
|
||||
// 处理IUpdatable
|
||||
if (isIUpdatable(component))
|
||||
new es.List(this._updatableComponents).remove(component);
|
||||
|
||||
this.decreaseBits(component);
|
||||
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
|
||||
if (this._components.length > 0) {
|
||||
for (let i = 0, s = this._components.length; i < s; ++ i) {
|
||||
let component = this._components[i];
|
||||
if (!component) continue;
|
||||
|
||||
// 处理IUpdatable
|
||||
if (isIUpdatable(component))
|
||||
new es.List(this._updatableComponents).remove(component);
|
||||
|
||||
this.decreaseBits(component);
|
||||
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public registerAllComponents() {
|
||||
for (let component of this._components) {
|
||||
if (isIUpdatable(component))
|
||||
this._updatableComponents.push(component);
|
||||
|
||||
this.addBits(component);
|
||||
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
|
||||
if (this._components.length > 0) {
|
||||
for (let i = 0, s = this._components.length; i < s; ++ i) {
|
||||
let component = this._components[i];
|
||||
if (isIUpdatable(component))
|
||||
this._updatableComponents.push(component);
|
||||
|
||||
this.addBits(component);
|
||||
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -275,25 +283,35 @@ module es {
|
||||
}
|
||||
|
||||
public onEntityTransformChanged(comp: transform.Component) {
|
||||
for (let component of this._components) {
|
||||
if (component.enabled)
|
||||
component.onEntityTransformChanged(comp);
|
||||
if (this._components.length > 0 ){
|
||||
for (let i = 0, s = this._components.length; i < s; ++ i) {
|
||||
let component = this._components[i];
|
||||
if (component.enabled)
|
||||
component.onEntityTransformChanged(comp);
|
||||
}
|
||||
}
|
||||
|
||||
for (let component of this._componentsToAddList) {
|
||||
if (component.enabled)
|
||||
component.onEntityTransformChanged(comp);
|
||||
|
||||
if (this._componentsToAddList.length > 0) {
|
||||
for (let i = 0, s = this._componentsToAddList.length; i < s; ++ i) {
|
||||
let component = this._componentsToAddList[i];
|
||||
if (component.enabled)
|
||||
component.onEntityTransformChanged(comp);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public onEntityEnabled() {
|
||||
for (let component of this._components)
|
||||
component.onEnabled();
|
||||
if (this._components.length > 0) {
|
||||
for (let i = 0, s = this._components.length; i < s; i ++)
|
||||
this._components[i].onEnabled();
|
||||
}
|
||||
}
|
||||
|
||||
public onEntityDisabled() {
|
||||
for (let component of this._components)
|
||||
component.onDisabled();
|
||||
if (this._components.length > 0) {
|
||||
for (let i = 0, s = this._components.length; i < s; i ++)
|
||||
this._components[i].onDisabled();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -179,16 +179,20 @@ module es {
|
||||
* @param name
|
||||
*/
|
||||
public findEntity(name: string) {
|
||||
for (let i = 0, s = this._entities.length; i < s; ++ i) {
|
||||
let entity = this._entities[i];
|
||||
if (entity[i].name == name)
|
||||
return entity;
|
||||
if (this._entities.length > 0) {
|
||||
for (let i = 0, s = this._entities.length; i < s; ++ i) {
|
||||
let entity = this._entities[i];
|
||||
if (entity.name == name)
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0, s = this._entitiesToAddedList.length; i < s; ++ i) {
|
||||
let entity = this._entitiesToAddedList[i];
|
||||
if (entity.name == name)
|
||||
return entity;
|
||||
|
||||
if (this._entitiesToAddedList.length > 0) {
|
||||
for (let i = 0, s = this._entitiesToAddedList.length; i < s; ++ i) {
|
||||
let entity = this._entitiesToAddedList[i];
|
||||
if (entity.name == name)
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -200,12 +204,14 @@ module es {
|
||||
* @returns
|
||||
*/
|
||||
public findEntityById(id: number) {
|
||||
for (let i = 0, s = this._entities.length; i < s; ++ i) {
|
||||
let entity = this._entities[i];
|
||||
if (entity.id == id)
|
||||
return entity;
|
||||
if (this._entities.length > 0) {
|
||||
for (let i = 0, s = this._entities.length; i < s; ++ i) {
|
||||
let entity = this._entities[i];
|
||||
if (entity.id == id)
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return this._entitiesToAdded[id];
|
||||
}
|
||||
|
||||
@@ -218,10 +224,12 @@ module es {
|
||||
let list = this.getTagList(tag);
|
||||
|
||||
let returnList = ListPool.obtain<Entity>();
|
||||
for (let entity of list) {
|
||||
returnList.push(entity);
|
||||
if (list.size > 0) {
|
||||
for (let entity of list) {
|
||||
returnList.push(entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return returnList;
|
||||
}
|
||||
|
||||
@@ -233,8 +241,10 @@ module es {
|
||||
public entityWithTag(tag: number) {
|
||||
let list = this.getTagList(tag);
|
||||
|
||||
for (let entity of list) {
|
||||
return entity;
|
||||
if (list.size > 0) {
|
||||
for (let entity of list) {
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -245,21 +255,25 @@ module es {
|
||||
* @param type
|
||||
*/
|
||||
public findComponentOfType<T extends Component>(type): T {
|
||||
for (let i = 0, s = this._entities.length; i < s; i++) {
|
||||
let entity = this._entities[i];
|
||||
if (entity.enabled) {
|
||||
let comp = entity.getComponent<T>(type);
|
||||
if (comp)
|
||||
return comp;
|
||||
if (this._entities.length > 0 ){
|
||||
for (let i = 0, s = this._entities.length; i < s; i++) {
|
||||
let entity = this._entities[i];
|
||||
if (entity.enabled) {
|
||||
let comp = entity.getComponent<T>(type);
|
||||
if (comp)
|
||||
return comp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let i = 0; i < this._entitiesToAddedList.length; i++) {
|
||||
let entity = this._entitiesToAddedList[i];
|
||||
if (entity.enabled) {
|
||||
let comp = entity.getComponent<T>(type);
|
||||
if (comp)
|
||||
return comp;
|
||||
|
||||
if (this._entitiesToAddedList.length > 0) {
|
||||
for (let i = 0; i < this._entitiesToAddedList.length; i++) {
|
||||
let entity = this._entitiesToAddedList[i];
|
||||
if (entity.enabled) {
|
||||
let comp = entity.getComponent<T>(type);
|
||||
if (comp)
|
||||
return comp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -273,14 +287,20 @@ 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[i].enabled)
|
||||
this._entities[i].getComponents(type, comps);
|
||||
if (this._entities.length > 0) {
|
||||
for (let i = 0, s = this._entities.length; i < s; i++) {
|
||||
let entity = this._entities[i];
|
||||
if (entity.enabled)
|
||||
entity.getComponents(type, comps);
|
||||
}
|
||||
}
|
||||
|
||||
for (let entity of this._entitiesToAddedList) {
|
||||
if (entity.enabled)
|
||||
entity.getComponents(type, comps);
|
||||
if (this._entitiesToAddedList.length > 0) {
|
||||
for (let i = 0, s = this._entitiesToAddedList.length; i < s; i ++) {
|
||||
let entity = this._entitiesToAddedList[i];
|
||||
if (entity.enabled)
|
||||
entity.getComponents(type, comps);
|
||||
}
|
||||
}
|
||||
|
||||
return comps;
|
||||
@@ -291,41 +311,51 @@ module es {
|
||||
* @param types
|
||||
* @returns
|
||||
*/
|
||||
public findEntitesOfComponent(...types): Entity[] {
|
||||
public findEntitesOfComponent(...types: any[]): Entity[] {
|
||||
let entities = [];
|
||||
for (let i = 0; i < this._entities.length; i++) {
|
||||
if (this._entities[i].enabled) {
|
||||
let meet = true;
|
||||
for (let type of types) {
|
||||
let hasComp = this._entities[i].hasComponent(type);
|
||||
if (!hasComp) {
|
||||
meet = false;
|
||||
break;
|
||||
if (this._entities.length > 0) {
|
||||
for (let i = 0, s = this._entities.length; i < s; i++) {
|
||||
if (this._entities[i].enabled) {
|
||||
let meet = true;
|
||||
if (types.length > 0)
|
||||
for (let t = 0, ts = types.length; t < ts; t ++) {
|
||||
let type = types[t];
|
||||
let hasComp = this._entities[i].hasComponent(type);
|
||||
if (!hasComp) {
|
||||
meet = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (meet) {
|
||||
entities.push(this._entities[i]);
|
||||
}
|
||||
}
|
||||
|
||||
if (meet) {
|
||||
entities.push(this._entities[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (let entity of this._entitiesToAddedList) {
|
||||
if (entity.enabled) {
|
||||
let meet = true;
|
||||
for (let type of types) {
|
||||
let hasComp = entity.hasComponent(type);
|
||||
if (!hasComp) {
|
||||
meet = false;
|
||||
break;
|
||||
|
||||
if (this._entitiesToAddedList.length > 0) {
|
||||
for (let i = 0, s = this._entitiesToAddedList.length; i < s; i ++) {
|
||||
let entity = this._entitiesToAddedList[i];
|
||||
if (entity.enabled) {
|
||||
let meet = true;
|
||||
if (types.length > 0)
|
||||
for (let t = 0, ts = types.length; t < ts; t ++) {
|
||||
let type = types[t];
|
||||
let hasComp = entity.hasComponent(type);
|
||||
if (!hasComp) {
|
||||
meet = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (meet) {
|
||||
entities.push(entity);
|
||||
}
|
||||
}
|
||||
|
||||
if (meet) {
|
||||
entities.push(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return entities;
|
||||
}
|
||||
|
||||
@@ -52,13 +52,13 @@ module es {
|
||||
|
||||
}
|
||||
|
||||
public getProcessor<T extends EntitySystem>(): T {
|
||||
public getProcessor<T extends EntitySystem>(type: new (...args: any[]) => T): T {
|
||||
if (this._processors.length == 0)
|
||||
return null;
|
||||
|
||||
for (let i = 0, s = this._processors.length; i < s; ++ i) {
|
||||
let processor = this._processors[i];
|
||||
if (processor instanceof EntitySystem)
|
||||
if (processor instanceof type)
|
||||
return processor as T;
|
||||
}
|
||||
|
||||
|
||||
@@ -26,22 +26,25 @@ module es {
|
||||
|
||||
public isInterested(components: Bits) {
|
||||
if (this.allSet.length != 0) {
|
||||
for (let s of this.allSet) {
|
||||
if (!components.get(ComponentTypeManager.getIndexFor(s)))
|
||||
for (let i = 0, s = this.allSet.length; i < s; ++ i) {
|
||||
let type = this.allSet[i];
|
||||
if (!components.get(ComponentTypeManager.getIndexFor(type)))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.exclusionSet.length != 0) {
|
||||
for (let s of this.exclusionSet) {
|
||||
if (components.get(ComponentTypeManager.getIndexFor(s)))
|
||||
for (let i = 0, s = this.exclusionSet.length; i < s; ++ i) {
|
||||
let type = this.exclusionSet[i];
|
||||
if (components.get(ComponentTypeManager.getIndexFor(type)))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (this.oneSet.length != 0) {
|
||||
for (let s of this.oneSet) {
|
||||
if (components.get(ComponentTypeManager.getIndexFor(s)))
|
||||
for (let i = 0, s = this.oneSet.length; i < s; ++ i) {
|
||||
let type = this.oneSet[i];
|
||||
if (components.get(ComponentTypeManager.getIndexFor(type)))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -51,7 +54,8 @@ module es {
|
||||
|
||||
public all(...types: any[]): Matcher {
|
||||
let t;
|
||||
for (t of types) {
|
||||
for (let i = 0, s = types.length; i < s; ++ i) {
|
||||
t = types[i];
|
||||
this.allSet.push(t);
|
||||
}
|
||||
|
||||
@@ -60,7 +64,8 @@ module es {
|
||||
|
||||
public exclude(...types: any[]) {
|
||||
let t;
|
||||
for (t of types) {
|
||||
for (let i = 0, s = types.length; i < s; ++ i) {
|
||||
t = types[i];
|
||||
this.exclusionSet.push(t);
|
||||
}
|
||||
|
||||
@@ -68,7 +73,8 @@ module es {
|
||||
}
|
||||
|
||||
public one(...types: any[]) {
|
||||
for (const t of types) {
|
||||
for (let i = 0, s = types.length; i < s; ++ i) {
|
||||
const t = types[i];
|
||||
this.oneSet.push(t);
|
||||
}
|
||||
|
||||
|
||||
@@ -18,12 +18,19 @@ module es {
|
||||
private static _lastTime = -1;
|
||||
|
||||
public static update(currentTime: number) {
|
||||
if (currentTime == -1)
|
||||
if (currentTime == -1) {
|
||||
currentTime = Date.now();
|
||||
}
|
||||
if (this._lastTime == -1)
|
||||
this._lastTime = currentTime;
|
||||
|
||||
let dt = (currentTime - this._lastTime) / 1000;
|
||||
let dt = 0;
|
||||
if (currentTime == -1) {
|
||||
dt = (currentTime - this._lastTime) / 1000;
|
||||
} else {
|
||||
dt = currentTime;
|
||||
}
|
||||
|
||||
if (dt > this.maxDeltaTime)
|
||||
dt = this.maxDeltaTime;
|
||||
this.totalTime += dt;
|
||||
|
||||
Reference in New Issue
Block a user