修复boxcollider碰撞问题

This commit is contained in:
yhh
2020-07-09 14:16:10 +08:00
parent aea50926a9
commit 6e3eb1189a
22 changed files with 241 additions and 177 deletions

View File

@@ -168,6 +168,7 @@ declare abstract class Component extends egret.DisplayObjectContainer {
updateInterval: number; updateInterval: number;
userData: any; userData: any;
enabled: boolean; enabled: boolean;
readonly localPosition: Vector2;
setEnabled(isEnabled: boolean): this; setEnabled(isEnabled: boolean): this;
initialize(): void; initialize(): void;
onAddedToEntity(): void; onAddedToEntity(): void;
@@ -200,6 +201,7 @@ declare class Entity extends egret.DisplayObjectContainer {
tag: number; tag: number;
readonly stage: egret.Stage; readonly stage: egret.Stage;
constructor(name: string); constructor(name: string);
private onAddToStage;
updateOrder: number; updateOrder: number;
roundPosition(): void; roundPosition(): void;
setUpdateOrder(updateOrder: number): this; setUpdateOrder(updateOrder: number): this;
@@ -446,6 +448,7 @@ declare abstract class Collider extends Component {
onEnabled(): void; onEnabled(): void;
onDisabled(): void; onDisabled(): void;
onEntityTransformChanged(comp: TransformComponent): void; onEntityTransformChanged(comp: TransformComponent): void;
update(): void;
} }
declare class BoxCollider extends Collider { declare class BoxCollider extends Collider {
width: number; width: number;
@@ -701,7 +704,7 @@ declare abstract class SceneTransition {
onBeginTransition(): Promise<void>; onBeginTransition(): Promise<void>;
protected transitionComplete(): void; protected transitionComplete(): void;
protected loadNextScene(): Promise<void>; protected loadNextScene(): Promise<void>;
tickEffectProgressProperty(filter: egret.CustomFilter, duration: number, easeType: Function, reverseDirection?: boolean): Promise<{}>; tickEffectProgressProperty(filter: egret.CustomFilter, duration: number, easeType: Function, reverseDirection?: boolean): Promise<boolean>;
} }
declare class FadeTransition extends SceneTransition { declare class FadeTransition extends SceneTransition {
fadeToColor: number; fadeToColor: number;
@@ -776,7 +779,6 @@ declare class Rectangle extends egret.Rectangle {
location: Vector2; location: Vector2;
size: Vector2; size: Vector2;
intersects(value: egret.Rectangle): boolean; intersects(value: egret.Rectangle): boolean;
containsInVec(value: Vector2): boolean;
containsRect(value: Rectangle): boolean; containsRect(value: Rectangle): boolean;
getHalfSize(): Vector2; getHalfSize(): Vector2;
static fromMinMax(minX: number, minY: number, maxX: number, maxY: number): Rectangle; static fromMinMax(minX: number, minY: number, maxX: number, maxY: number): Rectangle;
@@ -848,7 +850,7 @@ declare class Physics {
declare abstract class Shape { declare abstract class Shape {
bounds: Rectangle; bounds: Rectangle;
position: Vector2; position: Vector2;
center: Vector2; abstract center: Vector2;
abstract recalculateBounds(collider: Collider): any; abstract recalculateBounds(collider: Collider): any;
abstract pointCollidesWithShape(point: Vector2): CollisionResult; abstract pointCollidesWithShape(point: Vector2): CollisionResult;
abstract overlaps(other: Shape): any; abstract overlaps(other: Shape): any;
@@ -860,6 +862,7 @@ declare class Polygon extends Shape {
private _polygonCenter; private _polygonCenter;
private _areEdgeNormalsDirty; private _areEdgeNormalsDirty;
protected _originalPoints: Vector2[]; protected _originalPoints: Vector2[];
center: Vector2;
_edgeNormals: Vector2[]; _edgeNormals: Vector2[];
readonly edgeNormals: Vector2[]; readonly edgeNormals: Vector2[];
isBox: boolean; isBox: boolean;
@@ -893,6 +896,7 @@ declare class Box extends Polygon {
declare class Circle extends Shape { declare class Circle extends Shape {
radius: number; radius: number;
private _originalRadius; private _originalRadius;
center: Vector2;
constructor(radius: number); constructor(radius: number);
pointCollidesWithShape(point: Vector2): CollisionResult; pointCollidesWithShape(point: Vector2): CollisionResult;
collidesWithShape(other: Shape): CollisionResult; collidesWithShape(other: Shape): CollisionResult;
@@ -1027,7 +1031,6 @@ declare class Pair<T> {
} }
declare class RectangleExt { declare class RectangleExt {
static union(first: Rectangle, point: Vector2): Rectangle; static union(first: Rectangle, point: Vector2): Rectangle;
static unionR(value1: Rectangle, value2: Rectangle): Rectangle;
} }
declare class Triangulator { declare class Triangulator {
triangleIndices: number[]; triangleIndices: number[];

View File

@@ -891,6 +891,13 @@ var Component = (function (_super) {
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Object.defineProperty(Component.prototype, "localPosition", {
get: function () {
return new Vector2(this.entity.x + this.x, this.entity.y + this.y);
},
enumerable: true,
configurable: true
});
Component.prototype.setEnabled = function (isEnabled) { Component.prototype.setEnabled = function (isEnabled) {
if (this._enabled != isEnabled) { if (this._enabled != isEnabled) {
this._enabled = isEnabled; this._enabled = isEnabled;
@@ -940,6 +947,7 @@ var Entity = (function (_super) {
_this.components = new ComponentList(_this); _this.components = new ComponentList(_this);
_this.id = Entity._idGenerator++; _this.id = Entity._idGenerator++;
_this.componentBits = new BitSet(); _this.componentBits = new BitSet();
_this.addEventListener(egret.Event.ADDED_TO_STAGE, _this.onAddToStage, _this);
return _this; return _this;
} }
Object.defineProperty(Entity.prototype, "isDestoryed", { Object.defineProperty(Entity.prototype, "isDestoryed", {
@@ -974,6 +982,9 @@ var Entity = (function (_super) {
configurable: true configurable: true
}); });
Object.defineProperty(Entity.prototype, "rotation", { Object.defineProperty(Entity.prototype, "rotation", {
get: function () {
return this.$getRotation();
},
set: function (value) { set: function (value) {
this.$setRotation(value); this.$setRotation(value);
this.onEntityTransformChanged(TransformComponent.rotation); this.onEntityTransformChanged(TransformComponent.rotation);
@@ -1016,6 +1027,9 @@ var Entity = (function (_super) {
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Entity.prototype.onAddToStage = function () {
this.onEntityTransformChanged(TransformComponent.position);
};
Object.defineProperty(Entity.prototype, "updateOrder", { Object.defineProperty(Entity.prototype, "updateOrder", {
get: function () { get: function () {
return this._updateOrder; return this._updateOrder;
@@ -1116,8 +1130,11 @@ var Entity = (function (_super) {
}; };
Entity.prototype.destroy = function () { Entity.prototype.destroy = function () {
this._isDestoryed = true; this._isDestoryed = true;
this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
this.scene.entities.remove(this); this.scene.entities.remove(this);
this.removeChildren(); this.removeChildren();
if (this.parent)
this.parent.removeChild(this);
for (var i = this.numChildren - 1; i >= 0; i--) { for (var i = this.numChildren - 1; i >= 0; i--) {
var child = this.getChildAt(i); var child = this.getChildAt(i);
child.entity.destroy(); child.entity.destroy();
@@ -1976,15 +1993,15 @@ var Collider = (function (_super) {
} }
Object.defineProperty(Collider.prototype, "bounds", { Object.defineProperty(Collider.prototype, "bounds", {
get: function () { get: function () {
var bds = this.entity.getBounds(); this.shape.recalculateBounds(this);
return new Rectangle(bds.x, bds.y, bds.width, bds.height); return this.shape.bounds;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Object.defineProperty(Collider.prototype, "localOffset", { Object.defineProperty(Collider.prototype, "localOffset", {
get: function () { get: function () {
return new Vector2(this.x, this.y); return this._localOffset;
}, },
set: function (value) { set: function (value) {
this.setLocalOffset(value); this.setLocalOffset(value);
@@ -1995,8 +2012,7 @@ var Collider = (function (_super) {
Collider.prototype.setLocalOffset = function (offset) { Collider.prototype.setLocalOffset = function (offset) {
if (this._localOffset != offset) { if (this._localOffset != offset) {
this.unregisterColliderWithPhysicsSystem(); this.unregisterColliderWithPhysicsSystem();
this.$setX(offset.x); this._localOffset = offset;
this.$setY(offset.y);
this._localOffsetLength = this._localOffset.length(); this._localOffsetLength = this._localOffset.length();
this.registerColliderWithPhysicsSystem(); this.registerColliderWithPhysicsSystem();
} }
@@ -2030,15 +2046,20 @@ var Collider = (function (_super) {
if (!(this instanceof BoxCollider)) { if (!(this instanceof BoxCollider)) {
console.error("Only box and circle colliders can be created automatically"); console.error("Only box and circle colliders can be created automatically");
} }
var bounds = this.entity.getBounds(); var renderable = this.entity.getComponent(RenderableComponent);
var renderbaleBounds = new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height); if (renderable) {
var width = renderbaleBounds.width / this.entity.scale.x; var bounds = renderable.bounds;
var height = renderbaleBounds.height / this.entity.scale.y; var width = bounds.width / this.entity.scale.x;
if (this instanceof BoxCollider) { var height = bounds.height / this.entity.scale.y;
var boxCollider = this; if (this instanceof BoxCollider) {
boxCollider.width = width; var boxCollider = this;
boxCollider.height = height; boxCollider.width = width;
this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.position); boxCollider.height = height;
this.localOffset = bounds.location;
}
}
else {
console.warn("Collider has no shape and no RenderableComponent. Can't figure out how to size it.");
} }
} }
this._isParentEntityAddedToScene = true; this._isParentEntityAddedToScene = true;
@@ -2058,6 +2079,13 @@ var Collider = (function (_super) {
if (this._isColliderRegistered) if (this._isColliderRegistered)
Physics.updateCollider(this); Physics.updateCollider(this);
}; };
Collider.prototype.update = function () {
var renderable = this.entity.getComponent(RenderableComponent);
if (renderable) {
this.$setX(renderable.x + this.localOffset.x);
this.$setY(renderable.y + this.localOffset.y);
}
};
return Collider; return Collider;
}(Component)); }(Component));
var BoxCollider = (function (_super) { var BoxCollider = (function (_super) {
@@ -3525,11 +3553,6 @@ var Rectangle = (function (_super) {
value.top < this.bottom && value.top < this.bottom &&
this.top < value.bottom; this.top < value.bottom;
}; };
Rectangle.prototype.containsInVec = function (value) {
return ((((this.x <= value.x) && (value.x < (this.x + this.width))) &&
(this.y <= value.y)) &&
(value.y < (this.y + this.height)));
};
Rectangle.prototype.containsRect = function (value) { Rectangle.prototype.containsRect = function (value) {
return ((((this.x <= value.x) && (value.x < (this.x + this.width))) && return ((((this.x <= value.x) && (value.x < (this.x + this.width))) &&
(this.y <= value.y)) && (this.y <= value.y)) &&
@@ -3546,7 +3569,7 @@ var Rectangle = (function (_super) {
var res = new Vector2(); var res = new Vector2();
res.x = MathHelper.clamp(point.x, this.left, this.right); res.x = MathHelper.clamp(point.x, this.left, this.right);
res.y = MathHelper.clamp(point.y, this.top, this.bottom); res.y = MathHelper.clamp(point.y, this.top, this.bottom);
if (this.containsInVec(res)) { if (this.contains(res.x, res.y)) {
var dl = res.x - this.left; var dl = res.x - this.left;
var dr = this.right - res.x; var dr = this.right - res.x;
var dt = res.y - this.top; var dt = res.y - this.top;
@@ -3886,6 +3909,8 @@ var Physics = (function () {
}()); }());
var Shape = (function () { var Shape = (function () {
function Shape() { function Shape() {
this.bounds = new Rectangle();
this.position = Vector2.zero;
} }
return Shape; return Shape;
}()); }());
@@ -3895,6 +3920,7 @@ var Polygon = (function (_super) {
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.center = new Vector2();
_this.setPoints(points); _this.setPoints(points);
_this.isBox = isBox; _this.isBox = isBox;
return _this; return _this;
@@ -4019,7 +4045,7 @@ var Polygon = (function (_super) {
return verts; return verts;
}; };
Polygon.prototype.recalculateBounds = function (collider) { Polygon.prototype.recalculateBounds = function (collider) {
this.center = collider.localOffset; var localOffset = collider.localOffset;
if (collider.shouldColliderScaleAndRotateWithTransform) { if (collider.shouldColliderScaleAndRotateWithTransform) {
var hasUnitScale = true; var hasUnitScale = true;
var tempMat = void 0; var tempMat = void 0;
@@ -4029,23 +4055,24 @@ var Polygon = (function (_super) {
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
hasUnitScale = false; hasUnitScale = false;
var scaledOffset = Vector2.multiply(collider.localOffset, collider.entity.scale); var scaledOffset = Vector2.multiply(collider.localOffset, collider.entity.scale);
this.center = scaledOffset; localOffset = scaledOffset;
} }
if (collider.entity.rotation != 0) { if (collider.entity.rotation != 0) {
tempMat = Matrix2D.createRotation(collider.entity.rotation, tempMat); tempMat = Matrix2D.createRotation(collider.entity.rotation, tempMat);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg; var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
var offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.scale)).length(); var offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.scale)).length();
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, MathHelper.toDegrees(collider.entity.rotation) + offsetAngle); localOffset = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, MathHelper.toDegrees(collider.entity.rotation) + offsetAngle);
} }
tempMat = Matrix2D.createTranslation(this._polygonCenter.x, this._polygonCenter.y); tempMat = Matrix2D.createTranslation(this._polygonCenter.x, this._polygonCenter.y);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points); Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points);
this.isUnrotated = collider.entity.rotation == 0; this.isUnrotated = collider.entity.rotation == 0;
} }
this.position = Vector2.add(collider.entity.position, this.center); this.position = Vector2.add(collider.entity.position, localOffset);
this.bounds = Rectangle.rectEncompassingPoints(this.points); this.bounds = Rectangle.rectEncompassingPoints(this.points);
this.bounds.location = Vector2.add(this.bounds.location, this.position); this.bounds.location = Vector2.add(this.bounds.location, this.position);
this.center = localOffset;
}; };
return Polygon; return Polygon;
}(Shape)); }(Shape));
@@ -4096,7 +4123,7 @@ var Box = (function (_super) {
}; };
Box.prototype.containsPoint = function (point) { Box.prototype.containsPoint = function (point) {
if (this.isUnrotated) if (this.isUnrotated)
return this.bounds.containsInVec(point); return this.bounds.contains(point.x, point.y);
return _super.prototype.containsPoint.call(this, point); return _super.prototype.containsPoint.call(this, point);
}; };
return Box; return Box;
@@ -4105,6 +4132,7 @@ var Circle = (function (_super) {
__extends(Circle, _super); __extends(Circle, _super);
function Circle(radius) { function Circle(radius) {
var _this = _super.call(this) || this; var _this = _super.call(this) || this;
_this.center = new Vector2();
_this.radius = radius; _this.radius = radius;
_this._originalRadius = radius; _this._originalRadius = radius;
return _this; return _this;
@@ -4333,7 +4361,7 @@ var ShapeCollisions = (function () {
ShapeCollisions.boxToBox = function (first, second) { ShapeCollisions.boxToBox = function (first, second) {
var result = new CollisionResult(); var result = new CollisionResult();
var minkowskiDiff = this.minkowskiDifference(first, second); var minkowskiDiff = this.minkowskiDifference(first, second);
if (minkowskiDiff.containsInVec(new Vector2(0, 0))) { if (minkowskiDiff.contains(0, 0)) {
result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin(); result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin();
if (result.minimumTranslationVector.x == 0 && result.minimumTranslationVector.y == 0) if (result.minimumTranslationVector.x == 0 && result.minimumTranslationVector.y == 0)
return null; return null;
@@ -4381,10 +4409,10 @@ var SpatialHash = (function () {
collider.registeredPhysicsBounds = bounds; collider.registeredPhysicsBounds = bounds;
var p1 = this.cellCoords(bounds.x, bounds.y); var p1 = this.cellCoords(bounds.x, bounds.y);
var p2 = this.cellCoords(bounds.right, bounds.bottom); var p2 = this.cellCoords(bounds.right, bounds.bottom);
if (!this.gridBounds.containsInVec(new Vector2(p1.x, p1.y))) { if (!this.gridBounds.contains(p1.x, p1.y)) {
this.gridBounds = RectangleExt.union(this.gridBounds, p1); this.gridBounds = RectangleExt.union(this.gridBounds, p1);
} }
if (!this.gridBounds.containsInVec(new Vector2(p2.x, p2.y))) { if (!this.gridBounds.contains(p2.x, p2.y)) {
this.gridBounds = RectangleExt.union(this.gridBounds, p2); this.gridBounds = RectangleExt.union(this.gridBounds, p2);
} }
for (var x = p1.x; x <= p2.x; x++) { for (var x = p1.x; x <= p2.x; x++) {
@@ -4807,15 +4835,8 @@ var RectangleExt = (function () {
} }
RectangleExt.union = function (first, point) { RectangleExt.union = function (first, point) {
var rect = new Rectangle(point.x, point.y, 0, 0); var rect = new Rectangle(point.x, point.y, 0, 0);
return this.unionR(first, rect); var rectResult = first.union(rect);
}; return new Rectangle(rectResult.x, rectResult.y, rectResult.width, rectResult.height);
RectangleExt.unionR = function (value1, value2) {
var result = new Rectangle();
result.x = Math.min(value1.x, value2.x);
result.y = Math.min(value1.y, value2.y);
result.width = Math.max(value1.right, value2.right) - result.x;
result.height = Math.max(value1.bottom, value2.bottom) - result.y;
return result;
}; };
return RectangleExt; return RectangleExt;
}()); }());

File diff suppressed because one or more lines are too long

View File

@@ -15,13 +15,13 @@ class MainScene extends Scene {
bg.addComponent(new PlayerController()); bg.addComponent(new PlayerController());
bg.addComponent(new Mover()); bg.addComponent(new Mover());
bg.addComponent(new BoxCollider()); bg.addComponent(new BoxCollider());
bg.position = new Vector2(300, 300); bg.position = new Vector2(Math.random() * 300, Math.random() * 300);
for (let i = 0; i < 1; i++) { for (let i = 0; i < 20; i++) {
let sprite = new Sprite(RES.getRes("checkbox_select_disabled_png")); let sprite = new Sprite(RES.getRes("checkbox_select_disabled_png"));
let player2 = this.createEntity("player2"); let player2 = this.createEntity("player2");
player2.addComponent(new SpriteRenderer()).setSprite(sprite); player2.addComponent(new SpriteRenderer()).setSprite(sprite);
player2.position = new Vector2(200, 200); player2.position = new Vector2(Math.random() * 1000, Math.random() * 1000);
player2.addComponent(new BoxCollider()); player2.addComponent(new BoxCollider());
} }

View File

@@ -34,23 +34,23 @@ class PlayerController extends Component {
return; return;
if (this.down){ if (this.down){
// let camera = SceneManager.scene.camera; let camera = SceneManager.scene.camera;
// let moveLeft: number = 0; let moveLeft: number = 0;
// let moveRight: number = 0; let moveRight: number = 0;
// let speed = 100; let speed = 100;
// let worldPos = Input.touchPosition; let worldPos = Input.touchPosition;
// if (worldPos.x < this.spriteRenderer.x){ if (worldPos.x < this.spriteRenderer.localPosition.x){
// moveLeft = -1; moveLeft = -1;
// } else if(worldPos.x > this.spriteRenderer.x){ } else if(worldPos.x > this.spriteRenderer.localPosition.x){
// moveLeft = 1; moveLeft = 1;
// } }
// if (worldPos.y < this.spriteRenderer.y){ if (worldPos.y < this.spriteRenderer.localPosition.y){
// moveRight = -1; moveRight = -1;
// } else if(worldPos.y > this.spriteRenderer.y){ } else if(worldPos.y > this.spriteRenderer.localPosition.y){
// moveRight = 1; moveRight = 1;
// } }
this.mover.move(new Vector2(-1, -1)); this.mover.move(new Vector2(moveLeft * speed * Time.deltaTime, moveRight * speed * Time.deltaTime));
} }
} }
} }

View File

@@ -32,7 +32,7 @@ egret_native.egretStart = function () {
//The following is automatically modified, please do not modify //The following is automatically modified, please do not modify
//----auto option start---- //----auto option start----
entryClassName: "Main", entryClassName: "Main",
frameRate: 30, frameRate: 60,
scaleMode: "fixedWidth", scaleMode: "fixedWidth",
contentWidth: 640, contentWidth: 640,
contentHeight: 1136, contentHeight: 1136,

View File

@@ -168,6 +168,7 @@ declare abstract class Component extends egret.DisplayObjectContainer {
updateInterval: number; updateInterval: number;
userData: any; userData: any;
enabled: boolean; enabled: boolean;
readonly localPosition: Vector2;
setEnabled(isEnabled: boolean): this; setEnabled(isEnabled: boolean): this;
initialize(): void; initialize(): void;
onAddedToEntity(): void; onAddedToEntity(): void;
@@ -200,6 +201,7 @@ declare class Entity extends egret.DisplayObjectContainer {
tag: number; tag: number;
readonly stage: egret.Stage; readonly stage: egret.Stage;
constructor(name: string); constructor(name: string);
private onAddToStage;
updateOrder: number; updateOrder: number;
roundPosition(): void; roundPosition(): void;
setUpdateOrder(updateOrder: number): this; setUpdateOrder(updateOrder: number): this;
@@ -446,6 +448,7 @@ declare abstract class Collider extends Component {
onEnabled(): void; onEnabled(): void;
onDisabled(): void; onDisabled(): void;
onEntityTransformChanged(comp: TransformComponent): void; onEntityTransformChanged(comp: TransformComponent): void;
update(): void;
} }
declare class BoxCollider extends Collider { declare class BoxCollider extends Collider {
width: number; width: number;
@@ -701,7 +704,7 @@ declare abstract class SceneTransition {
onBeginTransition(): Promise<void>; onBeginTransition(): Promise<void>;
protected transitionComplete(): void; protected transitionComplete(): void;
protected loadNextScene(): Promise<void>; protected loadNextScene(): Promise<void>;
tickEffectProgressProperty(filter: egret.CustomFilter, duration: number, easeType: Function, reverseDirection?: boolean): Promise<{}>; tickEffectProgressProperty(filter: egret.CustomFilter, duration: number, easeType: Function, reverseDirection?: boolean): Promise<boolean>;
} }
declare class FadeTransition extends SceneTransition { declare class FadeTransition extends SceneTransition {
fadeToColor: number; fadeToColor: number;
@@ -776,7 +779,6 @@ declare class Rectangle extends egret.Rectangle {
location: Vector2; location: Vector2;
size: Vector2; size: Vector2;
intersects(value: egret.Rectangle): boolean; intersects(value: egret.Rectangle): boolean;
containsInVec(value: Vector2): boolean;
containsRect(value: Rectangle): boolean; containsRect(value: Rectangle): boolean;
getHalfSize(): Vector2; getHalfSize(): Vector2;
static fromMinMax(minX: number, minY: number, maxX: number, maxY: number): Rectangle; static fromMinMax(minX: number, minY: number, maxX: number, maxY: number): Rectangle;
@@ -848,7 +850,7 @@ declare class Physics {
declare abstract class Shape { declare abstract class Shape {
bounds: Rectangle; bounds: Rectangle;
position: Vector2; position: Vector2;
center: Vector2; abstract center: Vector2;
abstract recalculateBounds(collider: Collider): any; abstract recalculateBounds(collider: Collider): any;
abstract pointCollidesWithShape(point: Vector2): CollisionResult; abstract pointCollidesWithShape(point: Vector2): CollisionResult;
abstract overlaps(other: Shape): any; abstract overlaps(other: Shape): any;
@@ -860,6 +862,7 @@ declare class Polygon extends Shape {
private _polygonCenter; private _polygonCenter;
private _areEdgeNormalsDirty; private _areEdgeNormalsDirty;
protected _originalPoints: Vector2[]; protected _originalPoints: Vector2[];
center: Vector2;
_edgeNormals: Vector2[]; _edgeNormals: Vector2[];
readonly edgeNormals: Vector2[]; readonly edgeNormals: Vector2[];
isBox: boolean; isBox: boolean;
@@ -893,6 +896,7 @@ declare class Box extends Polygon {
declare class Circle extends Shape { declare class Circle extends Shape {
radius: number; radius: number;
private _originalRadius; private _originalRadius;
center: Vector2;
constructor(radius: number); constructor(radius: number);
pointCollidesWithShape(point: Vector2): CollisionResult; pointCollidesWithShape(point: Vector2): CollisionResult;
collidesWithShape(other: Shape): CollisionResult; collidesWithShape(other: Shape): CollisionResult;
@@ -1027,7 +1031,6 @@ declare class Pair<T> {
} }
declare class RectangleExt { declare class RectangleExt {
static union(first: Rectangle, point: Vector2): Rectangle; static union(first: Rectangle, point: Vector2): Rectangle;
static unionR(value1: Rectangle, value2: Rectangle): Rectangle;
} }
declare class Triangulator { declare class Triangulator {
triangleIndices: number[]; triangleIndices: number[];

View File

@@ -891,6 +891,13 @@ var Component = (function (_super) {
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Object.defineProperty(Component.prototype, "localPosition", {
get: function () {
return new Vector2(this.entity.x + this.x, this.entity.y + this.y);
},
enumerable: true,
configurable: true
});
Component.prototype.setEnabled = function (isEnabled) { Component.prototype.setEnabled = function (isEnabled) {
if (this._enabled != isEnabled) { if (this._enabled != isEnabled) {
this._enabled = isEnabled; this._enabled = isEnabled;
@@ -940,6 +947,7 @@ var Entity = (function (_super) {
_this.components = new ComponentList(_this); _this.components = new ComponentList(_this);
_this.id = Entity._idGenerator++; _this.id = Entity._idGenerator++;
_this.componentBits = new BitSet(); _this.componentBits = new BitSet();
_this.addEventListener(egret.Event.ADDED_TO_STAGE, _this.onAddToStage, _this);
return _this; return _this;
} }
Object.defineProperty(Entity.prototype, "isDestoryed", { Object.defineProperty(Entity.prototype, "isDestoryed", {
@@ -974,6 +982,9 @@ var Entity = (function (_super) {
configurable: true configurable: true
}); });
Object.defineProperty(Entity.prototype, "rotation", { Object.defineProperty(Entity.prototype, "rotation", {
get: function () {
return this.$getRotation();
},
set: function (value) { set: function (value) {
this.$setRotation(value); this.$setRotation(value);
this.onEntityTransformChanged(TransformComponent.rotation); this.onEntityTransformChanged(TransformComponent.rotation);
@@ -1016,6 +1027,9 @@ var Entity = (function (_super) {
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Entity.prototype.onAddToStage = function () {
this.onEntityTransformChanged(TransformComponent.position);
};
Object.defineProperty(Entity.prototype, "updateOrder", { Object.defineProperty(Entity.prototype, "updateOrder", {
get: function () { get: function () {
return this._updateOrder; return this._updateOrder;
@@ -1116,8 +1130,11 @@ var Entity = (function (_super) {
}; };
Entity.prototype.destroy = function () { Entity.prototype.destroy = function () {
this._isDestoryed = true; this._isDestoryed = true;
this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
this.scene.entities.remove(this); this.scene.entities.remove(this);
this.removeChildren(); this.removeChildren();
if (this.parent)
this.parent.removeChild(this);
for (var i = this.numChildren - 1; i >= 0; i--) { for (var i = this.numChildren - 1; i >= 0; i--) {
var child = this.getChildAt(i); var child = this.getChildAt(i);
child.entity.destroy(); child.entity.destroy();
@@ -1976,15 +1993,15 @@ var Collider = (function (_super) {
} }
Object.defineProperty(Collider.prototype, "bounds", { Object.defineProperty(Collider.prototype, "bounds", {
get: function () { get: function () {
var bds = this.entity.getBounds(); this.shape.recalculateBounds(this);
return new Rectangle(bds.x, bds.y, bds.width, bds.height); return this.shape.bounds;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Object.defineProperty(Collider.prototype, "localOffset", { Object.defineProperty(Collider.prototype, "localOffset", {
get: function () { get: function () {
return new Vector2(this.x, this.y); return this._localOffset;
}, },
set: function (value) { set: function (value) {
this.setLocalOffset(value); this.setLocalOffset(value);
@@ -1995,8 +2012,7 @@ var Collider = (function (_super) {
Collider.prototype.setLocalOffset = function (offset) { Collider.prototype.setLocalOffset = function (offset) {
if (this._localOffset != offset) { if (this._localOffset != offset) {
this.unregisterColliderWithPhysicsSystem(); this.unregisterColliderWithPhysicsSystem();
this.$setX(offset.x); this._localOffset = offset;
this.$setY(offset.y);
this._localOffsetLength = this._localOffset.length(); this._localOffsetLength = this._localOffset.length();
this.registerColliderWithPhysicsSystem(); this.registerColliderWithPhysicsSystem();
} }
@@ -2030,15 +2046,20 @@ var Collider = (function (_super) {
if (!(this instanceof BoxCollider)) { if (!(this instanceof BoxCollider)) {
console.error("Only box and circle colliders can be created automatically"); console.error("Only box and circle colliders can be created automatically");
} }
var bounds = this.entity.getBounds(); var renderable = this.entity.getComponent(RenderableComponent);
var renderbaleBounds = new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height); if (renderable) {
var width = renderbaleBounds.width / this.entity.scale.x; var bounds = renderable.bounds;
var height = renderbaleBounds.height / this.entity.scale.y; var width = bounds.width / this.entity.scale.x;
if (this instanceof BoxCollider) { var height = bounds.height / this.entity.scale.y;
var boxCollider = this; if (this instanceof BoxCollider) {
boxCollider.width = width; var boxCollider = this;
boxCollider.height = height; boxCollider.width = width;
this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.position); boxCollider.height = height;
this.localOffset = bounds.location;
}
}
else {
console.warn("Collider has no shape and no RenderableComponent. Can't figure out how to size it.");
} }
} }
this._isParentEntityAddedToScene = true; this._isParentEntityAddedToScene = true;
@@ -2058,6 +2079,13 @@ var Collider = (function (_super) {
if (this._isColliderRegistered) if (this._isColliderRegistered)
Physics.updateCollider(this); Physics.updateCollider(this);
}; };
Collider.prototype.update = function () {
var renderable = this.entity.getComponent(RenderableComponent);
if (renderable) {
this.$setX(renderable.x + this.localOffset.x);
this.$setY(renderable.y + this.localOffset.y);
}
};
return Collider; return Collider;
}(Component)); }(Component));
var BoxCollider = (function (_super) { var BoxCollider = (function (_super) {
@@ -3525,11 +3553,6 @@ var Rectangle = (function (_super) {
value.top < this.bottom && value.top < this.bottom &&
this.top < value.bottom; this.top < value.bottom;
}; };
Rectangle.prototype.containsInVec = function (value) {
return ((((this.x <= value.x) && (value.x < (this.x + this.width))) &&
(this.y <= value.y)) &&
(value.y < (this.y + this.height)));
};
Rectangle.prototype.containsRect = function (value) { Rectangle.prototype.containsRect = function (value) {
return ((((this.x <= value.x) && (value.x < (this.x + this.width))) && return ((((this.x <= value.x) && (value.x < (this.x + this.width))) &&
(this.y <= value.y)) && (this.y <= value.y)) &&
@@ -3546,7 +3569,7 @@ var Rectangle = (function (_super) {
var res = new Vector2(); var res = new Vector2();
res.x = MathHelper.clamp(point.x, this.left, this.right); res.x = MathHelper.clamp(point.x, this.left, this.right);
res.y = MathHelper.clamp(point.y, this.top, this.bottom); res.y = MathHelper.clamp(point.y, this.top, this.bottom);
if (this.containsInVec(res)) { if (this.contains(res.x, res.y)) {
var dl = res.x - this.left; var dl = res.x - this.left;
var dr = this.right - res.x; var dr = this.right - res.x;
var dt = res.y - this.top; var dt = res.y - this.top;
@@ -3886,6 +3909,8 @@ var Physics = (function () {
}()); }());
var Shape = (function () { var Shape = (function () {
function Shape() { function Shape() {
this.bounds = new Rectangle();
this.position = Vector2.zero;
} }
return Shape; return Shape;
}()); }());
@@ -3895,6 +3920,7 @@ var Polygon = (function (_super) {
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.center = new Vector2();
_this.setPoints(points); _this.setPoints(points);
_this.isBox = isBox; _this.isBox = isBox;
return _this; return _this;
@@ -4019,7 +4045,7 @@ var Polygon = (function (_super) {
return verts; return verts;
}; };
Polygon.prototype.recalculateBounds = function (collider) { Polygon.prototype.recalculateBounds = function (collider) {
this.center = collider.localOffset; var localOffset = collider.localOffset;
if (collider.shouldColliderScaleAndRotateWithTransform) { if (collider.shouldColliderScaleAndRotateWithTransform) {
var hasUnitScale = true; var hasUnitScale = true;
var tempMat = void 0; var tempMat = void 0;
@@ -4029,23 +4055,24 @@ var Polygon = (function (_super) {
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
hasUnitScale = false; hasUnitScale = false;
var scaledOffset = Vector2.multiply(collider.localOffset, collider.entity.scale); var scaledOffset = Vector2.multiply(collider.localOffset, collider.entity.scale);
this.center = scaledOffset; localOffset = scaledOffset;
} }
if (collider.entity.rotation != 0) { if (collider.entity.rotation != 0) {
tempMat = Matrix2D.createRotation(collider.entity.rotation, tempMat); tempMat = Matrix2D.createRotation(collider.entity.rotation, tempMat);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg; var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
var offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.scale)).length(); var offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.scale)).length();
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, MathHelper.toDegrees(collider.entity.rotation) + offsetAngle); localOffset = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, MathHelper.toDegrees(collider.entity.rotation) + offsetAngle);
} }
tempMat = Matrix2D.createTranslation(this._polygonCenter.x, this._polygonCenter.y); tempMat = Matrix2D.createTranslation(this._polygonCenter.x, this._polygonCenter.y);
combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat); combinedMatrix = Matrix2D.multiply(combinedMatrix, tempMat);
Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points); Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points);
this.isUnrotated = collider.entity.rotation == 0; this.isUnrotated = collider.entity.rotation == 0;
} }
this.position = Vector2.add(collider.entity.position, this.center); this.position = Vector2.add(collider.entity.position, localOffset);
this.bounds = Rectangle.rectEncompassingPoints(this.points); this.bounds = Rectangle.rectEncompassingPoints(this.points);
this.bounds.location = Vector2.add(this.bounds.location, this.position); this.bounds.location = Vector2.add(this.bounds.location, this.position);
this.center = localOffset;
}; };
return Polygon; return Polygon;
}(Shape)); }(Shape));
@@ -4096,7 +4123,7 @@ var Box = (function (_super) {
}; };
Box.prototype.containsPoint = function (point) { Box.prototype.containsPoint = function (point) {
if (this.isUnrotated) if (this.isUnrotated)
return this.bounds.containsInVec(point); return this.bounds.contains(point.x, point.y);
return _super.prototype.containsPoint.call(this, point); return _super.prototype.containsPoint.call(this, point);
}; };
return Box; return Box;
@@ -4105,6 +4132,7 @@ var Circle = (function (_super) {
__extends(Circle, _super); __extends(Circle, _super);
function Circle(radius) { function Circle(radius) {
var _this = _super.call(this) || this; var _this = _super.call(this) || this;
_this.center = new Vector2();
_this.radius = radius; _this.radius = radius;
_this._originalRadius = radius; _this._originalRadius = radius;
return _this; return _this;
@@ -4333,7 +4361,7 @@ var ShapeCollisions = (function () {
ShapeCollisions.boxToBox = function (first, second) { ShapeCollisions.boxToBox = function (first, second) {
var result = new CollisionResult(); var result = new CollisionResult();
var minkowskiDiff = this.minkowskiDifference(first, second); var minkowskiDiff = this.minkowskiDifference(first, second);
if (minkowskiDiff.containsInVec(new Vector2(0, 0))) { if (minkowskiDiff.contains(0, 0)) {
result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin(); result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin();
if (result.minimumTranslationVector.x == 0 && result.minimumTranslationVector.y == 0) if (result.minimumTranslationVector.x == 0 && result.minimumTranslationVector.y == 0)
return null; return null;
@@ -4381,10 +4409,10 @@ var SpatialHash = (function () {
collider.registeredPhysicsBounds = bounds; collider.registeredPhysicsBounds = bounds;
var p1 = this.cellCoords(bounds.x, bounds.y); var p1 = this.cellCoords(bounds.x, bounds.y);
var p2 = this.cellCoords(bounds.right, bounds.bottom); var p2 = this.cellCoords(bounds.right, bounds.bottom);
if (!this.gridBounds.containsInVec(new Vector2(p1.x, p1.y))) { if (!this.gridBounds.contains(p1.x, p1.y)) {
this.gridBounds = RectangleExt.union(this.gridBounds, p1); this.gridBounds = RectangleExt.union(this.gridBounds, p1);
} }
if (!this.gridBounds.containsInVec(new Vector2(p2.x, p2.y))) { if (!this.gridBounds.contains(p2.x, p2.y)) {
this.gridBounds = RectangleExt.union(this.gridBounds, p2); this.gridBounds = RectangleExt.union(this.gridBounds, p2);
} }
for (var x = p1.x; x <= p2.x; x++) { for (var x = p1.x; x <= p2.x; x++) {
@@ -4807,15 +4835,8 @@ var RectangleExt = (function () {
} }
RectangleExt.union = function (first, point) { RectangleExt.union = function (first, point) {
var rect = new Rectangle(point.x, point.y, 0, 0); var rect = new Rectangle(point.x, point.y, 0, 0);
return this.unionR(first, rect); var rectResult = first.union(rect);
}; return new Rectangle(rectResult.x, rectResult.y, rectResult.width, rectResult.height);
RectangleExt.unionR = function (value1, value2) {
var result = new Rectangle();
result.x = Math.min(value1.x, value2.x);
result.y = Math.min(value1.y, value2.y);
result.width = Math.max(value1.right, value2.right) - result.x;
result.height = Math.max(value1.bottom, value2.bottom) - result.y;
return result;
}; };
return RectangleExt; return RectangleExt;
}()); }());

File diff suppressed because one or more lines are too long

View File

@@ -13,6 +13,10 @@ abstract class Component extends egret.DisplayObjectContainer {
this.setEnabled(value); this.setEnabled(value);
} }
public get localPosition(){
return new Vector2(this.entity.x + this.x, this.entity.y + this.y);
}
public setEnabled(isEnabled: boolean){ public setEnabled(isEnabled: boolean){
if (this._enabled != isEnabled){ if (this._enabled != isEnabled){
this._enabled = isEnabled; this._enabled = isEnabled;

View File

@@ -1,4 +1,4 @@
abstract class Collider extends Component{ abstract class Collider extends Component {
/** 对撞机的基本形状 */ /** 对撞机的基本形状 */
public shape: Shape; public shape: Shape;
/** 在处理冲突时physicsLayer可以用作过滤器。Flags类有帮助位掩码的方法。 */ /** 在处理冲突时physicsLayer可以用作过滤器。Flags类有帮助位掩码的方法。 */
@@ -24,27 +24,25 @@ abstract class Collider extends Component{
protected _isColliderRegistered; protected _isColliderRegistered;
public get bounds(): Rectangle { public get bounds(): Rectangle {
// this.shape.recalculateBounds(this); this.shape.recalculateBounds(this);
let bds = this.entity.getBounds(); return this.shape.bounds;
return new Rectangle(bds.x, bds.y, bds.width, bds.height);
} }
public get localOffset(){ public get localOffset() {
return new Vector2(this.x, this.y); return this._localOffset;
} }
/** /**
* 将localOffset添加到实体。获取碰撞器的最终位置。这允许您向一个实体添加多个碰撞器并分别定位它们。 * 将localOffset添加到实体。获取碰撞器的最终位置。这允许您向一个实体添加多个碰撞器并分别定位它们。
*/ */
public set localOffset(value: Vector2){ public set localOffset(value: Vector2) {
this.setLocalOffset(value); this.setLocalOffset(value);
} }
public setLocalOffset(offset: Vector2){ public setLocalOffset(offset: Vector2) {
if (this._localOffset != offset){ if (this._localOffset != offset) {
this.unregisterColliderWithPhysicsSystem(); this.unregisterColliderWithPhysicsSystem();
this.$setX(offset.x); this._localOffset = offset;
this.$setY(offset.y);
this._localOffsetLength = this._localOffset.length(); this._localOffsetLength = this._localOffset.length();
this.registerColliderWithPhysicsSystem(); this.registerColliderWithPhysicsSystem();
} }
@@ -53,9 +51,9 @@ abstract class Collider extends Component{
/** /**
* 父实体会在不同的时间调用它(当添加到场景,启用,等等) * 父实体会在不同的时间调用它(当添加到场景,启用,等等)
*/ */
public registerColliderWithPhysicsSystem(){ public registerColliderWithPhysicsSystem() {
// 如果在将我们添加到实体之前更改了origin等属性则实体可以为null // 如果在将我们添加到实体之前更改了origin等属性则实体可以为null
if (this._isParentEntityAddedToScene && !this._isColliderRegistered){ if (this._isParentEntityAddedToScene && !this._isColliderRegistered) {
Physics.addCollider(this); Physics.addCollider(this);
this._isColliderRegistered = true; this._isColliderRegistered = true;
} }
@@ -64,8 +62,8 @@ abstract class Collider extends Component{
/** /**
* 父实体会在不同的时候调用它(从场景中移除,禁用,等等) * 父实体会在不同的时候调用它(从场景中移除,禁用,等等)
*/ */
public unregisterColliderWithPhysicsSystem(){ public unregisterColliderWithPhysicsSystem() {
if (this._isParentEntityAddedToScene && this._isColliderRegistered){ if (this._isParentEntityAddedToScene && this._isColliderRegistered) {
Physics.removeCollider(this); Physics.removeCollider(this);
} }
this._isColliderRegistered = false; this._isColliderRegistered = false;
@@ -75,7 +73,7 @@ abstract class Collider extends Component{
* 检查这个形状是否与物理系统中的其他对撞机重叠 * 检查这个形状是否与物理系统中的其他对撞机重叠
* @param other * @param other
*/ */
public overlaps(other: Collider){ public overlaps(other: Collider) {
return this.shape.overlaps(other.shape); return this.shape.overlaps(other.shape);
} }
@@ -84,7 +82,7 @@ abstract class Collider extends Component{
* @param collider * @param collider
* @param motion * @param motion
*/ */
public collidesWith(collider: Collider, motion: Vector2){ public collidesWith(collider: Collider, motion: Vector2) {
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠 // 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
let oldPosition = this.shape.position; let oldPosition = this.shape.position;
this.shape.position = Vector2.add(this.shape.position, motion); this.shape.position = Vector2.add(this.shape.position, motion);
@@ -99,48 +97,60 @@ abstract class Collider extends Component{
return result; return result;
} }
public onAddedToEntity(){ public onAddedToEntity() {
if (this._colliderRequiresAutoSizing){ if (this._colliderRequiresAutoSizing) {
if (!(this instanceof BoxCollider)){ if (!(this instanceof BoxCollider)) {
console.error("Only box and circle colliders can be created automatically"); console.error("Only box and circle colliders can be created automatically");
} }
let bounds = this.entity.getBounds(); let renderable = this.entity.getComponent<RenderableComponent>(RenderableComponent);
let renderbaleBounds = new Rectangle(bounds.x, bounds.y, bounds.width, bounds.height); if (renderable) {
let bounds = renderable.bounds;
// 这里我们需要大小*反尺度,因为当我们自动调整碰撞器的大小时,它需要没有缩放的渲染 // 这里我们需要大小*反尺度,因为当我们自动调整碰撞器的大小时,它需要没有缩放的渲染
let width = renderbaleBounds.width / this.entity.scale.x; let width = bounds.width / this.entity.scale.x;
let height = renderbaleBounds.height / this.entity.scale.y; let height = bounds.height / this.entity.scale.y;
if (this instanceof BoxCollider){ if (this instanceof BoxCollider) {
let boxCollider = this as BoxCollider; let boxCollider = this as BoxCollider;
boxCollider.width = width; boxCollider.width = width;
boxCollider.height = height; boxCollider.height = height;
// 获取渲染的中心将其转移到本地坐标并使用它作为碰撞器的localOffset // 获取渲染的中心将其转移到本地坐标并使用它作为碰撞器的localOffset
this.localOffset = Vector2.subtract(renderbaleBounds.center, this.entity.position); this.localOffset = bounds.location;
}
} else {
console.warn("Collider has no shape and no RenderableComponent. Can't figure out how to size it.");
} }
} }
this._isParentEntityAddedToScene = true; this._isParentEntityAddedToScene = true;
this.registerColliderWithPhysicsSystem(); this.registerColliderWithPhysicsSystem();
} }
public onRemovedFromEntity(){ public onRemovedFromEntity() {
this.unregisterColliderWithPhysicsSystem(); this.unregisterColliderWithPhysicsSystem();
this._isParentEntityAddedToScene = false; this._isParentEntityAddedToScene = false;
} }
public onEnabled(){ public onEnabled() {
this.registerColliderWithPhysicsSystem(); this.registerColliderWithPhysicsSystem();
} }
public onDisabled(){ public onDisabled() {
this.unregisterColliderWithPhysicsSystem(); this.unregisterColliderWithPhysicsSystem();
} }
public onEntityTransformChanged(comp: TransformComponent){ public onEntityTransformChanged(comp: TransformComponent) {
if (this._isColliderRegistered) if (this._isColliderRegistered)
Physics.updateCollider(this); Physics.updateCollider(this);
} }
public update(){
let renderable = this.entity.getComponent<RenderableComponent>(RenderableComponent);
if (renderable){
this.$setX(renderable.x + this.localOffset.x);
this.$setY(renderable.y + this.localOffset.y);
}
}
} }

View File

@@ -43,6 +43,10 @@ class Entity extends egret.DisplayObjectContainer {
this.onEntityTransformChanged(TransformComponent.rotation); this.onEntityTransformChanged(TransformComponent.rotation);
} }
public get rotation(){
return this.$getRotation();
}
public get enabled(){ public get enabled(){
return this._enabled; return this._enabled;
} }
@@ -81,6 +85,11 @@ class Entity extends egret.DisplayObjectContainer {
this.id = Entity._idGenerator ++; this.id = Entity._idGenerator ++;
this.componentBits = new BitSet(); this.componentBits = new BitSet();
this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
}
private onAddToStage(){
this.onEntityTransformChanged(TransformComponent.position);
} }
public get updateOrder(){ public get updateOrder(){
@@ -206,9 +215,14 @@ class Entity extends egret.DisplayObjectContainer {
public destroy(){ public destroy(){
this._isDestoryed = true; this._isDestoryed = true;
this.removeEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
this.scene.entities.remove(this); this.scene.entities.remove(this);
this.removeChildren(); this.removeChildren();
if (this.parent)
this.parent.removeChild(this);
for (let i = this.numChildren - 1; i >= 0; i --){ for (let i = this.numChildren - 1; i >= 0; i --){
let child = this.getChildAt(i); let child = this.getChildAt(i);
(child as Component).entity.destroy(); (child as Component).entity.destroy();

View File

@@ -62,7 +62,7 @@ abstract class SceneTransition {
this.isNewSceneLoaded = true; this.isNewSceneLoaded = true;
} }
public tickEffectProgressProperty(filter: egret.CustomFilter, duration: number, easeType: Function, reverseDirection = false){ public tickEffectProgressProperty(filter: egret.CustomFilter, duration: number, easeType: Function, reverseDirection = false): Promise<boolean>{
return new Promise((resolve)=>{ return new Promise((resolve)=>{
let start = reverseDirection ? 1 : 0; let start = reverseDirection ? 1 : 0;
let end = reverseDirection ? 0 : 1; let end = reverseDirection ? 0 : 1;

View File

@@ -10,7 +10,7 @@ class Flags {
* @param self * @param self
* @param flag * @param flag
*/ */
public static isFlagSet(self: number, flag: number){ public static isFlagSet(self: number, flag: number): boolean{
return (self & flag) != 0; return (self & flag) != 0;
} }
@@ -19,7 +19,7 @@ class Flags {
* @param self * @param self
* @param flag * @param flag
*/ */
public static isUnshiftedFlagSet(self: number, flag: number){ public static isUnshiftedFlagSet(self: number, flag: number): boolean{
flag = 1 << flag; flag = 1 << flag;
return (self & flag) != 0; return (self & flag) != 0;
} }

View File

@@ -41,16 +41,6 @@ class Rectangle extends egret.Rectangle {
this.top < value.bottom; this.top < value.bottom;
} }
/**
* 判断点是否在矩形内
* @param value
*/
public containsInVec(value: Vector2) {
return ((((this.x <= value.x) && (value.x < (this.x + this.width))) &&
(this.y <= value.y)) &&
(value.y < (this.y + this.height)));
}
/** /**
* 获取所提供的矩形是否在此矩形的边界内 * 获取所提供的矩形是否在此矩形的边界内
* @param value * @param value
@@ -89,7 +79,7 @@ class Rectangle extends egret.Rectangle {
res.y = MathHelper.clamp(point.y, this.top, this.bottom); res.y = MathHelper.clamp(point.y, this.top, this.bottom);
// 如果点在矩形内我们需要推res到边界因为它将在矩形内 // 如果点在矩形内我们需要推res到边界因为它将在矩形内
if (this.containsInVec(res)) { if (this.contains(res.x, res.y)) {
let dl = res.x - this.left; let dl = res.x - this.left;
let dr = this.right - res.x; let dr = this.right - res.x;
let dt = res.y - this.top; let dt = res.y - this.top;

View File

@@ -78,7 +78,7 @@ class Box extends Polygon {
public containsPoint(point: Vector2){ public containsPoint(point: Vector2){
if (this.isUnrotated) if (this.isUnrotated)
return this.bounds.containsInVec(point); return this.bounds.contains(point.x, point.y);
return super.containsPoint(point); return super.containsPoint(point);
} }

View File

@@ -2,6 +2,7 @@
class Circle extends Shape { class Circle extends Shape {
public radius: number; public radius: number;
private _originalRadius: number; private _originalRadius: number;
public center = new Vector2();
constructor(radius: number) { constructor(radius: number) {
super(); super();

View File

@@ -5,6 +5,7 @@ class Polygon extends Shape {
private _polygonCenter: Vector2; private _polygonCenter: Vector2;
private _areEdgeNormalsDirty = true; private _areEdgeNormalsDirty = true;
protected _originalPoints: Vector2[]; protected _originalPoints: Vector2[];
public center = new Vector2();
public _edgeNormals: Vector2[]; public _edgeNormals: Vector2[];
public get edgeNormals(){ public get edgeNormals(){
@@ -183,8 +184,8 @@ class Polygon extends Shape {
public recalculateBounds(collider: Collider) { public recalculateBounds(collider: Collider) {
// 如果我们没有旋转或不关心TRS我们使用localOffset作为中心我们会从那开始 // 如果我们没有旋转或不关心TRS我们使用localOffset作为中心我们会从那开始
this.center = collider.localOffset; // this.center = collider.localOffset;
let localOffset = collider.localOffset;
if (collider.shouldColliderScaleAndRotateWithTransform){ if (collider.shouldColliderScaleAndRotateWithTransform){
let hasUnitScale = true; let hasUnitScale = true;
let tempMat: Matrix2D; let tempMat: Matrix2D;
@@ -198,7 +199,7 @@ class Polygon extends Shape {
// 缩放偏移量并将其设置为中心。如果我们有旋转,它会在下面重置 // 缩放偏移量并将其设置为中心。如果我们有旋转,它会在下面重置
let scaledOffset = Vector2.multiply(collider.localOffset, collider.entity.scale); let scaledOffset = Vector2.multiply(collider.localOffset, collider.entity.scale);
this.center = scaledOffset; localOffset = scaledOffset;
} }
if (collider.entity.rotation != 0){ if (collider.entity.rotation != 0){
@@ -209,7 +210,7 @@ class Polygon extends Shape {
// 我们还需要处理这里的比例所以我们先对偏移进行缩放以得到合适的长度。 // 我们还需要处理这里的比例所以我们先对偏移进行缩放以得到合适的长度。
let offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg; let offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x) * MathHelper.Rad2Deg;
let offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.scale)).length(); let offsetLength = hasUnitScale ? collider._localOffsetLength : (Vector2.multiply(collider.localOffset, collider.entity.scale)).length();
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, MathHelper.toDegrees(collider.entity.rotation) + offsetAngle); localOffset = MathHelper.pointOnCirlce(Vector2.zero, offsetLength, MathHelper.toDegrees(collider.entity.rotation) + offsetAngle);
} }
tempMat = Matrix2D.createTranslation(this._polygonCenter.x, this._polygonCenter.y); tempMat = Matrix2D.createTranslation(this._polygonCenter.x, this._polygonCenter.y);
@@ -220,8 +221,9 @@ class Polygon extends Shape {
this.isUnrotated = collider.entity.rotation == 0; this.isUnrotated = collider.entity.rotation == 0;
} }
this.position = Vector2.add(collider.entity.position, this.center); this.position = Vector2.add(collider.entity.position, localOffset);
this.bounds = Rectangle.rectEncompassingPoints(this.points); this.bounds = Rectangle.rectEncompassingPoints(this.points);
this.bounds.location = Vector2.add(this.bounds.location, this.position); this.bounds.location = Vector2.add(this.bounds.location, this.position);
this.center = localOffset;
} }
} }

View File

@@ -1,7 +1,7 @@
abstract class Shape { abstract class Shape {
public bounds: Rectangle; public bounds: Rectangle = new Rectangle();
public position: Vector2; public position: Vector2 = Vector2.zero;
public center: Vector2; public abstract center: Vector2;
public abstract recalculateBounds(collider: Collider); public abstract recalculateBounds(collider: Collider);
public abstract pointCollidesWithShape(point: Vector2): CollisionResult; public abstract pointCollidesWithShape(point: Vector2): CollisionResult;

View File

@@ -274,7 +274,7 @@ class ShapeCollisions {
let result = new CollisionResult(); let result = new CollisionResult();
let minkowskiDiff = this.minkowskiDifference(first, second); let minkowskiDiff = this.minkowskiDifference(first, second);
if (minkowskiDiff.containsInVec(new Vector2(0, 0))){ if (minkowskiDiff.contains(0, 0)){
// 计算MTV。如果它是零我们就可以称它为非碰撞 // 计算MTV。如果它是零我们就可以称它为非碰撞
result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin(); result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin();

View File

@@ -51,11 +51,11 @@ class SpatialHash {
let p2 = this.cellCoords(bounds.right, bounds.bottom); let p2 = this.cellCoords(bounds.right, bounds.bottom);
// 更新边界以跟踪网格大小 // 更新边界以跟踪网格大小
if (!this.gridBounds.containsInVec(new Vector2(p1.x, p1.y))) { if (!this.gridBounds.contains(p1.x, p1.y)) {
this.gridBounds = RectangleExt.union(this.gridBounds, p1); this.gridBounds = RectangleExt.union(this.gridBounds, p1);
} }
if (!this.gridBounds.containsInVec(new Vector2(p2.x, p2.y))) { if (!this.gridBounds.contains(p2.x, p2.y)) {
this.gridBounds = RectangleExt.union(this.gridBounds, p2); this.gridBounds = RectangleExt.union(this.gridBounds, p2);
} }
@@ -190,6 +190,10 @@ class NumberDictionary {
return Long.fromNumber(x).shiftLeft(32).or(this.intToUint(y)).toString(); return Long.fromNumber(x).shiftLeft(32).or(this.intToUint(y)).toString();
} }
/**
*
* @param i
*/
private intToUint(i) { private intToUint(i) {
if (i >= 0) if (i >= 0)
return i; return i;

View File

@@ -1,16 +1,7 @@
class RectangleExt { class RectangleExt {
public static union(first: Rectangle, point: Vector2){ public static union(first: Rectangle, point: Vector2){
let rect = new Rectangle(point.x, point.y, 0, 0); let rect = new Rectangle(point.x, point.y, 0, 0);
return this.unionR(first, rect); let rectResult = first.union(rect);
} return new Rectangle(rectResult.x, rectResult.y, rectResult.width, rectResult.height);
public static unionR(value1: Rectangle, value2: Rectangle){
let result = new Rectangle();
result.x = Math.min(value1.x, value2.x);
result.y = Math.min(value1.y, value2.y);
result.width = Math.max(value1.right, value2.right) - result.x;
result.height = Math.max(value1.bottom, value2.bottom) - result.y;
return result;
} }
} }