优化triggerListener与ArrayUtils

This commit is contained in:
yhh
2020-11-26 17:26:49 +08:00
parent de3f7bff60
commit ae2cfdafdd
15 changed files with 418 additions and 261 deletions

View File

@@ -20,4 +20,26 @@ module es {
*/
onTriggerExit(other: Collider, local: Collider);
}
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];
if (isITriggerListener(component)) {
components.push(component);
}
}
for (let i = 0; i < entity.components._componentsToAdd.length; i++) {
let component = entity.components._componentsToAdd[i];
if (isITriggerListener(component)) {
components.push(component);
}
}
return components;
}
}
export var isITriggerListener = (props: any): props is ITriggerListener => typeof (props as ITriggerListener)['onTriggerEnter'] !== 'undefined';
}

View File

@@ -38,7 +38,7 @@ module es {
bounds.y += motion.y;
let neighbors = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers.value);
for (let j = 0; j < neighbors.length; j++) {
for (let j = 0; j < neighbors.size; j++) {
let neighbor = neighbors[j];
// 不检测触发器
if (neighbor.isTrigger)

View File

@@ -28,7 +28,8 @@ module es {
// 获取任何可能在新位置发生碰撞的东西
let neighbors = Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers.value);
for (let neighbor of neighbors){
for (let i = 0; i < neighbors.size; i ++){
let neighbor = neighbors[i];
if (this._collider.overlaps(neighbor) && neighbor.enabled){
didCollide = true;
this.notifyTriggerListeners(this._collider, neighbor);
@@ -40,14 +41,14 @@ module es {
private notifyTriggerListeners(self: Collider, other: Collider) {
// 通知我们重叠的碰撞器实体上的任何侦听器
other.entity.getComponents("ITriggerListener", this._tempTriggerList);
TriggerListenerHelper.getITriggerListener(other.entity, this._tempTriggerList);
for (let i = 0; i < this._tempTriggerList.length; i++) {
this._tempTriggerList[i].onTriggerEnter(self, other);
}
this._tempTriggerList.length = 0;
// 通知此实体上的任何侦听器
this.entity.getComponents("ITriggerListener", this._tempTriggerList);
TriggerListenerHelper.getITriggerListener(this.entity, this._tempTriggerList);
for (let i = 0; i < this._tempTriggerList.length; i++) {
this._tempTriggerList[i].onTriggerEnter(other, self);
}

View File

@@ -204,7 +204,8 @@ module es {
}
protected async update() {
protected async update(currentTime?: number) {
if (currentTime != null) Time.update(currentTime);
if (this._scene != null) {
for (let i = this._globalManagers.length - 1; i >= 0; i--) {
if (this._globalManagers[i].enabled)

View File

@@ -335,7 +335,7 @@ module es {
* @param type
*/
public getOrCreateComponent<T extends Component>(type: T) {
let comp = this.components.getComponent<T>(type, true);
let comp = this.components.getComponent<T>(TypeUtils.getType(type), true);
if (!comp) {
comp = this.addComponent<T>(type);
}

View File

@@ -87,7 +87,7 @@ module es {
this._updatableComponents.remove(component);
if (Core.entitySystemsEnabled) {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component["__proto__"]["constructor"]), false);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
}
}
@@ -101,7 +101,7 @@ module es {
this._updatableComponents.add(component);
if (Core.entitySystemsEnabled) {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component["__proto__"]["constructor"]));
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
}
}
@@ -128,7 +128,7 @@ module es {
this._updatableComponents.add(component);
if (Core.entitySystemsEnabled) {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component["__proto__"]["constructor"]));
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)));
this._entity.scene.entityProcessors.onComponentAdded(this._entity);
}
@@ -167,7 +167,7 @@ module es {
this._updatableComponents.remove(component);
if (Core.entitySystemsEnabled) {
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component["__proto__"]["constructor"]), false);
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(TypeUtils.getType(component)), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
}