修复box因缺少初始化报错问题

This commit is contained in:
yhh
2020-06-16 16:35:17 +08:00
parent 7f5b78f340
commit 447ea4efe4
18 changed files with 272 additions and 910 deletions

View File

@@ -402,7 +402,7 @@ declare abstract class Collider extends Component {
protected _isParentEntityAddedToScene: any;
protected _colliderRequiresAutoSizing: any;
protected _localOffset: Vector2;
protected _isColliderRegisterd: any;
protected _isColliderRegistered: any;
readonly bounds: Rectangle;
localOffset: Vector2;
setLocalOffset(offset: Vector2): void;
@@ -701,7 +701,7 @@ declare class Polygon extends Shape {
_edgeNormals: Vector2[];
readonly edgeNormals: Vector2[];
isBox: boolean;
constructor(vertCount: number, radius: number);
constructor(points: Vector2[], isBox?: boolean);
private buildEdgeNormals;
setPoints(points: Vector2[]): void;
collidesWithShape(other: Shape): CollisionResult;
@@ -721,6 +721,8 @@ declare class Polygon extends Shape {
declare class Box extends Polygon {
width: number;
height: number;
constructor(width: number, height: number);
private static buildBox;
updateBox(width: number, height: number): void;
containsPoint(point: Vector2): boolean;
}

View File

@@ -1186,7 +1186,7 @@ var Transform = (function () {
this._worldInverseTransform = Matrix2D.identity;
this._rotation = 0;
this.entity = entity;
this._scale = this._localScale = new Vector2(0, 0);
this._scale = this._localScale = Vector2.one;
this._children = [];
}
Object.defineProperty(Transform.prototype, "childCount", {
@@ -1882,16 +1882,16 @@ var Collider = (function (_super) {
}
};
Collider.prototype.registerColliderWithPhysicsSystem = function () {
if (this._isParentEntityAddedToScene && !this._isColliderRegisterd) {
if (this._isParentEntityAddedToScene && !this._isColliderRegistered) {
Physics.addCollider(this);
this._isColliderRegisterd = true;
this._isColliderRegistered = true;
}
};
Collider.prototype.unregisterColliderWithPhysicsSystem = function () {
if (this._isParentEntityAddedToScene && this._isColliderRegisterd) {
if (this._isParentEntityAddedToScene && this._isColliderRegistered) {
Physics.removeCollider(this);
}
this._isColliderRegisterd = false;
this._isColliderRegistered = false;
};
Collider.prototype.overlaps = function (other) {
return this.shape.overlaps(other.shape);
@@ -1938,7 +1938,7 @@ var Collider = (function (_super) {
this._isRotationDirty = true;
break;
}
if (this._isColliderRegisterd)
if (this._isColliderRegistered)
Physics.updateCollider(this);
};
Collider.prototype.onEnabled = function () {
@@ -2308,20 +2308,30 @@ var ComponentList = (function () {
components = [];
for (var i = 0; i < this._components.length; i++) {
var component = this._components[i];
if (typeof (typeName) == "string" && egret.is(component, typeName))
if (typeof (typeName) == "string") {
if (egret.is(component, typeName)) {
components.push(component);
else if (component instanceof typeName) {
}
}
else {
if (component instanceof typeName) {
components.push(component);
}
}
}
for (var i = 0; i < this._componentsToAdd.length; i++) {
var component = this._componentsToAdd[i];
if (typeof (typeName) == "string" && egret.is(component, typeName))
if (typeof (typeName) == "string") {
if (egret.is(component, typeName)) {
components.push(component);
else if (component instanceof typeName) {
}
}
else {
if (component instanceof typeName) {
components.push(component);
}
}
}
return components;
};
ComponentList.prototype.update = function () {
@@ -3099,9 +3109,13 @@ var ColliderTriggerHelper = (function () {
}
return false;
});
this._previousTriggerIntersections.forEach(function (pair) { return _this.notifyTriggerListeners(pair, false); });
for (var i = 0; i < this._previousTriggerIntersections.length; i++) {
this.notifyTriggerListeners(this._previousTriggerIntersections[i], false);
}
this._previousTriggerIntersections.length = 0;
tempIntersections.forEach(function (value) { return _this._previousTriggerIntersections.push(value); });
for (var i = 0; i < tempIntersections.length; i++) {
this._previousTriggerIntersections.push(tempIntersections[i]);
}
this._activeTriggerIntersections.length = 0;
};
ColliderTriggerHelper.prototype.notifyTriggerListeners = function (collisionPair, isEntering) {
@@ -3296,11 +3310,12 @@ var Shape = (function () {
}());
var Polygon = (function (_super) {
__extends(Polygon, _super);
function Polygon(vertCount, radius) {
function Polygon(points, isBox) {
var _this = _super.call(this) || this;
_this.isUnrotated = true;
_this._areEdgeNormalsDirty = true;
_this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius));
_this.setPoints(points);
_this.isBox = isBox;
return _this;
}
Object.defineProperty(Polygon.prototype, "edgeNormals", {
@@ -3329,11 +3344,12 @@ var Polygon = (function (_super) {
}
};
Polygon.prototype.setPoints = function (points) {
var _this = this;
this.points = points;
this.recalculateCenterAndEdgeNormals();
this._originalPoints = new Array(this.points.length);
this.points.forEach(function (point) { return _this._originalPoints.push(point); });
this._originalPoints = [];
for (var i = 0; i < this.points.length; i++) {
this._originalPoints.push(this.points[i]);
}
};
Polygon.prototype.collidesWithShape = function (other) {
var result = new CollisionResult();
@@ -3457,9 +3473,22 @@ var Polygon = (function (_super) {
}(Shape));
var Box = (function (_super) {
__extends(Box, _super);
function Box() {
return _super !== null && _super.apply(this, arguments) || this;
function Box(width, height) {
var _this = _super.call(this, Box.buildBox(width, height), true) || this;
_this.width = width;
_this.height = height;
return _this;
}
Box.buildBox = function (width, height) {
var halfWidth = width / 2;
var halfHeight = height / 2;
var verts = new Array(4);
verts[0] = new Vector2(-halfWidth, -halfHeight);
verts[1] = new Vector2(halfWidth, -halfHeight);
verts[2] = new Vector2(halfWidth, halfHeight);
verts[3] = new Vector2(-halfWidth, halfHeight);
return verts;
};
Box.prototype.updateBox = function (width, height) {
this.width = width;
this.height = height;
@@ -3755,10 +3784,18 @@ var SpatialHash = (function () {
this._overlapTestCircle.position = circleCenter;
var resultCounter = 0;
var potentials = this.aabbBroadphase(bounds, null, layerMask);
potentials.forEach(function (collider) {
for (var i = 0; i < potentials.length; i++) {
var collider = potentials[i];
if (collider instanceof BoxCollider) {
results[resultCounter] = collider;
resultCounter++;
}
else {
throw new Error("overlapCircle against this collider type is not implemented!");
}
if (resultCounter == results.length)
return resultCounter;
});
}
return resultCounter;
};
SpatialHash.prototype.aabbBroadphase = function (bounds, excludeCollider, layerMask) {
@@ -4020,7 +4057,7 @@ var Vector2Ext = (function () {
Vector2Ext.transformA = function (sourceArray, sourceIndex, matrix, destinationArray, destinationIndex, length) {
for (var i = 0; i < length; i++) {
var position = sourceArray[sourceIndex + i];
var destination = destinationArray[destinationIndex + 1];
var destination = destinationArray[destinationIndex + i];
destination.x = (position.x * matrix.m11) + (position.y * matrix.m21) + matrix.m31;
destination.y = (position.x * matrix.m12) + (position.y * matrix.m22) + matrix.m32;
destinationArray[destinationIndex + i] = destination;

File diff suppressed because one or more lines are too long

View File

@@ -105,7 +105,7 @@ class Main extends eui.UILayer {
new Vector2(0, 10),
new Vector2(0, 0)]));
player.addComponent(new SpawnComponent(EnemyType.worm));
player.addComponent(new BoxCollider()).setSize(100, 100);
player.addComponent(new BoxCollider()).setSize(100, 100).isTrigger = true;
player.addComponent(new Mover());
let player2 = scene.createEntity("player2");

View File

@@ -21,11 +21,11 @@ class SpawnComponent extends Component implements ITriggerListener {
}
public onTriggerEnter(other: Collider, local: Collider){
console.log("enter collider", other, local);
console.log("enter collider");
}
public onTriggerExit(other: Collider, local: Collider){
console.log("exit collider", other, local);
console.log("exit collider");
}
}

View File

@@ -402,7 +402,7 @@ declare abstract class Collider extends Component {
protected _isParentEntityAddedToScene: any;
protected _colliderRequiresAutoSizing: any;
protected _localOffset: Vector2;
protected _isColliderRegisterd: any;
protected _isColliderRegistered: any;
readonly bounds: Rectangle;
localOffset: Vector2;
setLocalOffset(offset: Vector2): void;
@@ -701,7 +701,7 @@ declare class Polygon extends Shape {
_edgeNormals: Vector2[];
readonly edgeNormals: Vector2[];
isBox: boolean;
constructor(vertCount: number, radius: number);
constructor(points: Vector2[], isBox?: boolean);
private buildEdgeNormals;
setPoints(points: Vector2[]): void;
collidesWithShape(other: Shape): CollisionResult;
@@ -721,6 +721,8 @@ declare class Polygon extends Shape {
declare class Box extends Polygon {
width: number;
height: number;
constructor(width: number, height: number);
private static buildBox;
updateBox(width: number, height: number): void;
containsPoint(point: Vector2): boolean;
}

View File

@@ -1186,7 +1186,7 @@ var Transform = (function () {
this._worldInverseTransform = Matrix2D.identity;
this._rotation = 0;
this.entity = entity;
this._scale = this._localScale = new Vector2(0, 0);
this._scale = this._localScale = Vector2.one;
this._children = [];
}
Object.defineProperty(Transform.prototype, "childCount", {
@@ -1882,16 +1882,16 @@ var Collider = (function (_super) {
}
};
Collider.prototype.registerColliderWithPhysicsSystem = function () {
if (this._isParentEntityAddedToScene && !this._isColliderRegisterd) {
if (this._isParentEntityAddedToScene && !this._isColliderRegistered) {
Physics.addCollider(this);
this._isColliderRegisterd = true;
this._isColliderRegistered = true;
}
};
Collider.prototype.unregisterColliderWithPhysicsSystem = function () {
if (this._isParentEntityAddedToScene && this._isColliderRegisterd) {
if (this._isParentEntityAddedToScene && this._isColliderRegistered) {
Physics.removeCollider(this);
}
this._isColliderRegisterd = false;
this._isColliderRegistered = false;
};
Collider.prototype.overlaps = function (other) {
return this.shape.overlaps(other.shape);
@@ -1938,7 +1938,7 @@ var Collider = (function (_super) {
this._isRotationDirty = true;
break;
}
if (this._isColliderRegisterd)
if (this._isColliderRegistered)
Physics.updateCollider(this);
};
Collider.prototype.onEnabled = function () {
@@ -2308,20 +2308,30 @@ var ComponentList = (function () {
components = [];
for (var i = 0; i < this._components.length; i++) {
var component = this._components[i];
if (typeof (typeName) == "string" && egret.is(component, typeName))
if (typeof (typeName) == "string") {
if (egret.is(component, typeName)) {
components.push(component);
else if (component instanceof typeName) {
}
}
else {
if (component instanceof typeName) {
components.push(component);
}
}
}
for (var i = 0; i < this._componentsToAdd.length; i++) {
var component = this._componentsToAdd[i];
if (typeof (typeName) == "string" && egret.is(component, typeName))
if (typeof (typeName) == "string") {
if (egret.is(component, typeName)) {
components.push(component);
else if (component instanceof typeName) {
}
}
else {
if (component instanceof typeName) {
components.push(component);
}
}
}
return components;
};
ComponentList.prototype.update = function () {
@@ -3099,9 +3109,13 @@ var ColliderTriggerHelper = (function () {
}
return false;
});
this._previousTriggerIntersections.forEach(function (pair) { return _this.notifyTriggerListeners(pair, false); });
for (var i = 0; i < this._previousTriggerIntersections.length; i++) {
this.notifyTriggerListeners(this._previousTriggerIntersections[i], false);
}
this._previousTriggerIntersections.length = 0;
tempIntersections.forEach(function (value) { return _this._previousTriggerIntersections.push(value); });
for (var i = 0; i < tempIntersections.length; i++) {
this._previousTriggerIntersections.push(tempIntersections[i]);
}
this._activeTriggerIntersections.length = 0;
};
ColliderTriggerHelper.prototype.notifyTriggerListeners = function (collisionPair, isEntering) {
@@ -3296,11 +3310,12 @@ var Shape = (function () {
}());
var Polygon = (function (_super) {
__extends(Polygon, _super);
function Polygon(vertCount, radius) {
function Polygon(points, isBox) {
var _this = _super.call(this) || this;
_this.isUnrotated = true;
_this._areEdgeNormalsDirty = true;
_this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius));
_this.setPoints(points);
_this.isBox = isBox;
return _this;
}
Object.defineProperty(Polygon.prototype, "edgeNormals", {
@@ -3329,11 +3344,12 @@ var Polygon = (function (_super) {
}
};
Polygon.prototype.setPoints = function (points) {
var _this = this;
this.points = points;
this.recalculateCenterAndEdgeNormals();
this._originalPoints = new Array(this.points.length);
this.points.forEach(function (point) { return _this._originalPoints.push(point); });
this._originalPoints = [];
for (var i = 0; i < this.points.length; i++) {
this._originalPoints.push(this.points[i]);
}
};
Polygon.prototype.collidesWithShape = function (other) {
var result = new CollisionResult();
@@ -3457,9 +3473,22 @@ var Polygon = (function (_super) {
}(Shape));
var Box = (function (_super) {
__extends(Box, _super);
function Box() {
return _super !== null && _super.apply(this, arguments) || this;
function Box(width, height) {
var _this = _super.call(this, Box.buildBox(width, height), true) || this;
_this.width = width;
_this.height = height;
return _this;
}
Box.buildBox = function (width, height) {
var halfWidth = width / 2;
var halfHeight = height / 2;
var verts = new Array(4);
verts[0] = new Vector2(-halfWidth, -halfHeight);
verts[1] = new Vector2(halfWidth, -halfHeight);
verts[2] = new Vector2(halfWidth, halfHeight);
verts[3] = new Vector2(-halfWidth, halfHeight);
return verts;
};
Box.prototype.updateBox = function (width, height) {
this.width = width;
this.height = height;
@@ -3755,10 +3784,18 @@ var SpatialHash = (function () {
this._overlapTestCircle.position = circleCenter;
var resultCounter = 0;
var potentials = this.aabbBroadphase(bounds, null, layerMask);
potentials.forEach(function (collider) {
for (var i = 0; i < potentials.length; i++) {
var collider = potentials[i];
if (collider instanceof BoxCollider) {
results[resultCounter] = collider;
resultCounter++;
}
else {
throw new Error("overlapCircle against this collider type is not implemented!");
}
if (resultCounter == results.length)
return resultCounter;
});
}
return resultCounter;
};
SpatialHash.prototype.aabbBroadphase = function (bounds, excludeCollider, layerMask) {
@@ -4020,7 +4057,7 @@ var Vector2Ext = (function () {
Vector2Ext.transformA = function (sourceArray, sourceIndex, matrix, destinationArray, destinationIndex, length) {
for (var i = 0; i < length; i++) {
var position = sourceArray[sourceIndex + i];
var destination = destinationArray[destinationIndex + 1];
var destination = destinationArray[destinationIndex + i];
destination.x = (position.x * matrix.m11) + (position.y * matrix.m21) + matrix.m31;
destination.y = (position.x * matrix.m12) + (position.y * matrix.m22) + matrix.m32;
destinationArray[destinationIndex + i] = destination;

File diff suppressed because one or more lines are too long

832
source/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -7,7 +7,8 @@
"lib": "lib"
},
"scripts": {
"test": "mocha --recursive --reporter tap --growl"
"test": "mocha --recursive --reporter tap --growl",
"eslint": "eslint src --ext .ts"
},
"author": "yhh",
"license": "MIT",
@@ -18,6 +19,7 @@
"gulp": "^3.9.1",
"gulp-babel": "^8.0.0",
"gulp-concat": "^2.6.1",
"gulp-inject-string": "^1.1.2",
"gulp-minify": "^3.1.0",
"gulp-string-replace": "^1.1.2",
"gulp-typescript": "^3.1.6",
@@ -25,14 +27,10 @@
"tsify": "^3.0.1",
"typescript": "^2.2.2",
"vinyl-source-stream": "^1.1.0",
"watchify": "^3.9.0",
"gulp-inject-string": "^1.1.2"
"watchify": "^3.9.0"
},
"publishConfig": {
"registry": "https://npm.pkg.github.com/359807859@qq.com"
},
"dependencies": {
"chai": "^4.2.0",
"mocha": "^8.0.1"
}
"dependencies": {}
}

View File

@@ -12,7 +12,7 @@ abstract class Collider extends Component{
protected _isParentEntityAddedToScene;
protected _colliderRequiresAutoSizing;
protected _localOffset: Vector2 = new Vector2(0, 0);
protected _isColliderRegisterd;
protected _isColliderRegistered;
public get bounds(): Rectangle {
if (this._isPositionDirty || this._isRotationDirty){
@@ -42,17 +42,17 @@ abstract class Collider extends Component{
}
public registerColliderWithPhysicsSystem(){
if (this._isParentEntityAddedToScene && !this._isColliderRegisterd){
if (this._isParentEntityAddedToScene && !this._isColliderRegistered){
Physics.addCollider(this);
this._isColliderRegisterd = true;
this._isColliderRegistered = true;
}
}
public unregisterColliderWithPhysicsSystem(){
if (this._isParentEntityAddedToScene && this._isColliderRegisterd){
if (this._isParentEntityAddedToScene && this._isColliderRegistered){
Physics.removeCollider(this);
}
this._isColliderRegisterd = false;
this._isColliderRegistered = false;
}
public overlaps(other: Collider){
@@ -112,7 +112,7 @@ abstract class Collider extends Component{
break;
}
if (this._isColliderRegisterd)
if (this._isColliderRegistered)
Physics.updateCollider(this);
}

View File

@@ -49,7 +49,7 @@ class Transform {
constructor(entity: Entity){
this.entity = entity;
this._scale = this._localScale = new Vector2(0, 0);
this._scale = this._localScale = Vector2.one;
this._children = [];
}

View File

@@ -126,21 +126,29 @@ class ComponentList {
for (let i = 0; i < this._components.length; i ++){
let component = this._components[i];
if (typeof(typeName) == "string" && egret.is(component, typeName))
if (typeof(typeName) == "string"){
if (egret.is(component, typeName)){
components.push(component);
else if (component instanceof typeName) {
}
}else{
if (component instanceof typeName){
components.push(component);
}
}
}
for (let i = 0; i < this._componentsToAdd.length; i ++){
let component = this._componentsToAdd[i];
if (typeof(typeName) == "string" && egret.is(component, typeName))
if (typeof(typeName) == "string"){
if (egret.is(component, typeName)){
components.push(component);
else if (component instanceof typeName){
}
}else{
if (component instanceof typeName){
components.push(component);
}
}
}
return components;
}

View File

@@ -55,9 +55,13 @@ class ColliderTriggerHelper {
return false;
});
this._previousTriggerIntersections.forEach(pair => this.notifyTriggerListeners(pair, false));
for (let i = 0; i < this._previousTriggerIntersections.length; i ++){
this.notifyTriggerListeners(this._previousTriggerIntersections[i], false)
}
this._previousTriggerIntersections.length = 0;
tempIntersections.forEach(value => this._previousTriggerIntersections.push(value));
for (let i = 0; i < tempIntersections.length; i ++){
this._previousTriggerIntersections.push(tempIntersections[i]);
}
this._activeTriggerIntersections.length = 0;
}

View File

@@ -3,6 +3,24 @@ class Box extends Polygon {
public width: number;
public height: number;
constructor(width: number, height: number){
super(Box.buildBox(width, height), true);
this.width = width;
this.height = height;
}
private static buildBox(width: number, height: number): Vector2[]{
let halfWidth = width / 2;
let halfHeight = height / 2;
let verts = new Array(4);
verts[0] = new Vector2(-halfWidth, -halfHeight);
verts[1] = new Vector2(halfWidth, -halfHeight);
verts[2] = new Vector2(halfWidth, halfHeight);
verts[3] = new Vector2(-halfWidth, halfHeight);
return verts;
}
public updateBox(width: number, height: number){
this.width = width;
this.height = height;

View File

@@ -14,9 +14,11 @@ class Polygon extends Shape {
}
public isBox: boolean;
constructor(vertCount: number, radius: number) {
constructor(points: Vector2[], isBox?: boolean){
super();
this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius));
this.setPoints(points);
this.isBox = isBox;
}
private buildEdgeNormals(){
@@ -42,8 +44,10 @@ class Polygon extends Shape {
this.points = points;
this.recalculateCenterAndEdgeNormals();
this._originalPoints = new Array(this.points.length);
this.points.forEach(point => this._originalPoints.push(point));
this._originalPoints = [];
for (let i = 0; i < this.points.length; i ++){
this._originalPoints.push(this.points[i]);
}
}
public collidesWithShape(other: Shape){

View File

@@ -8,19 +8,19 @@ class SpatialHash {
private _tempHashSet: Collider[] = [];
private _cellDict: NumberDictionary = new NumberDictionary();
constructor(cellSize: number = 100){
constructor(cellSize: number = 100) {
this._cellSize = cellSize;
this._inverseCellSize = 1 / this._cellSize;
this._raycastParser = new RaycastResultParser();
}
public remove(collider: Collider){
public remove(collider: Collider) {
let bounds = collider.registeredPhysicsBounds;
let p1 = this.cellCoords(bounds.x, bounds.y);
let p2 = this.cellCoords(bounds.right, bounds.bottom);
for (let x = p1.x; x <= p2.x; x ++){
for (let y = p1.y; y <= p2.y; y ++){
for (let x = p1.x; x <= p2.x; x++) {
for (let y = p1.y; y <= p2.y; y++) {
let cell = this.cellAtPosition(x, y);
if (!cell)
console.error(`removing Collider [${collider}] from a cell that it is not present in`);
@@ -30,29 +30,29 @@ class SpatialHash {
}
}
public register(collider: Collider){
public register(collider: Collider) {
let bounds = collider.bounds;
collider.registeredPhysicsBounds = bounds;
let p1 = this.cellCoords(bounds.x, bounds.y);
let p2 = this.cellCoords(bounds.right, bounds.bottom);
if (!this.gridBounds.contains(new Vector2(p1.x, p1.y))){
if (!this.gridBounds.contains(new Vector2(p1.x, p1.y))) {
this.gridBounds = RectangleExt.union(this.gridBounds, p1);
}
if (!this.gridBounds.contains(new Vector2(p2.x, p2.y))){
if (!this.gridBounds.contains(new Vector2(p2.x, p2.y))) {
this.gridBounds = RectangleExt.union(this.gridBounds, p2);
}
for (let x = p1.x; x <= p2.x; x++){
for (let y = p1.y; y <= p2.y; y++){
for (let x = p1.x; x <= p2.x; x++) {
for (let y = p1.y; y <= p2.y; y++) {
let c = this.cellAtPosition(x, y, true);
c.push(collider);
}
}
}
public overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask){
public overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask) {
let bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
this._overlapTestCircle.radius = radius;
@@ -60,27 +60,35 @@ class SpatialHash {
let resultCounter = 0;
let potentials = this.aabbBroadphase(bounds, null, layerMask);
potentials.forEach(collider => {
for (let i = 0; i < potentials.length; i ++){
let collider = potentials[i];
if (collider instanceof BoxCollider){
results[resultCounter] = collider;
resultCounter ++;
}else{
throw new Error("overlapCircle against this collider type is not implemented!");
}
if (resultCounter == results.length)
return resultCounter;
});
}
return resultCounter;
}
public aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number){
public aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number) {
this._tempHashSet.length = 0;
let p1 = this.cellCoords(bounds.x, bounds.y);
let p2 = this.cellCoords(bounds.right, bounds.bottom);
for (let x = p1.x; x <= p2.x; x ++){
for (let y = p1.y; y <= p2.y; y ++){
for (let x = p1.x; x <= p2.x; x++) {
for (let y = p1.y; y <= p2.y; y++) {
let cell = this.cellAtPosition(x, y);
if (!cell)
continue;
for (let i = 0; i < cell.length; i ++){
for (let i = 0; i < cell.length; i++) {
let collider = cell[i];
if (collider == excludeCollider || !Flags.isFlagSet(layerMask, collider.physicsLayer))
@@ -95,10 +103,10 @@ class SpatialHash {
return this._tempHashSet;
}
private cellAtPosition(x: number, y: number, createCellIfEmpty: boolean = false){
private cellAtPosition(x: number, y: number, createCellIfEmpty: boolean = false) {
let cell: Collider[] = this._cellDict.tryGetValue(x, y);
if (!cell){
if (createCellIfEmpty){
if (!cell) {
if (createCellIfEmpty) {
cell = [];
this._cellDict.add(x, y, cell);
}
@@ -118,26 +126,26 @@ class RaycastResultParser {
class NumberDictionary {
private _store: Map<number, Collider[]> = new Map<number, Collider[]>();
private getKey(x: number, y: number): number{
private getKey(x: number, y: number): number {
return x << 32 | y;
}
public add(x: number, y: number, list: Collider[]){
public add(x: number, y: number, list: Collider[]) {
this._store.set(this.getKey(x, y), list);
}
public remove(obj: Collider){
public remove(obj: Collider) {
this._store.forEach(list => {
if (list.contains(obj))
list.remove(obj);
})
}
public tryGetValue(x: number, y: number): Collider[]{
public tryGetValue(x: number, y: number): Collider[] {
return this._store.get(this.getKey(x, y));
}
public getAllObjects(): Collider[]{
public getAllObjects(): Collider[] {
let set: Collider[] = [];
this._store.forEach(list => set.concat(list));
@@ -145,7 +153,7 @@ class NumberDictionary {
return set;
}
public clear(){
public clear() {
this._store.clear();
}
}

View File

@@ -47,7 +47,7 @@ class Vector2Ext {
destinationArray: Vector2[], destinationIndex: number, length: number) {
for (let i = 0; i < length; i ++){
let position = sourceArray[sourceIndex + i];
let destination = destinationArray[destinationIndex + 1];
let destination = destinationArray[destinationIndex + i];
destination.x = (position.x * matrix.m11) + (position.y * matrix.m21) + matrix.m31;
destination.y = (position.x * matrix.m12) + (position.y * matrix.m22) + matrix.m32;
destinationArray[destinationIndex + i] = destination;