性能优化

This commit is contained in:
YHH
2022-03-07 16:00:48 +08:00
parent 3f7ef284fc
commit e207952786
9 changed files with 366 additions and 393 deletions

View File

@@ -2474,7 +2474,6 @@ var es;
es.Debug.warnIf(this._collider == null, "ArcadeRigidbody 没有 Collider。ArcadeRigidbody需要一个Collider!");
};
ArcadeRigidbody.prototype.update = function () {
var e_1, _a;
if (this.isImmovable || this._collider == null) {
this.velocity = es.Vector2.zero;
return;
@@ -2485,9 +2484,9 @@ var es;
var collisionResult = new es.CollisionResult();
// 捞取我们在新的位置上可能会碰撞到的任何东西
var neighbors = es.Physics.boxcastBroadphaseExcludingSelf(this._collider, this._collider.bounds, this._collider.collidesWithLayers.value);
try {
for (var neighbors_1 = __values(neighbors), neighbors_1_1 = neighbors_1.next(); !neighbors_1_1.done; neighbors_1_1 = neighbors_1.next()) {
var neighbor = neighbors_1_1.value;
if (neighbors.length > 0) {
for (var i = 0; i < neighbors.length; i++) {
var neighbor = neighbors[i];
if (!neighbor)
continue;
// 如果邻近的对撞机是同一个实体,则忽略它
@@ -2510,13 +2509,6 @@ var es;
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (neighbors_1_1 && !neighbors_1_1.done && (_a = neighbors_1.return)) _a.call(neighbors_1);
}
finally { if (e_1) throw e_1.error; }
}
};
/**
* 将两个重叠的刚体分开。也处理其中一个不可移动的情况
@@ -3004,22 +2996,14 @@ var es;
function TriggerListenerHelper() {
}
TriggerListenerHelper.getITriggerListener = function (entity, components) {
var e_2, _a;
try {
for (var _b = __values(entity.components._components), _c = _b.next(); !_c.done; _c = _b.next()) {
var component = _c.value;
if (entity.components._components.length > 0) {
for (var i = 0; i < entity.components._components.length; i++) {
var component = entity.components._components[i];
if (es.isITriggerListener(component)) {
components.push(component);
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_2) throw e_2.error; }
}
for (var i in entity.components._componentsToAdd) {
var component = entity.components._componentsToAdd[i];
if (es.isITriggerListener(component)) {
@@ -3056,8 +3040,8 @@ var es;
* @param collisionResult
*/
Mover.prototype.calculateMovement = function (motion, collisionResult) {
var e_3, _a;
var collider = null;
if (this.entity.components.buffer.length > 0)
for (var i = 0; i < this.entity.components.buffer.length; i++) {
var component = this.entity.components.buffer[i];
if (component instanceof es.Collider) {
@@ -3070,25 +3054,27 @@ var es;
}
// 移动所有的非触发碰撞器并获得最近的碰撞
var colliders = [];
if (this.entity.components.buffer.length > 0)
for (var i = 0; i < this.entity.components.buffer.length; i++) {
var component = this.entity.components.buffer[i];
if (component instanceof es.Collider) {
colliders.push(component);
}
}
if (colliders.length > 0) {
for (var i = 0; i < colliders.length; i++) {
var collider_1 = colliders[i];
// 不检测触发器 在我们移动后会重新访问它
if (collider_1.isTrigger)
continue;
// 获取我们在新位置可能发生碰撞的任何东西
var bounds = collider_1.bounds.clone();
var bounds = collider_1.bounds;
bounds.x += motion.x;
bounds.y += motion.y;
var neighbors = es.Physics.boxcastBroadphaseExcludingSelf(collider_1, bounds, collider_1.collidesWithLayers.value);
try {
for (var neighbors_2 = __values(neighbors), neighbors_2_1 = neighbors_2.next(); !neighbors_2_1.done; neighbors_2_1 = neighbors_2.next()) {
var neighbor = neighbors_2_1.value;
if (neighbors.length > 0) {
for (var i_1 = 0; i_1 < neighbors.length; i_1++) {
var neighbor = neighbors[i_1];
// 不检测触发器
if (neighbor.isTrigger)
return;
@@ -3106,12 +3092,6 @@ var es;
}
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (neighbors_2_1 && !neighbors_2_1.done && (_a = neighbors_2.return)) _a.call(neighbors_2);
}
finally { if (e_3) throw e_3.error; }
}
}
es.ListPool.free(es.Collider, colliders);
@@ -3412,19 +3392,18 @@ var es;
* @param result
*/
Collider.prototype.collidesWithAny = function (motion, result) {
var e_4, _a;
// 在我们的新位置上获取我们可能会碰到的任何东西
var colliderBounds = this.bounds.clone();
colliderBounds.x += motion.x;
colliderBounds.y += motion.y;
var neighbors = es.Physics.boxcastBroadphaseExcludingSelf(this, colliderBounds, this.collidesWithLayers.value);
// 更改形状位置,使其处于移动后的位置,以便我们检查是否有重叠
var oldPosition = this.shape.position.clone();
var oldPosition = this.shape.position;
this.shape.position = es.Vector2.add(this.shape.position, motion);
var didCollide = false;
try {
for (var neighbors_3 = __values(neighbors), neighbors_3_1 = neighbors_3.next(); !neighbors_3_1.done; neighbors_3_1 = neighbors_3.next()) {
var neighbor = neighbors_3_1.value;
if (neighbors.length > 0) {
for (var i = 0; i < neighbors.length; i++) {
var neighbor = neighbors[i];
if (neighbor.isTrigger)
continue;
if (this.collidesWithNonMotion(neighbor, result)) {
@@ -3434,15 +3413,8 @@ var es;
}
}
}
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (neighbors_3_1 && !neighbors_3_1.done && (_a = neighbors_3.return)) _a.call(neighbors_3);
}
finally { if (e_4) throw e_4.error; }
}
// 将形状位置返回到检查之前的位置
this.shape.position = oldPosition.clone();
this.shape.position = oldPosition;
return didCollide;
};
/**
@@ -3451,24 +3423,24 @@ var es;
*/
Collider.prototype.collidesWithAnyNonMotion = function (result) {
if (result === void 0) { result = new es.CollisionResult(); }
var e_5, _a;
var e_1, _a;
// 在我们的新位置上获取我们可能会碰到的任何东西
var neighbors = es.Physics.boxcastBroadphaseExcludingSelfNonRect(this, this.collidesWithLayers.value);
try {
for (var neighbors_4 = __values(neighbors), neighbors_4_1 = neighbors_4.next(); !neighbors_4_1.done; neighbors_4_1 = neighbors_4.next()) {
var neighbor = neighbors_4_1.value;
for (var neighbors_1 = __values(neighbors), neighbors_1_1 = neighbors_1.next(); !neighbors_1_1.done; neighbors_1_1 = neighbors_1.next()) {
var neighbor = neighbors_1_1.value;
if (neighbor.isTrigger)
continue;
if (this.collidesWithNonMotion(neighbor, result))
return true;
}
}
catch (e_5_1) { e_5 = { error: e_5_1 }; }
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (neighbors_4_1 && !neighbors_4_1.done && (_a = neighbors_4.return)) _a.call(neighbors_4);
if (neighbors_1_1 && !neighbors_1_1.done && (_a = neighbors_1.return)) _a.call(neighbors_1);
}
finally { if (e_5) throw e_5.error; }
finally { if (e_1) throw e_1.error; }
}
return false;
};
@@ -4772,7 +4744,7 @@ var es;
* @param tag
*/
EntityList.prototype.entitiesWithTag = function (tag) {
var e_6, _a;
var e_2, _a;
var list = this.getTagList(tag);
var returnList = es.ListPool.obtain(es.Entity);
if (list.size > 0) {
@@ -4782,12 +4754,12 @@ var es;
returnList.push(entity);
}
}
catch (e_6_1) { e_6 = { error: e_6_1 }; }
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (list_1_1 && !list_1_1.done && (_a = list_1.return)) _a.call(list_1);
}
finally { if (e_6) throw e_6.error; }
finally { if (e_2) throw e_2.error; }
}
}
return returnList;
@@ -4798,7 +4770,7 @@ var es;
* @returns
*/
EntityList.prototype.entityWithTag = function (tag) {
var e_7, _a;
var e_3, _a;
var list = this.getTagList(tag);
if (list.size > 0) {
try {
@@ -4807,12 +4779,12 @@ var es;
return entity;
}
}
catch (e_7_1) { e_7 = { error: e_7_1 }; }
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (list_2_1 && !list_2_1.done && (_a = list_2.return)) _a.call(list_2);
}
finally { if (e_7) throw e_7.error; }
finally { if (e_3) throw e_3.error; }
}
}
return null;
@@ -7803,11 +7775,11 @@ var es;
* 它将处理任何与Collider重叠的ITriggerListeners。
*/
ColliderTriggerHelper.prototype.update = function () {
var e_8, _a;
var lateColliders = [];
// 对所有实体.colliders进行重叠检查这些实体.colliders是触发器与所有宽相碰撞器无论是否触发器。
// 任何重叠都会导致触发事件
var colliders = this.getColliders();
if (colliders.length > 0) {
for (var i = 0; i < colliders.length; i++) {
var collider = colliders[i];
var neighbors = es.Physics.boxcastBroadphaseExcludingSelf(collider, collider.bounds, collider.collidesWithLayers.value);
@@ -7833,23 +7805,18 @@ var es;
}
}
}
try {
for (var lateColliders_1 = __values(lateColliders), lateColliders_1_1 = lateColliders_1.next(); !lateColliders_1_1.done; lateColliders_1_1 = lateColliders_1.next()) {
var pair = lateColliders_1_1.value;
}
if (lateColliders.length > 0) {
for (var i = 0; i < lateColliders.length; i++) {
var pair = lateColliders[i];
this.notifyTriggerListeners(pair, true);
}
}
catch (e_8_1) { e_8 = { error: e_8_1 }; }
finally {
try {
if (lateColliders_1_1 && !lateColliders_1_1.done && (_a = lateColliders_1.return)) _a.call(lateColliders_1);
}
finally { if (e_8) throw e_8.error; }
}
this.checkForExitedColliders();
};
ColliderTriggerHelper.prototype.getColliders = function () {
var colliders = [];
if (this._entity.components.buffer.length > 0)
for (var i = 0; i < this._entity.components.buffer.length; i++) {
var component = this._entity.components.buffer[i];
if (component instanceof es.Collider) {
@@ -7874,22 +7841,26 @@ var es;
};
ColliderTriggerHelper.prototype.notifyTriggerListeners = function (collisionPair, isEntering) {
es.TriggerListenerHelper.getITriggerListener(collisionPair.first.entity, this._tempTriggerList);
if (this._tempTriggerList.length > 0)
for (var i = 0; i < this._tempTriggerList.length; i++) {
var trigger = this._tempTriggerList[i];
if (isEntering) {
this._tempTriggerList[i].onTriggerEnter(collisionPair.second, collisionPair.first);
trigger.onTriggerEnter(collisionPair.second, collisionPair.first);
}
else {
this._tempTriggerList[i].onTriggerExit(collisionPair.second, collisionPair.first);
trigger.onTriggerExit(collisionPair.second, collisionPair.first);
}
this._tempTriggerList.length = 0;
if (collisionPair.second.entity) {
es.TriggerListenerHelper.getITriggerListener(collisionPair.second.entity, this._tempTriggerList);
for (var i_1 = 0; i_1 < this._tempTriggerList.length; i_1++) {
if (this._tempTriggerList.length > 0)
for (var i_2 = 0; i_2 < this._tempTriggerList.length; i_2++) {
var trigger_1 = this._tempTriggerList[i_2];
if (isEntering) {
this._tempTriggerList[i_1].onTriggerEnter(collisionPair.first, collisionPair.second);
trigger_1.onTriggerEnter(collisionPair.first, collisionPair.second);
}
else {
this._tempTriggerList[i_1].onTriggerExit(collisionPair.first, collisionPair.second);
trigger_1.onTriggerExit(collisionPair.first, collisionPair.second);
}
}
this._tempTriggerList.length = 0;
@@ -8454,6 +8425,7 @@ var es;
if (!cell)
continue;
// 当cell不为空。循环并取回所有碰撞器
if (cell.length > 0) {
for (var i = 0; i < cell.length; i++) {
var collider = cell[i];
// 如果它是自身或者如果它不匹配我们的层掩码 跳过这个碰撞器
@@ -8465,6 +8437,7 @@ var es;
}
}
}
}
return Array.from(this._tempHashSet);
};
/**
@@ -8535,14 +8508,12 @@ var es;
* @param layerMask
*/
SpatialHash.prototype.overlapRectangle = function (rect, results, layerMask) {
var e_9, _a;
this._overlapTestBox.updateBox(rect.width, rect.height);
this._overlapTestBox.position = rect.location.clone();
var resultCounter = 0;
var potentials = this.aabbBroadphase(rect, null, layerMask);
try {
for (var potentials_1 = __values(potentials), potentials_1_1 = potentials_1.next(); !potentials_1_1.done; potentials_1_1 = potentials_1.next()) {
var collider = potentials_1_1.value;
for (var i = 0; i < potentials.length; i++) {
var collider = potentials[i];
if (collider instanceof es.BoxCollider) {
results[resultCounter] = collider;
resultCounter++;
@@ -8565,14 +8536,6 @@ var es;
if (resultCounter == results.length)
return resultCounter;
}
}
catch (e_9_1) { e_9 = { error: e_9_1 }; }
finally {
try {
if (potentials_1_1 && !potentials_1_1.done && (_a = potentials_1.return)) _a.call(potentials_1);
}
finally { if (e_9) throw e_9.error; }
}
return resultCounter;
};
/**
@@ -8583,15 +8546,13 @@ var es;
* @param layerMask
*/
SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) {
var e_10, _a;
var bounds = new es.Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
this._overlapTestCircle.radius = radius;
this._overlapTestCircle.position = circleCenter;
var resultCounter = 0;
var potentials = this.aabbBroadphase(bounds, null, layerMask);
try {
for (var potentials_2 = __values(potentials), potentials_2_1 = potentials_2.next(); !potentials_2_1.done; potentials_2_1 = potentials_2.next()) {
var collider = potentials_2_1.value;
for (var i = 0; i < potentials.length; i++) {
var collider = potentials[i];
if (collider instanceof es.BoxCollider) {
if (collider.shape.overlaps(this._overlapTestCircle)) {
results[resultCounter] = collider;
@@ -8617,14 +8578,6 @@ var es;
if (resultCounter === results.length)
return resultCounter;
}
}
catch (e_10_1) { e_10 = { error: e_10_1 }; }
finally {
try {
if (potentials_2_1 && !potentials_2_1.done && (_a = potentials_2.return)) _a.call(potentials_2);
}
finally { if (e_10) throw e_10.error; }
}
return resultCounter;
};
/**
@@ -8669,9 +8622,8 @@ var es;
*/
NumberDictionary.prototype.remove = function (obj) {
this._store.forEach(function (list) {
var linqList = new es.List(list);
if (linqList.contains(obj))
linqList.remove(obj);
var index = list.indexOf(obj);
list.splice(index, 1);
});
};
NumberDictionary.prototype.tryGetValue = function (x, y) {
@@ -8712,7 +8664,7 @@ var es;
for (var i = 0; i < cell.length; i++) {
var potential = cell[i];
// 管理我们已经处理过的碰撞器
if (new es.List(this._checkedColliders).contains(potential))
if (this._checkedColliders.indexOf(potential) != -1)
continue;
this._checkedColliders.push(potential);
// 只有当我们被设置为这样做时才会点击触发器
@@ -12752,38 +12704,20 @@ var es;
this._all = [];
};
PairSet.prototype.union = function (other) {
var e_11, _a;
var otherAll = other.all;
try {
for (var otherAll_1 = __values(otherAll), otherAll_1_1 = otherAll_1.next(); !otherAll_1_1.done; otherAll_1_1 = otherAll_1.next()) {
var elem = otherAll_1_1.value;
if (otherAll.length > 0)
for (var i = 0; i < otherAll.length; i++) {
var elem = otherAll[i];
this.add(elem);
}
}
catch (e_11_1) { e_11 = { error: e_11_1 }; }
finally {
try {
if (otherAll_1_1 && !otherAll_1_1.done && (_a = otherAll_1.return)) _a.call(otherAll_1);
}
finally { if (e_11) throw e_11.error; }
}
};
PairSet.prototype.except = function (other) {
var e_12, _a;
var otherAll = other.all;
try {
for (var otherAll_2 = __values(otherAll), otherAll_2_1 = otherAll_2.next(); !otherAll_2_1.done; otherAll_2_1 = otherAll_2.next()) {
var elem = otherAll_2_1.value;
if (otherAll.length > 0)
for (var i = 0; i < otherAll.length; i++) {
var elem = otherAll[i];
this.remove(elem);
}
}
catch (e_12_1) { e_12 = { error: e_12_1 }; }
finally {
try {
if (otherAll_2_1 && !otherAll_2_1.done && (_a = otherAll_2.return)) _a.call(otherAll_2);
}
finally { if (e_12) throw e_12.error; }
}
};
return PairSet;
}());
@@ -14749,7 +14683,7 @@ var es;
* 创建一个Set从一个Enumerable.List< T>。
*/
List.prototype.toSet = function () {
var e_13, _a;
var e_4, _a;
var result = new Set();
try {
for (var _b = __values(this._elements), _c = _b.next(); !_c.done; _c = _b.next()) {
@@ -14757,12 +14691,12 @@ var es;
result.add(x);
}
}
catch (e_13_1) { e_13 = { error: e_13_1 }; }
catch (e_4_1) { e_4 = { error: e_4_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_13) throw e_13.error; }
finally { if (e_4) throw e_4.error; }
}
return result;
};
@@ -15011,7 +14945,7 @@ var es;
* 计算可见性多边形并返回三角形扇形的顶点减去中心顶点。返回的数组来自ListPool
*/
VisibilityComputer.prototype.end = function () {
var e_14, _a;
var e_5, _a;
var output = es.ListPool.obtain(es.Vector2);
this.updateSegments();
this._endPoints.sort(this._radialComparer.compare);
@@ -15050,12 +14984,12 @@ var es;
}
}
}
catch (e_14_1) { e_14 = { error: e_14_1 }; }
catch (e_5_1) { e_5 = { error: e_5_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_14) throw e_14.error; }
finally { if (e_5) throw e_5.error; }
}
}
VisibilityComputer._openSegments.clear();
@@ -15171,7 +15105,7 @@ var es;
* 处理片段,以便我们稍后对它们进行分类
*/
VisibilityComputer.prototype.updateSegments = function () {
var e_15, _a;
var e_6, _a;
try {
for (var _b = __values(this._segments), _c = _b.next(); !_c.done; _c = _b.next()) {
var segment = _c.value;
@@ -15189,12 +15123,12 @@ var es;
segment.p2.begin = !segment.p1.begin;
}
}
catch (e_15_1) { e_15 = { error: e_15_1 }; }
catch (e_6_1) { e_6 = { error: e_6_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
}
finally { if (e_15) throw e_15.error; }
finally { if (e_6) throw e_6.error; }
}
// 如果我们有一个聚光灯,我们需要存储前两个段的角度。
// 这些是光斑的边界,我们将用它们来过滤它们之外的任何顶点。

File diff suppressed because one or more lines are too long

View File

@@ -157,7 +157,9 @@ module es {
// 捞取我们在新的位置上可能会碰撞到的任何东西
let neighbors = Physics.boxcastBroadphaseExcludingSelf(this._collider, this._collider.bounds, this._collider.collidesWithLayers.value);
for (const neighbor of neighbors) {
if (neighbors.length > 0) {
for (let i = 0; i < neighbors.length; i ++) {
const neighbor = neighbors[i];
if (!neighbor)
continue;
@@ -182,6 +184,8 @@ module es {
}
}
}
/**
* 将两个重叠的刚体分开。也处理其中一个不可移动的情况
* @param other

View File

@@ -228,11 +228,13 @@ module es {
let neighbors = Physics.boxcastBroadphaseExcludingSelf(this, colliderBounds, this.collidesWithLayers.value);
// 更改形状位置,使其处于移动后的位置,以便我们检查是否有重叠
let oldPosition = this.shape.position.clone();
let oldPosition = this.shape.position;
this.shape.position = Vector2.add(this.shape.position, motion);
let didCollide = false;
for (let neighbor of neighbors) {
if (neighbors.length > 0) {
for (let i = 0; i < neighbors.length; i ++ ){
const neighbor = neighbors[i];
if (neighbor.isTrigger)
continue;
@@ -242,9 +244,10 @@ module es {
didCollide = true;
}
}
}
// 将形状位置返回到检查之前的位置
this.shape.position = oldPosition.clone();
this.shape.position = oldPosition;
return didCollide;
}

View File

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

View File

@@ -20,6 +20,7 @@ module es {
*/
public calculateMovement(motion: Vector2, collisionResult: CollisionResult): boolean {
let collider = null;
if (this.entity.components.buffer.length > 0)
for (let i = 0; i < this.entity.components.buffer.length; i++) {
let component = this.entity.components.buffer[i];
if (component instanceof Collider) {
@@ -27,18 +28,22 @@ module es {
break;
}
}
if (collider == null || this._triggerHelper == null) {
return false;
}
// 移动所有的非触发碰撞器并获得最近的碰撞
let colliders: Collider[] = [];
if (this.entity.components.buffer.length > 0)
for (let i = 0; i < this.entity.components.buffer.length; i ++) {
let component = this.entity.components.buffer[i];
if (component instanceof Collider) {
colliders.push(component);
}
}
if (colliders.length > 0) {
for (let i = 0; i < colliders.length; i++) {
let collider = colliders[i];
@@ -47,12 +52,14 @@ module es {
continue;
// 获取我们在新位置可能发生碰撞的任何东西
let bounds = collider.bounds.clone();
let bounds = collider.bounds;
bounds.x += motion.x;
bounds.y += motion.y;
let neighbors = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers.value);
for (let neighbor of neighbors) {
if (neighbors.length > 0) {
for (let i = 0; i < neighbors.length; i ++) {
const neighbor = neighbors[i];
// 不检测触发器
if (neighbor.isTrigger)
return;
@@ -71,7 +78,8 @@ module es {
}
}
}
}
}
ListPool.free(Collider, colliders);
return collisionResult.collider != null;

View File

@@ -23,6 +23,7 @@ module es {
// 对所有实体.colliders进行重叠检查这些实体.colliders是触发器与所有宽相碰撞器无论是否触发器。
// 任何重叠都会导致触发事件
let colliders: Collider[] = this.getColliders();
if (colliders.length > 0) {
for (let i = 0; i < colliders.length; i++) {
let collider = colliders[i];
@@ -52,16 +53,22 @@ module es {
}
}
}
}
for (const pair of lateColliders) {
if (lateColliders.length > 0) {
for (let i = 0; i < lateColliders.length; i ++) {
const pair = lateColliders[i];
this.notifyTriggerListeners(pair, true);
}
}
this.checkForExitedColliders();
}
private getColliders() {
const colliders: Collider[] = [];
if (this._entity.components.buffer.length > 0)
for (let i = 0; i < this._entity.components.buffer.length; i ++) {
const component = this._entity.components.buffer[i];
if (component instanceof Collider) {
@@ -91,22 +98,26 @@ module es {
private notifyTriggerListeners(collisionPair: Pair<Collider>, isEntering: boolean) {
TriggerListenerHelper.getITriggerListener(collisionPair.first.entity, this._tempTriggerList);
if (this._tempTriggerList.length > 0)
for (let i = 0; i < this._tempTriggerList.length; i++) {
const trigger = this._tempTriggerList[i];
if (isEntering) {
this._tempTriggerList[i].onTriggerEnter(collisionPair.second, collisionPair.first);
trigger.onTriggerEnter(collisionPair.second, collisionPair.first);
} else {
this._tempTriggerList[i].onTriggerExit(collisionPair.second, collisionPair.first);
trigger.onTriggerExit(collisionPair.second, collisionPair.first);
}
this._tempTriggerList.length = 0;
if (collisionPair.second.entity) {
TriggerListenerHelper.getITriggerListener(collisionPair.second.entity, this._tempTriggerList);
if (this._tempTriggerList.length > 0)
for (let i = 0; i < this._tempTriggerList.length; i++) {
const trigger = this._tempTriggerList[i];
if (isEntering) {
this._tempTriggerList[i].onTriggerEnter(collisionPair.first, collisionPair.second);
trigger.onTriggerEnter(collisionPair.first, collisionPair.second);
} else {
this._tempTriggerList[i].onTriggerExit(collisionPair.first, collisionPair.second);
trigger.onTriggerExit(collisionPair.first, collisionPair.second);
}
}

View File

@@ -113,6 +113,7 @@ module es {
continue;
// 当cell不为空。循环并取回所有碰撞器
if (cell.length > 0) {
for (let i = 0; i < cell.length; i++) {
const collider = cell[i];
@@ -126,6 +127,7 @@ module es {
}
}
}
}
return Array.from(this._tempHashSet);
}
@@ -215,7 +217,9 @@ module es {
let resultCounter = 0;
let potentials = this.aabbBroadphase(rect, null, layerMask);
for (let collider of potentials) {
for (let i = 0; i < potentials.length; i ++) {
const collider = potentials[i];
if (collider instanceof BoxCollider) {
results[resultCounter] = collider;
resultCounter++;
@@ -255,7 +259,8 @@ module es {
let resultCounter = 0;
const potentials = this.aabbBroadphase(bounds, null, layerMask);
for (let collider of potentials) {
for (let i = 0; i < potentials.length; i ++) {
const collider = potentials[i];
if (collider instanceof BoxCollider) {
if (collider.shape.overlaps(this._overlapTestCircle)) {
results[resultCounter] = collider;
@@ -324,9 +329,8 @@ module es {
*/
public remove(obj: T) {
this._store.forEach(list => {
let linqList = new es.List(list);
if (linqList.contains(obj))
linqList.remove(obj);
let index = list.indexOf(obj);
list.splice(index, 1);
})
}
@@ -383,7 +387,7 @@ module es {
const potential = cell[i];
// 管理我们已经处理过的碰撞器
if (new es.List(this._checkedColliders).contains(potential))
if (this._checkedColliders.indexOf(potential) != -1)
continue;
this._checkedColliders.push(potential);

View File

@@ -31,14 +31,20 @@ module es {
public union(other: PairSet<T>) {
const otherAll = other.all;
for (const elem of otherAll) {
if (otherAll.length > 0)
for (let i = 0; i < otherAll.length; i ++) {
const elem = otherAll[i];
this.add(elem);
}
}
public except(other: PairSet<T>) {
const otherAll = other.all;
for (const elem of otherAll) {
if (otherAll.length > 0)
for (let i = 0; i < otherAll.length; i ++) {
const elem = otherAll[i];
this.remove(elem);
}
}