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

View File

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

View File

@@ -21,11 +21,11 @@ class SpawnComponent extends Component implements ITriggerListener {
} }
public onTriggerEnter(other: Collider, local: Collider){ public onTriggerEnter(other: Collider, local: Collider){
console.log("enter collider", other, local); console.log("enter collider");
} }
public onTriggerExit(other: Collider, local: 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 _isParentEntityAddedToScene: any;
protected _colliderRequiresAutoSizing: any; protected _colliderRequiresAutoSizing: any;
protected _localOffset: Vector2; protected _localOffset: Vector2;
protected _isColliderRegisterd: any; protected _isColliderRegistered: any;
readonly bounds: Rectangle; readonly bounds: Rectangle;
localOffset: Vector2; localOffset: Vector2;
setLocalOffset(offset: Vector2): void; setLocalOffset(offset: Vector2): void;
@@ -701,7 +701,7 @@ declare class Polygon extends Shape {
_edgeNormals: Vector2[]; _edgeNormals: Vector2[];
readonly edgeNormals: Vector2[]; readonly edgeNormals: Vector2[];
isBox: boolean; isBox: boolean;
constructor(vertCount: number, radius: number); constructor(points: Vector2[], isBox?: boolean);
private buildEdgeNormals; private buildEdgeNormals;
setPoints(points: Vector2[]): void; setPoints(points: Vector2[]): void;
collidesWithShape(other: Shape): CollisionResult; collidesWithShape(other: Shape): CollisionResult;
@@ -721,6 +721,8 @@ declare class Polygon extends Shape {
declare class Box extends Polygon { declare class Box extends Polygon {
width: number; width: number;
height: number; height: number;
constructor(width: number, height: number);
private static buildBox;
updateBox(width: number, height: number): void; updateBox(width: number, height: number): void;
containsPoint(point: Vector2): boolean; containsPoint(point: Vector2): boolean;
} }

View File

@@ -1186,7 +1186,7 @@ var Transform = (function () {
this._worldInverseTransform = Matrix2D.identity; this._worldInverseTransform = Matrix2D.identity;
this._rotation = 0; this._rotation = 0;
this.entity = entity; this.entity = entity;
this._scale = this._localScale = new Vector2(0, 0); this._scale = this._localScale = Vector2.one;
this._children = []; this._children = [];
} }
Object.defineProperty(Transform.prototype, "childCount", { Object.defineProperty(Transform.prototype, "childCount", {
@@ -1882,16 +1882,16 @@ var Collider = (function (_super) {
} }
}; };
Collider.prototype.registerColliderWithPhysicsSystem = function () { Collider.prototype.registerColliderWithPhysicsSystem = function () {
if (this._isParentEntityAddedToScene && !this._isColliderRegisterd) { if (this._isParentEntityAddedToScene && !this._isColliderRegistered) {
Physics.addCollider(this); Physics.addCollider(this);
this._isColliderRegisterd = true; this._isColliderRegistered = true;
} }
}; };
Collider.prototype.unregisterColliderWithPhysicsSystem = function () { Collider.prototype.unregisterColliderWithPhysicsSystem = function () {
if (this._isParentEntityAddedToScene && this._isColliderRegisterd) { if (this._isParentEntityAddedToScene && this._isColliderRegistered) {
Physics.removeCollider(this); Physics.removeCollider(this);
} }
this._isColliderRegisterd = false; this._isColliderRegistered = false;
}; };
Collider.prototype.overlaps = function (other) { Collider.prototype.overlaps = function (other) {
return this.shape.overlaps(other.shape); return this.shape.overlaps(other.shape);
@@ -1938,7 +1938,7 @@ var Collider = (function (_super) {
this._isRotationDirty = true; this._isRotationDirty = true;
break; break;
} }
if (this._isColliderRegisterd) if (this._isColliderRegistered)
Physics.updateCollider(this); Physics.updateCollider(this);
}; };
Collider.prototype.onEnabled = function () { Collider.prototype.onEnabled = function () {
@@ -2308,18 +2308,28 @@ var ComponentList = (function () {
components = []; components = [];
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
var component = this._components[i]; var component = this._components[i];
if (typeof (typeName) == "string" && egret.is(component, typeName)) if (typeof (typeName) == "string") {
components.push(component); if (egret.is(component, typeName)) {
else if (component instanceof typeName) { components.push(component);
components.push(component); }
}
else {
if (component instanceof typeName) {
components.push(component);
}
} }
} }
for (var i = 0; i < this._componentsToAdd.length; i++) { for (var i = 0; i < this._componentsToAdd.length; i++) {
var component = this._componentsToAdd[i]; var component = this._componentsToAdd[i];
if (typeof (typeName) == "string" && egret.is(component, typeName)) if (typeof (typeName) == "string") {
components.push(component); if (egret.is(component, typeName)) {
else if (component instanceof typeName) { components.push(component);
components.push(component); }
}
else {
if (component instanceof typeName) {
components.push(component);
}
} }
} }
return components; return components;
@@ -3099,9 +3109,13 @@ var ColliderTriggerHelper = (function () {
} }
return false; 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; 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; this._activeTriggerIntersections.length = 0;
}; };
ColliderTriggerHelper.prototype.notifyTriggerListeners = function (collisionPair, isEntering) { ColliderTriggerHelper.prototype.notifyTriggerListeners = function (collisionPair, isEntering) {
@@ -3296,11 +3310,12 @@ var Shape = (function () {
}()); }());
var Polygon = (function (_super) { var Polygon = (function (_super) {
__extends(Polygon, _super); __extends(Polygon, _super);
function Polygon(vertCount, radius) { function Polygon(points, isBox) {
var _this = _super.call(this) || this; var _this = _super.call(this) || this;
_this.isUnrotated = true; _this.isUnrotated = true;
_this._areEdgeNormalsDirty = true; _this._areEdgeNormalsDirty = true;
_this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius)); _this.setPoints(points);
_this.isBox = isBox;
return _this; return _this;
} }
Object.defineProperty(Polygon.prototype, "edgeNormals", { Object.defineProperty(Polygon.prototype, "edgeNormals", {
@@ -3329,11 +3344,12 @@ var Polygon = (function (_super) {
} }
}; };
Polygon.prototype.setPoints = function (points) { Polygon.prototype.setPoints = function (points) {
var _this = this;
this.points = points; this.points = points;
this.recalculateCenterAndEdgeNormals(); this.recalculateCenterAndEdgeNormals();
this._originalPoints = new Array(this.points.length); this._originalPoints = [];
this.points.forEach(function (point) { return _this._originalPoints.push(point); }); for (var i = 0; i < this.points.length; i++) {
this._originalPoints.push(this.points[i]);
}
}; };
Polygon.prototype.collidesWithShape = function (other) { Polygon.prototype.collidesWithShape = function (other) {
var result = new CollisionResult(); var result = new CollisionResult();
@@ -3457,9 +3473,22 @@ var Polygon = (function (_super) {
}(Shape)); }(Shape));
var Box = (function (_super) { var Box = (function (_super) {
__extends(Box, _super); __extends(Box, _super);
function Box() { function Box(width, height) {
return _super !== null && _super.apply(this, arguments) || this; 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) { Box.prototype.updateBox = function (width, height) {
this.width = width; this.width = width;
this.height = height; this.height = height;
@@ -3755,10 +3784,18 @@ var SpatialHash = (function () {
this._overlapTestCircle.position = circleCenter; this._overlapTestCircle.position = circleCenter;
var resultCounter = 0; var resultCounter = 0;
var potentials = this.aabbBroadphase(bounds, null, layerMask); 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) if (resultCounter == results.length)
return resultCounter; return resultCounter;
}); }
return resultCounter; return resultCounter;
}; };
SpatialHash.prototype.aabbBroadphase = function (bounds, excludeCollider, layerMask) { SpatialHash.prototype.aabbBroadphase = function (bounds, excludeCollider, layerMask) {
@@ -4020,7 +4057,7 @@ var Vector2Ext = (function () {
Vector2Ext.transformA = function (sourceArray, sourceIndex, matrix, destinationArray, destinationIndex, length) { Vector2Ext.transformA = function (sourceArray, sourceIndex, matrix, destinationArray, destinationIndex, length) {
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
var position = sourceArray[sourceIndex + 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.x = (position.x * matrix.m11) + (position.y * matrix.m21) + matrix.m31;
destination.y = (position.x * matrix.m12) + (position.y * matrix.m22) + matrix.m32; destination.y = (position.x * matrix.m12) + (position.y * matrix.m22) + matrix.m32;
destinationArray[destinationIndex + i] = destination; 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" "lib": "lib"
}, },
"scripts": { "scripts": {
"test": "mocha --recursive --reporter tap --growl" "test": "mocha --recursive --reporter tap --growl",
"eslint": "eslint src --ext .ts"
}, },
"author": "yhh", "author": "yhh",
"license": "MIT", "license": "MIT",
@@ -18,6 +19,7 @@
"gulp": "^3.9.1", "gulp": "^3.9.1",
"gulp-babel": "^8.0.0", "gulp-babel": "^8.0.0",
"gulp-concat": "^2.6.1", "gulp-concat": "^2.6.1",
"gulp-inject-string": "^1.1.2",
"gulp-minify": "^3.1.0", "gulp-minify": "^3.1.0",
"gulp-string-replace": "^1.1.2", "gulp-string-replace": "^1.1.2",
"gulp-typescript": "^3.1.6", "gulp-typescript": "^3.1.6",
@@ -25,14 +27,10 @@
"tsify": "^3.0.1", "tsify": "^3.0.1",
"typescript": "^2.2.2", "typescript": "^2.2.2",
"vinyl-source-stream": "^1.1.0", "vinyl-source-stream": "^1.1.0",
"watchify": "^3.9.0", "watchify": "^3.9.0"
"gulp-inject-string": "^1.1.2"
}, },
"publishConfig": { "publishConfig": {
"registry": "https://npm.pkg.github.com/359807859@qq.com" "registry": "https://npm.pkg.github.com/359807859@qq.com"
}, },
"dependencies": { "dependencies": {}
"chai": "^4.2.0",
"mocha": "^8.0.1"
}
} }

View File

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

View File

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

View File

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

View File

@@ -55,9 +55,13 @@ class ColliderTriggerHelper {
return false; 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; 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; this._activeTriggerIntersections.length = 0;
} }

View File

@@ -3,6 +3,24 @@ class Box extends Polygon {
public width: number; public width: number;
public height: 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){ public updateBox(width: number, height: number){
this.width = width; this.width = width;
this.height = height; this.height = height;

View File

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

View File

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

View File

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