新增renderablecomponent显示

优化返回值
This commit is contained in:
yhh
2020-07-27 16:10:36 +08:00
parent 149a3e5833
commit 506f8ddc0f
28 changed files with 631 additions and 512 deletions
+36 -45
View File
@@ -133,6 +133,7 @@ declare module es {
static transform(position: Vector2, matrix: Matrix2D): Vector2; static transform(position: Vector2, matrix: Matrix2D): Vector2;
static distance(value1: Vector2, value2: Vector2): number; static distance(value1: Vector2, value2: Vector2): number;
static negate(value: Vector2): Vector2; static negate(value: Vector2): Vector2;
equals(other: Vector2): boolean;
} }
} }
declare module es { declare module es {
@@ -387,13 +388,14 @@ declare module transform {
} }
} }
declare module es { declare module es {
import HashObject = egret.HashObject;
enum DirtyType { enum DirtyType {
clean = 0, clean = 0,
positionDirty = 1, positionDirty = 1,
scaleDirty = 2, scaleDirty = 2,
rotationDirty = 3 rotationDirty = 3
} }
class Transform { class Transform extends HashObject {
readonly entity: Entity; readonly entity: Entity;
parent: Transform; parent: Transform;
readonly childCount: number; readonly childCount: number;
@@ -448,6 +450,7 @@ declare module es {
setDirty(dirtyFlagType: DirtyType): void; setDirty(dirtyFlagType: DirtyType): void;
copyFrom(transform: Transform): void; copyFrom(transform: Transform): void;
toString(): string; toString(): string;
equals(other: Transform): boolean;
} }
} }
declare module es { declare module es {
@@ -536,6 +539,7 @@ declare module es {
} }
declare module es { declare module es {
abstract class RenderableComponent extends Component implements IRenderable { abstract class RenderableComponent extends Component implements IRenderable {
displayObject: egret.DisplayObject;
readonly width: number; readonly width: number;
readonly height: number; readonly height: number;
readonly bounds: Rectangle; readonly bounds: Rectangle;
@@ -556,6 +560,7 @@ declare module es {
setRenderLayer(renderLayer: number): RenderableComponent; setRenderLayer(renderLayer: number): RenderableComponent;
setColor(color: number): RenderableComponent; setColor(color: number): RenderableComponent;
setLocalOffset(offset: Vector2): RenderableComponent; setLocalOffset(offset: Vector2): RenderableComponent;
sync(camera: Camera): void;
toString(): string; toString(): string;
} }
} }
@@ -667,12 +672,9 @@ declare module es {
class Mover extends Component { class Mover extends Component {
private _triggerHelper; private _triggerHelper;
onAddedToEntity(): void; onAddedToEntity(): void;
calculateMovement(motion: Vector2): { calculateMovement(motion: Vector2, collisionResult: CollisionResult): boolean;
collisionResult: CollisionResult;
motion: Vector2;
};
applyMovement(motion: Vector2): void; applyMovement(motion: Vector2): void;
move(motion: Vector2): CollisionResult; move(motion: Vector2, collisionResult: CollisionResult): boolean;
} }
} }
declare module es { declare module es {
@@ -712,8 +714,8 @@ declare module es {
onDisabled(): void; onDisabled(): void;
registerColliderWithPhysicsSystem(): void; registerColliderWithPhysicsSystem(): void;
unregisterColliderWithPhysicsSystem(): void; unregisterColliderWithPhysicsSystem(): void;
overlaps(other: Collider): any; overlaps(other: Collider): boolean;
collidesWith(collider: Collider, motion: Vector2): CollisionResult; collidesWith(collider: Collider, motion: Vector2, result: CollisionResult): boolean;
clone(): Component; clone(): Component;
} }
} }
@@ -1054,15 +1056,20 @@ declare module es {
declare module es { declare module es {
abstract class Renderer { abstract class Renderer {
camera: Camera; camera: Camera;
readonly renderOrder: number;
protected constructor(renderOrder: number, camera?: Camera);
onAddedToScene(scene: Scene): void; onAddedToScene(scene: Scene): void;
unload(): void;
protected beginRender(cam: Camera): void; protected beginRender(cam: Camera): void;
abstract render(scene: Scene): any; abstract render(scene: Scene): any;
unload(): void;
protected renderAfterStateCheck(renderable: IRenderable, cam: Camera): void; protected renderAfterStateCheck(renderable: IRenderable, cam: Camera): void;
onSceneBackBufferSizeChanged(newWidth: number, newHeight: number): void;
compareTo(other: Renderer): number;
} }
} }
declare module es { declare module es {
class DefaultRenderer extends Renderer { class DefaultRenderer extends Renderer {
constructor();
render(scene: Scene): void; render(scene: Scene): void;
} }
} }
@@ -1197,10 +1204,7 @@ declare module es {
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;
getClosestPointOnRectangleBorderToPoint(point: Vector2): { getClosestPointOnRectangleBorderToPoint(point: Vector2, edgeNormal: Vector2): Vector2;
res: Vector2;
edgeNormal: Vector2;
};
getClosestPointOnBoundsToOrigin(): Vector2; getClosestPointOnBoundsToOrigin(): Vector2;
calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void; calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void;
setEgretRect(rect: egret.Rectangle): Rectangle; setEgretRect(rect: egret.Rectangle): Rectangle;
@@ -1260,14 +1264,8 @@ declare module es {
static reset(): void; static reset(): void;
static clear(): void; static clear(): void;
static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number; static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number;
static boxcastBroadphase(rect: Rectangle, layerMask?: number): { static boxcastBroadphase(rect: Rectangle, layerMask?: number): Collider[];
colliders: Collider[]; static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask?: number): Collider[];
rect: Rectangle;
};
static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask?: number): {
tempHashSet: Collider[];
bounds: Rectangle;
};
static addCollider(collider: Collider): void; static addCollider(collider: Collider): void;
static removeCollider(collider: Collider): void; static removeCollider(collider: Collider): void;
static updateCollider(collider: Collider): void; static updateCollider(collider: Collider): void;
@@ -1280,9 +1278,9 @@ declare module es {
center: Vector2; center: Vector2;
bounds: Rectangle; bounds: Rectangle;
abstract recalculateBounds(collider: Collider): any; abstract recalculateBounds(collider: Collider): any;
abstract pointCollidesWithShape(point: Vector2): CollisionResult; abstract overlaps(other: Shape): boolean;
abstract overlaps(other: Shape): any; abstract collidesWithShape(other: Shape, collisionResult: CollisionResult): boolean;
abstract collidesWithShape(other: Shape): CollisionResult; abstract pointCollidesWithShape(point: Vector2, result: CollisionResult): boolean;
clone(): Shape; clone(): Shape;
} }
} }
@@ -1303,16 +1301,12 @@ declare module es {
static buildSymmetricalPolygon(vertCount: number, radius: number): any[]; static buildSymmetricalPolygon(vertCount: number, radius: number): any[];
static recenterPolygonVerts(points: Vector2[]): void; static recenterPolygonVerts(points: Vector2[]): void;
static findPolygonCenter(points: Vector2[]): Vector2; static findPolygonCenter(points: Vector2[]): Vector2;
static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): { static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2, distanceSquared: number, edgeNormal: Vector2): Vector2;
closestPoint: any;
distanceSquared: any;
edgeNormal: any;
};
recalculateBounds(collider: Collider): void; recalculateBounds(collider: Collider): void;
overlaps(other: Shape): any; overlaps(other: Shape): any;
collidesWithShape(other: Shape): any; collidesWithShape(other: Shape, result: CollisionResult): boolean;
containsPoint(point: Vector2): boolean; containsPoint(point: Vector2): boolean;
pointCollidesWithShape(point: Vector2): CollisionResult; pointCollidesWithShape(point: Vector2, result: CollisionResult): boolean;
} }
} }
declare module es { declare module es {
@@ -1323,7 +1317,7 @@ declare module es {
private static buildBox; private static buildBox;
updateBox(width: number, height: number): void; updateBox(width: number, height: number): void;
overlaps(other: Shape): any; overlaps(other: Shape): any;
collidesWithShape(other: Shape): any; collidesWithShape(other: Shape, result: CollisionResult): boolean;
containsPoint(point: Vector2): boolean; containsPoint(point: Vector2): boolean;
} }
} }
@@ -1334,8 +1328,8 @@ declare module es {
constructor(radius: number); constructor(radius: number);
recalculateBounds(collider: es.Collider): void; recalculateBounds(collider: es.Collider): void;
overlaps(other: Shape): any; overlaps(other: Shape): any;
collidesWithShape(other: Shape): CollisionResult; collidesWithShape(other: Shape, result: CollisionResult): boolean;
pointCollidesWithShape(point: Vector2): CollisionResult; pointCollidesWithShape(point: Vector2, result: CollisionResult): boolean;
} }
} }
declare module es { declare module es {
@@ -1349,19 +1343,19 @@ declare module es {
} }
declare module es { declare module es {
class ShapeCollisions { class ShapeCollisions {
static polygonToPolygon(first: Polygon, second: Polygon): CollisionResult; static polygonToPolygon(first: Polygon, second: Polygon, result: CollisionResult): boolean;
static intervalDistance(minA: number, maxA: number, minB: number, maxB: any): number; static intervalDistance(minA: number, maxA: number, minB: number, maxB: any): number;
static getInterval(axis: Vector2, polygon: Polygon, min: number, max: number): { static getInterval(axis: Vector2, polygon: Polygon, min: number, max: number): {
min: number; min: number;
max: number; max: number;
}; };
static circleToPolygon(circle: Circle, polygon: Polygon): CollisionResult; static circleToPolygon(circle: Circle, polygon: Polygon, result: CollisionResult): boolean;
static circleToBox(circle: Circle, box: Box): CollisionResult; static circleToBox(circle: Circle, box: Box, result: CollisionResult): boolean;
static pointToCircle(point: Vector2, circle: Circle): CollisionResult; static pointToCircle(point: Vector2, circle: Circle, result: CollisionResult): boolean;
static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2; static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2;
static pointToPoly(point: Vector2, poly: Polygon): CollisionResult; static pointToPoly(point: Vector2, poly: Polygon, result: CollisionResult): boolean;
static circleToCircle(first: Circle, second: Circle): CollisionResult; static circleToCircle(first: Circle, second: Circle, result: CollisionResult): boolean;
static boxToBox(first: Box, second: Box): CollisionResult; static boxToBox(first: Box, second: Box, result: CollisionResult): boolean;
private static minkowskiDifference; private static minkowskiDifference;
} }
} }
@@ -1383,10 +1377,7 @@ declare module es {
clear(): void; clear(): void;
debugDraw(secondsToDisplay: number, textScale?: number): void; debugDraw(secondsToDisplay: number, textScale?: number): void;
private debugDrawCellDetails; private debugDrawCellDetails;
aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number): { aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number): Collider[];
tempHashSet: Collider[];
bounds: Rectangle;
};
overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask: any): number; overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask: any): number;
} }
class NumberDictionary { class NumberDictionary {
+166 -139
View File
@@ -735,7 +735,7 @@ var es;
return new Vector2(es.MathHelper.lerp(value1.x, value2.x, amount), es.MathHelper.lerp(value1.y, value2.y, amount)); return new Vector2(es.MathHelper.lerp(value1.x, value2.x, amount), es.MathHelper.lerp(value1.y, value2.y, amount));
}; };
Vector2.transform = function (position, matrix) { Vector2.transform = function (position, matrix) {
return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22)); return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21) + matrix.m31, (position.x * matrix.m12) + (position.y * matrix.m22) + matrix.m32);
}; };
Vector2.distance = function (value1, value2) { Vector2.distance = function (value1, value2) {
var v1 = value1.x - value2.x, v2 = value1.y - value2.y; var v1 = value1.x - value2.x, v2 = value1.y - value2.y;
@@ -747,6 +747,9 @@ var es;
result.y = -value.y; result.y = -value.y;
return result; return result;
}; };
Vector2.prototype.equals = function (other) {
return other.x == this.x && other.y == this.y;
};
Vector2.unitYVector = new Vector2(0, 1); Vector2.unitYVector = new Vector2(0, 1);
Vector2.unitXVector = new Vector2(1, 0); Vector2.unitXVector = new Vector2(1, 0);
Vector2.unitVector2 = new Vector2(1, 1); Vector2.unitVector2 = new Vector2(1, 1);
@@ -1145,7 +1148,7 @@ var es;
this.addEventListener(egret.Event.RESIZE, this.onGraphicsDeviceReset, this); this.addEventListener(egret.Event.RESIZE, this.onGraphicsDeviceReset, this);
this.addEventListener(egret.StageOrientationEvent.ORIENTATION_CHANGE, this.onOrientationChanged, this); this.addEventListener(egret.StageOrientationEvent.ORIENTATION_CHANGE, this.onOrientationChanged, this);
this.addEventListener(egret.Event.ENTER_FRAME, this.update, this); this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
this.addEventListener(egret.Event.RENDER, this.draw, this); es.Input.initialize();
this.initialize(); this.initialize();
}; };
Core.prototype.onOrientationChanged = function () { Core.prototype.onOrientationChanged = function () {
@@ -1183,7 +1186,10 @@ var es;
case 1: case 1:
_a.sent(); _a.sent();
_a.label = 2; _a.label = 2;
case 2: return [2]; case 2: return [4, this.draw()];
case 3:
_a.sent();
return [2];
} }
}); });
}); });
@@ -1778,6 +1784,7 @@ var transform;
})(transform || (transform = {})); })(transform || (transform = {}));
var es; var es;
(function (es) { (function (es) {
var HashObject = egret.HashObject;
var DirtyType; var DirtyType;
(function (DirtyType) { (function (DirtyType) {
DirtyType[DirtyType["clean"] = 0] = "clean"; DirtyType[DirtyType["clean"] = 0] = "clean";
@@ -1785,24 +1792,27 @@ var es;
DirtyType[DirtyType["scaleDirty"] = 2] = "scaleDirty"; DirtyType[DirtyType["scaleDirty"] = 2] = "scaleDirty";
DirtyType[DirtyType["rotationDirty"] = 3] = "rotationDirty"; DirtyType[DirtyType["rotationDirty"] = 3] = "rotationDirty";
})(DirtyType = es.DirtyType || (es.DirtyType = {})); })(DirtyType = es.DirtyType || (es.DirtyType = {}));
var Transform = (function () { var Transform = (function (_super) {
__extends(Transform, _super);
function Transform(entity) { function Transform(entity) {
this._localTransform = es.Matrix2D.create(); var _this = _super.call(this) || this;
this._worldTransform = es.Matrix2D.create().identity(); _this._localTransform = es.Matrix2D.create();
this._worldToLocalTransform = es.Matrix2D.create().identity(); _this._worldTransform = es.Matrix2D.create().identity();
this._worldInverseTransform = es.Matrix2D.create().identity(); _this._worldToLocalTransform = es.Matrix2D.create().identity();
this._rotationMatrix = es.Matrix2D.create(); _this._worldInverseTransform = es.Matrix2D.create().identity();
this._translationMatrix = es.Matrix2D.create(); _this._rotationMatrix = es.Matrix2D.create();
this._scaleMatrix = es.Matrix2D.create(); _this._translationMatrix = es.Matrix2D.create();
this._position = es.Vector2.zero; _this._scaleMatrix = es.Matrix2D.create();
this._scale = es.Vector2.one; _this._position = es.Vector2.zero;
this._rotation = 0; _this._scale = es.Vector2.one;
this._localPosition = es.Vector2.zero; _this._rotation = 0;
this._localScale = es.Vector2.one; _this._localPosition = es.Vector2.zero;
this._localRotation = 0; _this._localScale = es.Vector2.one;
this.entity = entity; _this._localRotation = 0;
this.scale = es.Vector2.one; _this.entity = entity;
this._children = []; _this.scale = es.Vector2.one;
_this._children = [];
return _this;
} }
Object.defineProperty(Transform.prototype, "parent", { Object.defineProperty(Transform.prototype, "parent", {
get: function () { get: function () {
@@ -1958,7 +1968,7 @@ var es;
return this._children[index]; return this._children[index];
}; };
Transform.prototype.setParent = function (parent) { Transform.prototype.setParent = function (parent) {
if (this._parent == parent) if (this._parent.equals(parent))
return this; return this;
if (!this._parent) { if (!this._parent) {
this._parent._children.remove(this); this._parent._children.remove(this);
@@ -1970,7 +1980,7 @@ var es;
}; };
Transform.prototype.setPosition = function (x, y) { Transform.prototype.setPosition = function (x, y) {
var position = new es.Vector2(x, y); var position = new es.Vector2(x, y);
if (position == this._position) if (position.equals(this._position))
return this; return this;
this._position = position; this._position = position;
if (this.parent) { if (this.parent) {
@@ -1983,7 +1993,7 @@ var es;
return this; return this;
}; };
Transform.prototype.setLocalPosition = function (localPosition) { Transform.prototype.setLocalPosition = function (localPosition) {
if (localPosition == this._localPosition) if (localPosition.equals(this._localPosition))
return this; return this;
this._localPosition = localPosition; this._localPosition = localPosition;
this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true; this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true;
@@ -2108,8 +2118,11 @@ var es;
Transform.prototype.toString = function () { Transform.prototype.toString = function () {
return "[Transform: parent: " + this.parent + ", position: " + this.position + ", rotation: " + this.rotation + ",\n scale: " + this.scale + ", localPosition: " + this._localPosition + ", localRotation: " + this._localRotation + ",\n localScale: " + this._localScale + "]"; return "[Transform: parent: " + this.parent + ", position: " + this.position + ", rotation: " + this.rotation + ",\n scale: " + this.scale + ", localPosition: " + this._localPosition + ", localRotation: " + this._localRotation + ",\n localScale: " + this._localScale + "]";
}; };
Transform.prototype.equals = function (other) {
return other.hashCode == this.hashCode;
};
return Transform; return Transform;
}()); }(HashObject));
es.Transform = Transform; es.Transform = Transform;
})(es || (es = {})); })(es || (es = {}));
var es; var es;
@@ -2227,7 +2240,7 @@ var es;
var maxY = Math.max(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.location = new es.Vector2(minX, minY);
this._bounds.width = maxX - minX; this._bounds.width = maxX - minX;
this._bounds.height = maxX - minY; this._bounds.height = maxY - minY;
} }
else { else {
this._bounds.location = topLeft; this._bounds.location = topLeft;
@@ -2499,6 +2512,7 @@ var es;
__extends(RenderableComponent, _super); __extends(RenderableComponent, _super);
function RenderableComponent() { function RenderableComponent() {
var _this = _super !== null && _super.apply(this, arguments) || this; var _this = _super !== null && _super.apply(this, arguments) || this;
_this.displayObject = new egret.DisplayObject();
_this.color = 0x000000; _this.color = 0x000000;
_this._localOffset = es.Vector2.zero; _this._localOffset = es.Vector2.zero;
_this._renderLayer = 0; _this._renderLayer = 0;
@@ -2570,8 +2584,10 @@ var es;
this._areBoundsDirty = true; this._areBoundsDirty = true;
}; };
RenderableComponent.prototype.onBecameVisible = function () { RenderableComponent.prototype.onBecameVisible = function () {
this.displayObject.visible = this.isVisible;
}; };
RenderableComponent.prototype.onBecameInvisible = function () { RenderableComponent.prototype.onBecameInvisible = function () {
this.displayObject.visible = this.isVisible;
}; };
RenderableComponent.prototype.isVisibleFromCamera = function (camera) { RenderableComponent.prototype.isVisibleFromCamera = function (camera) {
this.isVisible = camera.bounds.intersects(this.bounds); this.isVisible = camera.bounds.intersects(this.bounds);
@@ -2596,6 +2612,13 @@ var es;
} }
return this; return this;
}; };
RenderableComponent.prototype.sync = function (camera) {
this.displayObject.x = this.entity.position.x + this.localOffset.x - camera.position.x + camera.origin.x;
this.displayObject.y = this.entity.position.y + this.localOffset.y - camera.position.y + camera.origin.y;
this.displayObject.scaleX = this.entity.scale.x;
this.displayObject.scaleY = this.entity.scale.y;
this.displayObject.rotation = this.entity.rotation;
};
RenderableComponent.prototype.toString = function () { RenderableComponent.prototype.toString = function () {
return "[RenderableComponent] renderLayer: " + this.renderLayer; return "[RenderableComponent] renderLayer: " + this.renderLayer;
}; };
@@ -2626,6 +2649,7 @@ var es;
})(es || (es = {})); })(es || (es = {}));
var es; var es;
(function (es) { (function (es) {
var Bitmap = egret.Bitmap;
var SpriteRenderer = (function (_super) { var SpriteRenderer = (function (_super) {
__extends(SpriteRenderer, _super); __extends(SpriteRenderer, _super);
function SpriteRenderer(sprite) { function SpriteRenderer(sprite) {
@@ -2644,8 +2668,8 @@ var es;
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, this._origin, this.entity.transform.scale, this.entity.transform.rotation, this._sprite.sourceRect.width, this._sprite.sourceRect.height); this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, this._origin, this.entity.transform.scale, this.entity.transform.rotation, this._sprite.sourceRect.width, this._sprite.sourceRect.height);
this._areBoundsDirty = false; this._areBoundsDirty = false;
} }
return this._bounds;
} }
return this._bounds;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
@@ -2684,12 +2708,17 @@ var es;
this._sprite = sprite; this._sprite = sprite;
if (this._sprite) { if (this._sprite) {
this._origin = this._sprite.origin; this._origin = this._sprite.origin;
this.displayObject.anchorOffsetX = this._origin.x;
this.displayObject.anchorOffsetY = this._origin.y;
} }
this.displayObject = new Bitmap(sprite.texture2D);
return this; return this;
}; };
SpriteRenderer.prototype.setOrigin = function (origin) { SpriteRenderer.prototype.setOrigin = function (origin) {
if (this._origin != origin) { if (this._origin != origin) {
this._origin = origin; this._origin = origin;
this.displayObject.anchorOffsetX = this._origin.x;
this.displayObject.anchorOffsetY = this._origin.y;
this._areBoundsDirty = true; this._areBoundsDirty = true;
} }
return this; return this;
@@ -2699,6 +2728,7 @@ var es;
return this; return this;
}; };
SpriteRenderer.prototype.render = function (camera) { SpriteRenderer.prototype.render = function (camera) {
this.sync(camera);
}; };
return SpriteRenderer; return SpriteRenderer;
}(es.RenderableComponent)); }(es.RenderableComponent));
@@ -2942,10 +2972,9 @@ var es;
Mover.prototype.onAddedToEntity = function () { Mover.prototype.onAddedToEntity = function () {
this._triggerHelper = new es.ColliderTriggerHelper(this.entity); this._triggerHelper = new es.ColliderTriggerHelper(this.entity);
}; };
Mover.prototype.calculateMovement = function (motion) { Mover.prototype.calculateMovement = function (motion, collisionResult) {
var collisionResult = new es.CollisionResult();
if (!this.entity.getComponent(es.Collider) || !this._triggerHelper) { if (!this.entity.getComponent(es.Collider) || !this._triggerHelper) {
return null; return false;
} }
var colliders = this.entity.getComponents(es.Collider); var colliders = this.entity.getComponents(es.Collider);
for (var i = 0; i < colliders.length; i++) { for (var i = 0; i < colliders.length; i++) {
@@ -2955,36 +2984,32 @@ var es;
var bounds = collider.bounds; var bounds = collider.bounds;
bounds.x += motion.x; bounds.x += motion.x;
bounds.y += motion.y; bounds.y += motion.y;
var boxcastResult = es.Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers); var neighbors = es.Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers);
bounds = boxcastResult.bounds;
var neighbors = boxcastResult.tempHashSet;
for (var j = 0; j < neighbors.length; j++) { for (var j = 0; j < neighbors.length; j++) {
var neighbor = neighbors[j]; var neighbor = neighbors[j];
if (neighbor.isTrigger) if (neighbor.isTrigger)
continue; continue;
var _internalcollisionResult = collider.collidesWith(neighbor, motion); var _internalcollisionResult = new es.CollisionResult();
if (_internalcollisionResult) { if (collider.collidesWith(neighbor, motion, _internalcollisionResult)) {
motion = es.Vector2.subtract(motion, _internalcollisionResult.minimumTranslationVector); motion.subtract(_internalcollisionResult.minimumTranslationVector);
if (_internalcollisionResult.collider) { if (_internalcollisionResult.collider != null) {
collisionResult = _internalcollisionResult; collisionResult = _internalcollisionResult;
} }
} }
} }
} }
es.ListPool.free(colliders); es.ListPool.free(colliders);
return { collisionResult: collisionResult, motion: motion }; return collisionResult.collider != null;
}; };
Mover.prototype.applyMovement = function (motion) { Mover.prototype.applyMovement = function (motion) {
this.entity.position = es.Vector2.add(this.entity.position, motion); this.entity.position = es.Vector2.add(this.entity.position, motion);
if (this._triggerHelper) if (this._triggerHelper)
this._triggerHelper.update(); this._triggerHelper.update();
}; };
Mover.prototype.move = function (motion) { Mover.prototype.move = function (motion, collisionResult) {
var movementResult = this.calculateMovement(motion); this.calculateMovement(motion, collisionResult);
var collisionResult = movementResult.collisionResult;
motion = movementResult.motion;
this.applyMovement(motion); this.applyMovement(motion);
return collisionResult; return collisionResult.collider != null;
}; };
return Mover; return Mover;
}(es.Component)); }(es.Component));
@@ -3010,8 +3035,8 @@ var es;
var didCollide = false; var didCollide = false;
this.entity.position = es.Vector2.add(this.entity.position, motion); this.entity.position = es.Vector2.add(this.entity.position, motion);
var neighbors = es.Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers); var neighbors = es.Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers);
for (var i = 0; i < neighbors.colliders.length; i++) { for (var i = 0; i < neighbors.length; i++) {
var neighbor = neighbors.colliders[i]; var neighbor = neighbors[i];
if (this._collider.overlaps(neighbor) && neighbor.enabled) { if (this._collider.overlaps(neighbor) && neighbor.enabled) {
didCollide = true; didCollide = true;
this.notifyTriggerListeners(this._collider, neighbor); this.notifyTriggerListeners(this._collider, neighbor);
@@ -3171,14 +3196,14 @@ var es;
Collider.prototype.overlaps = function (other) { Collider.prototype.overlaps = function (other) {
return this.shape.overlaps(other.shape); return this.shape.overlaps(other.shape);
}; };
Collider.prototype.collidesWith = function (collider, motion) { Collider.prototype.collidesWith = function (collider, motion, result) {
var oldPosition = this.entity.position; var oldPosition = this.entity.position;
this.entity.position = es.Vector2.add(this.entity.position, motion); this.entity.position.add(motion);
var result = this.shape.collidesWithShape(collider.shape); var didCollide = this.shape.collidesWithShape(collider.shape, result);
if (result) if (didCollide)
result.collider = collider; result.collider = collider;
this.entity.position = oldPosition; this.entity.position = oldPosition;
return result; return didCollide;
}; };
Collider.prototype.clone = function () { Collider.prototype.clone = function () {
var collider = ObjectUtils.clone(this); var collider = ObjectUtils.clone(this);
@@ -3601,8 +3626,10 @@ var es;
ComponentList.prototype.deregisterAllComponents = function () { ComponentList.prototype.deregisterAllComponents = function () {
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
var component = this._components[i]; var component = this._components[i];
if (component instanceof es.RenderableComponent) if (component instanceof es.RenderableComponent) {
this._entity.scene.removeChild(component.displayObject);
this._entity.scene.renderableComponents.remove(component); this._entity.scene.renderableComponents.remove(component);
}
this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component), false); this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity); this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
} }
@@ -3610,8 +3637,10 @@ var es;
ComponentList.prototype.registerAllComponents = function () { ComponentList.prototype.registerAllComponents = function () {
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
var component = this._components[i]; var component = this._components[i];
if (component instanceof es.RenderableComponent) if (component instanceof es.RenderableComponent) {
this._entity.scene.addChild(component.displayObject);
this._entity.scene.renderableComponents.add(component); this._entity.scene.renderableComponents.add(component);
}
this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component)); this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
} }
@@ -3627,8 +3656,10 @@ var es;
if (this._componentsToAdd.length > 0) { if (this._componentsToAdd.length > 0) {
for (var i = 0, count = this._componentsToAdd.length; i < count; i++) { for (var i = 0, count = this._componentsToAdd.length; i < count; i++) {
var component = this._componentsToAdd[i]; var component = this._componentsToAdd[i];
if (component instanceof es.RenderableComponent) if (component instanceof es.RenderableComponent) {
this._entity.scene.addChild(component.displayObject);
this._entity.scene.renderableComponents.add(component); this._entity.scene.renderableComponents.add(component);
}
this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component)); this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.push(component); this._components.push(component);
@@ -3651,8 +3682,10 @@ var es;
} }
}; };
ComponentList.prototype.handleRemove = function (component) { ComponentList.prototype.handleRemove = function (component) {
if (component instanceof es.RenderableComponent) if (component instanceof es.RenderableComponent) {
this._entity.scene.removeChild(component.displayObject);
this._entity.scene.renderableComponents.remove(component); this._entity.scene.renderableComponents.remove(component);
}
this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component), false); this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity); this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
component.onRemovedFromEntity(); component.onRemovedFromEntity();
@@ -4909,15 +4942,23 @@ var es;
var es; var es;
(function (es) { (function (es) {
var Renderer = (function () { var Renderer = (function () {
function Renderer() { function Renderer(renderOrder, camera) {
if (camera === void 0) { camera = null; }
this.renderOrder = 0;
this.camera = camera;
this.renderOrder = renderOrder;
} }
Renderer.prototype.onAddedToScene = function (scene) { }; Renderer.prototype.onAddedToScene = function (scene) { };
Renderer.prototype.beginRender = function (cam) {
};
Renderer.prototype.unload = function () { }; Renderer.prototype.unload = function () { };
Renderer.prototype.beginRender = function (cam) { };
Renderer.prototype.renderAfterStateCheck = function (renderable, cam) { Renderer.prototype.renderAfterStateCheck = function (renderable, cam) {
renderable.render(cam); renderable.render(cam);
}; };
Renderer.prototype.onSceneBackBufferSizeChanged = function (newWidth, newHeight) {
};
Renderer.prototype.compareTo = function (other) {
return this.renderOrder - other.renderOrder;
};
return Renderer; return Renderer;
}()); }());
es.Renderer = Renderer; es.Renderer = Renderer;
@@ -4927,7 +4968,7 @@ var es;
var DefaultRenderer = (function (_super) { var DefaultRenderer = (function (_super) {
__extends(DefaultRenderer, _super); __extends(DefaultRenderer, _super);
function DefaultRenderer() { function DefaultRenderer() {
return _super !== null && _super.apply(this, arguments) || this; return _super.call(this, 0, null) || this;
} }
DefaultRenderer.prototype.render = function (scene) { DefaultRenderer.prototype.render = function (scene) {
var cam = this.camera ? this.camera : scene.camera; var cam = this.camera ? this.camera : scene.camera;
@@ -5567,8 +5608,8 @@ var es;
Rectangle.fromMinMax = function (minX, minY, maxX, maxY) { Rectangle.fromMinMax = function (minX, minY, maxX, maxY) {
return new Rectangle(minX, minY, maxX - minX, maxY - minY); return new Rectangle(minX, minY, maxX - minX, maxY - minY);
}; };
Rectangle.prototype.getClosestPointOnRectangleBorderToPoint = function (point) { Rectangle.prototype.getClosestPointOnRectangleBorderToPoint = function (point, edgeNormal) {
var edgeNormal = es.Vector2.zero; edgeNormal = es.Vector2.zero;
var res = new es.Vector2(); var res = new es.Vector2();
res.x = es.MathHelper.clamp(point.x, this.left, this.right); res.x = es.MathHelper.clamp(point.x, this.left, this.right);
res.y = es.MathHelper.clamp(point.y, this.top, this.bottom); res.y = es.MathHelper.clamp(point.y, this.top, this.bottom);
@@ -5605,7 +5646,7 @@ var es;
if (res.y == this.bottom) if (res.y == this.bottom)
edgeNormal.y = 1; edgeNormal.y = 1;
} }
return { res: res, edgeNormal: edgeNormal }; return res;
}; };
Rectangle.prototype.getClosestPointOnBoundsToOrigin = function () { Rectangle.prototype.getClosestPointOnBoundsToOrigin = function () {
var max = this.max; var max = this.max;
@@ -5688,9 +5729,7 @@ var es;
var colliders = this._entity.getComponents(es.Collider); var colliders = this._entity.getComponents(es.Collider);
for (var i = 0; i < colliders.length; i++) { for (var i = 0; i < colliders.length; i++) {
var collider = colliders[i]; var collider = colliders[i];
var boxcastResult = es.Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers); var neighbors = es.Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
collider.bounds = boxcastResult.rect;
var neighbors = boxcastResult.colliders;
var _loop_5 = function (j) { var _loop_5 = function (j) {
var neighbor = neighbors[j]; var neighbor = neighbors[j];
if (!collider.isTrigger && !neighbor.isTrigger) if (!collider.isTrigger && !neighbor.isTrigger)
@@ -5914,12 +5953,15 @@ var es;
}; };
Physics.overlapCircleAll = function (center, randius, results, layerMask) { Physics.overlapCircleAll = function (center, randius, results, layerMask) {
if (layerMask === void 0) { layerMask = -1; } if (layerMask === void 0) { layerMask = -1; }
if (results.length == 0) {
console.error("An empty results array was passed in. No results will ever be returned.");
return;
}
return this._spatialHash.overlapCircle(center, randius, results, layerMask); return this._spatialHash.overlapCircle(center, randius, results, layerMask);
}; };
Physics.boxcastBroadphase = function (rect, layerMask) { Physics.boxcastBroadphase = function (rect, layerMask) {
if (layerMask === void 0) { layerMask = this.allLayers; } if (layerMask === void 0) { layerMask = this.allLayers; }
var boxcastResult = this._spatialHash.aabbBroadphase(rect, null, layerMask); return this._spatialHash.aabbBroadphase(rect, null, layerMask);
return { colliders: boxcastResult.tempHashSet, rect: boxcastResult.bounds };
}; };
Physics.boxcastBroadphaseExcludingSelf = function (collider, rect, layerMask) { Physics.boxcastBroadphaseExcludingSelf = function (collider, rect, layerMask) {
if (layerMask === void 0) { layerMask = this.allLayers; } if (layerMask === void 0) { layerMask = this.allLayers; }
@@ -6026,9 +6068,9 @@ var es;
} }
return new es.Vector2(x / points.length, y / points.length); return new es.Vector2(x / points.length, y / points.length);
}; };
Polygon.getClosestPointOnPolygonToPoint = function (points, point) { Polygon.getClosestPointOnPolygonToPoint = function (points, point, distanceSquared, edgeNormal) {
var distanceSquared = Number.MAX_VALUE; distanceSquared = Number.MAX_VALUE;
var edgeNormal = new es.Vector2(0, 0); edgeNormal = new es.Vector2(0, 0);
var closestPoint = new es.Vector2(0, 0); var closestPoint = new es.Vector2(0, 0);
var tempDistanceSquared; var tempDistanceSquared;
for (var i = 0; i < points.length; i++) { for (var i = 0; i < points.length; i++) {
@@ -6044,8 +6086,8 @@ var es;
edgeNormal = new es.Vector2(-line.y, line.x); edgeNormal = new es.Vector2(-line.y, line.x);
} }
} }
edgeNormal = es.Vector2.normalize(edgeNormal); es.Vector2Ext.normalize(edgeNormal);
return { closestPoint: closestPoint, distanceSquared: distanceSquared, edgeNormal: edgeNormal }; return closestPoint;
}; };
Polygon.prototype.recalculateBounds = function (collider) { Polygon.prototype.recalculateBounds = function (collider) {
this.center = collider.localOffset; this.center = collider.localOffset;
@@ -6079,12 +6121,11 @@ var es;
this.bounds.location = es.Vector2.add(this.bounds.location, this.position); this.bounds.location = es.Vector2.add(this.bounds.location, this.position);
}; };
Polygon.prototype.overlaps = function (other) { Polygon.prototype.overlaps = function (other) {
var result; var result = new es.CollisionResult();
if (other instanceof Polygon) if (other instanceof Polygon)
return es.ShapeCollisions.polygonToPolygon(this, other); return es.ShapeCollisions.polygonToPolygon(this, other, result);
if (other instanceof es.Circle) { if (other instanceof es.Circle) {
result = es.ShapeCollisions.circleToPolygon(other, this); if (es.ShapeCollisions.circleToPolygon(other, this, result)) {
if (result) {
result.invertResult(); result.invertResult();
return true; return true;
} }
@@ -6092,18 +6133,16 @@ var es;
} }
throw new Error("overlaps of Pologon to " + other + " are not supported"); throw new Error("overlaps of Pologon to " + other + " are not supported");
}; };
Polygon.prototype.collidesWithShape = function (other) { Polygon.prototype.collidesWithShape = function (other, result) {
var result = new es.CollisionResult();
if (other instanceof Polygon) { if (other instanceof Polygon) {
return es.ShapeCollisions.polygonToPolygon(this, other); return es.ShapeCollisions.polygonToPolygon(this, other, result);
} }
if (other instanceof es.Circle) { if (other instanceof es.Circle) {
result = es.ShapeCollisions.circleToPolygon(other, this); if (es.ShapeCollisions.circleToPolygon(other, this, result)) {
if (result) {
result.invertResult(); result.invertResult();
return result; return true;
} }
return null; return false;
} }
throw new Error("overlaps of Polygon to " + other + " are not supported"); throw new Error("overlaps of Polygon to " + other + " are not supported");
}; };
@@ -6119,8 +6158,8 @@ var es;
} }
return isInside; return isInside;
}; };
Polygon.prototype.pointCollidesWithShape = function (point) { Polygon.prototype.pointCollidesWithShape = function (point, result) {
return es.ShapeCollisions.pointToPoly(point, this); return es.ShapeCollisions.pointToPoly(point, this, result);
}; };
return Polygon; return Polygon;
}(es.Shape)); }(es.Shape));
@@ -6167,11 +6206,11 @@ var es;
} }
return _super.prototype.overlaps.call(this, other); return _super.prototype.overlaps.call(this, other);
}; };
Box.prototype.collidesWithShape = function (other) { Box.prototype.collidesWithShape = function (other, result) {
if (other instanceof Box && other.isUnrotated) { if (other instanceof Box && other.isUnrotated) {
return es.ShapeCollisions.boxToBox(this, other); return es.ShapeCollisions.boxToBox(this, other, result);
} }
return _super.prototype.collidesWithShape.call(this, other); return _super.prototype.collidesWithShape.call(this, other, result);
}; };
Box.prototype.containsPoint = function (point) { Box.prototype.containsPoint = function (point) {
if (this.isUnrotated) if (this.isUnrotated)
@@ -6209,28 +6248,29 @@ var es;
this.bounds = new es.Rectangle(this.position.x - this.radius, this.position.y - this.radius, this.radius * 2, this.radius * 2); this.bounds = new es.Rectangle(this.position.x - this.radius, this.position.y - this.radius, this.radius * 2, this.radius * 2);
}; };
Circle.prototype.overlaps = function (other) { Circle.prototype.overlaps = function (other) {
var result = new es.CollisionResult();
if (other instanceof es.Box && other.isUnrotated) if (other instanceof es.Box && other.isUnrotated)
return es.Collisions.isRectToCircle(other.bounds, this.position, this.radius); return es.Collisions.isRectToCircle(other.bounds, this.position, this.radius);
if (other instanceof Circle) if (other instanceof Circle)
return es.Collisions.isCircleToCircle(this.position, this.radius, other.position, other.radius); return es.Collisions.isCircleToCircle(this.position, this.radius, other.position, other.radius);
if (other instanceof es.Polygon) if (other instanceof es.Polygon)
return es.ShapeCollisions.circleToPolygon(this, other); return es.ShapeCollisions.circleToPolygon(this, other, result);
throw new Error("overlaps of circle to " + other + " are not supported"); throw new Error("overlaps of circle to " + other + " are not supported");
}; };
Circle.prototype.collidesWithShape = function (other) { Circle.prototype.collidesWithShape = function (other, result) {
if (other instanceof es.Box && other.isUnrotated) { if (other instanceof es.Box && other.isUnrotated) {
return es.ShapeCollisions.circleToBox(this, other); return es.ShapeCollisions.circleToBox(this, other, result);
} }
if (other instanceof Circle) { if (other instanceof Circle) {
return es.ShapeCollisions.circleToCircle(this, other); return es.ShapeCollisions.circleToCircle(this, other, result);
} }
if (other instanceof es.Polygon) { if (other instanceof es.Polygon) {
return es.ShapeCollisions.circleToPolygon(this, other); return es.ShapeCollisions.circleToPolygon(this, other, result);
} }
throw new Error("Collisions of Circle to " + other + " are not supported"); throw new Error("Collisions of Circle to " + other + " are not supported");
}; };
Circle.prototype.pointCollidesWithShape = function (point) { Circle.prototype.pointCollidesWithShape = function (point, result) {
return es.ShapeCollisions.pointToCircle(point, this); return es.ShapeCollisions.pointToCircle(point, this, result);
}; };
return Circle; return Circle;
}(es.Shape)); }(es.Shape));
@@ -6257,8 +6297,7 @@ var es;
var ShapeCollisions = (function () { var ShapeCollisions = (function () {
function ShapeCollisions() { function ShapeCollisions() {
} }
ShapeCollisions.polygonToPolygon = function (first, second) { ShapeCollisions.polygonToPolygon = function (first, second, result) {
var result = new es.CollisionResult();
var isIntersecting = true; var isIntersecting = true;
var firstEdges = first.edgeNormals; var firstEdges = first.edgeNormals;
var secondEdges = second.edgeNormals; var secondEdges = second.edgeNormals;
@@ -6291,7 +6330,7 @@ var es;
if (intervalDist > 0) if (intervalDist > 0)
isIntersecting = false; isIntersecting = false;
if (!isIntersecting) if (!isIntersecting)
return null; return false;
intervalDist = Math.abs(intervalDist); intervalDist = Math.abs(intervalDist);
if (intervalDist < minIntervalDistance) { if (intervalDist < minIntervalDistance) {
minIntervalDistance = intervalDist; minIntervalDistance = intervalDist;
@@ -6302,7 +6341,7 @@ var es;
} }
result.normal = translationAxis; result.normal = translationAxis;
result.minimumTranslationVector = es.Vector2.multiply(new es.Vector2(-translationAxis.x, -translationAxis.y), new es.Vector2(minIntervalDistance)); result.minimumTranslationVector = es.Vector2.multiply(new es.Vector2(-translationAxis.x, -translationAxis.y), new es.Vector2(minIntervalDistance));
return result; return true;
}; };
ShapeCollisions.intervalDistance = function (minA, maxA, minB, maxB) { ShapeCollisions.intervalDistance = function (minA, maxA, minB, maxB) {
if (minA < minB) if (minA < minB)
@@ -6323,16 +6362,13 @@ var es;
} }
return { min: min, max: max }; return { min: min, max: max };
}; };
ShapeCollisions.circleToPolygon = function (circle, polygon) { ShapeCollisions.circleToPolygon = function (circle, polygon, result) {
var result = new es.CollisionResult();
var poly2Circle = es.Vector2.subtract(circle.position, polygon.position); var poly2Circle = es.Vector2.subtract(circle.position, polygon.position);
var gpp = es.Polygon.getClosestPointOnPolygonToPoint(polygon.points, poly2Circle); var distanceSquared = 0;
var closestPoint = gpp.closestPoint; var closestPoint = es.Polygon.getClosestPointOnPolygonToPoint(polygon.points, poly2Circle, distanceSquared, result.normal);
var distanceSquared = gpp.distanceSquared;
result.normal = gpp.edgeNormal;
var circleCenterInsidePoly = polygon.containsPoint(circle.position); var circleCenterInsidePoly = polygon.containsPoint(circle.position);
if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly) if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly)
return null; return false;
var mtv; var mtv;
if (circleCenterInsidePoly) { if (circleCenterInsidePoly) {
mtv = es.Vector2.multiply(result.normal, new es.Vector2(Math.sqrt(distanceSquared) - circle.radius)); mtv = es.Vector2.multiply(result.normal, new es.Vector2(Math.sqrt(distanceSquared) - circle.radius));
@@ -6348,16 +6384,15 @@ var es;
} }
result.minimumTranslationVector = mtv; result.minimumTranslationVector = mtv;
result.point = es.Vector2.add(closestPoint, polygon.position); result.point = es.Vector2.add(closestPoint, polygon.position);
return result; return true;
}; };
ShapeCollisions.circleToBox = function (circle, box) { ShapeCollisions.circleToBox = function (circle, box, result) {
var result = new es.CollisionResult(); var closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position, result.normal);
var closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res;
if (box.containsPoint(circle.position)) { if (box.containsPoint(circle.position)) {
result.point = closestPointOnBounds; result.point = closestPointOnBounds;
var safePlace = es.Vector2.add(closestPointOnBounds, es.Vector2.subtract(result.normal, new es.Vector2(circle.radius))); var safePlace = es.Vector2.add(closestPointOnBounds, es.Vector2.subtract(result.normal, new es.Vector2(circle.radius)));
result.minimumTranslationVector = es.Vector2.subtract(circle.position, safePlace); result.minimumTranslationVector = es.Vector2.subtract(circle.position, safePlace);
return result; return true;
} }
var sqrDistance = es.Vector2.distanceSquared(closestPointOnBounds, circle.position); var sqrDistance = es.Vector2.distanceSquared(closestPointOnBounds, circle.position);
if (sqrDistance == 0) { if (sqrDistance == 0) {
@@ -6366,14 +6401,14 @@ var es;
else if (sqrDistance <= circle.radius * circle.radius) { else if (sqrDistance <= circle.radius * circle.radius) {
result.normal = es.Vector2.subtract(circle.position, closestPointOnBounds); result.normal = es.Vector2.subtract(circle.position, closestPointOnBounds);
var depth = result.normal.length() - circle.radius; var depth = result.normal.length() - circle.radius;
result.normal = es.Vector2Ext.normalize(result.normal); result.point = closestPointOnBounds;
es.Vector2Ext.normalize(result.normal);
result.minimumTranslationVector = es.Vector2.multiply(new es.Vector2(depth), result.normal); result.minimumTranslationVector = es.Vector2.multiply(new es.Vector2(depth), result.normal);
return result; return true;
} }
return null; return false;
}; };
ShapeCollisions.pointToCircle = function (point, circle) { ShapeCollisions.pointToCircle = function (point, circle, result) {
var result = new es.CollisionResult();
var distanceSquared = es.Vector2.distanceSquared(point, circle.position); var distanceSquared = es.Vector2.distanceSquared(point, circle.position);
var sumOfRadii = 1 + circle.radius; var sumOfRadii = 1 + circle.radius;
var collided = distanceSquared < sumOfRadii * sumOfRadii; var collided = distanceSquared < sumOfRadii * sumOfRadii;
@@ -6382,9 +6417,9 @@ var es;
var depth = sumOfRadii - Math.sqrt(distanceSquared); var depth = sumOfRadii - Math.sqrt(distanceSquared);
result.minimumTranslationVector = es.Vector2.multiply(new es.Vector2(-depth, -depth), result.normal); result.minimumTranslationVector = es.Vector2.multiply(new es.Vector2(-depth, -depth), result.normal);
result.point = es.Vector2.add(circle.position, es.Vector2.multiply(result.normal, new es.Vector2(circle.radius, circle.radius))); result.point = es.Vector2.add(circle.position, es.Vector2.multiply(result.normal, new es.Vector2(circle.radius, circle.radius)));
return result; return true;
} }
return null; return false;
}; };
ShapeCollisions.closestPointOnLine = function (lineA, lineB, closestTo) { ShapeCollisions.closestPointOnLine = function (lineA, lineB, closestTo) {
var v = es.Vector2.subtract(lineB, lineA); var v = es.Vector2.subtract(lineB, lineA);
@@ -6393,22 +6428,17 @@ var es;
t = es.MathHelper.clamp(t, 0, 1); t = es.MathHelper.clamp(t, 0, 1);
return es.Vector2.add(lineA, es.Vector2.multiply(v, new es.Vector2(t, t))); return es.Vector2.add(lineA, es.Vector2.multiply(v, new es.Vector2(t, t)));
}; };
ShapeCollisions.pointToPoly = function (point, poly) { ShapeCollisions.pointToPoly = function (point, poly, result) {
var result = new es.CollisionResult();
if (poly.containsPoint(point)) { if (poly.containsPoint(point)) {
var distanceSquared = void 0; var distanceSquared = 0;
var gpp = es.Polygon.getClosestPointOnPolygonToPoint(poly.points, es.Vector2.subtract(point, poly.position)); var closestPoint = es.Polygon.getClosestPointOnPolygonToPoint(poly.points, es.Vector2.subtract(point, poly.position), distanceSquared, result.normal);
var closestPoint = gpp.closestPoint;
distanceSquared = gpp.distanceSquared;
result.normal = gpp.edgeNormal;
result.minimumTranslationVector = es.Vector2.multiply(result.normal, new es.Vector2(Math.sqrt(distanceSquared), Math.sqrt(distanceSquared))); result.minimumTranslationVector = es.Vector2.multiply(result.normal, new es.Vector2(Math.sqrt(distanceSquared), Math.sqrt(distanceSquared)));
result.point = es.Vector2.add(closestPoint, poly.position); result.point = es.Vector2.add(closestPoint, poly.position);
return result; return true;
} }
return null; return false;
}; };
ShapeCollisions.circleToCircle = function (first, second) { ShapeCollisions.circleToCircle = function (first, second, result) {
var result = new es.CollisionResult();
var distanceSquared = es.Vector2.distanceSquared(first.position, second.position); var distanceSquared = es.Vector2.distanceSquared(first.position, second.position);
var sumOfRadii = first.radius + second.radius; var sumOfRadii = first.radius + second.radius;
var collided = distanceSquared < sumOfRadii * sumOfRadii; var collided = distanceSquared < sumOfRadii * sumOfRadii;
@@ -6417,22 +6447,21 @@ var es;
var depth = sumOfRadii - Math.sqrt(distanceSquared); var depth = sumOfRadii - Math.sqrt(distanceSquared);
result.minimumTranslationVector = es.Vector2.multiply(new es.Vector2(-depth), result.normal); result.minimumTranslationVector = es.Vector2.multiply(new es.Vector2(-depth), result.normal);
result.point = es.Vector2.add(second.position, es.Vector2.multiply(result.normal, new es.Vector2(second.radius))); result.point = es.Vector2.add(second.position, es.Vector2.multiply(result.normal, new es.Vector2(second.radius)));
return result; return true;
} }
return null; return false;
}; };
ShapeCollisions.boxToBox = function (first, second) { ShapeCollisions.boxToBox = function (first, second, result) {
var result = new es.CollisionResult();
var minkowskiDiff = this.minkowskiDifference(first, second); var minkowskiDiff = this.minkowskiDifference(first, second);
if (minkowskiDiff.contains(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.equals(es.Vector2.zero))
return null; return false;
result.normal = new es.Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y); result.normal = new es.Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y);
result.normal.normalize(); result.normal.normalize();
return result; return true;
} }
return null; return false;
}; };
ShapeCollisions.minkowskiDifference = function (first, second) { ShapeCollisions.minkowskiDifference = function (first, second) {
var positionOffset = es.Vector2.subtract(first.position, es.Vector2.add(first.bounds.location, es.Vector2.divide(first.bounds.size, new es.Vector2(2)))); var positionOffset = es.Vector2.subtract(first.position, es.Vector2.add(first.bounds.location, es.Vector2.divide(first.bounds.size, new es.Vector2(2))));
@@ -6544,16 +6573,14 @@ var es;
} }
} }
} }
return { tempHashSet: this._tempHashSet, bounds: bounds }; return this._tempHashSet;
}; };
SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) { SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) {
var bounds = new es.Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2); var bounds = new es.Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
this._overlapTestCircle.radius = radius; this._overlapTestCircle.radius = radius;
this._overlapTestCircle.position = circleCenter; this._overlapTestCircle.position = circleCenter;
var resultCounter = 0; var resultCounter = 0;
var aabbBroadphaseResult = this.aabbBroadphase(bounds, null, layerMask); var potentials = this.aabbBroadphase(bounds, null, layerMask);
bounds = aabbBroadphaseResult.bounds;
var potentials = aabbBroadphaseResult.tempHashSet;
for (var i = 0; i < potentials.length; i++) { for (var i = 0; i < potentials.length; i++) {
var collider = potentials[i]; var collider = potentials[i];
if (collider instanceof es.BoxCollider) { if (collider instanceof es.BoxCollider) {
File diff suppressed because one or more lines are too long
+4 -2
View File
@@ -5,6 +5,7 @@ module component {
import SpriteRenderer = es.SpriteRenderer; import SpriteRenderer = es.SpriteRenderer;
import Time = es.Time; import Time = es.Time;
import Input = es.Input; import Input = es.Input;
import CollisionResult = es.CollisionResult;
export class PlayerController extends Component { export class PlayerController extends Component {
private down: boolean = false; private down: boolean = false;
@@ -45,7 +46,7 @@ module component {
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 = this.entity.scene.camera.mouseToWorldPoint();
if (worldPos.x < this.spriteRenderer.transform.position.x){ if (worldPos.x < this.spriteRenderer.transform.position.x){
moveLeft = -1; moveLeft = -1;
} else if(worldPos.x > this.spriteRenderer.transform.position.x){ } else if(worldPos.x > this.spriteRenderer.transform.position.x){
@@ -57,7 +58,8 @@ module component {
} else if(worldPos.y > this.spriteRenderer.transform.position.y){ } else if(worldPos.y > this.spriteRenderer.transform.position.y){
moveRight = 1; moveRight = 1;
} }
this.mover.move(new Vector2(moveLeft * speed * Time.deltaTime, moveRight * speed * Time.deltaTime)); let collisionResult = new CollisionResult();
this.mover.move(new Vector2(moveLeft * speed * Time.deltaTime, moveRight * speed * Time.deltaTime), collisionResult);
} }
} }
} }
+36 -45
View File
@@ -133,6 +133,7 @@ declare module es {
static transform(position: Vector2, matrix: Matrix2D): Vector2; static transform(position: Vector2, matrix: Matrix2D): Vector2;
static distance(value1: Vector2, value2: Vector2): number; static distance(value1: Vector2, value2: Vector2): number;
static negate(value: Vector2): Vector2; static negate(value: Vector2): Vector2;
equals(other: Vector2): boolean;
} }
} }
declare module es { declare module es {
@@ -387,13 +388,14 @@ declare module transform {
} }
} }
declare module es { declare module es {
import HashObject = egret.HashObject;
enum DirtyType { enum DirtyType {
clean = 0, clean = 0,
positionDirty = 1, positionDirty = 1,
scaleDirty = 2, scaleDirty = 2,
rotationDirty = 3 rotationDirty = 3
} }
class Transform { class Transform extends HashObject {
readonly entity: Entity; readonly entity: Entity;
parent: Transform; parent: Transform;
readonly childCount: number; readonly childCount: number;
@@ -448,6 +450,7 @@ declare module es {
setDirty(dirtyFlagType: DirtyType): void; setDirty(dirtyFlagType: DirtyType): void;
copyFrom(transform: Transform): void; copyFrom(transform: Transform): void;
toString(): string; toString(): string;
equals(other: Transform): boolean;
} }
} }
declare module es { declare module es {
@@ -536,6 +539,7 @@ declare module es {
} }
declare module es { declare module es {
abstract class RenderableComponent extends Component implements IRenderable { abstract class RenderableComponent extends Component implements IRenderable {
displayObject: egret.DisplayObject;
readonly width: number; readonly width: number;
readonly height: number; readonly height: number;
readonly bounds: Rectangle; readonly bounds: Rectangle;
@@ -556,6 +560,7 @@ declare module es {
setRenderLayer(renderLayer: number): RenderableComponent; setRenderLayer(renderLayer: number): RenderableComponent;
setColor(color: number): RenderableComponent; setColor(color: number): RenderableComponent;
setLocalOffset(offset: Vector2): RenderableComponent; setLocalOffset(offset: Vector2): RenderableComponent;
sync(camera: Camera): void;
toString(): string; toString(): string;
} }
} }
@@ -667,12 +672,9 @@ declare module es {
class Mover extends Component { class Mover extends Component {
private _triggerHelper; private _triggerHelper;
onAddedToEntity(): void; onAddedToEntity(): void;
calculateMovement(motion: Vector2): { calculateMovement(motion: Vector2, collisionResult: CollisionResult): boolean;
collisionResult: CollisionResult;
motion: Vector2;
};
applyMovement(motion: Vector2): void; applyMovement(motion: Vector2): void;
move(motion: Vector2): CollisionResult; move(motion: Vector2, collisionResult: CollisionResult): boolean;
} }
} }
declare module es { declare module es {
@@ -712,8 +714,8 @@ declare module es {
onDisabled(): void; onDisabled(): void;
registerColliderWithPhysicsSystem(): void; registerColliderWithPhysicsSystem(): void;
unregisterColliderWithPhysicsSystem(): void; unregisterColliderWithPhysicsSystem(): void;
overlaps(other: Collider): any; overlaps(other: Collider): boolean;
collidesWith(collider: Collider, motion: Vector2): CollisionResult; collidesWith(collider: Collider, motion: Vector2, result: CollisionResult): boolean;
clone(): Component; clone(): Component;
} }
} }
@@ -1054,15 +1056,20 @@ declare module es {
declare module es { declare module es {
abstract class Renderer { abstract class Renderer {
camera: Camera; camera: Camera;
readonly renderOrder: number;
protected constructor(renderOrder: number, camera?: Camera);
onAddedToScene(scene: Scene): void; onAddedToScene(scene: Scene): void;
unload(): void;
protected beginRender(cam: Camera): void; protected beginRender(cam: Camera): void;
abstract render(scene: Scene): any; abstract render(scene: Scene): any;
unload(): void;
protected renderAfterStateCheck(renderable: IRenderable, cam: Camera): void; protected renderAfterStateCheck(renderable: IRenderable, cam: Camera): void;
onSceneBackBufferSizeChanged(newWidth: number, newHeight: number): void;
compareTo(other: Renderer): number;
} }
} }
declare module es { declare module es {
class DefaultRenderer extends Renderer { class DefaultRenderer extends Renderer {
constructor();
render(scene: Scene): void; render(scene: Scene): void;
} }
} }
@@ -1197,10 +1204,7 @@ declare module es {
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;
getClosestPointOnRectangleBorderToPoint(point: Vector2): { getClosestPointOnRectangleBorderToPoint(point: Vector2, edgeNormal: Vector2): Vector2;
res: Vector2;
edgeNormal: Vector2;
};
getClosestPointOnBoundsToOrigin(): Vector2; getClosestPointOnBoundsToOrigin(): Vector2;
calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void; calculateBounds(parentPosition: Vector2, position: Vector2, origin: Vector2, scale: Vector2, rotation: number, width: number, height: number): void;
setEgretRect(rect: egret.Rectangle): Rectangle; setEgretRect(rect: egret.Rectangle): Rectangle;
@@ -1260,14 +1264,8 @@ declare module es {
static reset(): void; static reset(): void;
static clear(): void; static clear(): void;
static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number; static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number;
static boxcastBroadphase(rect: Rectangle, layerMask?: number): { static boxcastBroadphase(rect: Rectangle, layerMask?: number): Collider[];
colliders: Collider[]; static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask?: number): Collider[];
rect: Rectangle;
};
static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask?: number): {
tempHashSet: Collider[];
bounds: Rectangle;
};
static addCollider(collider: Collider): void; static addCollider(collider: Collider): void;
static removeCollider(collider: Collider): void; static removeCollider(collider: Collider): void;
static updateCollider(collider: Collider): void; static updateCollider(collider: Collider): void;
@@ -1280,9 +1278,9 @@ declare module es {
center: Vector2; center: Vector2;
bounds: Rectangle; bounds: Rectangle;
abstract recalculateBounds(collider: Collider): any; abstract recalculateBounds(collider: Collider): any;
abstract pointCollidesWithShape(point: Vector2): CollisionResult; abstract overlaps(other: Shape): boolean;
abstract overlaps(other: Shape): any; abstract collidesWithShape(other: Shape, collisionResult: CollisionResult): boolean;
abstract collidesWithShape(other: Shape): CollisionResult; abstract pointCollidesWithShape(point: Vector2, result: CollisionResult): boolean;
clone(): Shape; clone(): Shape;
} }
} }
@@ -1303,16 +1301,12 @@ declare module es {
static buildSymmetricalPolygon(vertCount: number, radius: number): any[]; static buildSymmetricalPolygon(vertCount: number, radius: number): any[];
static recenterPolygonVerts(points: Vector2[]): void; static recenterPolygonVerts(points: Vector2[]): void;
static findPolygonCenter(points: Vector2[]): Vector2; static findPolygonCenter(points: Vector2[]): Vector2;
static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): { static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2, distanceSquared: number, edgeNormal: Vector2): Vector2;
closestPoint: any;
distanceSquared: any;
edgeNormal: any;
};
recalculateBounds(collider: Collider): void; recalculateBounds(collider: Collider): void;
overlaps(other: Shape): any; overlaps(other: Shape): any;
collidesWithShape(other: Shape): any; collidesWithShape(other: Shape, result: CollisionResult): boolean;
containsPoint(point: Vector2): boolean; containsPoint(point: Vector2): boolean;
pointCollidesWithShape(point: Vector2): CollisionResult; pointCollidesWithShape(point: Vector2, result: CollisionResult): boolean;
} }
} }
declare module es { declare module es {
@@ -1323,7 +1317,7 @@ declare module es {
private static buildBox; private static buildBox;
updateBox(width: number, height: number): void; updateBox(width: number, height: number): void;
overlaps(other: Shape): any; overlaps(other: Shape): any;
collidesWithShape(other: Shape): any; collidesWithShape(other: Shape, result: CollisionResult): boolean;
containsPoint(point: Vector2): boolean; containsPoint(point: Vector2): boolean;
} }
} }
@@ -1334,8 +1328,8 @@ declare module es {
constructor(radius: number); constructor(radius: number);
recalculateBounds(collider: es.Collider): void; recalculateBounds(collider: es.Collider): void;
overlaps(other: Shape): any; overlaps(other: Shape): any;
collidesWithShape(other: Shape): CollisionResult; collidesWithShape(other: Shape, result: CollisionResult): boolean;
pointCollidesWithShape(point: Vector2): CollisionResult; pointCollidesWithShape(point: Vector2, result: CollisionResult): boolean;
} }
} }
declare module es { declare module es {
@@ -1349,19 +1343,19 @@ declare module es {
} }
declare module es { declare module es {
class ShapeCollisions { class ShapeCollisions {
static polygonToPolygon(first: Polygon, second: Polygon): CollisionResult; static polygonToPolygon(first: Polygon, second: Polygon, result: CollisionResult): boolean;
static intervalDistance(minA: number, maxA: number, minB: number, maxB: any): number; static intervalDistance(minA: number, maxA: number, minB: number, maxB: any): number;
static getInterval(axis: Vector2, polygon: Polygon, min: number, max: number): { static getInterval(axis: Vector2, polygon: Polygon, min: number, max: number): {
min: number; min: number;
max: number; max: number;
}; };
static circleToPolygon(circle: Circle, polygon: Polygon): CollisionResult; static circleToPolygon(circle: Circle, polygon: Polygon, result: CollisionResult): boolean;
static circleToBox(circle: Circle, box: Box): CollisionResult; static circleToBox(circle: Circle, box: Box, result: CollisionResult): boolean;
static pointToCircle(point: Vector2, circle: Circle): CollisionResult; static pointToCircle(point: Vector2, circle: Circle, result: CollisionResult): boolean;
static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2; static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2;
static pointToPoly(point: Vector2, poly: Polygon): CollisionResult; static pointToPoly(point: Vector2, poly: Polygon, result: CollisionResult): boolean;
static circleToCircle(first: Circle, second: Circle): CollisionResult; static circleToCircle(first: Circle, second: Circle, result: CollisionResult): boolean;
static boxToBox(first: Box, second: Box): CollisionResult; static boxToBox(first: Box, second: Box, result: CollisionResult): boolean;
private static minkowskiDifference; private static minkowskiDifference;
} }
} }
@@ -1383,10 +1377,7 @@ declare module es {
clear(): void; clear(): void;
debugDraw(secondsToDisplay: number, textScale?: number): void; debugDraw(secondsToDisplay: number, textScale?: number): void;
private debugDrawCellDetails; private debugDrawCellDetails;
aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number): { aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number): Collider[];
tempHashSet: Collider[];
bounds: Rectangle;
};
overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask: any): number; overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask: any): number;
} }
class NumberDictionary { class NumberDictionary {
+166 -139
View File
@@ -735,7 +735,7 @@ var es;
return new Vector2(es.MathHelper.lerp(value1.x, value2.x, amount), es.MathHelper.lerp(value1.y, value2.y, amount)); return new Vector2(es.MathHelper.lerp(value1.x, value2.x, amount), es.MathHelper.lerp(value1.y, value2.y, amount));
}; };
Vector2.transform = function (position, matrix) { Vector2.transform = function (position, matrix) {
return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22)); return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21) + matrix.m31, (position.x * matrix.m12) + (position.y * matrix.m22) + matrix.m32);
}; };
Vector2.distance = function (value1, value2) { Vector2.distance = function (value1, value2) {
var v1 = value1.x - value2.x, v2 = value1.y - value2.y; var v1 = value1.x - value2.x, v2 = value1.y - value2.y;
@@ -747,6 +747,9 @@ var es;
result.y = -value.y; result.y = -value.y;
return result; return result;
}; };
Vector2.prototype.equals = function (other) {
return other.x == this.x && other.y == this.y;
};
Vector2.unitYVector = new Vector2(0, 1); Vector2.unitYVector = new Vector2(0, 1);
Vector2.unitXVector = new Vector2(1, 0); Vector2.unitXVector = new Vector2(1, 0);
Vector2.unitVector2 = new Vector2(1, 1); Vector2.unitVector2 = new Vector2(1, 1);
@@ -1145,7 +1148,7 @@ var es;
this.addEventListener(egret.Event.RESIZE, this.onGraphicsDeviceReset, this); this.addEventListener(egret.Event.RESIZE, this.onGraphicsDeviceReset, this);
this.addEventListener(egret.StageOrientationEvent.ORIENTATION_CHANGE, this.onOrientationChanged, this); this.addEventListener(egret.StageOrientationEvent.ORIENTATION_CHANGE, this.onOrientationChanged, this);
this.addEventListener(egret.Event.ENTER_FRAME, this.update, this); this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
this.addEventListener(egret.Event.RENDER, this.draw, this); es.Input.initialize();
this.initialize(); this.initialize();
}; };
Core.prototype.onOrientationChanged = function () { Core.prototype.onOrientationChanged = function () {
@@ -1183,7 +1186,10 @@ var es;
case 1: case 1:
_a.sent(); _a.sent();
_a.label = 2; _a.label = 2;
case 2: return [2]; case 2: return [4, this.draw()];
case 3:
_a.sent();
return [2];
} }
}); });
}); });
@@ -1778,6 +1784,7 @@ var transform;
})(transform || (transform = {})); })(transform || (transform = {}));
var es; var es;
(function (es) { (function (es) {
var HashObject = egret.HashObject;
var DirtyType; var DirtyType;
(function (DirtyType) { (function (DirtyType) {
DirtyType[DirtyType["clean"] = 0] = "clean"; DirtyType[DirtyType["clean"] = 0] = "clean";
@@ -1785,24 +1792,27 @@ var es;
DirtyType[DirtyType["scaleDirty"] = 2] = "scaleDirty"; DirtyType[DirtyType["scaleDirty"] = 2] = "scaleDirty";
DirtyType[DirtyType["rotationDirty"] = 3] = "rotationDirty"; DirtyType[DirtyType["rotationDirty"] = 3] = "rotationDirty";
})(DirtyType = es.DirtyType || (es.DirtyType = {})); })(DirtyType = es.DirtyType || (es.DirtyType = {}));
var Transform = (function () { var Transform = (function (_super) {
__extends(Transform, _super);
function Transform(entity) { function Transform(entity) {
this._localTransform = es.Matrix2D.create(); var _this = _super.call(this) || this;
this._worldTransform = es.Matrix2D.create().identity(); _this._localTransform = es.Matrix2D.create();
this._worldToLocalTransform = es.Matrix2D.create().identity(); _this._worldTransform = es.Matrix2D.create().identity();
this._worldInverseTransform = es.Matrix2D.create().identity(); _this._worldToLocalTransform = es.Matrix2D.create().identity();
this._rotationMatrix = es.Matrix2D.create(); _this._worldInverseTransform = es.Matrix2D.create().identity();
this._translationMatrix = es.Matrix2D.create(); _this._rotationMatrix = es.Matrix2D.create();
this._scaleMatrix = es.Matrix2D.create(); _this._translationMatrix = es.Matrix2D.create();
this._position = es.Vector2.zero; _this._scaleMatrix = es.Matrix2D.create();
this._scale = es.Vector2.one; _this._position = es.Vector2.zero;
this._rotation = 0; _this._scale = es.Vector2.one;
this._localPosition = es.Vector2.zero; _this._rotation = 0;
this._localScale = es.Vector2.one; _this._localPosition = es.Vector2.zero;
this._localRotation = 0; _this._localScale = es.Vector2.one;
this.entity = entity; _this._localRotation = 0;
this.scale = es.Vector2.one; _this.entity = entity;
this._children = []; _this.scale = es.Vector2.one;
_this._children = [];
return _this;
} }
Object.defineProperty(Transform.prototype, "parent", { Object.defineProperty(Transform.prototype, "parent", {
get: function () { get: function () {
@@ -1958,7 +1968,7 @@ var es;
return this._children[index]; return this._children[index];
}; };
Transform.prototype.setParent = function (parent) { Transform.prototype.setParent = function (parent) {
if (this._parent == parent) if (this._parent.equals(parent))
return this; return this;
if (!this._parent) { if (!this._parent) {
this._parent._children.remove(this); this._parent._children.remove(this);
@@ -1970,7 +1980,7 @@ var es;
}; };
Transform.prototype.setPosition = function (x, y) { Transform.prototype.setPosition = function (x, y) {
var position = new es.Vector2(x, y); var position = new es.Vector2(x, y);
if (position == this._position) if (position.equals(this._position))
return this; return this;
this._position = position; this._position = position;
if (this.parent) { if (this.parent) {
@@ -1983,7 +1993,7 @@ var es;
return this; return this;
}; };
Transform.prototype.setLocalPosition = function (localPosition) { Transform.prototype.setLocalPosition = function (localPosition) {
if (localPosition == this._localPosition) if (localPosition.equals(this._localPosition))
return this; return this;
this._localPosition = localPosition; this._localPosition = localPosition;
this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true; this._localDirty = this._positionDirty = this._localPositionDirty = this._localRotationDirty = this._localScaleDirty = true;
@@ -2108,8 +2118,11 @@ var es;
Transform.prototype.toString = function () { Transform.prototype.toString = function () {
return "[Transform: parent: " + this.parent + ", position: " + this.position + ", rotation: " + this.rotation + ",\n scale: " + this.scale + ", localPosition: " + this._localPosition + ", localRotation: " + this._localRotation + ",\n localScale: " + this._localScale + "]"; return "[Transform: parent: " + this.parent + ", position: " + this.position + ", rotation: " + this.rotation + ",\n scale: " + this.scale + ", localPosition: " + this._localPosition + ", localRotation: " + this._localRotation + ",\n localScale: " + this._localScale + "]";
}; };
Transform.prototype.equals = function (other) {
return other.hashCode == this.hashCode;
};
return Transform; return Transform;
}()); }(HashObject));
es.Transform = Transform; es.Transform = Transform;
})(es || (es = {})); })(es || (es = {}));
var es; var es;
@@ -2227,7 +2240,7 @@ var es;
var maxY = Math.max(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.location = new es.Vector2(minX, minY);
this._bounds.width = maxX - minX; this._bounds.width = maxX - minX;
this._bounds.height = maxX - minY; this._bounds.height = maxY - minY;
} }
else { else {
this._bounds.location = topLeft; this._bounds.location = topLeft;
@@ -2499,6 +2512,7 @@ var es;
__extends(RenderableComponent, _super); __extends(RenderableComponent, _super);
function RenderableComponent() { function RenderableComponent() {
var _this = _super !== null && _super.apply(this, arguments) || this; var _this = _super !== null && _super.apply(this, arguments) || this;
_this.displayObject = new egret.DisplayObject();
_this.color = 0x000000; _this.color = 0x000000;
_this._localOffset = es.Vector2.zero; _this._localOffset = es.Vector2.zero;
_this._renderLayer = 0; _this._renderLayer = 0;
@@ -2570,8 +2584,10 @@ var es;
this._areBoundsDirty = true; this._areBoundsDirty = true;
}; };
RenderableComponent.prototype.onBecameVisible = function () { RenderableComponent.prototype.onBecameVisible = function () {
this.displayObject.visible = this.isVisible;
}; };
RenderableComponent.prototype.onBecameInvisible = function () { RenderableComponent.prototype.onBecameInvisible = function () {
this.displayObject.visible = this.isVisible;
}; };
RenderableComponent.prototype.isVisibleFromCamera = function (camera) { RenderableComponent.prototype.isVisibleFromCamera = function (camera) {
this.isVisible = camera.bounds.intersects(this.bounds); this.isVisible = camera.bounds.intersects(this.bounds);
@@ -2596,6 +2612,13 @@ var es;
} }
return this; return this;
}; };
RenderableComponent.prototype.sync = function (camera) {
this.displayObject.x = this.entity.position.x + this.localOffset.x - camera.position.x + camera.origin.x;
this.displayObject.y = this.entity.position.y + this.localOffset.y - camera.position.y + camera.origin.y;
this.displayObject.scaleX = this.entity.scale.x;
this.displayObject.scaleY = this.entity.scale.y;
this.displayObject.rotation = this.entity.rotation;
};
RenderableComponent.prototype.toString = function () { RenderableComponent.prototype.toString = function () {
return "[RenderableComponent] renderLayer: " + this.renderLayer; return "[RenderableComponent] renderLayer: " + this.renderLayer;
}; };
@@ -2626,6 +2649,7 @@ var es;
})(es || (es = {})); })(es || (es = {}));
var es; var es;
(function (es) { (function (es) {
var Bitmap = egret.Bitmap;
var SpriteRenderer = (function (_super) { var SpriteRenderer = (function (_super) {
__extends(SpriteRenderer, _super); __extends(SpriteRenderer, _super);
function SpriteRenderer(sprite) { function SpriteRenderer(sprite) {
@@ -2644,8 +2668,8 @@ var es;
this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, this._origin, this.entity.transform.scale, this.entity.transform.rotation, this._sprite.sourceRect.width, this._sprite.sourceRect.height); this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, this._origin, this.entity.transform.scale, this.entity.transform.rotation, this._sprite.sourceRect.width, this._sprite.sourceRect.height);
this._areBoundsDirty = false; this._areBoundsDirty = false;
} }
return this._bounds;
} }
return this._bounds;
}, },
enumerable: true, enumerable: true,
configurable: true configurable: true
@@ -2684,12 +2708,17 @@ var es;
this._sprite = sprite; this._sprite = sprite;
if (this._sprite) { if (this._sprite) {
this._origin = this._sprite.origin; this._origin = this._sprite.origin;
this.displayObject.anchorOffsetX = this._origin.x;
this.displayObject.anchorOffsetY = this._origin.y;
} }
this.displayObject = new Bitmap(sprite.texture2D);
return this; return this;
}; };
SpriteRenderer.prototype.setOrigin = function (origin) { SpriteRenderer.prototype.setOrigin = function (origin) {
if (this._origin != origin) { if (this._origin != origin) {
this._origin = origin; this._origin = origin;
this.displayObject.anchorOffsetX = this._origin.x;
this.displayObject.anchorOffsetY = this._origin.y;
this._areBoundsDirty = true; this._areBoundsDirty = true;
} }
return this; return this;
@@ -2699,6 +2728,7 @@ var es;
return this; return this;
}; };
SpriteRenderer.prototype.render = function (camera) { SpriteRenderer.prototype.render = function (camera) {
this.sync(camera);
}; };
return SpriteRenderer; return SpriteRenderer;
}(es.RenderableComponent)); }(es.RenderableComponent));
@@ -2942,10 +2972,9 @@ var es;
Mover.prototype.onAddedToEntity = function () { Mover.prototype.onAddedToEntity = function () {
this._triggerHelper = new es.ColliderTriggerHelper(this.entity); this._triggerHelper = new es.ColliderTriggerHelper(this.entity);
}; };
Mover.prototype.calculateMovement = function (motion) { Mover.prototype.calculateMovement = function (motion, collisionResult) {
var collisionResult = new es.CollisionResult();
if (!this.entity.getComponent(es.Collider) || !this._triggerHelper) { if (!this.entity.getComponent(es.Collider) || !this._triggerHelper) {
return null; return false;
} }
var colliders = this.entity.getComponents(es.Collider); var colliders = this.entity.getComponents(es.Collider);
for (var i = 0; i < colliders.length; i++) { for (var i = 0; i < colliders.length; i++) {
@@ -2955,36 +2984,32 @@ var es;
var bounds = collider.bounds; var bounds = collider.bounds;
bounds.x += motion.x; bounds.x += motion.x;
bounds.y += motion.y; bounds.y += motion.y;
var boxcastResult = es.Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers); var neighbors = es.Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers);
bounds = boxcastResult.bounds;
var neighbors = boxcastResult.tempHashSet;
for (var j = 0; j < neighbors.length; j++) { for (var j = 0; j < neighbors.length; j++) {
var neighbor = neighbors[j]; var neighbor = neighbors[j];
if (neighbor.isTrigger) if (neighbor.isTrigger)
continue; continue;
var _internalcollisionResult = collider.collidesWith(neighbor, motion); var _internalcollisionResult = new es.CollisionResult();
if (_internalcollisionResult) { if (collider.collidesWith(neighbor, motion, _internalcollisionResult)) {
motion = es.Vector2.subtract(motion, _internalcollisionResult.minimumTranslationVector); motion.subtract(_internalcollisionResult.minimumTranslationVector);
if (_internalcollisionResult.collider) { if (_internalcollisionResult.collider != null) {
collisionResult = _internalcollisionResult; collisionResult = _internalcollisionResult;
} }
} }
} }
} }
es.ListPool.free(colliders); es.ListPool.free(colliders);
return { collisionResult: collisionResult, motion: motion }; return collisionResult.collider != null;
}; };
Mover.prototype.applyMovement = function (motion) { Mover.prototype.applyMovement = function (motion) {
this.entity.position = es.Vector2.add(this.entity.position, motion); this.entity.position = es.Vector2.add(this.entity.position, motion);
if (this._triggerHelper) if (this._triggerHelper)
this._triggerHelper.update(); this._triggerHelper.update();
}; };
Mover.prototype.move = function (motion) { Mover.prototype.move = function (motion, collisionResult) {
var movementResult = this.calculateMovement(motion); this.calculateMovement(motion, collisionResult);
var collisionResult = movementResult.collisionResult;
motion = movementResult.motion;
this.applyMovement(motion); this.applyMovement(motion);
return collisionResult; return collisionResult.collider != null;
}; };
return Mover; return Mover;
}(es.Component)); }(es.Component));
@@ -3010,8 +3035,8 @@ var es;
var didCollide = false; var didCollide = false;
this.entity.position = es.Vector2.add(this.entity.position, motion); this.entity.position = es.Vector2.add(this.entity.position, motion);
var neighbors = es.Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers); var neighbors = es.Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers);
for (var i = 0; i < neighbors.colliders.length; i++) { for (var i = 0; i < neighbors.length; i++) {
var neighbor = neighbors.colliders[i]; var neighbor = neighbors[i];
if (this._collider.overlaps(neighbor) && neighbor.enabled) { if (this._collider.overlaps(neighbor) && neighbor.enabled) {
didCollide = true; didCollide = true;
this.notifyTriggerListeners(this._collider, neighbor); this.notifyTriggerListeners(this._collider, neighbor);
@@ -3171,14 +3196,14 @@ var es;
Collider.prototype.overlaps = function (other) { Collider.prototype.overlaps = function (other) {
return this.shape.overlaps(other.shape); return this.shape.overlaps(other.shape);
}; };
Collider.prototype.collidesWith = function (collider, motion) { Collider.prototype.collidesWith = function (collider, motion, result) {
var oldPosition = this.entity.position; var oldPosition = this.entity.position;
this.entity.position = es.Vector2.add(this.entity.position, motion); this.entity.position.add(motion);
var result = this.shape.collidesWithShape(collider.shape); var didCollide = this.shape.collidesWithShape(collider.shape, result);
if (result) if (didCollide)
result.collider = collider; result.collider = collider;
this.entity.position = oldPosition; this.entity.position = oldPosition;
return result; return didCollide;
}; };
Collider.prototype.clone = function () { Collider.prototype.clone = function () {
var collider = ObjectUtils.clone(this); var collider = ObjectUtils.clone(this);
@@ -3601,8 +3626,10 @@ var es;
ComponentList.prototype.deregisterAllComponents = function () { ComponentList.prototype.deregisterAllComponents = function () {
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
var component = this._components[i]; var component = this._components[i];
if (component instanceof es.RenderableComponent) if (component instanceof es.RenderableComponent) {
this._entity.scene.removeChild(component.displayObject);
this._entity.scene.renderableComponents.remove(component); this._entity.scene.renderableComponents.remove(component);
}
this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component), false); this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity); this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
} }
@@ -3610,8 +3637,10 @@ var es;
ComponentList.prototype.registerAllComponents = function () { ComponentList.prototype.registerAllComponents = function () {
for (var i = 0; i < this._components.length; i++) { for (var i = 0; i < this._components.length; i++) {
var component = this._components[i]; var component = this._components[i];
if (component instanceof es.RenderableComponent) if (component instanceof es.RenderableComponent) {
this._entity.scene.addChild(component.displayObject);
this._entity.scene.renderableComponents.add(component); this._entity.scene.renderableComponents.add(component);
}
this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component)); this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
} }
@@ -3627,8 +3656,10 @@ var es;
if (this._componentsToAdd.length > 0) { if (this._componentsToAdd.length > 0) {
for (var i = 0, count = this._componentsToAdd.length; i < count; i++) { for (var i = 0, count = this._componentsToAdd.length; i < count; i++) {
var component = this._componentsToAdd[i]; var component = this._componentsToAdd[i];
if (component instanceof es.RenderableComponent) if (component instanceof es.RenderableComponent) {
this._entity.scene.addChild(component.displayObject);
this._entity.scene.renderableComponents.add(component); this._entity.scene.renderableComponents.add(component);
}
this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component)); this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
this._components.push(component); this._components.push(component);
@@ -3651,8 +3682,10 @@ var es;
} }
}; };
ComponentList.prototype.handleRemove = function (component) { ComponentList.prototype.handleRemove = function (component) {
if (component instanceof es.RenderableComponent) if (component instanceof es.RenderableComponent) {
this._entity.scene.removeChild(component.displayObject);
this._entity.scene.renderableComponents.remove(component); this._entity.scene.renderableComponents.remove(component);
}
this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component), false); this._entity.componentBits.set(es.ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity); this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
component.onRemovedFromEntity(); component.onRemovedFromEntity();
@@ -4909,15 +4942,23 @@ var es;
var es; var es;
(function (es) { (function (es) {
var Renderer = (function () { var Renderer = (function () {
function Renderer() { function Renderer(renderOrder, camera) {
if (camera === void 0) { camera = null; }
this.renderOrder = 0;
this.camera = camera;
this.renderOrder = renderOrder;
} }
Renderer.prototype.onAddedToScene = function (scene) { }; Renderer.prototype.onAddedToScene = function (scene) { };
Renderer.prototype.beginRender = function (cam) {
};
Renderer.prototype.unload = function () { }; Renderer.prototype.unload = function () { };
Renderer.prototype.beginRender = function (cam) { };
Renderer.prototype.renderAfterStateCheck = function (renderable, cam) { Renderer.prototype.renderAfterStateCheck = function (renderable, cam) {
renderable.render(cam); renderable.render(cam);
}; };
Renderer.prototype.onSceneBackBufferSizeChanged = function (newWidth, newHeight) {
};
Renderer.prototype.compareTo = function (other) {
return this.renderOrder - other.renderOrder;
};
return Renderer; return Renderer;
}()); }());
es.Renderer = Renderer; es.Renderer = Renderer;
@@ -4927,7 +4968,7 @@ var es;
var DefaultRenderer = (function (_super) { var DefaultRenderer = (function (_super) {
__extends(DefaultRenderer, _super); __extends(DefaultRenderer, _super);
function DefaultRenderer() { function DefaultRenderer() {
return _super !== null && _super.apply(this, arguments) || this; return _super.call(this, 0, null) || this;
} }
DefaultRenderer.prototype.render = function (scene) { DefaultRenderer.prototype.render = function (scene) {
var cam = this.camera ? this.camera : scene.camera; var cam = this.camera ? this.camera : scene.camera;
@@ -5567,8 +5608,8 @@ var es;
Rectangle.fromMinMax = function (minX, minY, maxX, maxY) { Rectangle.fromMinMax = function (minX, minY, maxX, maxY) {
return new Rectangle(minX, minY, maxX - minX, maxY - minY); return new Rectangle(minX, minY, maxX - minX, maxY - minY);
}; };
Rectangle.prototype.getClosestPointOnRectangleBorderToPoint = function (point) { Rectangle.prototype.getClosestPointOnRectangleBorderToPoint = function (point, edgeNormal) {
var edgeNormal = es.Vector2.zero; edgeNormal = es.Vector2.zero;
var res = new es.Vector2(); var res = new es.Vector2();
res.x = es.MathHelper.clamp(point.x, this.left, this.right); res.x = es.MathHelper.clamp(point.x, this.left, this.right);
res.y = es.MathHelper.clamp(point.y, this.top, this.bottom); res.y = es.MathHelper.clamp(point.y, this.top, this.bottom);
@@ -5605,7 +5646,7 @@ var es;
if (res.y == this.bottom) if (res.y == this.bottom)
edgeNormal.y = 1; edgeNormal.y = 1;
} }
return { res: res, edgeNormal: edgeNormal }; return res;
}; };
Rectangle.prototype.getClosestPointOnBoundsToOrigin = function () { Rectangle.prototype.getClosestPointOnBoundsToOrigin = function () {
var max = this.max; var max = this.max;
@@ -5688,9 +5729,7 @@ var es;
var colliders = this._entity.getComponents(es.Collider); var colliders = this._entity.getComponents(es.Collider);
for (var i = 0; i < colliders.length; i++) { for (var i = 0; i < colliders.length; i++) {
var collider = colliders[i]; var collider = colliders[i];
var boxcastResult = es.Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers); var neighbors = es.Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
collider.bounds = boxcastResult.rect;
var neighbors = boxcastResult.colliders;
var _loop_5 = function (j) { var _loop_5 = function (j) {
var neighbor = neighbors[j]; var neighbor = neighbors[j];
if (!collider.isTrigger && !neighbor.isTrigger) if (!collider.isTrigger && !neighbor.isTrigger)
@@ -5914,12 +5953,15 @@ var es;
}; };
Physics.overlapCircleAll = function (center, randius, results, layerMask) { Physics.overlapCircleAll = function (center, randius, results, layerMask) {
if (layerMask === void 0) { layerMask = -1; } if (layerMask === void 0) { layerMask = -1; }
if (results.length == 0) {
console.error("An empty results array was passed in. No results will ever be returned.");
return;
}
return this._spatialHash.overlapCircle(center, randius, results, layerMask); return this._spatialHash.overlapCircle(center, randius, results, layerMask);
}; };
Physics.boxcastBroadphase = function (rect, layerMask) { Physics.boxcastBroadphase = function (rect, layerMask) {
if (layerMask === void 0) { layerMask = this.allLayers; } if (layerMask === void 0) { layerMask = this.allLayers; }
var boxcastResult = this._spatialHash.aabbBroadphase(rect, null, layerMask); return this._spatialHash.aabbBroadphase(rect, null, layerMask);
return { colliders: boxcastResult.tempHashSet, rect: boxcastResult.bounds };
}; };
Physics.boxcastBroadphaseExcludingSelf = function (collider, rect, layerMask) { Physics.boxcastBroadphaseExcludingSelf = function (collider, rect, layerMask) {
if (layerMask === void 0) { layerMask = this.allLayers; } if (layerMask === void 0) { layerMask = this.allLayers; }
@@ -6026,9 +6068,9 @@ var es;
} }
return new es.Vector2(x / points.length, y / points.length); return new es.Vector2(x / points.length, y / points.length);
}; };
Polygon.getClosestPointOnPolygonToPoint = function (points, point) { Polygon.getClosestPointOnPolygonToPoint = function (points, point, distanceSquared, edgeNormal) {
var distanceSquared = Number.MAX_VALUE; distanceSquared = Number.MAX_VALUE;
var edgeNormal = new es.Vector2(0, 0); edgeNormal = new es.Vector2(0, 0);
var closestPoint = new es.Vector2(0, 0); var closestPoint = new es.Vector2(0, 0);
var tempDistanceSquared; var tempDistanceSquared;
for (var i = 0; i < points.length; i++) { for (var i = 0; i < points.length; i++) {
@@ -6044,8 +6086,8 @@ var es;
edgeNormal = new es.Vector2(-line.y, line.x); edgeNormal = new es.Vector2(-line.y, line.x);
} }
} }
edgeNormal = es.Vector2.normalize(edgeNormal); es.Vector2Ext.normalize(edgeNormal);
return { closestPoint: closestPoint, distanceSquared: distanceSquared, edgeNormal: edgeNormal }; return closestPoint;
}; };
Polygon.prototype.recalculateBounds = function (collider) { Polygon.prototype.recalculateBounds = function (collider) {
this.center = collider.localOffset; this.center = collider.localOffset;
@@ -6079,12 +6121,11 @@ var es;
this.bounds.location = es.Vector2.add(this.bounds.location, this.position); this.bounds.location = es.Vector2.add(this.bounds.location, this.position);
}; };
Polygon.prototype.overlaps = function (other) { Polygon.prototype.overlaps = function (other) {
var result; var result = new es.CollisionResult();
if (other instanceof Polygon) if (other instanceof Polygon)
return es.ShapeCollisions.polygonToPolygon(this, other); return es.ShapeCollisions.polygonToPolygon(this, other, result);
if (other instanceof es.Circle) { if (other instanceof es.Circle) {
result = es.ShapeCollisions.circleToPolygon(other, this); if (es.ShapeCollisions.circleToPolygon(other, this, result)) {
if (result) {
result.invertResult(); result.invertResult();
return true; return true;
} }
@@ -6092,18 +6133,16 @@ var es;
} }
throw new Error("overlaps of Pologon to " + other + " are not supported"); throw new Error("overlaps of Pologon to " + other + " are not supported");
}; };
Polygon.prototype.collidesWithShape = function (other) { Polygon.prototype.collidesWithShape = function (other, result) {
var result = new es.CollisionResult();
if (other instanceof Polygon) { if (other instanceof Polygon) {
return es.ShapeCollisions.polygonToPolygon(this, other); return es.ShapeCollisions.polygonToPolygon(this, other, result);
} }
if (other instanceof es.Circle) { if (other instanceof es.Circle) {
result = es.ShapeCollisions.circleToPolygon(other, this); if (es.ShapeCollisions.circleToPolygon(other, this, result)) {
if (result) {
result.invertResult(); result.invertResult();
return result; return true;
} }
return null; return false;
} }
throw new Error("overlaps of Polygon to " + other + " are not supported"); throw new Error("overlaps of Polygon to " + other + " are not supported");
}; };
@@ -6119,8 +6158,8 @@ var es;
} }
return isInside; return isInside;
}; };
Polygon.prototype.pointCollidesWithShape = function (point) { Polygon.prototype.pointCollidesWithShape = function (point, result) {
return es.ShapeCollisions.pointToPoly(point, this); return es.ShapeCollisions.pointToPoly(point, this, result);
}; };
return Polygon; return Polygon;
}(es.Shape)); }(es.Shape));
@@ -6167,11 +6206,11 @@ var es;
} }
return _super.prototype.overlaps.call(this, other); return _super.prototype.overlaps.call(this, other);
}; };
Box.prototype.collidesWithShape = function (other) { Box.prototype.collidesWithShape = function (other, result) {
if (other instanceof Box && other.isUnrotated) { if (other instanceof Box && other.isUnrotated) {
return es.ShapeCollisions.boxToBox(this, other); return es.ShapeCollisions.boxToBox(this, other, result);
} }
return _super.prototype.collidesWithShape.call(this, other); return _super.prototype.collidesWithShape.call(this, other, result);
}; };
Box.prototype.containsPoint = function (point) { Box.prototype.containsPoint = function (point) {
if (this.isUnrotated) if (this.isUnrotated)
@@ -6209,28 +6248,29 @@ var es;
this.bounds = new es.Rectangle(this.position.x - this.radius, this.position.y - this.radius, this.radius * 2, this.radius * 2); this.bounds = new es.Rectangle(this.position.x - this.radius, this.position.y - this.radius, this.radius * 2, this.radius * 2);
}; };
Circle.prototype.overlaps = function (other) { Circle.prototype.overlaps = function (other) {
var result = new es.CollisionResult();
if (other instanceof es.Box && other.isUnrotated) if (other instanceof es.Box && other.isUnrotated)
return es.Collisions.isRectToCircle(other.bounds, this.position, this.radius); return es.Collisions.isRectToCircle(other.bounds, this.position, this.radius);
if (other instanceof Circle) if (other instanceof Circle)
return es.Collisions.isCircleToCircle(this.position, this.radius, other.position, other.radius); return es.Collisions.isCircleToCircle(this.position, this.radius, other.position, other.radius);
if (other instanceof es.Polygon) if (other instanceof es.Polygon)
return es.ShapeCollisions.circleToPolygon(this, other); return es.ShapeCollisions.circleToPolygon(this, other, result);
throw new Error("overlaps of circle to " + other + " are not supported"); throw new Error("overlaps of circle to " + other + " are not supported");
}; };
Circle.prototype.collidesWithShape = function (other) { Circle.prototype.collidesWithShape = function (other, result) {
if (other instanceof es.Box && other.isUnrotated) { if (other instanceof es.Box && other.isUnrotated) {
return es.ShapeCollisions.circleToBox(this, other); return es.ShapeCollisions.circleToBox(this, other, result);
} }
if (other instanceof Circle) { if (other instanceof Circle) {
return es.ShapeCollisions.circleToCircle(this, other); return es.ShapeCollisions.circleToCircle(this, other, result);
} }
if (other instanceof es.Polygon) { if (other instanceof es.Polygon) {
return es.ShapeCollisions.circleToPolygon(this, other); return es.ShapeCollisions.circleToPolygon(this, other, result);
} }
throw new Error("Collisions of Circle to " + other + " are not supported"); throw new Error("Collisions of Circle to " + other + " are not supported");
}; };
Circle.prototype.pointCollidesWithShape = function (point) { Circle.prototype.pointCollidesWithShape = function (point, result) {
return es.ShapeCollisions.pointToCircle(point, this); return es.ShapeCollisions.pointToCircle(point, this, result);
}; };
return Circle; return Circle;
}(es.Shape)); }(es.Shape));
@@ -6257,8 +6297,7 @@ var es;
var ShapeCollisions = (function () { var ShapeCollisions = (function () {
function ShapeCollisions() { function ShapeCollisions() {
} }
ShapeCollisions.polygonToPolygon = function (first, second) { ShapeCollisions.polygonToPolygon = function (first, second, result) {
var result = new es.CollisionResult();
var isIntersecting = true; var isIntersecting = true;
var firstEdges = first.edgeNormals; var firstEdges = first.edgeNormals;
var secondEdges = second.edgeNormals; var secondEdges = second.edgeNormals;
@@ -6291,7 +6330,7 @@ var es;
if (intervalDist > 0) if (intervalDist > 0)
isIntersecting = false; isIntersecting = false;
if (!isIntersecting) if (!isIntersecting)
return null; return false;
intervalDist = Math.abs(intervalDist); intervalDist = Math.abs(intervalDist);
if (intervalDist < minIntervalDistance) { if (intervalDist < minIntervalDistance) {
minIntervalDistance = intervalDist; minIntervalDistance = intervalDist;
@@ -6302,7 +6341,7 @@ var es;
} }
result.normal = translationAxis; result.normal = translationAxis;
result.minimumTranslationVector = es.Vector2.multiply(new es.Vector2(-translationAxis.x, -translationAxis.y), new es.Vector2(minIntervalDistance)); result.minimumTranslationVector = es.Vector2.multiply(new es.Vector2(-translationAxis.x, -translationAxis.y), new es.Vector2(minIntervalDistance));
return result; return true;
}; };
ShapeCollisions.intervalDistance = function (minA, maxA, minB, maxB) { ShapeCollisions.intervalDistance = function (minA, maxA, minB, maxB) {
if (minA < minB) if (minA < minB)
@@ -6323,16 +6362,13 @@ var es;
} }
return { min: min, max: max }; return { min: min, max: max };
}; };
ShapeCollisions.circleToPolygon = function (circle, polygon) { ShapeCollisions.circleToPolygon = function (circle, polygon, result) {
var result = new es.CollisionResult();
var poly2Circle = es.Vector2.subtract(circle.position, polygon.position); var poly2Circle = es.Vector2.subtract(circle.position, polygon.position);
var gpp = es.Polygon.getClosestPointOnPolygonToPoint(polygon.points, poly2Circle); var distanceSquared = 0;
var closestPoint = gpp.closestPoint; var closestPoint = es.Polygon.getClosestPointOnPolygonToPoint(polygon.points, poly2Circle, distanceSquared, result.normal);
var distanceSquared = gpp.distanceSquared;
result.normal = gpp.edgeNormal;
var circleCenterInsidePoly = polygon.containsPoint(circle.position); var circleCenterInsidePoly = polygon.containsPoint(circle.position);
if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly) if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly)
return null; return false;
var mtv; var mtv;
if (circleCenterInsidePoly) { if (circleCenterInsidePoly) {
mtv = es.Vector2.multiply(result.normal, new es.Vector2(Math.sqrt(distanceSquared) - circle.radius)); mtv = es.Vector2.multiply(result.normal, new es.Vector2(Math.sqrt(distanceSquared) - circle.radius));
@@ -6348,16 +6384,15 @@ var es;
} }
result.minimumTranslationVector = mtv; result.minimumTranslationVector = mtv;
result.point = es.Vector2.add(closestPoint, polygon.position); result.point = es.Vector2.add(closestPoint, polygon.position);
return result; return true;
}; };
ShapeCollisions.circleToBox = function (circle, box) { ShapeCollisions.circleToBox = function (circle, box, result) {
var result = new es.CollisionResult(); var closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position, result.normal);
var closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res;
if (box.containsPoint(circle.position)) { if (box.containsPoint(circle.position)) {
result.point = closestPointOnBounds; result.point = closestPointOnBounds;
var safePlace = es.Vector2.add(closestPointOnBounds, es.Vector2.subtract(result.normal, new es.Vector2(circle.radius))); var safePlace = es.Vector2.add(closestPointOnBounds, es.Vector2.subtract(result.normal, new es.Vector2(circle.radius)));
result.minimumTranslationVector = es.Vector2.subtract(circle.position, safePlace); result.minimumTranslationVector = es.Vector2.subtract(circle.position, safePlace);
return result; return true;
} }
var sqrDistance = es.Vector2.distanceSquared(closestPointOnBounds, circle.position); var sqrDistance = es.Vector2.distanceSquared(closestPointOnBounds, circle.position);
if (sqrDistance == 0) { if (sqrDistance == 0) {
@@ -6366,14 +6401,14 @@ var es;
else if (sqrDistance <= circle.radius * circle.radius) { else if (sqrDistance <= circle.radius * circle.radius) {
result.normal = es.Vector2.subtract(circle.position, closestPointOnBounds); result.normal = es.Vector2.subtract(circle.position, closestPointOnBounds);
var depth = result.normal.length() - circle.radius; var depth = result.normal.length() - circle.radius;
result.normal = es.Vector2Ext.normalize(result.normal); result.point = closestPointOnBounds;
es.Vector2Ext.normalize(result.normal);
result.minimumTranslationVector = es.Vector2.multiply(new es.Vector2(depth), result.normal); result.minimumTranslationVector = es.Vector2.multiply(new es.Vector2(depth), result.normal);
return result; return true;
} }
return null; return false;
}; };
ShapeCollisions.pointToCircle = function (point, circle) { ShapeCollisions.pointToCircle = function (point, circle, result) {
var result = new es.CollisionResult();
var distanceSquared = es.Vector2.distanceSquared(point, circle.position); var distanceSquared = es.Vector2.distanceSquared(point, circle.position);
var sumOfRadii = 1 + circle.radius; var sumOfRadii = 1 + circle.radius;
var collided = distanceSquared < sumOfRadii * sumOfRadii; var collided = distanceSquared < sumOfRadii * sumOfRadii;
@@ -6382,9 +6417,9 @@ var es;
var depth = sumOfRadii - Math.sqrt(distanceSquared); var depth = sumOfRadii - Math.sqrt(distanceSquared);
result.minimumTranslationVector = es.Vector2.multiply(new es.Vector2(-depth, -depth), result.normal); result.minimumTranslationVector = es.Vector2.multiply(new es.Vector2(-depth, -depth), result.normal);
result.point = es.Vector2.add(circle.position, es.Vector2.multiply(result.normal, new es.Vector2(circle.radius, circle.radius))); result.point = es.Vector2.add(circle.position, es.Vector2.multiply(result.normal, new es.Vector2(circle.radius, circle.radius)));
return result; return true;
} }
return null; return false;
}; };
ShapeCollisions.closestPointOnLine = function (lineA, lineB, closestTo) { ShapeCollisions.closestPointOnLine = function (lineA, lineB, closestTo) {
var v = es.Vector2.subtract(lineB, lineA); var v = es.Vector2.subtract(lineB, lineA);
@@ -6393,22 +6428,17 @@ var es;
t = es.MathHelper.clamp(t, 0, 1); t = es.MathHelper.clamp(t, 0, 1);
return es.Vector2.add(lineA, es.Vector2.multiply(v, new es.Vector2(t, t))); return es.Vector2.add(lineA, es.Vector2.multiply(v, new es.Vector2(t, t)));
}; };
ShapeCollisions.pointToPoly = function (point, poly) { ShapeCollisions.pointToPoly = function (point, poly, result) {
var result = new es.CollisionResult();
if (poly.containsPoint(point)) { if (poly.containsPoint(point)) {
var distanceSquared = void 0; var distanceSquared = 0;
var gpp = es.Polygon.getClosestPointOnPolygonToPoint(poly.points, es.Vector2.subtract(point, poly.position)); var closestPoint = es.Polygon.getClosestPointOnPolygonToPoint(poly.points, es.Vector2.subtract(point, poly.position), distanceSquared, result.normal);
var closestPoint = gpp.closestPoint;
distanceSquared = gpp.distanceSquared;
result.normal = gpp.edgeNormal;
result.minimumTranslationVector = es.Vector2.multiply(result.normal, new es.Vector2(Math.sqrt(distanceSquared), Math.sqrt(distanceSquared))); result.minimumTranslationVector = es.Vector2.multiply(result.normal, new es.Vector2(Math.sqrt(distanceSquared), Math.sqrt(distanceSquared)));
result.point = es.Vector2.add(closestPoint, poly.position); result.point = es.Vector2.add(closestPoint, poly.position);
return result; return true;
} }
return null; return false;
}; };
ShapeCollisions.circleToCircle = function (first, second) { ShapeCollisions.circleToCircle = function (first, second, result) {
var result = new es.CollisionResult();
var distanceSquared = es.Vector2.distanceSquared(first.position, second.position); var distanceSquared = es.Vector2.distanceSquared(first.position, second.position);
var sumOfRadii = first.radius + second.radius; var sumOfRadii = first.radius + second.radius;
var collided = distanceSquared < sumOfRadii * sumOfRadii; var collided = distanceSquared < sumOfRadii * sumOfRadii;
@@ -6417,22 +6447,21 @@ var es;
var depth = sumOfRadii - Math.sqrt(distanceSquared); var depth = sumOfRadii - Math.sqrt(distanceSquared);
result.minimumTranslationVector = es.Vector2.multiply(new es.Vector2(-depth), result.normal); result.minimumTranslationVector = es.Vector2.multiply(new es.Vector2(-depth), result.normal);
result.point = es.Vector2.add(second.position, es.Vector2.multiply(result.normal, new es.Vector2(second.radius))); result.point = es.Vector2.add(second.position, es.Vector2.multiply(result.normal, new es.Vector2(second.radius)));
return result; return true;
} }
return null; return false;
}; };
ShapeCollisions.boxToBox = function (first, second) { ShapeCollisions.boxToBox = function (first, second, result) {
var result = new es.CollisionResult();
var minkowskiDiff = this.minkowskiDifference(first, second); var minkowskiDiff = this.minkowskiDifference(first, second);
if (minkowskiDiff.contains(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.equals(es.Vector2.zero))
return null; return false;
result.normal = new es.Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y); result.normal = new es.Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y);
result.normal.normalize(); result.normal.normalize();
return result; return true;
} }
return null; return false;
}; };
ShapeCollisions.minkowskiDifference = function (first, second) { ShapeCollisions.minkowskiDifference = function (first, second) {
var positionOffset = es.Vector2.subtract(first.position, es.Vector2.add(first.bounds.location, es.Vector2.divide(first.bounds.size, new es.Vector2(2)))); var positionOffset = es.Vector2.subtract(first.position, es.Vector2.add(first.bounds.location, es.Vector2.divide(first.bounds.size, new es.Vector2(2))));
@@ -6544,16 +6573,14 @@ var es;
} }
} }
} }
return { tempHashSet: this._tempHashSet, bounds: bounds }; return this._tempHashSet;
}; };
SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) { SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) {
var bounds = new es.Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2); var bounds = new es.Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
this._overlapTestCircle.radius = radius; this._overlapTestCircle.radius = radius;
this._overlapTestCircle.position = circleCenter; this._overlapTestCircle.position = circleCenter;
var resultCounter = 0; var resultCounter = 0;
var aabbBroadphaseResult = this.aabbBroadphase(bounds, null, layerMask); var potentials = this.aabbBroadphase(bounds, null, layerMask);
bounds = aabbBroadphaseResult.bounds;
var potentials = aabbBroadphaseResult.tempHashSet;
for (var i = 0; i < potentials.length; i++) { for (var i = 0; i < potentials.length; i++) {
var collider = potentials[i]; var collider = potentials[i];
if (collider instanceof es.BoxCollider) { if (collider instanceof es.BoxCollider) {
+1 -1
View File
File diff suppressed because one or more lines are too long
+1 -1
View File
@@ -122,7 +122,7 @@ module es {
this._bounds.location = new Vector2(minX, minY); this._bounds.location = new Vector2(minX, minY);
this._bounds.width = maxX - minX; this._bounds.width = maxX - minX;
this._bounds.height = maxX - minY; this._bounds.height = maxY - minY;
} else { } else {
this._bounds.location = topLeft; this._bounds.location = topLeft;
this._bounds.width = bottomRight.x - topLeft.x; this._bounds.width = bottomRight.x - topLeft.x;
@@ -205,7 +205,7 @@ module es {
* *
* @param other * @param other
*/ */
public overlaps(other: Collider) { public overlaps(other: Collider): boolean {
return this.shape.overlaps(other.shape); return this.shape.overlaps(other.shape);
} }
@@ -213,20 +213,21 @@ module es {
* ()true * ()true
* @param collider * @param collider
* @param motion * @param motion
* @param result
*/ */
public collidesWith(collider: Collider, motion: Vector2) { public collidesWith(collider: Collider, motion: Vector2, result: CollisionResult): boolean {
// 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠 // 改变形状的位置,使它在移动后的位置,这样我们可以检查重叠
let oldPosition = this.entity.position; let oldPosition = this.entity.position;
this.entity.position = Vector2.add(this.entity.position, motion); this.entity.position.add(motion);
let result = this.shape.collidesWithShape(collider.shape); let didCollide = this.shape.collidesWithShape(collider.shape, result);
if (result) if (didCollide)
result.collider = collider; result.collider = collider;
// 将图形位置返回到检查前的位置 // 将图形位置返回到检查前的位置
this.entity.position = oldPosition; this.entity.position = oldPosition;
return result; return didCollide;
} }
public clone(): Component{ public clone(): Component{
+13 -19
View File
@@ -16,12 +16,11 @@ module es {
/** /**
* *
* @param motion * @param motion
* @param collisionResult
*/ */
public calculateMovement(motion: Vector2){ public calculateMovement(motion: Vector2, collisionResult: CollisionResult): boolean{
let collisionResult = new CollisionResult();
if (!this.entity.getComponent(Collider) || !this._triggerHelper){ if (!this.entity.getComponent(Collider) || !this._triggerHelper){
return null; return false;
} }
// 移动所有的非触发碰撞器并获得最近的碰撞 // 移动所有的非触发碰撞器并获得最近的碰撞
@@ -37,9 +36,7 @@ module es {
let bounds = collider.bounds; let bounds = collider.bounds;
bounds.x += motion.x; bounds.x += motion.x;
bounds.y += motion.y; bounds.y += motion.y;
let boxcastResult = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers); let neighbors = Physics.boxcastBroadphaseExcludingSelf(collider, bounds, collider.collidesWithLayers);
bounds = boxcastResult.bounds;
let neighbors = boxcastResult.tempHashSet;
for (let j = 0; j < neighbors.length; j ++){ for (let j = 0; j < neighbors.length; j ++){
let neighbor = neighbors[j]; let neighbor = neighbors[j];
@@ -47,13 +44,13 @@ module es {
if (neighbor.isTrigger) if (neighbor.isTrigger)
continue; continue;
let _internalcollisionResult = collider.collidesWith(neighbor, motion); let _internalcollisionResult: CollisionResult = new CollisionResult();
if (_internalcollisionResult){ if (collider.collidesWith(neighbor, motion, _internalcollisionResult)){
// 如果碰撞 则退回之前的移动量 // 如果碰撞 则退回之前的移动量
motion = Vector2.subtract(motion, _internalcollisionResult.minimumTranslationVector); motion.subtract(_internalcollisionResult.minimumTranslationVector);
// 如果我们碰到多个对象,为了简单起见,只取第一个。 // 如果我们碰到多个对象,为了简单起见,只取第一个。
if (_internalcollisionResult.collider){ if (_internalcollisionResult.collider != null){
collisionResult = _internalcollisionResult; collisionResult = _internalcollisionResult;
} }
} }
@@ -62,7 +59,7 @@ module es {
ListPool.free(colliders); ListPool.free(colliders);
return {collisionResult: collisionResult, motion: motion}; return collisionResult.collider != null;
} }
/** /**
@@ -81,15 +78,12 @@ module es {
/** /**
* calculateMovement和applyMovement来移动考虑碰撞的实体; * calculateMovement和applyMovement来移动考虑碰撞的实体;
* @param motion * @param motion
* @param collisionResult
*/ */
public move(motion: Vector2){ public move(motion: Vector2, collisionResult: CollisionResult){
let movementResult = this.calculateMovement(motion); this.calculateMovement(motion, collisionResult);
let collisionResult = movementResult.collisionResult;
motion = movementResult.motion;
this.applyMovement(motion); this.applyMovement(motion);
return collisionResult.collider != null;
return collisionResult;
} }
} }
} }
@@ -28,8 +28,8 @@ module es {
// 获取任何可能在新位置发生碰撞的东西 // 获取任何可能在新位置发生碰撞的东西
let neighbors = Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers); let neighbors = Physics.boxcastBroadphase(this._collider.bounds, this._collider.collidesWithLayers);
for (let i = 0; i < neighbors.colliders.length; i ++){ for (let i = 0; i < neighbors.length; i ++){
let neighbor = neighbors.colliders[i]; let neighbor = neighbors[i];
if (this._collider.overlaps(neighbor) && neighbor.enabled){ if (this._collider.overlaps(neighbor) && neighbor.enabled){
didCollide = true; didCollide = true;
this.notifyTriggerListeners(this._collider, neighbor); this.notifyTriggerListeners(this._collider, neighbor);
@@ -4,6 +4,10 @@ module es {
* *
*/ */
export abstract class RenderableComponent extends Component implements IRenderable { export abstract class RenderableComponent extends Component implements IRenderable {
/**
* egret显示对象
*/
public displayObject: egret.DisplayObject = new egret.DisplayObject();
/** /**
* renderableComponent的宽度 * renderableComponent的宽度
* bounds属性则需要实现这个 * bounds属性则需要实现这个
@@ -107,6 +111,7 @@ module es {
* isVisibleFromCamera进行剔除检查 * isVisibleFromCamera进行剔除检查
*/ */
protected onBecameVisible() { protected onBecameVisible() {
this.displayObject.visible = this.isVisible;
} }
/** /**
@@ -114,6 +119,7 @@ module es {
* isVisibleFromCamera进行剔除检查 * isVisibleFromCamera进行剔除检查
*/ */
protected onBecameInvisible() { protected onBecameInvisible() {
this.displayObject.visible = this.isVisible;
} }
/** /**
@@ -165,6 +171,17 @@ module es {
return this; return this;
} }
/**
*
*/
public sync(camera: Camera){
this.displayObject.x = this.entity.position.x + this.localOffset.x - camera.position.x + camera.origin.x;
this.displayObject.y = this.entity.position.y + this.localOffset.y - camera.position.y + camera.origin.y;
this.displayObject.scaleX = this.entity.scale.x;
this.displayObject.scaleY = this.entity.scale.y;
this.displayObject.rotation = this.entity.rotation;
}
public toString(){ public toString(){
return `[RenderableComponent] renderLayer: ${this.renderLayer}`; return `[RenderableComponent] renderLayer: ${this.renderLayer}`;
} }
+10 -3
View File
@@ -1,4 +1,6 @@
module es { module es {
import Bitmap = egret.Bitmap;
export class SpriteRenderer extends RenderableComponent { export class SpriteRenderer extends RenderableComponent {
public get bounds() { public get bounds() {
if (this._areBoundsDirty) { if (this._areBoundsDirty) {
@@ -8,9 +10,9 @@ module es {
this._sprite.sourceRect.height); this._sprite.sourceRect.height);
this._areBoundsDirty = false; this._areBoundsDirty = false;
} }
return this._bounds;
} }
return this._bounds;
} }
/** /**
@@ -83,7 +85,10 @@ module es {
this._sprite = sprite; this._sprite = sprite;
if (this._sprite) { if (this._sprite) {
this._origin = this._sprite.origin; this._origin = this._sprite.origin;
this.displayObject.anchorOffsetX = this._origin.x;
this.displayObject.anchorOffsetY = this._origin.y;
} }
this.displayObject = new Bitmap(sprite.texture2D);
return this; return this;
} }
@@ -95,6 +100,8 @@ module es {
public setOrigin(origin: Vector2): SpriteRenderer { public setOrigin(origin: Vector2): SpriteRenderer {
if (this._origin != origin) { if (this._origin != origin) {
this._origin = origin; this._origin = origin;
this.displayObject.anchorOffsetX = this._origin.x;
this.displayObject.anchorOffsetY = this._origin.y;
this._areBoundsDirty = true; this._areBoundsDirty = true;
} }
@@ -113,7 +120,7 @@ module es {
} }
public render(camera: Camera) { public render(camera: Camera) {
// TODO: render this.sync(camera);
} }
} }
} }
+3 -1
View File
@@ -81,8 +81,8 @@ module es {
this.addEventListener(egret.Event.RESIZE, this.onGraphicsDeviceReset, this); this.addEventListener(egret.Event.RESIZE, this.onGraphicsDeviceReset, this);
this.addEventListener(egret.StageOrientationEvent.ORIENTATION_CHANGE, this.onOrientationChanged, this); this.addEventListener(egret.StageOrientationEvent.ORIENTATION_CHANGE, this.onOrientationChanged, this);
this.addEventListener(egret.Event.ENTER_FRAME, this.update, this); this.addEventListener(egret.Event.ENTER_FRAME, this.update, this);
this.addEventListener(egret.Event.RENDER, this.draw, this);
Input.initialize();
this.initialize(); this.initialize();
} }
@@ -135,6 +135,8 @@ module es {
} }
// this.endDebugUpdate(); // this.endDebugUpdate();
await this.draw();
} }
public async draw() { public async draw() {
+11 -4
View File
@@ -7,6 +7,8 @@ module transform {
} }
module es { module es {
import HashObject = egret.HashObject;
export enum DirtyType { export enum DirtyType {
clean, clean,
positionDirty, positionDirty,
@@ -14,7 +16,7 @@ module es {
rotationDirty, rotationDirty,
} }
export class Transform { export class Transform extends HashObject {
/** 与此转换关联的实体 */ /** 与此转换关联的实体 */
public readonly entity: Entity; public readonly entity: Entity;
/** /**
@@ -243,6 +245,7 @@ module es {
public _children: Transform[]; public _children: Transform[];
constructor(entity: Entity) { constructor(entity: Entity) {
super();
this.entity = entity; this.entity = entity;
this.scale = Vector2.one; this.scale = Vector2.one;
this._children = []; this._children = [];
@@ -261,7 +264,7 @@ module es {
* @param parent * @param parent
*/ */
public setParent(parent: Transform): Transform { public setParent(parent: Transform): Transform {
if (this._parent == parent) if (this._parent.equals(parent))
return this; return this;
if (!this._parent) { if (!this._parent) {
@@ -282,7 +285,7 @@ module es {
*/ */
public setPosition(x: number, y: number): Transform { public setPosition(x: number, y: number): Transform {
let position = new Vector2(x, y); let position = new Vector2(x, y);
if (position == this._position) if (position.equals(this._position))
return this; return this;
this._position = position; this._position = position;
@@ -301,7 +304,7 @@ module es {
* @param localPosition * @param localPosition
*/ */
public setLocalPosition(localPosition: Vector2): Transform { public setLocalPosition(localPosition: Vector2): Transform {
if (localPosition == this._localPosition) if (localPosition.equals(this._localPosition))
return this; return this;
this._localPosition = localPosition; this._localPosition = localPosition;
@@ -493,5 +496,9 @@ module es {
scale: ${this.scale}, localPosition: ${this._localPosition}, localRotation: ${this._localRotation}, scale: ${this.scale}, localPosition: ${this._localPosition}, localRotation: ${this._localRotation},
localScale: ${this._localScale}]`; localScale: ${this._localScale}]`;
} }
public equals(other: Transform){
return other.hashCode == this.hashCode;
}
} }
} }
+15 -4
View File
@@ -76,8 +76,11 @@ module es {
let component = this._components[i]; let component = this._components[i];
// 处理渲染层列表 // 处理渲染层列表
if (component instanceof RenderableComponent) if (component instanceof RenderableComponent){
this._entity.scene.removeChild(component.displayObject);
this._entity.scene.renderableComponents.remove(component); this._entity.scene.renderableComponents.remove(component);
}
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false); this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity); this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
@@ -88,8 +91,10 @@ module es {
for (let i = 0; i < this._components.length; i++) { for (let i = 0; i < this._components.length; i++) {
let component = this._components[i]; let component = this._components[i];
if (component instanceof RenderableComponent) if (component instanceof RenderableComponent){
this._entity.scene.addChild(component.displayObject);
this._entity.scene.renderableComponents.add(component); this._entity.scene.renderableComponents.add(component);
}
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component)); this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
@@ -112,8 +117,11 @@ module es {
if (this._componentsToAdd.length > 0) { if (this._componentsToAdd.length > 0) {
for (let i = 0, count = this._componentsToAdd.length; i < count; i++) { for (let i = 0, count = this._componentsToAdd.length; i < count; i++) {
let component = this._componentsToAdd[i]; let component = this._componentsToAdd[i];
if (component instanceof RenderableComponent) if (component instanceof RenderableComponent){
this._entity.scene.addChild(component.displayObject);
this._entity.scene.renderableComponents.add(component); this._entity.scene.renderableComponents.add(component);
}
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component)); this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component));
this._entity.scene.entityProcessors.onComponentAdded(this._entity); this._entity.scene.entityProcessors.onComponentAdded(this._entity);
@@ -148,8 +156,11 @@ module es {
public handleRemove(component: Component) { public handleRemove(component: Component) {
// 处理渲染层列表 // 处理渲染层列表
if (component instanceof RenderableComponent) if (component instanceof RenderableComponent){
this._entity.scene.removeChild(component.displayObject);
this._entity.scene.renderableComponents.remove(component); this._entity.scene.renderableComponents.remove(component);
}
this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false); this._entity.componentBits.set(ComponentTypeManager.getIndexFor(component), false);
this._entity.scene.entityProcessors.onComponentRemoved(this._entity); this._entity.scene.entityProcessors.onComponentRemoved(this._entity);
@@ -1,6 +1,10 @@
///<reference path="./Renderer.ts" /> ///<reference path="./Renderer.ts" />
module es { module es {
export class DefaultRenderer extends Renderer { export class DefaultRenderer extends Renderer {
constructor(){
super(0, null);
}
public render(scene: Scene) { public render(scene: Scene) {
let cam = this.camera ? this.camera : scene.camera; let cam = this.camera ? this.camera : scene.camera;
this.beginRender(cam); this.beginRender(cam);
+29 -6
View File
@@ -9,6 +9,15 @@ module es {
* Renderer子类可以选择调用beginRender时使用的摄像头 * Renderer子类可以选择调用beginRender时使用的摄像头
*/ */
public camera: Camera; public camera: Camera;
/**
*
*/
public readonly renderOrder: number = 0;
protected constructor(renderOrder: number, camera: Camera = null){
this.camera = camera;
this.renderOrder = renderOrder;
}
/** /**
* *
@@ -16,17 +25,18 @@ module es {
*/ */
public onAddedToScene(scene: Scene){} public onAddedToScene(scene: Scene){}
protected beginRender(cam: Camera){ /**
* 使
} */
public unload(){ }
/** /**
* *
* @param scene * @param cam
*/ */
public abstract render(scene: Scene); protected beginRender(cam: Camera){ }
public unload(){ } public abstract render(scene: Scene);
/** /**
* *
@@ -36,5 +46,18 @@ module es {
protected renderAfterStateCheck(renderable: IRenderable, cam: Camera){ protected renderAfterStateCheck(renderable: IRenderable, cam: Camera){
renderable.render(cam); renderable.render(cam);
} }
/**
*
* @param newWidth
* @param newHeight
*/
public onSceneBackBufferSizeChanged(newWidth: number, newHeight: number){
}
public compareTo(other: Renderer): number{
return this.renderOrder - other.renderOrder;
}
} }
} }
+4 -3
View File
@@ -70,9 +70,10 @@ module es {
/** /**
* *
* @param point * @param point
* @param edgeNormal
*/ */
public getClosestPointOnRectangleBorderToPoint(point: Vector2): { res: Vector2, edgeNormal: Vector2 } { public getClosestPointOnRectangleBorderToPoint(point: Vector2, edgeNormal: Vector2): Vector2 {
let edgeNormal = Vector2.zero; edgeNormal = Vector2.zero;
// 对于每个轴,如果点在盒子外面 // 对于每个轴,如果点在盒子外面
let res = new Vector2(); let res = new Vector2();
@@ -107,7 +108,7 @@ module es {
if (res.y == this.bottom) edgeNormal.y = 1; if (res.y == this.bottom) edgeNormal.y = 1;
} }
return { res: res, edgeNormal: edgeNormal }; return res;
} }
/** /**
+6 -1
View File
@@ -197,7 +197,8 @@ module es {
* @param matrix * @param matrix
*/ */
public static transform(position: Vector2, matrix: Matrix2D){ public static transform(position: Vector2, matrix: Matrix2D){
return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22)); return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21) + matrix.m31,
(position.x * matrix.m12) + (position.y * matrix.m22) + matrix.m32);
} }
/** /**
@@ -221,5 +222,9 @@ module es {
return result; return result;
} }
public equals(other: Vector2){
return other.x == this.x && other.y == this.y;
}
} }
} }
+1 -3
View File
@@ -22,9 +22,7 @@ module es {
for (let i = 0; i < colliders.length; i++) { for (let i = 0; i < colliders.length; i++) {
let collider = colliders[i]; let collider = colliders[i];
let boxcastResult = Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers); let neighbors = Physics.boxcastBroadphase(collider.bounds, collider.collidesWithLayers);
collider.bounds = boxcastResult.rect;
let neighbors = boxcastResult.colliders;
for (let j = 0; j < neighbors.length; j++) { for (let j = 0; j < neighbors.length; j++) {
let neighbor = neighbors[j]; let neighbor = neighbors[j];
if (!collider.isTrigger && !neighbor.isTrigger) if (!collider.isTrigger && !neighbor.isTrigger)
+24 -2
View File
@@ -17,15 +17,37 @@ module es {
this._spatialHash.clear(); this._spatialHash.clear();
} }
/**
*
* @param center
* @param randius
* @param results
* @param layerMask
*/
public static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask = -1){ public static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask = -1){
if (results.length == 0){
console.error("An empty results array was passed in. No results will ever be returned.");
return;
}
return this._spatialHash.overlapCircle(center, randius, results, layerMask); return this._spatialHash.overlapCircle(center, randius, results, layerMask);
} }
/**
* boundsbroadphase检查!
* @param rect
* @param layerMask
*/
public static boxcastBroadphase(rect: Rectangle, layerMask: number = this.allLayers){ public static boxcastBroadphase(rect: Rectangle, layerMask: number = this.allLayers){
let boxcastResult = this._spatialHash.aabbBroadphase(rect, null, layerMask); return this._spatialHash.aabbBroadphase(rect, null, layerMask);
return {colliders: boxcastResult.tempHashSet, rect: boxcastResult.bounds};
} }
/**
* (self)
* @param collider
* @param rect
* @param layerMask
*/
public static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask = this.allLayers){ public static boxcastBroadphaseExcludingSelf(collider: Collider, rect: Rectangle, layerMask = this.allLayers){
return this._spatialHash.aabbBroadphase(rect, collider, layerMask); return this._spatialHash.aabbBroadphase(rect, collider, layerMask);
} }
+3 -3
View File
@@ -66,15 +66,15 @@ module es {
return super.overlaps(other); return super.overlaps(other);
} }
public collidesWithShape(other: Shape){ public collidesWithShape(other: Shape, result: CollisionResult): boolean{
// 特殊情况,这一个高性能方式实现,其他情况则使用polygon方法检测 // 特殊情况,这一个高性能方式实现,其他情况则使用polygon方法检测
if (other instanceof Box && (other as Box).isUnrotated){ if (other instanceof Box && (other as Box).isUnrotated){
return ShapeCollisions.boxToBox(this, other); return ShapeCollisions.boxToBox(this, other, result);
} }
// TODO: 让 minkowski 运行于 cricleToBox // TODO: 让 minkowski 运行于 cricleToBox
return super.collidesWithShape(other); return super.collidesWithShape(other, result);
} }
public containsPoint(point: Vector2){ public containsPoint(point: Vector2){
+8 -7
View File
@@ -34,6 +34,7 @@ module es {
} }
public overlaps(other: Shape) { public overlaps(other: Shape) {
let result: CollisionResult = new CollisionResult();
if (other instanceof Box && (other as Box).isUnrotated) if (other instanceof Box && (other as Box).isUnrotated)
return Collisions.isRectToCircle(other.bounds, this.position, this.radius); return Collisions.isRectToCircle(other.bounds, this.position, this.radius);
@@ -41,29 +42,29 @@ module es {
return Collisions.isCircleToCircle(this.position, this.radius, other.position, (other as Circle).radius); return Collisions.isCircleToCircle(this.position, this.radius, other.position, (other as Circle).radius);
if (other instanceof Polygon) if (other instanceof Polygon)
return ShapeCollisions.circleToPolygon(this, other); return ShapeCollisions.circleToPolygon(this, other, result);
throw new Error(`overlaps of circle to ${other} are not supported`); throw new Error(`overlaps of circle to ${other} are not supported`);
} }
public collidesWithShape(other: Shape): CollisionResult { public collidesWithShape(other: Shape, result: CollisionResult): boolean {
if (other instanceof Box && (other as Box).isUnrotated) { if (other instanceof Box && (other as Box).isUnrotated) {
return ShapeCollisions.circleToBox(this, other); return ShapeCollisions.circleToBox(this, other, result);
} }
if (other instanceof Circle) { if (other instanceof Circle) {
return ShapeCollisions.circleToCircle(this, other); return ShapeCollisions.circleToCircle(this, other, result);
} }
if (other instanceof Polygon) { if (other instanceof Polygon) {
return ShapeCollisions.circleToPolygon(this, other); return ShapeCollisions.circleToPolygon(this, other, result);
} }
throw new Error(`Collisions of Circle to ${other} are not supported`); throw new Error(`Collisions of Circle to ${other} are not supported`);
} }
public pointCollidesWithShape(point: Vector2): CollisionResult { public pointCollidesWithShape(point: Vector2, result: CollisionResult): boolean {
return ShapeCollisions.pointToCircle(point, this); return ShapeCollisions.pointToCircle(point, this, result);
} }
} }
} }
+17 -18
View File
@@ -141,10 +141,12 @@ module es {
* (-.) * (-.)
* @param points * @param points
* @param point * @param point
* @param distanceSquared
* @param edgeNormal
*/ */
public static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): { closestPoint, distanceSquared, edgeNormal } { public static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2, distanceSquared: number, edgeNormal: Vector2): Vector2 {
let distanceSquared = Number.MAX_VALUE; distanceSquared = Number.MAX_VALUE;
let edgeNormal = new Vector2(0, 0); edgeNormal = new Vector2(0, 0);
let closestPoint = new Vector2(0, 0); let closestPoint = new Vector2(0, 0);
let tempDistanceSquared; let tempDistanceSquared;
@@ -166,9 +168,9 @@ module es {
} }
} }
edgeNormal = Vector2.normalize(edgeNormal); Vector2Ext.normalize(edgeNormal);
return { closestPoint: closestPoint, distanceSquared: distanceSquared, edgeNormal: edgeNormal }; return closestPoint;
} }
public recalculateBounds(collider: Collider){ public recalculateBounds(collider: Collider){
@@ -221,13 +223,12 @@ module es {
} }
public overlaps(other: Shape){ public overlaps(other: Shape){
let result: CollisionResult; let result: CollisionResult = new CollisionResult();
if (other instanceof Polygon) if (other instanceof Polygon)
return ShapeCollisions.polygonToPolygon(this, other); return ShapeCollisions.polygonToPolygon(this, other, result);
if (other instanceof Circle){ if (other instanceof Circle){
result = ShapeCollisions.circleToPolygon(other, this); if (ShapeCollisions.circleToPolygon(other, this, result)){
if (result){
result.invertResult(); result.invertResult();
return true; return true;
} }
@@ -238,20 +239,18 @@ module es {
throw new Error(`overlaps of Pologon to ${other} are not supported`); throw new Error(`overlaps of Pologon to ${other} are not supported`);
} }
public collidesWithShape(other: Shape){ public collidesWithShape(other: Shape, result: CollisionResult): boolean{
let result = new CollisionResult();
if (other instanceof Polygon){ if (other instanceof Polygon){
return ShapeCollisions.polygonToPolygon(this, other); return ShapeCollisions.polygonToPolygon(this, other, result);
} }
if (other instanceof Circle){ if (other instanceof Circle){
result = ShapeCollisions.circleToPolygon(other, this); if (ShapeCollisions.circleToPolygon(other, this, result)){
if (result){
result.invertResult(); result.invertResult();
return result; return true;
} }
return null; return false;
} }
throw new Error(`overlaps of Polygon to ${other} are not supported`); throw new Error(`overlaps of Polygon to ${other} are not supported`);
@@ -278,8 +277,8 @@ module es {
return isInside; return isInside;
} }
public pointCollidesWithShape(point: Vector2): CollisionResult { public pointCollidesWithShape(point: Vector2, result: CollisionResult): boolean {
return ShapeCollisions.pointToPoly(point, this); return ShapeCollisions.pointToPoly(point, this, result);
} }
} }
} }
+3 -3
View File
@@ -16,9 +16,9 @@ module es {
public bounds: Rectangle; public bounds: Rectangle;
public abstract recalculateBounds(collider: Collider); public abstract recalculateBounds(collider: Collider);
public abstract pointCollidesWithShape(point: Vector2): CollisionResult; public abstract overlaps(other: Shape): boolean;
public abstract overlaps(other: Shape); public abstract collidesWithShape(other: Shape, collisionResult: CollisionResult): boolean;
public abstract collidesWithShape(other: Shape): CollisionResult; public abstract pointCollidesWithShape(point: Vector2, result: CollisionResult): boolean;
public clone(): Shape{ public clone(): Shape{
return ObjectUtils.clone<Shape>(this); return ObjectUtils.clone<Shape>(this);
@@ -4,9 +4,9 @@ module es {
* *
* @param first * @param first
* @param second * @param second
* @param result
*/ */
public static polygonToPolygon(first: Polygon, second: Polygon) { public static polygonToPolygon(first: Polygon, second: Polygon, result: CollisionResult): boolean {
let result = new CollisionResult();
let isIntersecting = true; let isIntersecting = true;
let firstEdges = first.edgeNormals; let firstEdges = first.edgeNormals;
@@ -54,7 +54,7 @@ module es {
// 如果多边形不相交,也不会相交,退出循环 // 如果多边形不相交,也不会相交,退出循环
if (!isIntersecting) if (!isIntersecting)
return null; return false;
// 检查当前间隔距离是否为最小值。如果是,则存储间隔距离和当前距离。这将用于计算最小平移向量 // 检查当前间隔距离是否为最小值。如果是,则存储间隔距离和当前距离。这将用于计算最小平移向量
intervalDist = Math.abs(intervalDist); intervalDist = Math.abs(intervalDist);
@@ -71,7 +71,7 @@ module es {
result.normal = translationAxis; result.normal = translationAxis;
result.minimumTranslationVector = Vector2.multiply(new Vector2(-translationAxis.x, -translationAxis.y), new Vector2(minIntervalDistance)); result.minimumTranslationVector = Vector2.multiply(new Vector2(-translationAxis.x, -translationAxis.y), new Vector2(minIntervalDistance));
return result; return true;
} }
/** /**
@@ -115,20 +115,17 @@ module es {
* *
* @param circle * @param circle
* @param polygon * @param polygon
* @param result
*/ */
public static circleToPolygon(circle: Circle, polygon: Polygon) { public static circleToPolygon(circle: Circle, polygon: Polygon, result: CollisionResult): boolean {
let result = new CollisionResult();
let poly2Circle = Vector2.subtract(circle.position, polygon.position); let poly2Circle = Vector2.subtract(circle.position, polygon.position);
let gpp = Polygon.getClosestPointOnPolygonToPoint(polygon.points, poly2Circle); let distanceSquared = 0;
let closestPoint: Vector2 = gpp.closestPoint; let closestPoint = Polygon.getClosestPointOnPolygonToPoint(polygon.points, poly2Circle, distanceSquared, result.normal);
let distanceSquared: number = gpp.distanceSquared;
result.normal = gpp.edgeNormal;
let circleCenterInsidePoly = polygon.containsPoint(circle.position); let circleCenterInsidePoly = polygon.containsPoint(circle.position);
if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly) if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly)
return null; return false;
let mtv: Vector2; let mtv: Vector2;
if (circleCenterInsidePoly) { if (circleCenterInsidePoly) {
@@ -145,17 +142,17 @@ module es {
result.minimumTranslationVector = mtv; result.minimumTranslationVector = mtv;
result.point = Vector2.add(closestPoint, polygon.position); result.point = Vector2.add(closestPoint, polygon.position);
return result; return true;
} }
/** /**
* *
* @param circle * @param circle
* @param box * @param box
* @param result
*/ */
public static circleToBox(circle: Circle, box: Box): CollisionResult { public static circleToBox(circle: Circle, box: Box, result: CollisionResult): boolean {
let result = new CollisionResult(); let closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position, result.normal);
let closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res;
if (box.containsPoint(circle.position)) { if (box.containsPoint(circle.position)) {
result.point = closestPointOnBounds; result.point = closestPointOnBounds;
@@ -163,7 +160,7 @@ module es {
let safePlace = Vector2.add(closestPointOnBounds, Vector2.subtract(result.normal, new Vector2(circle.radius))); let safePlace = Vector2.add(closestPointOnBounds, Vector2.subtract(result.normal, new Vector2(circle.radius)));
result.minimumTranslationVector = Vector2.subtract(circle.position, safePlace); result.minimumTranslationVector = Vector2.subtract(circle.position, safePlace);
return result; return true;
} }
let sqrDistance = Vector2.distanceSquared(closestPointOnBounds, circle.position); let sqrDistance = Vector2.distanceSquared(closestPointOnBounds, circle.position);
@@ -172,23 +169,24 @@ module es {
} else if (sqrDistance <= circle.radius * circle.radius) { } else if (sqrDistance <= circle.radius * circle.radius) {
result.normal = Vector2.subtract(circle.position, closestPointOnBounds); result.normal = Vector2.subtract(circle.position, closestPointOnBounds);
let depth = result.normal.length() - circle.radius; let depth = result.normal.length() - circle.radius;
result.normal = Vector2Ext.normalize(result.normal);
result.point = closestPointOnBounds;
Vector2Ext.normalize(result.normal);
result.minimumTranslationVector = Vector2.multiply(new Vector2(depth), result.normal); result.minimumTranslationVector = Vector2.multiply(new Vector2(depth), result.normal);
return result; return true;
} }
return null; return false;
} }
/** /**
* *
* @param point * @param point
* @param circle * @param circle
* @param result
*/ */
public static pointToCircle(point: Vector2, circle: Circle) { public static pointToCircle(point: Vector2, circle: Circle, result: CollisionResult): boolean {
let result = new CollisionResult();
let distanceSquared = Vector2.distanceSquared(point, circle.position); let distanceSquared = Vector2.distanceSquared(point, circle.position);
let sumOfRadii = 1 + circle.radius; let sumOfRadii = 1 + circle.radius;
let collided = distanceSquared < sumOfRadii * sumOfRadii; let collided = distanceSquared < sumOfRadii * sumOfRadii;
@@ -198,10 +196,10 @@ module es {
result.minimumTranslationVector = Vector2.multiply(new Vector2(-depth, -depth), result.normal); result.minimumTranslationVector = Vector2.multiply(new Vector2(-depth, -depth), result.normal);
result.point = Vector2.add(circle.position, Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius))); result.point = Vector2.add(circle.position, Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius)));
return result; return true;
} }
return null; return false;
} }
/** /**
@@ -210,7 +208,7 @@ module es {
* @param lineB * @param lineB
* @param closestTo * @param closestTo
*/ */
public static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2) { public static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2 {
let v = Vector2.subtract(lineB, lineA); let v = Vector2.subtract(lineB, lineA);
let w = Vector2.subtract(closestTo, lineA); let w = Vector2.subtract(closestTo, lineA);
let t = Vector2.dot(w, v) / Vector2.dot(v, v); let t = Vector2.dot(w, v) / Vector2.dot(v, v);
@@ -223,24 +221,20 @@ module es {
* *
* @param point * @param point
* @param poly * @param poly
* @param result
*/ */
public static pointToPoly(point: Vector2, poly: Polygon) { public static pointToPoly(point: Vector2, poly: Polygon, result: CollisionResult): boolean {
let result = new CollisionResult();
if (poly.containsPoint(point)) { if (poly.containsPoint(point)) {
let distanceSquared: number; let distanceSquared: number = 0;
let gpp = Polygon.getClosestPointOnPolygonToPoint(poly.points, Vector2.subtract(point, poly.position)); let closestPoint = Polygon.getClosestPointOnPolygonToPoint(poly.points, Vector2.subtract(point, poly.position), distanceSquared, result.normal);
let closestPoint = gpp.closestPoint;
distanceSquared = gpp.distanceSquared;
result.normal = gpp.edgeNormal;
result.minimumTranslationVector = Vector2.multiply(result.normal, new Vector2(Math.sqrt(distanceSquared), Math.sqrt(distanceSquared))); result.minimumTranslationVector = Vector2.multiply(result.normal, new Vector2(Math.sqrt(distanceSquared), Math.sqrt(distanceSquared)));
result.point = Vector2.add(closestPoint, poly.position); result.point = Vector2.add(closestPoint, poly.position);
return result; return true;
} }
return null; return false;
} }
/** /**
@@ -248,9 +242,7 @@ module es {
* @param first * @param first
* @param second * @param second
*/ */
public static circleToCircle(first: Circle, second: Circle){ public static circleToCircle(first: Circle, second: Circle, result: CollisionResult): boolean{
let result = new CollisionResult();
let distanceSquared = Vector2.distanceSquared(first.position, second.position); let distanceSquared = Vector2.distanceSquared(first.position, second.position);
let sumOfRadii = first.radius + second.radius; let sumOfRadii = first.radius + second.radius;
let collided = distanceSquared < sumOfRadii * sumOfRadii; let collided = distanceSquared < sumOfRadii * sumOfRadii;
@@ -260,35 +252,34 @@ module es {
result.minimumTranslationVector = Vector2.multiply(new Vector2(-depth), result.normal); result.minimumTranslationVector = Vector2.multiply(new Vector2(-depth), result.normal);
result.point = Vector2.add(second.position, Vector2.multiply(result.normal, new Vector2(second.radius))); result.point = Vector2.add(second.position, Vector2.multiply(result.normal, new Vector2(second.radius)));
return result; return true;
} }
return null; return false;
} }
/** /**
* *
* @param first * @param first
* @param second * @param second
* @param result
*/ */
public static boxToBox(first: Box, second: Box){ public static boxToBox(first: Box, second: Box, result: CollisionResult): boolean{
let result = new CollisionResult();
let minkowskiDiff = this.minkowskiDifference(first, second); let minkowskiDiff = this.minkowskiDifference(first, second);
if (minkowskiDiff.contains(0, 0)){ if (minkowskiDiff.contains(0, 0)){
// 计算MTV。如果它是零,我们就可以称它为非碰撞 // 计算MTV。如果它是零,我们就可以称它为非碰撞
result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin(); result.minimumTranslationVector = minkowskiDiff.getClosestPointOnBoundsToOrigin();
if (result.minimumTranslationVector.x == 0 && result.minimumTranslationVector.y == 0) if (result.minimumTranslationVector.equals(Vector2.zero))
return null; return false;
result.normal = new Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y); result.normal = new Vector2(-result.minimumTranslationVector.x, -result.minimumTranslationVector.y);
result.normal.normalize(); result.normal.normalize();
return result; return true;
} }
return null; return false;
} }
private static minkowskiDifference(first: Box, second: Box){ private static minkowskiDifference(first: Box, second: Box){
+4 -6
View File
@@ -144,7 +144,7 @@ module es {
* @param excludeCollider * @param excludeCollider
* @param layerMask * @param layerMask
*/ */
public aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number) { public aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number) : Collider[]{
this._tempHashSet.length = 0; this._tempHashSet.length = 0;
let p1 = this.cellCoords(bounds.x, bounds.y); let p1 = this.cellCoords(bounds.x, bounds.y);
@@ -172,7 +172,7 @@ module es {
} }
} }
return {tempHashSet: this._tempHashSet, bounds: bounds}; return this._tempHashSet;
} }
/** /**
@@ -182,16 +182,14 @@ module es {
* @param results * @param results
* @param layerMask * @param layerMask
*/ */
public overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask) { public overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask): number {
let bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2); let bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2);
this._overlapTestCircle.radius = radius; this._overlapTestCircle.radius = radius;
this._overlapTestCircle.position = circleCenter; this._overlapTestCircle.position = circleCenter;
let resultCounter = 0; let resultCounter = 0;
let aabbBroadphaseResult = this.aabbBroadphase(bounds, null, layerMask); let potentials = this.aabbBroadphase(bounds, null, layerMask);
bounds = aabbBroadphaseResult.bounds;
let potentials = aabbBroadphaseResult.tempHashSet;
for (let i = 0; i < potentials.length; i++) { for (let i = 0; i < potentials.length; i++) {
let collider = potentials[i]; let collider = potentials[i];
if (collider instanceof BoxCollider) { if (collider instanceof BoxCollider) {