优化matrix

This commit is contained in:
yhh
2020-07-23 13:25:10 +08:00
parent 1b52bc5fd1
commit e61dd0c16b
12 changed files with 1009 additions and 494 deletions
+44 -22
View File
@@ -114,6 +114,10 @@ declare module es {
static readonly unitX: Vector2; static readonly unitX: Vector2;
static readonly unitY: Vector2; static readonly unitY: Vector2;
constructor(x?: number, y?: number); constructor(x?: number, y?: number);
add(value: Vector2): Vector2;
divide(value: Vector2): Vector2;
multiply(value: Vector2): Vector2;
subtract(value: Vector2): this;
static add(value1: Vector2, value2: Vector2): Vector2; static add(value1: Vector2, value2: Vector2): Vector2;
static divide(value1: Vector2, value2: Vector2): Vector2; static divide(value1: Vector2, value2: Vector2): Vector2;
static multiply(value1: Vector2, value2: Vector2): Vector2; static multiply(value1: Vector2, value2: Vector2): Vector2;
@@ -401,6 +405,12 @@ declare module es {
lockOn = 0, lockOn = 0,
cameraWindow = 1 cameraWindow = 1
} }
class CameraInset {
left: number;
right: number;
top: number;
bottom: number;
}
class Camera extends Component { class Camera extends Component {
position: Vector2; position: Vector2;
rotation: number; rotation: number;
@@ -408,11 +418,20 @@ declare module es {
minimumZoom: number; minimumZoom: number;
maximumZoom: number; maximumZoom: number;
readonly bounds: Rectangle; readonly bounds: Rectangle;
readonly transformMatrix: Matrix2D;
readonly inverseTransformMatrix: Matrix2D;
origin: Vector2; origin: Vector2;
private _zoom; _zoom: any;
private _minimumZoom; _minimumZoom: number;
private _maximumZoom; _maximumZoom: number;
private _origin; _bounds: Rectangle;
_inset: CameraInset;
_transformMatrix: Matrix2D;
_inverseTransformMatrix: Matrix2D;
_origin: Vector2;
_areMatrixedDirty: boolean;
_areBoundsDirty: boolean;
_isProjectionMatrixDirty: boolean;
followLerp: number; followLerp: number;
deadzone: Rectangle; deadzone: Rectangle;
focusOffset: Vector2; focusOffset: Vector2;
@@ -425,13 +444,19 @@ declare module es {
_worldSpaceDeadZone: Rectangle; _worldSpaceDeadZone: Rectangle;
constructor(targetEntity?: Entity, cameraStyle?: CameraStyle); constructor(targetEntity?: Entity, cameraStyle?: CameraStyle);
onSceneSizeChanged(newWidth: number, newHeight: number): void; onSceneSizeChanged(newWidth: number, newHeight: number): void;
protected updateMatrixes(): void;
setInset(left: number, right: number, top: number, bottom: number): Camera;
setPosition(position: Vector2): this; setPosition(position: Vector2): this;
setRotation(rotation: number): Camera; setRotation(rotation: number): Camera;
setZoom(zoom: number): Camera; setZoom(zoom: number): Camera;
setMinimumZoom(minZoom: number): Camera; setMinimumZoom(minZoom: number): Camera;
setMaximumZoom(maxZoom: number): Camera; setMaximumZoom(maxZoom: number): Camera;
onEntityTransformChanged(comp: transform.Component): void;
zoomIn(deltaZoom: number): void; zoomIn(deltaZoom: number): void;
zoomOut(deltaZoom: number): void; zoomOut(deltaZoom: number): void;
worldToScreenPoint(worldPosition: Vector2): Vector2;
screenToWorldPoint(screenPosition: Vector2): Vector2;
mouseToWorldPoint(): Vector2;
onAddedToEntity(): void; onAddedToEntity(): void;
update(): void; update(): void;
clampToMapSize(position: Vector2): Vector2; clampToMapSize(position: Vector2): Vector2;
@@ -626,6 +651,8 @@ declare module es {
_localOffsetLength: number; _localOffsetLength: number;
protected _isParentEntityAddedToScene: any; protected _isParentEntityAddedToScene: any;
protected _isColliderRegistered: any; protected _isColliderRegistered: any;
_isPositionDirty: boolean;
_isRotationDirty: boolean;
setLocalOffset(offset: Vector2): Collider; setLocalOffset(offset: Vector2): Collider;
setShouldColliderScaleAndRotateWithTransform(shouldColliderScaleAndRotationWithTransform: boolean): Collider; setShouldColliderScaleAndRotateWithTransform(shouldColliderScaleAndRotationWithTransform: boolean): Collider;
onAddedToEntity(): void; onAddedToEntity(): void;
@@ -637,6 +664,7 @@ declare module es {
unregisterColliderWithPhysicsSystem(): void; unregisterColliderWithPhysicsSystem(): void;
overlaps(other: Collider): any; overlaps(other: Collider): any;
collidesWith(collider: Collider, motion: Vector2): CollisionResult; collidesWith(collider: Collider, motion: Vector2): CollisionResult;
clone(): Component;
} }
} }
declare module es { declare module es {
@@ -1079,31 +1107,24 @@ declare module es {
} }
} }
declare module es { declare module es {
class Matrix2D { class Matrix2D extends egret.Matrix {
m11: number; m11: number;
m12: number; m12: number;
m21: number; m21: number;
m22: number; m22: number;
m31: number; m31: number;
m32: number; m32: number;
private static _identity; static create(): Matrix2D;
static readonly identity: Matrix2D; identity(): Matrix2D;
constructor(m11?: number, m12?: number, m21?: number, m22?: number, m31?: number, m32?: number); translate(dx: number, dy: number): Matrix2D;
translation: Vector2; scale(sx: number, sy: number): Matrix2D;
rotation: number; rotate(angle: number): Matrix2D;
rotationDegrees: number; invert(): Matrix2D;
scale: Vector2; add(matrix: Matrix2D): Matrix2D;
static add(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D; substract(matrix: Matrix2D): Matrix2D;
static divide(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D; divide(matrix: Matrix2D): Matrix2D;
static multiply(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D; multiply(matrix: Matrix2D): Matrix2D;
static multiplyTranslation(matrix: Matrix2D, x: number, y: number): Matrix2D;
determinant(): number; determinant(): number;
static invert(matrix: Matrix2D, result?: Matrix2D): Matrix2D;
static createTranslation(xPosition: number, yPosition: number): Matrix2D;
static createTranslationVector(position: Vector2): Matrix2D;
static createRotation(radians: number, result?: Matrix2D): Matrix2D;
static createScale(xScale: number, yScale: number, result?: Matrix2D): Matrix2D;
toEgretMatrix(): egret.Matrix;
} }
} }
declare module es { declare module es {
@@ -1202,6 +1223,7 @@ declare module es {
abstract pointCollidesWithShape(point: Vector2): CollisionResult; abstract pointCollidesWithShape(point: Vector2): CollisionResult;
abstract overlaps(other: Shape): any; abstract overlaps(other: Shape): any;
abstract collidesWithShape(other: Shape): CollisionResult; abstract collidesWithShape(other: Shape): CollisionResult;
clone(): Shape;
} }
} }
declare module es { declare module es {
+273 -128
View File
@@ -660,6 +660,26 @@ var es;
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Vector2.prototype.add = function (value) {
this.x += value.x;
this.y += value.y;
return this;
};
Vector2.prototype.divide = function (value) {
this.x /= value.x;
this.y /= value.y;
return this;
};
Vector2.prototype.multiply = function (value) {
this.x *= value.x;
this.y *= value.y;
return this;
};
Vector2.prototype.subtract = function (value) {
this.x -= value.x;
this.y -= value.y;
return this;
};
Vector2.add = function (value1, value2) { Vector2.add = function (value1, value2) {
var result = new Vector2(0, 0); var result = new Vector2(0, 0);
result.x = value1.x + value2.x; result.x = value1.x + value2.x;
@@ -1739,6 +1759,12 @@ var es;
CameraStyle[CameraStyle["lockOn"] = 0] = "lockOn"; CameraStyle[CameraStyle["lockOn"] = 0] = "lockOn";
CameraStyle[CameraStyle["cameraWindow"] = 1] = "cameraWindow"; CameraStyle[CameraStyle["cameraWindow"] = 1] = "cameraWindow";
})(CameraStyle = es.CameraStyle || (es.CameraStyle = {})); })(CameraStyle = es.CameraStyle || (es.CameraStyle = {}));
var CameraInset = (function () {
function CameraInset() {
}
return CameraInset;
}());
es.CameraInset = CameraInset;
var Camera = (function (_super) { var Camera = (function (_super) {
__extends(Camera, _super); __extends(Camera, _super);
function Camera(targetEntity, cameraStyle) { function Camera(targetEntity, cameraStyle) {
@@ -1747,7 +1773,12 @@ var es;
var _this = _super.call(this) || this; var _this = _super.call(this) || this;
_this._minimumZoom = 0.3; _this._minimumZoom = 0.3;
_this._maximumZoom = 3; _this._maximumZoom = 3;
_this._transformMatrix = new es.Matrix2D().identity();
_this._inverseTransformMatrix = new es.Matrix2D().identity();
_this._origin = es.Vector2.zero; _this._origin = es.Vector2.zero;
_this._areMatrixedDirty = true;
_this._areBoundsDirty = true;
_this._isProjectionMatrixDirty = true;
_this.followLerp = 0.1; _this.followLerp = 0.1;
_this.deadzone = new es.Rectangle(); _this.deadzone = new es.Rectangle();
_this.focusOffset = new es.Vector2(); _this.focusOffset = new es.Vector2();
@@ -1816,7 +1847,48 @@ var es;
}); });
Object.defineProperty(Camera.prototype, "bounds", { Object.defineProperty(Camera.prototype, "bounds", {
get: function () { get: function () {
return new es.Rectangle(0, 0, es.SceneManager.stage.stageWidth, es.SceneManager.stage.stageHeight); if (this._areMatrixedDirty)
this.updateMatrixes();
if (this._areBoundsDirty) {
var topLeft = this.screenToWorldPoint(new es.Vector2(this._inset.left, this._inset.top));
var bottomRight = this.screenToWorldPoint(new es.Vector2(es.SceneManager.stage.stageWidth - this._inset.right, es.SceneManager.stage.stageHeight - this._inset.bottom));
if (this.entity.transform.rotation != 0) {
var topRight = this.screenToWorldPoint(new es.Vector2(es.SceneManager.stage.stageWidth - this._inset.right, this._inset.top));
var bottomLeft = this.screenToWorldPoint(new es.Vector2(this._inset.left, es.SceneManager.stage.stageHeight - this._inset.bottom));
var minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
var maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
var minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
var maxY = Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
this._bounds.location = new es.Vector2(minX, minY);
this._bounds.width = maxX - minX;
this._bounds.height = maxX - minY;
}
else {
this._bounds.location = topLeft;
this._bounds.width = bottomRight.x - topLeft.x;
this._bounds.height = bottomRight.y - topLeft.y;
}
this._areBoundsDirty = false;
}
return this._bounds;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Camera.prototype, "transformMatrix", {
get: function () {
if (this._areMatrixedDirty)
this.updateMatrixes();
return this._transformMatrix;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Camera.prototype, "inverseTransformMatrix", {
get: function () {
if (this._areMatrixedDirty)
this.updateMatrixes();
return this._inverseTransformMatrix;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
@@ -1828,6 +1900,7 @@ var es;
set: function (value) { set: function (value) {
if (this._origin != value) { if (this._origin != value) {
this._origin = value; this._origin = value;
this._areMatrixedDirty = true;
} }
}, },
enumerable: true, enumerable: true,
@@ -1838,6 +1911,34 @@ var es;
this.origin = new es.Vector2(newWidth / 2, newHeight / 2); this.origin = new es.Vector2(newWidth / 2, newHeight / 2);
this.entity.transform.position = es.Vector2.add(this.entity.transform.position, es.Vector2.subtract(this._origin, oldOrigin)); this.entity.transform.position = es.Vector2.add(this.entity.transform.position, es.Vector2.subtract(this._origin, oldOrigin));
}; };
Camera.prototype.updateMatrixes = function () {
if (!this._areMatrixedDirty)
return;
var tempMat;
this._transformMatrix = es.Matrix2D.create().translate(-this.entity.transform.position.x, -this.entity.transform.position.y);
if (this._zoom != 1) {
tempMat = es.Matrix2D.create().scale(this._zoom, this._zoom);
this._transformMatrix = this._transformMatrix.multiply(tempMat);
}
if (this.entity.transform.rotation != 0) {
tempMat = es.Matrix2D.create().rotate(this.entity.transform.rotation);
this._transformMatrix = this._transformMatrix.multiply(tempMat);
}
tempMat = es.Matrix2D.create().translate(this._origin.x, this._origin.y);
this._transformMatrix = this._transformMatrix.multiply(tempMat);
this._inverseTransformMatrix = this._transformMatrix.invert();
this._areBoundsDirty = true;
this._areMatrixedDirty = false;
};
Camera.prototype.setInset = function (left, right, top, bottom) {
this._inset = new CameraInset();
this._inset.left = left;
this._inset.right = right;
this._inset.top = top;
this._inset.bottom = bottom;
this._areBoundsDirty = true;
return this;
};
Camera.prototype.setPosition = function (position) { Camera.prototype.setPosition = function (position) {
this.entity.transform.setPosition(position.x, position.y); this.entity.transform.setPosition(position.x, position.y);
return this; return this;
@@ -1857,11 +1958,14 @@ var es;
else { else {
this._zoom = es.MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom); this._zoom = es.MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom);
} }
es.SceneManager.scene.scaleX = this._zoom; this._areMatrixedDirty = true;
es.SceneManager.scene.scaleY = this._zoom;
return this; return this;
}; };
Camera.prototype.setMinimumZoom = function (minZoom) { Camera.prototype.setMinimumZoom = function (minZoom) {
if (minZoom <= 0) {
console.error("minimumZoom must be greater than zero");
return;
}
if (this._zoom < minZoom) if (this._zoom < minZoom)
this._zoom = this.minimumZoom; this._zoom = this.minimumZoom;
this._minimumZoom = minZoom; this._minimumZoom = minZoom;
@@ -1877,12 +1981,28 @@ var es;
this._maximumZoom = maxZoom; this._maximumZoom = maxZoom;
return this; return this;
}; };
Camera.prototype.onEntityTransformChanged = function (comp) {
this._areMatrixedDirty = true;
};
Camera.prototype.zoomIn = function (deltaZoom) { Camera.prototype.zoomIn = function (deltaZoom) {
this.zoom += deltaZoom; this.zoom += deltaZoom;
}; };
Camera.prototype.zoomOut = function (deltaZoom) { Camera.prototype.zoomOut = function (deltaZoom) {
this.zoom -= deltaZoom; this.zoom -= deltaZoom;
}; };
Camera.prototype.worldToScreenPoint = function (worldPosition) {
this.updateMatrixes();
worldPosition = es.Vector2.transform(worldPosition, this._transformMatrix);
return worldPosition;
};
Camera.prototype.screenToWorldPoint = function (screenPosition) {
this.updateMatrixes();
screenPosition = es.Vector2.transform(screenPosition, this._inverseTransformMatrix);
return screenPosition;
};
Camera.prototype.mouseToWorldPoint = function () {
return this.screenToWorldPoint(es.Input.touchPosition);
};
Camera.prototype.onAddedToEntity = function () { Camera.prototype.onAddedToEntity = function () {
this.follow(this._targetEntity, this._cameraStyle); this.follow(this._targetEntity, this._cameraStyle);
}; };
@@ -2560,6 +2680,8 @@ var es;
_this.shouldColliderScaleAndRotateWithTransform = true; _this.shouldColliderScaleAndRotateWithTransform = true;
_this.registeredPhysicsBounds = new es.Rectangle(); _this.registeredPhysicsBounds = new es.Rectangle();
_this._localOffset = es.Vector2.zero; _this._localOffset = es.Vector2.zero;
_this._isPositionDirty = true;
_this._isRotationDirty = true;
return _this; return _this;
} }
Object.defineProperty(Collider.prototype, "localOffset", { Object.defineProperty(Collider.prototype, "localOffset", {
@@ -2590,7 +2712,10 @@ var es;
}); });
Object.defineProperty(Collider.prototype, "bounds", { Object.defineProperty(Collider.prototype, "bounds", {
get: function () { get: function () {
this.shape.recalculateBounds(this); if (this._isPositionDirty || this._isRotationDirty) {
this.shape.recalculateBounds(this);
this._isPositionDirty = this._isRotationDirty = false;
}
return this.shape.bounds; return this.shape.bounds;
}, },
enumerable: true, enumerable: true,
@@ -2601,12 +2726,14 @@ var es;
this.unregisterColliderWithPhysicsSystem(); this.unregisterColliderWithPhysicsSystem();
this._localOffset = offset; this._localOffset = offset;
this._localOffsetLength = this._localOffset.length(); this._localOffsetLength = this._localOffset.length();
this._isPositionDirty = true;
this.registerColliderWithPhysicsSystem(); this.registerColliderWithPhysicsSystem();
} }
return this; return this;
}; };
Collider.prototype.setShouldColliderScaleAndRotateWithTransform = function (shouldColliderScaleAndRotationWithTransform) { Collider.prototype.setShouldColliderScaleAndRotateWithTransform = function (shouldColliderScaleAndRotationWithTransform) {
this.shouldColliderScaleAndRotateWithTransform = shouldColliderScaleAndRotationWithTransform; this.shouldColliderScaleAndRotateWithTransform = shouldColliderScaleAndRotationWithTransform;
this._isPositionDirty = this._isRotationDirty = true;
return this; return this;
}; };
Collider.prototype.onAddedToEntity = function () { Collider.prototype.onAddedToEntity = function () {
@@ -2642,11 +2769,23 @@ var es;
this._isParentEntityAddedToScene = false; this._isParentEntityAddedToScene = false;
}; };
Collider.prototype.onEntityTransformChanged = function (comp) { Collider.prototype.onEntityTransformChanged = function (comp) {
switch (comp) {
case transform.Component.position:
this._isPositionDirty = true;
break;
case transform.Component.scale:
this._isPositionDirty = true;
break;
case transform.Component.rotation:
this._isRotationDirty = true;
break;
}
if (this._isColliderRegistered) if (this._isColliderRegistered)
es.Physics.updateCollider(this); es.Physics.updateCollider(this);
}; };
Collider.prototype.onEnabled = function () { Collider.prototype.onEnabled = function () {
this.registerColliderWithPhysicsSystem(); this.registerColliderWithPhysicsSystem();
this._isPositionDirty = this._isRotationDirty = true;
}; };
Collider.prototype.onDisabled = function () { Collider.prototype.onDisabled = function () {
this.unregisterColliderWithPhysicsSystem(); this.unregisterColliderWithPhysicsSystem();
@@ -2675,6 +2814,13 @@ var es;
this.entity.position = oldPosition; this.entity.position = oldPosition;
return result; return result;
}; };
Collider.prototype.clone = function () {
var collider = ObjectUtils.clone(this);
collider.entity = null;
if (this.shape)
collider.shape = this.shape.clone();
return collider;
};
return Collider; return Collider;
}(es.Component)); }(es.Component));
es.Collider = Collider; es.Collider = Collider;
@@ -4769,167 +4915,141 @@ var es;
})(es || (es = {})); })(es || (es = {}));
var es; var es;
(function (es) { (function (es) {
var Matrix2D = (function () { var Matrix2D = (function (_super) {
function Matrix2D(m11, m12, m21, m22, m31, m32) { __extends(Matrix2D, _super);
this.m11 = 0; function Matrix2D() {
this.m12 = 0; return _super !== null && _super.apply(this, arguments) || this;
this.m21 = 0;
this.m22 = 0;
this.m31 = 0;
this.m32 = 0;
this.m11 = m11 ? m11 : 1;
this.m12 = m12 ? m12 : 0;
this.m21 = m21 ? m21 : 0;
this.m22 = m22 ? m22 : 1;
this.m31 = m31 ? m31 : 0;
this.m32 = m32 ? m32 : 0;
} }
Object.defineProperty(Matrix2D, "identity", { Object.defineProperty(Matrix2D.prototype, "m11", {
get: function () { get: function () {
return Matrix2D._identity; return this.a;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Matrix2D.prototype, "translation", {
get: function () {
return new es.Vector2(this.m31, this.m32);
}, },
set: function (value) { set: function (value) {
this.m31 = value.x; this.a = value;
this.m32 = value.y;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Object.defineProperty(Matrix2D.prototype, "rotation", { Object.defineProperty(Matrix2D.prototype, "m12", {
get: function () { get: function () {
return Math.atan2(this.m21, this.m11); return this.b;
}, },
set: function (value) { set: function (value) {
var val1 = Math.cos(value); this.b = value;
var val2 = Math.sin(value);
this.m11 = val1;
this.m12 = val2;
this.m21 = -val2;
this.m22 = val1;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Object.defineProperty(Matrix2D.prototype, "rotationDegrees", { Object.defineProperty(Matrix2D.prototype, "m21", {
get: function () { get: function () {
return es.MathHelper.toDegrees(this.rotation); return this.c;
}, },
set: function (value) { set: function (value) {
this.rotation = es.MathHelper.toRadians(value); this.c = value;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Object.defineProperty(Matrix2D.prototype, "scale", { Object.defineProperty(Matrix2D.prototype, "m22", {
get: function () { get: function () {
return new es.Vector2(this.m11, this.m22); return this.d;
}, },
set: function (value) { set: function (value) {
this.m11 = value.x; this.d = value;
this.m12 = value.y;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Matrix2D.add = function (matrix1, matrix2) { Object.defineProperty(Matrix2D.prototype, "m31", {
matrix1.m11 += matrix2.m11; get: function () {
matrix1.m12 += matrix2.m12; return this.tx;
matrix1.m21 += matrix2.m21; },
matrix1.m22 += matrix2.m22; set: function (value) {
matrix1.m31 += matrix2.m31; this.tx = value;
matrix1.m32 += matrix2.m32; },
return matrix1; enumerable: true,
configurable: true
});
Object.defineProperty(Matrix2D.prototype, "m32", {
get: function () {
return this.ty;
},
set: function (value) {
this.ty = value;
},
enumerable: true,
configurable: true
});
Matrix2D.create = function () {
return egret.Matrix.create();
}; };
Matrix2D.divide = function (matrix1, matrix2) { Matrix2D.prototype.identity = function () {
matrix1.m11 /= matrix2.m11; _super.prototype.identity.call(this);
matrix1.m12 /= matrix2.m12; return this;
matrix1.m21 /= matrix2.m21;
matrix1.m22 /= matrix2.m22;
matrix1.m31 /= matrix2.m31;
matrix1.m32 /= matrix2.m32;
return matrix1;
}; };
Matrix2D.multiply = function (matrix1, matrix2) { Matrix2D.prototype.translate = function (dx, dy) {
var result = new Matrix2D(); _super.prototype.translate.call(this, dx, dy);
var m11 = (matrix1.m11 * matrix2.m11) + (matrix1.m12 * matrix2.m21); return this;
var m12 = (matrix1.m11 * matrix2.m12) + (matrix1.m12 * matrix2.m22);
var m21 = (matrix1.m21 * matrix2.m11) + (matrix1.m22 * matrix2.m21);
var m22 = (matrix1.m21 * matrix2.m12) + (matrix1.m22 * matrix2.m22);
var m31 = (matrix1.m31 * matrix2.m11) + (matrix1.m32 * matrix2.m21) + matrix2.m31;
var m32 = (matrix1.m31 * matrix2.m12) + (matrix1.m32 * matrix2.m22) + matrix2.m32;
result.m11 = m11;
result.m12 = m12;
result.m21 = m21;
result.m22 = m22;
result.m31 = m31;
result.m32 = m32;
return result;
}; };
Matrix2D.multiplyTranslation = function (matrix, x, y) { Matrix2D.prototype.scale = function (sx, sy) {
var trans = Matrix2D.createTranslation(x, y); _super.prototype.scale.call(this, sx, sy);
return Matrix2D.multiply(matrix, trans); return this;
};
Matrix2D.prototype.rotate = function (angle) {
_super.prototype.rotate.call(this, angle);
return this;
};
Matrix2D.prototype.invert = function () {
_super.prototype.invert.call(this);
return this;
};
Matrix2D.prototype.add = function (matrix) {
this.m11 += matrix.m11;
this.m12 += matrix.m12;
this.m21 += matrix.m21;
this.m22 += matrix.m22;
this.m31 += matrix.m31;
this.m32 += matrix.m32;
return this;
};
Matrix2D.prototype.substract = function (matrix) {
this.m11 -= matrix.m11;
this.m12 -= matrix.m12;
this.m21 -= matrix.m21;
this.m22 -= matrix.m22;
this.m31 -= matrix.m31;
this.m32 -= matrix.m32;
return this;
};
Matrix2D.prototype.divide = function (matrix) {
this.m11 /= matrix.m11;
this.m12 /= matrix.m12;
this.m21 /= matrix.m21;
this.m22 /= matrix.m22;
this.m31 /= matrix.m31;
this.m32 /= matrix.m32;
return this;
};
Matrix2D.prototype.multiply = function (matrix) {
var m11 = (this.m11 * matrix.m11) + (this.m12 * matrix.m21);
var m12 = (this.m11 * matrix.m12) + (this.m12 * matrix.m22);
var m21 = (this.m21 * matrix.m11) + (this.m22 * matrix.m21);
var m22 = (this.m21 * matrix.m12) + (this.m22 * matrix.m22);
var m31 = (this.m31 * matrix.m11) + (this.m32 * matrix.m21) + matrix.m31;
var m32 = (this.m31 * matrix.m12) + (this.m32 * matrix.m22) + matrix.m32;
this.m11 = m11;
this.m12 = m12;
this.m21 = m21;
this.m22 = m22;
this.m31 = m31;
this.m32 = m32;
return this;
}; };
Matrix2D.prototype.determinant = function () { Matrix2D.prototype.determinant = function () {
return this.m11 * this.m22 - this.m12 * this.m21; return this.m11 * this.m22 - this.m12 * this.m21;
}; };
Matrix2D.invert = function (matrix, result) {
if (result === void 0) { result = new Matrix2D(); }
var det = 1 / matrix.determinant();
result.m11 = matrix.m22 * det;
result.m12 = -matrix.m12 * det;
result.m21 = -matrix.m21 * det;
result.m22 = matrix.m11 * det;
result.m31 = (matrix.m32 * matrix.m21 - matrix.m31 * matrix.m22) * det;
result.m32 = -(matrix.m32 * matrix.m11 - matrix.m31 * matrix.m12) * det;
return result;
};
Matrix2D.createTranslation = function (xPosition, yPosition) {
var result = new Matrix2D();
result.m11 = 1;
result.m12 = 0;
result.m21 = 0;
result.m22 = 1;
result.m31 = xPosition;
result.m32 = yPosition;
return result;
};
Matrix2D.createTranslationVector = function (position) {
return this.createTranslation(position.x, position.y);
};
Matrix2D.createRotation = function (radians, result) {
result = new Matrix2D();
var val1 = Math.cos(radians);
var val2 = Math.sin(radians);
result.m11 = val1;
result.m12 = val2;
result.m21 = -val2;
result.m22 = val1;
return result;
};
Matrix2D.createScale = function (xScale, yScale, result) {
if (result === void 0) { result = new Matrix2D(); }
result.m11 = xScale;
result.m12 = 0;
result.m21 = 0;
result.m22 = yScale;
result.m31 = 0;
result.m32 = 0;
return result;
};
Matrix2D.prototype.toEgretMatrix = function () {
var matrix = new egret.Matrix(this.m11, this.m12, this.m21, this.m22, this.m31, this.m32);
return matrix;
};
Matrix2D._identity = new Matrix2D(1, 0, 0, 1, 0, 0);
return Matrix2D; return Matrix2D;
}()); }(egret.Matrix));
es.Matrix2D = Matrix2D; es.Matrix2D = Matrix2D;
})(es || (es = {})); })(es || (es = {}));
var es; var es;
@@ -5374,6 +5494,9 @@ var es;
var Shape = (function () { var Shape = (function () {
function Shape() { function Shape() {
} }
Shape.prototype.clone = function () {
return ObjectUtils.clone(this);
};
return Shape; return Shape;
}()); }());
es.Shape = Shape; es.Shape = Shape;
@@ -5473,7 +5596,29 @@ var es;
Polygon.prototype.recalculateBounds = function (collider) { Polygon.prototype.recalculateBounds = function (collider) {
this.center = collider.localOffset; this.center = collider.localOffset;
if (collider.shouldColliderScaleAndRotateWithTransform) { if (collider.shouldColliderScaleAndRotateWithTransform) {
var hasUnitScale = true;
var tempMat = void 0;
var combinedMatrix = es.Matrix2D.create().translate(-this._polygonCenter.x, -this._polygonCenter.y);
if (collider.entity.transform.scale != es.Vector2.zero) {
tempMat = es.Matrix2D.create().scale(collider.entity.transform.scale.x, collider.entity.transform.scale.y);
combinedMatrix = combinedMatrix.multiply(tempMat);
hasUnitScale = false;
this.center = es.Vector2.multiply(collider.localOffset, collider.entity.transform.scale);
}
if (collider.entity.transform.rotation != 0) {
tempMat = es.Matrix2D.create().rotate(collider.entity.transform.rotation);
combinedMatrix = combinedMatrix.multiply(tempMat);
var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x);
var offsetLength = hasUnitScale ? collider._localOffsetLength :
es.Vector2.multiply(collider.localOffset, collider.entity.transform.scale).length();
this.center = es.MathHelper.pointOnCirlce(es.Vector2.zero, offsetLength, collider.entity.transform.rotation + offsetAngle);
}
tempMat = es.Matrix2D.create().translate(this._polygonCenter.x, this._polygonCenter.y);
combinedMatrix = combinedMatrix.multiply(tempMat);
es.Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points);
this.isUnrotated = collider.entity.transform.rotation == 0; this.isUnrotated = collider.entity.transform.rotation == 0;
if (collider._isRotationDirty)
this._areEdgeNormalsDirty = true;
} }
this.position = es.Vector2.add(collider.entity.transform.position, this.center); this.position = es.Vector2.add(collider.entity.transform.position, this.center);
this.bounds = es.Rectangle.rectEncompassingPoints(this.points); this.bounds = es.Rectangle.rectEncompassingPoints(this.points);
File diff suppressed because one or more lines are too long
+44 -22
View File
@@ -114,6 +114,10 @@ declare module es {
static readonly unitX: Vector2; static readonly unitX: Vector2;
static readonly unitY: Vector2; static readonly unitY: Vector2;
constructor(x?: number, y?: number); constructor(x?: number, y?: number);
add(value: Vector2): Vector2;
divide(value: Vector2): Vector2;
multiply(value: Vector2): Vector2;
subtract(value: Vector2): this;
static add(value1: Vector2, value2: Vector2): Vector2; static add(value1: Vector2, value2: Vector2): Vector2;
static divide(value1: Vector2, value2: Vector2): Vector2; static divide(value1: Vector2, value2: Vector2): Vector2;
static multiply(value1: Vector2, value2: Vector2): Vector2; static multiply(value1: Vector2, value2: Vector2): Vector2;
@@ -401,6 +405,12 @@ declare module es {
lockOn = 0, lockOn = 0,
cameraWindow = 1 cameraWindow = 1
} }
class CameraInset {
left: number;
right: number;
top: number;
bottom: number;
}
class Camera extends Component { class Camera extends Component {
position: Vector2; position: Vector2;
rotation: number; rotation: number;
@@ -408,11 +418,20 @@ declare module es {
minimumZoom: number; minimumZoom: number;
maximumZoom: number; maximumZoom: number;
readonly bounds: Rectangle; readonly bounds: Rectangle;
readonly transformMatrix: Matrix2D;
readonly inverseTransformMatrix: Matrix2D;
origin: Vector2; origin: Vector2;
private _zoom; _zoom: any;
private _minimumZoom; _minimumZoom: number;
private _maximumZoom; _maximumZoom: number;
private _origin; _bounds: Rectangle;
_inset: CameraInset;
_transformMatrix: Matrix2D;
_inverseTransformMatrix: Matrix2D;
_origin: Vector2;
_areMatrixedDirty: boolean;
_areBoundsDirty: boolean;
_isProjectionMatrixDirty: boolean;
followLerp: number; followLerp: number;
deadzone: Rectangle; deadzone: Rectangle;
focusOffset: Vector2; focusOffset: Vector2;
@@ -425,13 +444,19 @@ declare module es {
_worldSpaceDeadZone: Rectangle; _worldSpaceDeadZone: Rectangle;
constructor(targetEntity?: Entity, cameraStyle?: CameraStyle); constructor(targetEntity?: Entity, cameraStyle?: CameraStyle);
onSceneSizeChanged(newWidth: number, newHeight: number): void; onSceneSizeChanged(newWidth: number, newHeight: number): void;
protected updateMatrixes(): void;
setInset(left: number, right: number, top: number, bottom: number): Camera;
setPosition(position: Vector2): this; setPosition(position: Vector2): this;
setRotation(rotation: number): Camera; setRotation(rotation: number): Camera;
setZoom(zoom: number): Camera; setZoom(zoom: number): Camera;
setMinimumZoom(minZoom: number): Camera; setMinimumZoom(minZoom: number): Camera;
setMaximumZoom(maxZoom: number): Camera; setMaximumZoom(maxZoom: number): Camera;
onEntityTransformChanged(comp: transform.Component): void;
zoomIn(deltaZoom: number): void; zoomIn(deltaZoom: number): void;
zoomOut(deltaZoom: number): void; zoomOut(deltaZoom: number): void;
worldToScreenPoint(worldPosition: Vector2): Vector2;
screenToWorldPoint(screenPosition: Vector2): Vector2;
mouseToWorldPoint(): Vector2;
onAddedToEntity(): void; onAddedToEntity(): void;
update(): void; update(): void;
clampToMapSize(position: Vector2): Vector2; clampToMapSize(position: Vector2): Vector2;
@@ -626,6 +651,8 @@ declare module es {
_localOffsetLength: number; _localOffsetLength: number;
protected _isParentEntityAddedToScene: any; protected _isParentEntityAddedToScene: any;
protected _isColliderRegistered: any; protected _isColliderRegistered: any;
_isPositionDirty: boolean;
_isRotationDirty: boolean;
setLocalOffset(offset: Vector2): Collider; setLocalOffset(offset: Vector2): Collider;
setShouldColliderScaleAndRotateWithTransform(shouldColliderScaleAndRotationWithTransform: boolean): Collider; setShouldColliderScaleAndRotateWithTransform(shouldColliderScaleAndRotationWithTransform: boolean): Collider;
onAddedToEntity(): void; onAddedToEntity(): void;
@@ -637,6 +664,7 @@ declare module es {
unregisterColliderWithPhysicsSystem(): void; unregisterColliderWithPhysicsSystem(): void;
overlaps(other: Collider): any; overlaps(other: Collider): any;
collidesWith(collider: Collider, motion: Vector2): CollisionResult; collidesWith(collider: Collider, motion: Vector2): CollisionResult;
clone(): Component;
} }
} }
declare module es { declare module es {
@@ -1079,31 +1107,24 @@ declare module es {
} }
} }
declare module es { declare module es {
class Matrix2D { class Matrix2D extends egret.Matrix {
m11: number; m11: number;
m12: number; m12: number;
m21: number; m21: number;
m22: number; m22: number;
m31: number; m31: number;
m32: number; m32: number;
private static _identity; static create(): Matrix2D;
static readonly identity: Matrix2D; identity(): Matrix2D;
constructor(m11?: number, m12?: number, m21?: number, m22?: number, m31?: number, m32?: number); translate(dx: number, dy: number): Matrix2D;
translation: Vector2; scale(sx: number, sy: number): Matrix2D;
rotation: number; rotate(angle: number): Matrix2D;
rotationDegrees: number; invert(): Matrix2D;
scale: Vector2; add(matrix: Matrix2D): Matrix2D;
static add(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D; substract(matrix: Matrix2D): Matrix2D;
static divide(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D; divide(matrix: Matrix2D): Matrix2D;
static multiply(matrix1: Matrix2D, matrix2: Matrix2D): Matrix2D; multiply(matrix: Matrix2D): Matrix2D;
static multiplyTranslation(matrix: Matrix2D, x: number, y: number): Matrix2D;
determinant(): number; determinant(): number;
static invert(matrix: Matrix2D, result?: Matrix2D): Matrix2D;
static createTranslation(xPosition: number, yPosition: number): Matrix2D;
static createTranslationVector(position: Vector2): Matrix2D;
static createRotation(radians: number, result?: Matrix2D): Matrix2D;
static createScale(xScale: number, yScale: number, result?: Matrix2D): Matrix2D;
toEgretMatrix(): egret.Matrix;
} }
} }
declare module es { declare module es {
@@ -1202,6 +1223,7 @@ declare module es {
abstract pointCollidesWithShape(point: Vector2): CollisionResult; abstract pointCollidesWithShape(point: Vector2): CollisionResult;
abstract overlaps(other: Shape): any; abstract overlaps(other: Shape): any;
abstract collidesWithShape(other: Shape): CollisionResult; abstract collidesWithShape(other: Shape): CollisionResult;
clone(): Shape;
} }
} }
declare module es { declare module es {
+273 -128
View File
@@ -660,6 +660,26 @@ var es;
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Vector2.prototype.add = function (value) {
this.x += value.x;
this.y += value.y;
return this;
};
Vector2.prototype.divide = function (value) {
this.x /= value.x;
this.y /= value.y;
return this;
};
Vector2.prototype.multiply = function (value) {
this.x *= value.x;
this.y *= value.y;
return this;
};
Vector2.prototype.subtract = function (value) {
this.x -= value.x;
this.y -= value.y;
return this;
};
Vector2.add = function (value1, value2) { Vector2.add = function (value1, value2) {
var result = new Vector2(0, 0); var result = new Vector2(0, 0);
result.x = value1.x + value2.x; result.x = value1.x + value2.x;
@@ -1739,6 +1759,12 @@ var es;
CameraStyle[CameraStyle["lockOn"] = 0] = "lockOn"; CameraStyle[CameraStyle["lockOn"] = 0] = "lockOn";
CameraStyle[CameraStyle["cameraWindow"] = 1] = "cameraWindow"; CameraStyle[CameraStyle["cameraWindow"] = 1] = "cameraWindow";
})(CameraStyle = es.CameraStyle || (es.CameraStyle = {})); })(CameraStyle = es.CameraStyle || (es.CameraStyle = {}));
var CameraInset = (function () {
function CameraInset() {
}
return CameraInset;
}());
es.CameraInset = CameraInset;
var Camera = (function (_super) { var Camera = (function (_super) {
__extends(Camera, _super); __extends(Camera, _super);
function Camera(targetEntity, cameraStyle) { function Camera(targetEntity, cameraStyle) {
@@ -1747,7 +1773,12 @@ var es;
var _this = _super.call(this) || this; var _this = _super.call(this) || this;
_this._minimumZoom = 0.3; _this._minimumZoom = 0.3;
_this._maximumZoom = 3; _this._maximumZoom = 3;
_this._transformMatrix = new es.Matrix2D().identity();
_this._inverseTransformMatrix = new es.Matrix2D().identity();
_this._origin = es.Vector2.zero; _this._origin = es.Vector2.zero;
_this._areMatrixedDirty = true;
_this._areBoundsDirty = true;
_this._isProjectionMatrixDirty = true;
_this.followLerp = 0.1; _this.followLerp = 0.1;
_this.deadzone = new es.Rectangle(); _this.deadzone = new es.Rectangle();
_this.focusOffset = new es.Vector2(); _this.focusOffset = new es.Vector2();
@@ -1816,7 +1847,48 @@ var es;
}); });
Object.defineProperty(Camera.prototype, "bounds", { Object.defineProperty(Camera.prototype, "bounds", {
get: function () { get: function () {
return new es.Rectangle(0, 0, es.SceneManager.stage.stageWidth, es.SceneManager.stage.stageHeight); if (this._areMatrixedDirty)
this.updateMatrixes();
if (this._areBoundsDirty) {
var topLeft = this.screenToWorldPoint(new es.Vector2(this._inset.left, this._inset.top));
var bottomRight = this.screenToWorldPoint(new es.Vector2(es.SceneManager.stage.stageWidth - this._inset.right, es.SceneManager.stage.stageHeight - this._inset.bottom));
if (this.entity.transform.rotation != 0) {
var topRight = this.screenToWorldPoint(new es.Vector2(es.SceneManager.stage.stageWidth - this._inset.right, this._inset.top));
var bottomLeft = this.screenToWorldPoint(new es.Vector2(this._inset.left, es.SceneManager.stage.stageHeight - this._inset.bottom));
var minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
var maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
var minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
var maxY = Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
this._bounds.location = new es.Vector2(minX, minY);
this._bounds.width = maxX - minX;
this._bounds.height = maxX - minY;
}
else {
this._bounds.location = topLeft;
this._bounds.width = bottomRight.x - topLeft.x;
this._bounds.height = bottomRight.y - topLeft.y;
}
this._areBoundsDirty = false;
}
return this._bounds;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Camera.prototype, "transformMatrix", {
get: function () {
if (this._areMatrixedDirty)
this.updateMatrixes();
return this._transformMatrix;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Camera.prototype, "inverseTransformMatrix", {
get: function () {
if (this._areMatrixedDirty)
this.updateMatrixes();
return this._inverseTransformMatrix;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
@@ -1828,6 +1900,7 @@ var es;
set: function (value) { set: function (value) {
if (this._origin != value) { if (this._origin != value) {
this._origin = value; this._origin = value;
this._areMatrixedDirty = true;
} }
}, },
enumerable: true, enumerable: true,
@@ -1838,6 +1911,34 @@ var es;
this.origin = new es.Vector2(newWidth / 2, newHeight / 2); this.origin = new es.Vector2(newWidth / 2, newHeight / 2);
this.entity.transform.position = es.Vector2.add(this.entity.transform.position, es.Vector2.subtract(this._origin, oldOrigin)); this.entity.transform.position = es.Vector2.add(this.entity.transform.position, es.Vector2.subtract(this._origin, oldOrigin));
}; };
Camera.prototype.updateMatrixes = function () {
if (!this._areMatrixedDirty)
return;
var tempMat;
this._transformMatrix = es.Matrix2D.create().translate(-this.entity.transform.position.x, -this.entity.transform.position.y);
if (this._zoom != 1) {
tempMat = es.Matrix2D.create().scale(this._zoom, this._zoom);
this._transformMatrix = this._transformMatrix.multiply(tempMat);
}
if (this.entity.transform.rotation != 0) {
tempMat = es.Matrix2D.create().rotate(this.entity.transform.rotation);
this._transformMatrix = this._transformMatrix.multiply(tempMat);
}
tempMat = es.Matrix2D.create().translate(this._origin.x, this._origin.y);
this._transformMatrix = this._transformMatrix.multiply(tempMat);
this._inverseTransformMatrix = this._transformMatrix.invert();
this._areBoundsDirty = true;
this._areMatrixedDirty = false;
};
Camera.prototype.setInset = function (left, right, top, bottom) {
this._inset = new CameraInset();
this._inset.left = left;
this._inset.right = right;
this._inset.top = top;
this._inset.bottom = bottom;
this._areBoundsDirty = true;
return this;
};
Camera.prototype.setPosition = function (position) { Camera.prototype.setPosition = function (position) {
this.entity.transform.setPosition(position.x, position.y); this.entity.transform.setPosition(position.x, position.y);
return this; return this;
@@ -1857,11 +1958,14 @@ var es;
else { else {
this._zoom = es.MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom); this._zoom = es.MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom);
} }
es.SceneManager.scene.scaleX = this._zoom; this._areMatrixedDirty = true;
es.SceneManager.scene.scaleY = this._zoom;
return this; return this;
}; };
Camera.prototype.setMinimumZoom = function (minZoom) { Camera.prototype.setMinimumZoom = function (minZoom) {
if (minZoom <= 0) {
console.error("minimumZoom must be greater than zero");
return;
}
if (this._zoom < minZoom) if (this._zoom < minZoom)
this._zoom = this.minimumZoom; this._zoom = this.minimumZoom;
this._minimumZoom = minZoom; this._minimumZoom = minZoom;
@@ -1877,12 +1981,28 @@ var es;
this._maximumZoom = maxZoom; this._maximumZoom = maxZoom;
return this; return this;
}; };
Camera.prototype.onEntityTransformChanged = function (comp) {
this._areMatrixedDirty = true;
};
Camera.prototype.zoomIn = function (deltaZoom) { Camera.prototype.zoomIn = function (deltaZoom) {
this.zoom += deltaZoom; this.zoom += deltaZoom;
}; };
Camera.prototype.zoomOut = function (deltaZoom) { Camera.prototype.zoomOut = function (deltaZoom) {
this.zoom -= deltaZoom; this.zoom -= deltaZoom;
}; };
Camera.prototype.worldToScreenPoint = function (worldPosition) {
this.updateMatrixes();
worldPosition = es.Vector2.transform(worldPosition, this._transformMatrix);
return worldPosition;
};
Camera.prototype.screenToWorldPoint = function (screenPosition) {
this.updateMatrixes();
screenPosition = es.Vector2.transform(screenPosition, this._inverseTransformMatrix);
return screenPosition;
};
Camera.prototype.mouseToWorldPoint = function () {
return this.screenToWorldPoint(es.Input.touchPosition);
};
Camera.prototype.onAddedToEntity = function () { Camera.prototype.onAddedToEntity = function () {
this.follow(this._targetEntity, this._cameraStyle); this.follow(this._targetEntity, this._cameraStyle);
}; };
@@ -2560,6 +2680,8 @@ var es;
_this.shouldColliderScaleAndRotateWithTransform = true; _this.shouldColliderScaleAndRotateWithTransform = true;
_this.registeredPhysicsBounds = new es.Rectangle(); _this.registeredPhysicsBounds = new es.Rectangle();
_this._localOffset = es.Vector2.zero; _this._localOffset = es.Vector2.zero;
_this._isPositionDirty = true;
_this._isRotationDirty = true;
return _this; return _this;
} }
Object.defineProperty(Collider.prototype, "localOffset", { Object.defineProperty(Collider.prototype, "localOffset", {
@@ -2590,7 +2712,10 @@ var es;
}); });
Object.defineProperty(Collider.prototype, "bounds", { Object.defineProperty(Collider.prototype, "bounds", {
get: function () { get: function () {
this.shape.recalculateBounds(this); if (this._isPositionDirty || this._isRotationDirty) {
this.shape.recalculateBounds(this);
this._isPositionDirty = this._isRotationDirty = false;
}
return this.shape.bounds; return this.shape.bounds;
}, },
enumerable: true, enumerable: true,
@@ -2601,12 +2726,14 @@ var es;
this.unregisterColliderWithPhysicsSystem(); this.unregisterColliderWithPhysicsSystem();
this._localOffset = offset; this._localOffset = offset;
this._localOffsetLength = this._localOffset.length(); this._localOffsetLength = this._localOffset.length();
this._isPositionDirty = true;
this.registerColliderWithPhysicsSystem(); this.registerColliderWithPhysicsSystem();
} }
return this; return this;
}; };
Collider.prototype.setShouldColliderScaleAndRotateWithTransform = function (shouldColliderScaleAndRotationWithTransform) { Collider.prototype.setShouldColliderScaleAndRotateWithTransform = function (shouldColliderScaleAndRotationWithTransform) {
this.shouldColliderScaleAndRotateWithTransform = shouldColliderScaleAndRotationWithTransform; this.shouldColliderScaleAndRotateWithTransform = shouldColliderScaleAndRotationWithTransform;
this._isPositionDirty = this._isRotationDirty = true;
return this; return this;
}; };
Collider.prototype.onAddedToEntity = function () { Collider.prototype.onAddedToEntity = function () {
@@ -2642,11 +2769,23 @@ var es;
this._isParentEntityAddedToScene = false; this._isParentEntityAddedToScene = false;
}; };
Collider.prototype.onEntityTransformChanged = function (comp) { Collider.prototype.onEntityTransformChanged = function (comp) {
switch (comp) {
case transform.Component.position:
this._isPositionDirty = true;
break;
case transform.Component.scale:
this._isPositionDirty = true;
break;
case transform.Component.rotation:
this._isRotationDirty = true;
break;
}
if (this._isColliderRegistered) if (this._isColliderRegistered)
es.Physics.updateCollider(this); es.Physics.updateCollider(this);
}; };
Collider.prototype.onEnabled = function () { Collider.prototype.onEnabled = function () {
this.registerColliderWithPhysicsSystem(); this.registerColliderWithPhysicsSystem();
this._isPositionDirty = this._isRotationDirty = true;
}; };
Collider.prototype.onDisabled = function () { Collider.prototype.onDisabled = function () {
this.unregisterColliderWithPhysicsSystem(); this.unregisterColliderWithPhysicsSystem();
@@ -2675,6 +2814,13 @@ var es;
this.entity.position = oldPosition; this.entity.position = oldPosition;
return result; return result;
}; };
Collider.prototype.clone = function () {
var collider = ObjectUtils.clone(this);
collider.entity = null;
if (this.shape)
collider.shape = this.shape.clone();
return collider;
};
return Collider; return Collider;
}(es.Component)); }(es.Component));
es.Collider = Collider; es.Collider = Collider;
@@ -4769,167 +4915,141 @@ var es;
})(es || (es = {})); })(es || (es = {}));
var es; var es;
(function (es) { (function (es) {
var Matrix2D = (function () { var Matrix2D = (function (_super) {
function Matrix2D(m11, m12, m21, m22, m31, m32) { __extends(Matrix2D, _super);
this.m11 = 0; function Matrix2D() {
this.m12 = 0; return _super !== null && _super.apply(this, arguments) || this;
this.m21 = 0;
this.m22 = 0;
this.m31 = 0;
this.m32 = 0;
this.m11 = m11 ? m11 : 1;
this.m12 = m12 ? m12 : 0;
this.m21 = m21 ? m21 : 0;
this.m22 = m22 ? m22 : 1;
this.m31 = m31 ? m31 : 0;
this.m32 = m32 ? m32 : 0;
} }
Object.defineProperty(Matrix2D, "identity", { Object.defineProperty(Matrix2D.prototype, "m11", {
get: function () { get: function () {
return Matrix2D._identity; return this.a;
},
enumerable: true,
configurable: true
});
Object.defineProperty(Matrix2D.prototype, "translation", {
get: function () {
return new es.Vector2(this.m31, this.m32);
}, },
set: function (value) { set: function (value) {
this.m31 = value.x; this.a = value;
this.m32 = value.y;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Object.defineProperty(Matrix2D.prototype, "rotation", { Object.defineProperty(Matrix2D.prototype, "m12", {
get: function () { get: function () {
return Math.atan2(this.m21, this.m11); return this.b;
}, },
set: function (value) { set: function (value) {
var val1 = Math.cos(value); this.b = value;
var val2 = Math.sin(value);
this.m11 = val1;
this.m12 = val2;
this.m21 = -val2;
this.m22 = val1;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Object.defineProperty(Matrix2D.prototype, "rotationDegrees", { Object.defineProperty(Matrix2D.prototype, "m21", {
get: function () { get: function () {
return es.MathHelper.toDegrees(this.rotation); return this.c;
}, },
set: function (value) { set: function (value) {
this.rotation = es.MathHelper.toRadians(value); this.c = value;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Object.defineProperty(Matrix2D.prototype, "scale", { Object.defineProperty(Matrix2D.prototype, "m22", {
get: function () { get: function () {
return new es.Vector2(this.m11, this.m22); return this.d;
}, },
set: function (value) { set: function (value) {
this.m11 = value.x; this.d = value;
this.m12 = value.y;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
}); });
Matrix2D.add = function (matrix1, matrix2) { Object.defineProperty(Matrix2D.prototype, "m31", {
matrix1.m11 += matrix2.m11; get: function () {
matrix1.m12 += matrix2.m12; return this.tx;
matrix1.m21 += matrix2.m21; },
matrix1.m22 += matrix2.m22; set: function (value) {
matrix1.m31 += matrix2.m31; this.tx = value;
matrix1.m32 += matrix2.m32; },
return matrix1; enumerable: true,
configurable: true
});
Object.defineProperty(Matrix2D.prototype, "m32", {
get: function () {
return this.ty;
},
set: function (value) {
this.ty = value;
},
enumerable: true,
configurable: true
});
Matrix2D.create = function () {
return egret.Matrix.create();
}; };
Matrix2D.divide = function (matrix1, matrix2) { Matrix2D.prototype.identity = function () {
matrix1.m11 /= matrix2.m11; _super.prototype.identity.call(this);
matrix1.m12 /= matrix2.m12; return this;
matrix1.m21 /= matrix2.m21;
matrix1.m22 /= matrix2.m22;
matrix1.m31 /= matrix2.m31;
matrix1.m32 /= matrix2.m32;
return matrix1;
}; };
Matrix2D.multiply = function (matrix1, matrix2) { Matrix2D.prototype.translate = function (dx, dy) {
var result = new Matrix2D(); _super.prototype.translate.call(this, dx, dy);
var m11 = (matrix1.m11 * matrix2.m11) + (matrix1.m12 * matrix2.m21); return this;
var m12 = (matrix1.m11 * matrix2.m12) + (matrix1.m12 * matrix2.m22);
var m21 = (matrix1.m21 * matrix2.m11) + (matrix1.m22 * matrix2.m21);
var m22 = (matrix1.m21 * matrix2.m12) + (matrix1.m22 * matrix2.m22);
var m31 = (matrix1.m31 * matrix2.m11) + (matrix1.m32 * matrix2.m21) + matrix2.m31;
var m32 = (matrix1.m31 * matrix2.m12) + (matrix1.m32 * matrix2.m22) + matrix2.m32;
result.m11 = m11;
result.m12 = m12;
result.m21 = m21;
result.m22 = m22;
result.m31 = m31;
result.m32 = m32;
return result;
}; };
Matrix2D.multiplyTranslation = function (matrix, x, y) { Matrix2D.prototype.scale = function (sx, sy) {
var trans = Matrix2D.createTranslation(x, y); _super.prototype.scale.call(this, sx, sy);
return Matrix2D.multiply(matrix, trans); return this;
};
Matrix2D.prototype.rotate = function (angle) {
_super.prototype.rotate.call(this, angle);
return this;
};
Matrix2D.prototype.invert = function () {
_super.prototype.invert.call(this);
return this;
};
Matrix2D.prototype.add = function (matrix) {
this.m11 += matrix.m11;
this.m12 += matrix.m12;
this.m21 += matrix.m21;
this.m22 += matrix.m22;
this.m31 += matrix.m31;
this.m32 += matrix.m32;
return this;
};
Matrix2D.prototype.substract = function (matrix) {
this.m11 -= matrix.m11;
this.m12 -= matrix.m12;
this.m21 -= matrix.m21;
this.m22 -= matrix.m22;
this.m31 -= matrix.m31;
this.m32 -= matrix.m32;
return this;
};
Matrix2D.prototype.divide = function (matrix) {
this.m11 /= matrix.m11;
this.m12 /= matrix.m12;
this.m21 /= matrix.m21;
this.m22 /= matrix.m22;
this.m31 /= matrix.m31;
this.m32 /= matrix.m32;
return this;
};
Matrix2D.prototype.multiply = function (matrix) {
var m11 = (this.m11 * matrix.m11) + (this.m12 * matrix.m21);
var m12 = (this.m11 * matrix.m12) + (this.m12 * matrix.m22);
var m21 = (this.m21 * matrix.m11) + (this.m22 * matrix.m21);
var m22 = (this.m21 * matrix.m12) + (this.m22 * matrix.m22);
var m31 = (this.m31 * matrix.m11) + (this.m32 * matrix.m21) + matrix.m31;
var m32 = (this.m31 * matrix.m12) + (this.m32 * matrix.m22) + matrix.m32;
this.m11 = m11;
this.m12 = m12;
this.m21 = m21;
this.m22 = m22;
this.m31 = m31;
this.m32 = m32;
return this;
}; };
Matrix2D.prototype.determinant = function () { Matrix2D.prototype.determinant = function () {
return this.m11 * this.m22 - this.m12 * this.m21; return this.m11 * this.m22 - this.m12 * this.m21;
}; };
Matrix2D.invert = function (matrix, result) {
if (result === void 0) { result = new Matrix2D(); }
var det = 1 / matrix.determinant();
result.m11 = matrix.m22 * det;
result.m12 = -matrix.m12 * det;
result.m21 = -matrix.m21 * det;
result.m22 = matrix.m11 * det;
result.m31 = (matrix.m32 * matrix.m21 - matrix.m31 * matrix.m22) * det;
result.m32 = -(matrix.m32 * matrix.m11 - matrix.m31 * matrix.m12) * det;
return result;
};
Matrix2D.createTranslation = function (xPosition, yPosition) {
var result = new Matrix2D();
result.m11 = 1;
result.m12 = 0;
result.m21 = 0;
result.m22 = 1;
result.m31 = xPosition;
result.m32 = yPosition;
return result;
};
Matrix2D.createTranslationVector = function (position) {
return this.createTranslation(position.x, position.y);
};
Matrix2D.createRotation = function (radians, result) {
result = new Matrix2D();
var val1 = Math.cos(radians);
var val2 = Math.sin(radians);
result.m11 = val1;
result.m12 = val2;
result.m21 = -val2;
result.m22 = val1;
return result;
};
Matrix2D.createScale = function (xScale, yScale, result) {
if (result === void 0) { result = new Matrix2D(); }
result.m11 = xScale;
result.m12 = 0;
result.m21 = 0;
result.m22 = yScale;
result.m31 = 0;
result.m32 = 0;
return result;
};
Matrix2D.prototype.toEgretMatrix = function () {
var matrix = new egret.Matrix(this.m11, this.m12, this.m21, this.m22, this.m31, this.m32);
return matrix;
};
Matrix2D._identity = new Matrix2D(1, 0, 0, 1, 0, 0);
return Matrix2D; return Matrix2D;
}()); }(egret.Matrix));
es.Matrix2D = Matrix2D; es.Matrix2D = Matrix2D;
})(es || (es = {})); })(es || (es = {}));
var es; var es;
@@ -5374,6 +5494,9 @@ var es;
var Shape = (function () { var Shape = (function () {
function Shape() { function Shape() {
} }
Shape.prototype.clone = function () {
return ObjectUtils.clone(this);
};
return Shape; return Shape;
}()); }());
es.Shape = Shape; es.Shape = Shape;
@@ -5473,7 +5596,29 @@ var es;
Polygon.prototype.recalculateBounds = function (collider) { Polygon.prototype.recalculateBounds = function (collider) {
this.center = collider.localOffset; this.center = collider.localOffset;
if (collider.shouldColliderScaleAndRotateWithTransform) { if (collider.shouldColliderScaleAndRotateWithTransform) {
var hasUnitScale = true;
var tempMat = void 0;
var combinedMatrix = es.Matrix2D.create().translate(-this._polygonCenter.x, -this._polygonCenter.y);
if (collider.entity.transform.scale != es.Vector2.zero) {
tempMat = es.Matrix2D.create().scale(collider.entity.transform.scale.x, collider.entity.transform.scale.y);
combinedMatrix = combinedMatrix.multiply(tempMat);
hasUnitScale = false;
this.center = es.Vector2.multiply(collider.localOffset, collider.entity.transform.scale);
}
if (collider.entity.transform.rotation != 0) {
tempMat = es.Matrix2D.create().rotate(collider.entity.transform.rotation);
combinedMatrix = combinedMatrix.multiply(tempMat);
var offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x);
var offsetLength = hasUnitScale ? collider._localOffsetLength :
es.Vector2.multiply(collider.localOffset, collider.entity.transform.scale).length();
this.center = es.MathHelper.pointOnCirlce(es.Vector2.zero, offsetLength, collider.entity.transform.rotation + offsetAngle);
}
tempMat = es.Matrix2D.create().translate(this._polygonCenter.x, this._polygonCenter.y);
combinedMatrix = combinedMatrix.multiply(tempMat);
es.Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points);
this.isUnrotated = collider.entity.transform.rotation == 0; this.isUnrotated = collider.entity.transform.rotation == 0;
if (collider._isRotationDirty)
this._areEdgeNormalsDirty = true;
} }
this.position = es.Vector2.add(collider.entity.transform.position, this.center); this.position = es.Vector2.add(collider.entity.transform.position, this.center);
this.bounds = es.Rectangle.rectEncompassingPoints(this.points); this.bounds = es.Rectangle.rectEncompassingPoints(this.points);
+1 -1
View File
File diff suppressed because one or more lines are too long
+155 -8
View File
@@ -4,6 +4,13 @@ module es {
cameraWindow, cameraWindow,
} }
export class CameraInset {
public left: number;
public right: number;
public top: number;
public bottom: number;
}
export class Camera extends Component { export class Camera extends Component {
/** /**
* entity.transform.position的快速访问 * entity.transform.position的快速访问
@@ -89,10 +96,61 @@ module es {
} }
/** /**
* * -
*/ */
public get bounds(){ public get bounds(){
return new Rectangle(0, 0, SceneManager.stage.stageWidth, SceneManager.stage.stageHeight); if (this._areMatrixedDirty)
this.updateMatrixes();
if (this._areBoundsDirty){
// 旋转或非旋转的边界都需要左上角和右下角
let topLeft = this.screenToWorldPoint(new Vector2(this._inset.left, this._inset.top));
let bottomRight = this.screenToWorldPoint(new Vector2(SceneManager.stage.stageWidth - this._inset.right,
SceneManager.stage.stageHeight - this._inset.bottom));
if (this.entity.transform.rotation != 0){
// 特别注意旋转的边界。我们需要找到绝对的最小/最大值并从中创建边界
let topRight = this.screenToWorldPoint(new Vector2(SceneManager.stage.stageWidth - this._inset.right,
this._inset.top));
let bottomLeft = this.screenToWorldPoint(new Vector2(this._inset.left,
SceneManager.stage.stageHeight - this._inset.bottom));
let minX = Math.min(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
let maxX = Math.max(topLeft.x, bottomRight.x, topRight.x, bottomLeft.x);
let minY = Math.min(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
let maxY = Math.max(topLeft.y, bottomRight.y, topRight.y, bottomLeft.y);
this._bounds.location = new Vector2(minX, minY);
this._bounds.width = maxX - minX;
this._bounds.height = maxX - minY;
} else {
this._bounds.location = topLeft;
this._bounds.width = bottomRight.x - topLeft.x;
this._bounds.height = bottomRight.y - topLeft.y;
}
this._areBoundsDirty = false;
}
return this._bounds;
}
/**
*
*/
public get transformMatrix(): Matrix2D {
if (this._areMatrixedDirty)
this.updateMatrixes();
return this._transformMatrix;
}
/**
*
*/
public get inverseTransformMatrix(): Matrix2D {
if (this._areMatrixedDirty)
this.updateMatrixes();
return this._inverseTransformMatrix;
} }
public get origin() { public get origin() {
@@ -102,13 +160,23 @@ module es {
public set origin(value: Vector2) { public set origin(value: Vector2) {
if (this._origin != value) { if (this._origin != value) {
this._origin = value; this._origin = value;
this._areMatrixedDirty = true;
} }
} }
private _zoom; public _zoom;
private _minimumZoom = 0.3; public _minimumZoom = 0.3;
private _maximumZoom = 3; public _maximumZoom = 3;
private _origin: Vector2 = Vector2.zero; public _bounds: Rectangle;
public _inset: CameraInset;
public _transformMatrix: Matrix2D = new Matrix2D().identity();
public _inverseTransformMatrix: Matrix2D = new Matrix2D().identity();
public _origin: Vector2 = Vector2.zero;
public _areMatrixedDirty: boolean = true;
public _areBoundsDirty: boolean = true;
public _isProjectionMatrixDirty = true;
/** /**
* cameraWindow * cameraWindow
* *
@@ -158,6 +226,50 @@ module es {
this.entity.transform.position = Vector2.add(this.entity.transform.position, Vector2.subtract(this._origin, oldOrigin)); this.entity.transform.position = Vector2.add(this.entity.transform.position, Vector2.subtract(this._origin, oldOrigin));
} }
protected updateMatrixes(){
if (!this._areMatrixedDirty)
return;
let tempMat: Matrix2D;
this._transformMatrix = Matrix2D.create().translate(-this.entity.transform.position.x, -this.entity.transform.position.y);
if (this._zoom != 1){
tempMat = Matrix2D.create().scale(this._zoom, this._zoom);
this._transformMatrix = this._transformMatrix.multiply(tempMat);
}
if (this.entity.transform.rotation != 0){
tempMat = Matrix2D.create().rotate(this.entity.transform.rotation);
this._transformMatrix = this._transformMatrix.multiply(tempMat);
}
tempMat = Matrix2D.create().translate(this._origin.x, this._origin.y);
this._transformMatrix =this._transformMatrix.multiply(tempMat);
this._inverseTransformMatrix = this._transformMatrix.invert();
// 无论何时矩阵改变边界都是无效的
this._areBoundsDirty = true;
this._areMatrixedDirty = false;
}
/**
*
* @param left
* @param right
* @param top
* @param bottom
*/
public setInset(left: number, right: number, top: number, bottom: number): Camera {
this._inset = new CameraInset();
this._inset.left = left;
this._inset.right = right;
this._inset.top = top;
this._inset.bottom = bottom;
this._areBoundsDirty = true;
return this;
}
/** /**
* entity.transform.setPosition快速访问 * entity.transform.setPosition快速访问
* @param position * @param position
@@ -190,9 +302,8 @@ module es {
} else { } else {
this._zoom = MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom); this._zoom = MathHelper.map(newZoom, 0, 1, 1, this._maximumZoom);
} }
this._areMatrixedDirty = true;
SceneManager.scene.scaleX = this._zoom;
SceneManager.scene.scaleY = this._zoom;
return this; return this;
} }
@@ -201,6 +312,11 @@ module es {
* @param minZoom * @param minZoom
*/ */
public setMinimumZoom(minZoom: number): Camera { public setMinimumZoom(minZoom: number): Camera {
if (minZoom <= 0) {
console.error("minimumZoom must be greater than zero");
return;
}
if (this._zoom < minZoom) if (this._zoom < minZoom)
this._zoom = this.minimumZoom; this._zoom = this.minimumZoom;
@@ -225,6 +341,10 @@ module es {
return this; return this;
} }
public onEntityTransformChanged(comp: transform.Component) {
this._areMatrixedDirty = true;
}
public zoomIn(deltaZoom: number) { public zoomIn(deltaZoom: number) {
this.zoom += deltaZoom; this.zoom += deltaZoom;
} }
@@ -233,6 +353,33 @@ module es {
this.zoom -= deltaZoom; this.zoom -= deltaZoom;
} }
/**
*
* @param worldPosition
*/
public worldToScreenPoint(worldPosition: Vector2): Vector2{
this.updateMatrixes();
worldPosition = Vector2.transform(worldPosition, this._transformMatrix);
return worldPosition;
}
/**
*
* @param screenPosition
*/
public screenToWorldPoint(screenPosition: Vector2): Vector2{
this.updateMatrixes();
screenPosition = Vector2.transform(screenPosition, this._inverseTransformMatrix);
return screenPosition;
}
/**
*
*/
public mouseToWorldPoint(): Vector2{
return this.screenToWorldPoint(Input.touchPosition);
}
public onAddedToEntity() { public onAddedToEntity() {
this.follow(this._targetEntity, this._cameraStyle); this.follow(this._targetEntity, this._cameraStyle);
} }
@@ -4,11 +4,12 @@ module es {
* *
*/ */
public shape: Shape; public shape: Shape;
/** /**
* localOffset添加到实体 * localOffset添加到实体
* / * /
*/ */
public get localOffset(): Vector2{ public get localOffset(): Vector2 {
return this._localOffset; return this._localOffset;
} }
@@ -17,21 +18,21 @@ module es {
* / * /
* @param value * @param value
*/ */
public set localOffset(value: Vector2){ public set localOffset(value: Vector2) {
this.setLocalOffset(value); this.setLocalOffset(value);
} }
/** /**
* *
*/ */
public get absolutePosition(): Vector2{ public get absolutePosition(): Vector2 {
return Vector2.add(this.entity.transform.position, this._localOffset); return Vector2.add(this.entity.transform.position, this._localOffset);
} }
/** /**
* transform.rotation * transform.rotation
*/ */
public get rotation(): number{ public get rotation(): number {
if (this.shouldColliderScaleAndRotateWithTransform && this.entity) if (this.shouldColliderScaleAndRotateWithTransform && this.entity)
return this.entity.transform.rotation; return this.entity.transform.rotation;
@@ -58,7 +59,10 @@ module es {
public shouldColliderScaleAndRotateWithTransform = true; public shouldColliderScaleAndRotateWithTransform = true;
public get bounds(): Rectangle { public get bounds(): Rectangle {
this.shape.recalculateBounds(this); if (this._isPositionDirty || this._isRotationDirty){
this.shape.recalculateBounds(this);
this._isPositionDirty = this._isRotationDirty = false;
}
return this.shape.bounds; return this.shape.bounds;
} }
@@ -81,16 +85,20 @@ module es {
*/ */
protected _isColliderRegistered; protected _isColliderRegistered;
public _isPositionDirty: boolean = true;
public _isRotationDirty: boolean = true;
/** /**
* localOffset添加到实体 * localOffset添加到实体
* *
* @param offset * @param offset
*/ */
public setLocalOffset(offset: Vector2): Collider{ public setLocalOffset(offset: Vector2): Collider {
if (this._localOffset != offset){ if (this._localOffset != offset) {
this.unregisterColliderWithPhysicsSystem(); this.unregisterColliderWithPhysicsSystem();
this._localOffset = offset; this._localOffset = offset;
this._localOffsetLength = this._localOffset.length(); this._localOffsetLength = this._localOffset.length();
this._isPositionDirty = true;
this.registerColliderWithPhysicsSystem(); this.registerColliderWithPhysicsSystem();
} }
@@ -103,6 +111,7 @@ module es {
*/ */
public setShouldColliderScaleAndRotateWithTransform(shouldColliderScaleAndRotationWithTransform: boolean): Collider { public setShouldColliderScaleAndRotateWithTransform(shouldColliderScaleAndRotationWithTransform: boolean): Collider {
this.shouldColliderScaleAndRotateWithTransform = shouldColliderScaleAndRotationWithTransform; this.shouldColliderScaleAndRotateWithTransform = shouldColliderScaleAndRotationWithTransform;
this._isPositionDirty = this._isRotationDirty = true;
return this; return this;
} }
@@ -121,7 +130,7 @@ module es {
let width = bounds.width / this.entity.scale.x; let width = bounds.width / this.entity.scale.x;
let height = bounds.height / this.entity.scale.y; let height = bounds.height / this.entity.scale.y;
// 圆碰撞器需要特别注意原点 // 圆碰撞器需要特别注意原点
if (this instanceof CircleCollider){ if (this instanceof CircleCollider) {
let circleCollider = this as CircleCollider; let circleCollider = this as CircleCollider;
circleCollider.radius = Math.max(width, height) * 0.5; circleCollider.radius = Math.max(width, height) * 0.5;
} else { } else {
@@ -146,12 +155,25 @@ module es {
} }
public onEntityTransformChanged(comp: transform.Component) { public onEntityTransformChanged(comp: transform.Component) {
switch (comp) {
case transform.Component.position:
this._isPositionDirty = true;
break;
case transform.Component.scale:
this._isPositionDirty = true;
break;
case transform.Component.rotation:
this._isRotationDirty = true;
break;
}
if (this._isColliderRegistered) if (this._isColliderRegistered)
Physics.updateCollider(this); Physics.updateCollider(this);
} }
public onEnabled() { public onEnabled() {
this.registerColliderWithPhysicsSystem(); this.registerColliderWithPhysicsSystem();
this._isPositionDirty = this._isRotationDirty = true;
} }
public onDisabled() { public onDisabled() {
@@ -206,5 +228,15 @@ module es {
return result; return result;
} }
public clone(): Component{
let collider = ObjectUtils.clone<Collider>(this);
collider.entity = null;
if (this.shape)
collider.shape = this.shape.clone();
return collider;
}
} }
} }
+98 -176
View File
@@ -2,218 +2,140 @@ module es {
/** /**
* 3 * 3 * 3 * 3
*/ */
export class Matrix2D { export class Matrix2D extends egret.Matrix{
public m11: number = 0; public get m11(): number{
public m12: number = 0; return this.a;
}
public m21: number = 0; public set m11(value: number){
public m22: number = 0; this.a = value;
}
public m31: number = 0; public get m12(): number{
public m32: number = 0; return this.b;
}
private static _identity: Matrix2D = new Matrix2D(1, 0, 0, 1, 0, 0); public set m12(value: number){
this.b = value;
/** }
* public get m21(): number{
*/ return this.c;
public static get identity(){ }
return Matrix2D._identity; public set m21(value: number){
this.c = value;
}
public get m22(): number{
return this.d;
}
public set m22(value: number){
this.d = value;
}
public get m31(): number {
return this.tx;
}
public set m31(value: number){
this.tx = value;
}
public get m32(): number{
return this.ty;
}
public set m32(value: number){
this.ty = value;
} }
constructor(m11?: number, m12?: number, m21?: number, m22?: number, m31?: number, m32?: number){ public static create(): Matrix2D{
this.m11 = m11 ? m11 : 1; return egret.Matrix.create() as Matrix2D;
this.m12 = m12 ? m12 : 0;
this.m21 = m21 ? m21 : 0;
this.m22 = m22 ? m22 : 1;
this.m31 = m31 ? m31 : 0;
this.m32 = m32 ? m32 : 0;
} }
/** 存储在这个矩阵中的位置 */ public identity(): Matrix2D{
public get translation(){ super.identity();
return new Vector2(this.m31, this.m32); return this;
} }
public set translation(value: Vector2){ public translate(dx: number, dy: number): Matrix2D {
this.m31 = value.x; super.translate(dx, dy);
this.m32 = value.y; return this;
} }
/** 以弧度表示的旋转存储在这个矩阵中 */ public scale(sx: number, sy: number): Matrix2D {
public get rotation(){ super.scale(sx, sy);
return Math.atan2(this.m21, this.m11); return this;
} }
public set rotation(value: number){ public rotate(angle: number): Matrix2D {
let val1 = Math.cos(value); super.rotate(angle);
let val2 = Math.sin(value); return this;
this.m11 = val1;
this.m12 = val2;
this.m21 = -val2;
this.m22 = val1;
} }
/** public invert(): Matrix2D {
* super.invert();
*/ return this;
public get rotationDegrees(){
return MathHelper.toDegrees(this.rotation);
}
public set rotationDegrees(value: number){
this.rotation = MathHelper.toRadians(value);
}
public get scale(){
return new Vector2(this.m11, this.m22);
}
public set scale(value: Vector2){
this.m11 = value.x;
this.m12 = value.y;
} }
/** /**
* matrix, * matrix,
* @param matrix1 * @param matrix
* @param matrix2
*/ */
public static add(matrix1: Matrix2D, matrix2: Matrix2D){ public add(matrix: Matrix2D): Matrix2D{
matrix1.m11 += matrix2.m11; this.m11 += matrix.m11;
matrix1.m12 += matrix2.m12; this.m12 += matrix.m12;
matrix1.m21 += matrix2.m21; this.m21 += matrix.m21;
matrix1.m22 += matrix2.m22; this.m22 += matrix.m22;
matrix1.m31 += matrix2.m31; this.m31 += matrix.m31;
matrix1.m32 += matrix2.m32; this.m32 += matrix.m32;
return matrix1; return this;
} }
public static divide(matrix1: Matrix2D, matrix2: Matrix2D){ public substract(matrix: Matrix2D): Matrix2D {
matrix1.m11 /= matrix2.m11; this.m11 -= matrix.m11;
matrix1.m12 /= matrix2.m12; this.m12 -= matrix.m12;
matrix1.m21 /= matrix2.m21; this.m21 -= matrix.m21;
matrix1.m22 /= matrix2.m22; this.m22 -= matrix.m22;
matrix1.m31 /= matrix2.m31; this.m31 -= matrix.m31;
matrix1.m32 /= matrix2.m32; this.m32 -= matrix.m32;
return matrix1; return this;
} }
public static multiply(matrix1: Matrix2D, matrix2: Matrix2D){ public divide(matrix: Matrix2D): Matrix2D{
let result = new Matrix2D(); this.m11 /= matrix.m11;
this.m12 /= matrix.m12;
let m11 = ( matrix1.m11 * matrix2.m11 ) + ( matrix1.m12 * matrix2.m21 ); this.m21 /= matrix.m21;
let m12 = ( matrix1.m11 * matrix2.m12 ) + ( matrix1.m12 * matrix2.m22 ); this.m22 /= matrix.m22;
let m21 = ( matrix1.m21 * matrix2.m11 ) + ( matrix1.m22 * matrix2.m21 ); this.m31 /= matrix.m31;
let m22 = ( matrix1.m21 * matrix2.m12 ) + ( matrix1.m22 * matrix2.m22 ); this.m32 /= matrix.m32;
let m31 = ( matrix1.m31 * matrix2.m11 ) + ( matrix1.m32 * matrix2.m21 ) + matrix2.m31; return this;
let m32 = ( matrix1.m31 * matrix2.m12 ) + ( matrix1.m32 * matrix2.m22 ) + matrix2.m32;
result.m11 = m11;
result.m12 = m12;
result.m21 = m21;
result.m22 = m22;
result.m31 = m31;
result.m32 = m32;
return result;
} }
public static multiplyTranslation(matrix: Matrix2D, x: number, y: number){ public multiply(matrix: Matrix2D): Matrix2D{
let trans = Matrix2D.createTranslation(x, y); let m11 = ( this.m11 * matrix.m11 ) + ( this.m12 * matrix.m21 );
return Matrix2D.multiply(matrix, trans); let m12 = ( this.m11 * matrix.m12 ) + ( this.m12 * matrix.m22 );
let m21 = ( this.m21 * matrix.m11 ) + ( this.m22 * matrix.m21 );
let m22 = ( this.m21 * matrix.m12 ) + ( this.m22 * matrix.m22 );
let m31 = ( this.m31 * matrix.m11 ) + ( this.m32 * matrix.m21 ) + matrix.m31;
let m32 = ( this.m31 * matrix.m12 ) + ( this.m32 * matrix.m22 ) + matrix.m32;
this.m11 = m11;
this.m12 = m12;
this.m21 = m21;
this.m22 = m22;
this.m31 = m31;
this.m32 = m32;
return this;
} }
public determinant(){ public determinant(){
return this.m11 * this.m22 - this.m12 * this.m21; return this.m11 * this.m22 - this.m12 * this.m21;
} }
public static invert(matrix: Matrix2D, result: Matrix2D = new Matrix2D()){
let det = 1 / matrix.determinant();
result.m11 = matrix.m22 * det;
result.m12 = -matrix.m12 * det;
result.m21 = -matrix.m21 * det;
result.m22 = matrix.m11 * det;
result.m31 = (matrix.m32 * matrix.m21 - matrix.m31 * matrix.m22) * det;
result.m32 = -(matrix.m32 * matrix.m11 - matrix.m31 * matrix.m12) * det;
return result;
}
/**
* tranlation
* @param xPosition
* @param yPosition
*/
public static createTranslation(xPosition: number, yPosition: number){
let result = new Matrix2D();
result.m11 = 1;
result.m12 = 0;
result.m21 = 0;
result.m22 = 1;
result.m31 = xPosition;
result.m32 = yPosition;
return result;
}
/**
* position translation
* @param position
*/
public static createTranslationVector(position: Vector2){
return this.createTranslation(position.x, position.y);
}
public static createRotation(radians: number, result?: Matrix2D){
result = new Matrix2D();
let val1 = Math.cos(radians);
let val2 = Math.sin(radians);
result.m11 = val1;
result.m12 = val2;
result.m21 = -val2;
result.m22 = val1;
return result;
}
public static createScale(xScale: number, yScale: number, result: Matrix2D = new Matrix2D()){
result.m11 = xScale;
result.m12 = 0;
result.m21 = 0;
result.m22 = yScale;
result.m31 = 0;
result.m32 = 0;
return result;
}
public toEgretMatrix(): egret.Matrix{
let matrix = new egret.Matrix(this.m11, this.m12, this.m21, this.m22, this.m31, this.m32);
return matrix;
}
} }
} }
+40
View File
@@ -34,6 +34,46 @@ module es {
this.y = y ? y : this.x; this.y = y ? y : this.x;
} }
/**
*
* @param value
*/
public add(value: Vector2): Vector2{
this.x += value.x;
this.y += value.y;
return this;
}
/**
*
* @param value
*/
public divide(value: Vector2): Vector2{
this.x /= value.x;
this.y /= value.y;
return this;
}
/**
*
* @param value
*/
public multiply(value: Vector2): Vector2{
this.x *= value.x;
this.y *= value.y;
return this;
}
/**
*
* @param value
*/
public subtract(value: Vector2){
this.x -= value.x;
this.y -= value.y;
return this;
}
/** /**
* *
* @param value1 * @param value1
+36
View File
@@ -177,7 +177,43 @@ module es {
this.center = collider.localOffset; this.center = collider.localOffset;
if (collider.shouldColliderScaleAndRotateWithTransform){ if (collider.shouldColliderScaleAndRotateWithTransform){
let hasUnitScale = true;
let tempMat: Matrix2D;
let combinedMatrix = Matrix2D.create().translate(-this._polygonCenter.x, -this._polygonCenter.y);
if (collider.entity.transform.scale != Vector2.zero){
tempMat = Matrix2D.create().scale(collider.entity.transform.scale.x, collider.entity.transform.scale.y);
combinedMatrix = combinedMatrix.multiply(tempMat);
hasUnitScale = false;
// 缩放偏移量并将其设置为中心。如果我们有旋转,它会在下面重置
this.center = Vector2.multiply(collider.localOffset, collider.entity.transform.scale);
}
if (collider.entity.transform.rotation != 0){
tempMat = Matrix2D.create().rotate(collider.entity.transform.rotation);
combinedMatrix = combinedMatrix.multiply(tempMat);
// 为了处理偏移原点的旋转我们只需要将圆心在(0,0)附近移动
// 我们的偏移使角度为0我们还需要处理这里的比例所以我们先对偏移进行缩放以得到合适的长度。
let offsetAngle = Math.atan2(collider.localOffset.y, collider.localOffset.x);
let offsetLength = hasUnitScale ? collider._localOffsetLength :
Vector2.multiply(collider.localOffset, collider.entity.transform.scale).length();
this.center = MathHelper.pointOnCirlce(Vector2.zero, offsetLength,
collider.entity.transform.rotation + offsetAngle);
}
tempMat = Matrix2D.create().translate(this._polygonCenter.x, this._polygonCenter.y);
combinedMatrix = combinedMatrix.multiply(tempMat);
// 最后变换原始点
Vector2Ext.transform(this._originalPoints, combinedMatrix, this.points);
this.isUnrotated = collider.entity.transform.rotation == 0; this.isUnrotated = collider.entity.transform.rotation == 0;
// 如果旋转的话,我们只需要重建边的法线
if (collider._isRotationDirty)
this._areEdgeNormalsDirty = true;
} }
this.position = Vector2.add(collider.entity.transform.position, this.center); this.position = Vector2.add(collider.entity.transform.position, this.center);
+4
View File
@@ -19,5 +19,9 @@ module es {
public abstract pointCollidesWithShape(point: Vector2): CollisionResult; public abstract pointCollidesWithShape(point: Vector2): CollisionResult;
public abstract overlaps(other: Shape); public abstract overlaps(other: Shape);
public abstract collidesWithShape(other: Shape): CollisionResult; public abstract collidesWithShape(other: Shape): CollisionResult;
public clone(): Shape{
return ObjectUtils.clone<Shape>(this);
}
} }
} }