diff --git a/demo/libs/framework/framework.d.ts b/demo/libs/framework/framework.d.ts index b5144d06..7d4af9fe 100644 --- a/demo/libs/framework/framework.d.ts +++ b/demo/libs/framework/framework.d.ts @@ -380,7 +380,16 @@ declare abstract class Collider extends Component { shape: Shape; physicsLayer: number; isTrigger: boolean; + registeredPhysicsBounds: Rectangle; + protected _isParentEntityAddedToScene: any; + protected _isPositionDirty: boolean; + protected _colliderRequiresAutoSizing: any; readonly bounds: Rectangle; + initialize(): void; +} +declare class BoxCollider extends Collider { + width: number; + setWidth(width: number): BoxCollider; } declare class EntitySystem { private _scene; @@ -513,6 +522,7 @@ declare class MathHelper { static toDegrees(radians: number): number; static toRadians(degrees: number): number; static map(value: number, leftMin: number, leftMax: number, rightMin: number, rightMax: number): number; + static lerp(value1: number, value2: number, amount: number): number; static clamp(value: number, min: number, max: number): number; static minOf(a: number, b: number, c: number, d: number): number; static maxOf(a: number, b: number, c: number, d: number): number; @@ -553,7 +563,7 @@ declare class Rectangle { readonly top: number; readonly bottom: number; location: Vector2; - constructor(x: number, y: number, width: number, height: number); + constructor(x?: number, y?: number, width?: number, height?: number); intersects(value: Rectangle): boolean; contains(value: Vector2): boolean; static fromMinMax(minX: number, minY: number, maxX: number, maxY: number): Rectangle; @@ -566,7 +576,7 @@ declare class Rectangle { declare class Vector2 { x: number; y: number; - constructor(x: number, y: number); + constructor(x: number, y?: number); static add(value1: Vector2, value2: Vector2): Vector2; static divide(value1: Vector2, value2: Vector2): Vector2; static multiply(value1: Vector2, value2: Vector2): Vector2; @@ -576,6 +586,7 @@ declare class Vector2 { static normalize(value: Vector2): Vector2; static dot(value1: Vector2, value2: Vector2): number; static distanceSquared(value1: Vector2, value2: Vector2): number; + static lerp(value1: Vector2, value2: Vector2, amount: number): Vector2; static transform(position: Vector2, matrix: Matrix2D): Vector2; static distance(value1: Vector2, value2: Vector2): number; } @@ -606,12 +617,44 @@ declare class Physics { private static _spatialHash; static readonly allLayers: number; static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number; + static boxcastBroadphase(rect: Rectangle, layerMask?: number): Collider[]; + static updateCollider(collider: Collider): void; } declare abstract class Shape { bounds: Rectangle; position: Vector2; abstract pointCollidesWithShape(point: Vector2): CollisionResult; } +declare class Polygon extends Shape { + points: Vector2[]; + isUnrotated: boolean; + private _polygonCenter; + private _areEdgeNormalsDirty; + protected _originalPoints: Vector2[]; + _edgeNormals: Vector2[]; + readonly edgeNormals: Vector2[]; + isBox: boolean; + constructor(vertCount: number, radius: number); + private buildEdgeNormals; + setPoints(points: Vector2[]): void; + collidesWithShape(other: Shape): void; + recalculateCenterAndEdgeNormals(): void; + static findPolygonCenter(points: Vector2[]): Vector2; + static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): { + closestPoint: any; + distanceSquared: any; + edgeNormal: any; + }; + pointCollidesWithShape(point: Vector2): CollisionResult; + containsPoint(point: Vector2): boolean; + static buildSymmertricalPolygon(vertCount: number, radius: number): any; +} +declare class Box extends Polygon { + width: number; + height: number; + updateBox(width: number, height: number): void; + containsPoint(point: Vector2): boolean; +} declare class Circle extends Shape { radius: number; private _originalRadius; @@ -624,30 +667,10 @@ declare class CollisionResult { normal: Vector2; point: Vector2; } -declare class Polygon extends Shape { - points: Vector2[]; - isUnrotated: boolean; - private _polygonCenter; - private _areEdgeNormalsDirty; - private _originalPoint; - constructor(vertCount: number, radius: number); - setPoints(points: Vector2[]): void; - recalculateCenterAndEdgeNormals(): void; - static findPolygonCenter(points: Vector2[]): Vector2; - static getClosestPointOnPolygonToPoint(points: Vector2[], point: Vector2): { - closestPoint: any; - distanceSquared: any; - edgeNormal: any; - }; - pointCollidesWithShape(point: Vector2): CollisionResult; - containsPoint(point: Vector2): boolean; - static buildSymmertricalPolygon(vertCount: number, radius: number): any; -} -declare class Rect extends Polygon { - containsPoint(point: Vector2): boolean; -} declare class ShapeCollisions { - static circleToRect(circle: Circle, box: Rect): CollisionResult; + static polygonToPolygon(first: Polygon, second: Polygon): void; + static circleToPolygon(circle: Circle, polygon: Polygon): CollisionResult; + static circleToRect(circle: Circle, box: Box): CollisionResult; static pointToCicle(point: Vector2, circle: Circle): CollisionResult; static closestPointOnLine(lineA: Vector2, lineB: Vector2, closestTo: Vector2): Vector2; static pointToPoly(point: Vector2, poly: Polygon): CollisionResult; @@ -665,6 +688,7 @@ declare class Particle { applyForce(force: Vector2): void; } declare class SpatialHash { + gridBounds: Rectangle; private _raycastParser; private _cellSize; private _inverseCellSize; @@ -672,6 +696,8 @@ declare class SpatialHash { private _tempHashSet; private _cellDict; constructor(cellSize?: number); + remove(collider: Collider): void; + register(collider: Collider): void; overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask: any): number; aabbBroadphase(bounds: Rectangle, excludeCollider: Collider, layerMask: number): Collider[]; private cellAtPosition; @@ -688,64 +714,6 @@ declare class NumberDictionary { getAllObjects(): Collider[]; clear(): void; } -declare class VerletWorld { - gravity: Vector2; - maximumStepIterations: number; - constraintIterations: number; - simulationBounds: Rectangle; - private _leftOverTime; - private _iterationSteps; - private _fixedDeltaTime; - private _composites; - private _fixedDeltaTimeSq; - private static _colliders; - private _tempCircle; - constructor(simulationBounds?: Rectangle); - update(): void; - private handleCollisions; - private constrainParticleToBounds; - debugRender(displayObject: egret.DisplayObject): void; - addComposite(composite: T): T; - private updateTiming; -} -declare class Composite { - private _constraints; - friction: Vector2; - drawParticles: boolean; - drawConstraints: boolean; - particles: Particle[]; - collidesWithLayers: number; - solveConstraints(): void; - addParticle(particle: Particle): Particle; - addConstraint(constraint: T): T; - removeConstraint(constraint: Constraint): void; - updateParticles(deltaTimeSquared: number, gravity: Vector2): void; - handleConstraintCollisions(): void; - debugRender(graphics: egret.Graphics): void; -} -declare class Box extends Composite { - constructor(center: Vector2, width: number, height: number, borderStiffness?: number, diagonalStiffness?: number); -} -declare abstract class Constraint { - composite: Composite; - collidesWithColliders: boolean; - abstract solve(): any; - handleCollisions(collidesWithLayers: number): void; - debugRender(graphics: egret.Graphics): void; -} -declare class DistanceConstraint extends Constraint { - stiffness: number; - restingDistance: number; - tearSensitivity: number; - private _particleOne; - private _particleTwo; - private static _polygon; - constructor(first: Particle, second: Particle, stiffness: number, distance?: number); - setCollidesWithColliders(collidesWithColliders: boolean): this; - handleCollisions(collidersWithLayers: any): void; - solve(): void; - debugRender(graphics: egret.Graphics): void; -} declare class Triangulator { triangleIndices: number[]; private _triPrev; @@ -757,6 +725,7 @@ declare class Triangulator { declare class Vector2Ext { static isTriangleCCW(a: Vector2, center: Vector2, c: Vector2): boolean; static cross(u: Vector2, v: Vector2): number; + static perpendicular(first: Vector2, second: Vector2): Vector2; } declare class WebGLUtils { static getWebGL(): WebGLRenderingContext; diff --git a/demo/libs/framework/framework.js b/demo/libs/framework/framework.js index 7e9e9b53..3f081713 100644 --- a/demo/libs/framework/framework.js +++ b/demo/libs/framework/framework.js @@ -1786,6 +1786,7 @@ var Collider = (function (_super) { function Collider() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.physicsLayer = 1 << 0; + _this._isPositionDirty = true; return _this; } Object.defineProperty(Collider.prototype, "bounds", { @@ -1795,8 +1796,38 @@ var Collider = (function (_super) { enumerable: true, configurable: true }); + Collider.prototype.initialize = function () { + }; return Collider; }(Component)); +var BoxCollider = (function (_super) { + __extends(BoxCollider, _super); + function BoxCollider() { + return _super !== null && _super.apply(this, arguments) || this; + } + Object.defineProperty(BoxCollider.prototype, "width", { + get: function () { + return this.shape.width; + }, + set: function (value) { + this.setWidth(value); + }, + enumerable: true, + configurable: true + }); + BoxCollider.prototype.setWidth = function (width) { + this._colliderRequiresAutoSizing = false; + var box = this.shape; + if (width != box.width) { + box.updateBox(width, box.height); + this._isPositionDirty = true; + if (this.entity && this._isParentEntityAddedToScene) + Physics.updateCollider(this); + } + return this; + }; + return BoxCollider; +}(Collider)); var EntitySystem = (function () { function EntitySystem(matcher) { this._entities = []; @@ -2373,6 +2404,9 @@ var MathHelper = (function () { MathHelper.map = function (value, leftMin, leftMax, rightMin, rightMax) { return rightMin + (value - leftMin) * (rightMax - rightMin) / (leftMax - leftMin); }; + MathHelper.lerp = function (value1, value2, amount) { + return value1 + (value2 - value1) * amount; + }; MathHelper.clamp = function (value, min, max) { if (value < min) return min; @@ -2543,10 +2577,10 @@ var Matrix2D = (function () { }()); var Rectangle = (function () { function Rectangle(x, y, width, height) { - this.x = x; - this.y = y; - this.width = width; - this.height = height; + this.x = x ? x : 0; + this.y = y ? y : 0; + this.width = width ? width : 0; + this.height = height ? height : 0; } Object.defineProperty(Rectangle.prototype, "left", { get: function () { @@ -2686,7 +2720,7 @@ var Vector2 = (function () { this.x = 0; this.y = 0; this.x = x; - this.y = y; + this.y = y ? y : x; } Vector2.add = function (value1, value2) { var result = new Vector2(0, 0); @@ -2733,6 +2767,9 @@ var Vector2 = (function () { var v1 = value1.x - value2.x, v2 = value1.y - value2.y; return (v1 * v1) + (v2 * v2); }; + Vector2.lerp = function (value1, value2, amount) { + return new Vector2(MathHelper.lerp(value1.x, value2.x, amount), MathHelper.lerp(value1.y, value2.y, amount)); + }; Vector2.transform = function (position, matrix) { return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22)); }; @@ -2876,6 +2913,14 @@ var Physics = (function () { if (layerMask === void 0) { layerMask = -1; } return this._spatialHash.overlapCircle(center, randius, results, layerMask); }; + Physics.boxcastBroadphase = function (rect, layerMask) { + if (layerMask === void 0) { layerMask = this.allLayers; } + return this._spatialHash.aabbBroadphase(rect, null, layerMask); + }; + Physics.updateCollider = function (collider) { + this._spatialHash.remove(collider); + this._spatialHash.register(collider); + }; Physics.allLayers = -1; return Physics; }()); @@ -2884,30 +2929,6 @@ var Shape = (function () { } return Shape; }()); -var Circle = (function (_super) { - __extends(Circle, _super); - function Circle(radius) { - var _this = _super.call(this) || this; - _this.radius = radius; - _this._originalRadius = radius; - return _this; - } - Circle.prototype.pointCollidesWithShape = function (point) { - return ShapeCollisions.pointToCicle(point, this); - }; - Circle.prototype.collidesWithShape = function (other) { - if (other instanceof Rect && other.isUnrotated) { - return ShapeCollisions.circleToRect(this, other); - } - throw new Error("Collisions of Circle to " + other + " are not supported"); - }; - return Circle; -}(Shape)); -var CollisionResult = (function () { - function CollisionResult() { - } - return CollisionResult; -}()); var Polygon = (function (_super) { __extends(Polygon, _super); function Polygon(vertCount, radius) { @@ -2917,11 +2938,40 @@ var Polygon = (function (_super) { _this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius)); return _this; } + Object.defineProperty(Polygon.prototype, "edgeNormals", { + get: function () { + if (this._areEdgeNormalsDirty) + this.buildEdgeNormals(); + return this._edgeNormals; + }, + enumerable: true, + configurable: true + }); + Polygon.prototype.buildEdgeNormals = function () { + var totalEdges = this.isBox ? 2 : this.points.length; + if (this._edgeNormals == null || this._edgeNormals.length != totalEdges) + this._edgeNormals = new Vector2[totalEdges]; + var p2; + for (var i = 0; i < totalEdges; i++) { + var p1 = this.points[i]; + if (i + 1 >= this.points.length) + p2 = this.points[0]; + else + p2 = this.points[i + 1]; + var perp = Vector2Ext.perpendicular(p1, p2); + perp = Vector2.normalize(perp); + this._edgeNormals[i] = perp; + } + }; Polygon.prototype.setPoints = function (points) { this.points = points; this.recalculateCenterAndEdgeNormals(); - this._originalPoint = new Vector2[points.length]; - this._originalPoint = points; + this._originalPoints = new Vector2[points.length]; + this._originalPoints = points; + }; + Polygon.prototype.collidesWithShape = function (other) { + if (other instanceof Polygon) + return ShapeCollisions.polygonToPolygon(this, other); }; Polygon.prototype.recalculateCenterAndEdgeNormals = function () { this._polygonCenter = Polygon.findPolygonCenter(this.points); @@ -2982,21 +3032,84 @@ var Polygon = (function (_super) { }; return Polygon; }(Shape)); -var Rect = (function (_super) { - __extends(Rect, _super); - function Rect() { +var Box = (function (_super) { + __extends(Box, _super); + function Box() { return _super !== null && _super.apply(this, arguments) || this; } - Rect.prototype.containsPoint = function (point) { + Box.prototype.updateBox = function (width, height) { + this.width = width; + this.height = height; + var halfWidth = width / 2; + var halfHeight = height / 2; + this.points[0] = new Vector2(-halfWidth, -halfHeight); + this.points[1] = new Vector2(halfWidth, -halfHeight); + this.points[2] = new Vector2(halfWidth, halfHeight); + this.points[3] = new Vector2(-halfWidth, halfHeight); + for (var i = 0; i < this.points.length; i++) + this._originalPoints[i] = this.points[i]; + }; + Box.prototype.containsPoint = function (point) { if (this.isUnrotated) return this.bounds.contains(point); return _super.prototype.containsPoint.call(this, point); }; - return Rect; + return Box; }(Polygon)); +var Circle = (function (_super) { + __extends(Circle, _super); + function Circle(radius) { + var _this = _super.call(this) || this; + _this.radius = radius; + _this._originalRadius = radius; + return _this; + } + Circle.prototype.pointCollidesWithShape = function (point) { + return ShapeCollisions.pointToCicle(point, this); + }; + Circle.prototype.collidesWithShape = function (other) { + if (other instanceof Box && other.isUnrotated) { + return ShapeCollisions.circleToRect(this, other); + } + throw new Error("Collisions of Circle to " + other + " are not supported"); + }; + return Circle; +}(Shape)); +var CollisionResult = (function () { + function CollisionResult() { + } + return CollisionResult; +}()); var ShapeCollisions = (function () { function ShapeCollisions() { } + ShapeCollisions.polygonToPolygon = function (first, second) { + var result = new CollisionResult(); + var isIntersecting = true; + }; + ShapeCollisions.circleToPolygon = function (circle, polygon) { + var result = new CollisionResult(); + var poly2Circle = Vector2.subtract(circle.position, polygon.position); + var gpp = Polygon.getClosestPointOnPolygonToPoint(polygon.points, poly2Circle); + var closestPoint = gpp.closestPoint; + var distanceSquared = gpp.distanceSquared; + result.normal = gpp.edgeNormal; + var circleCenterInsidePoly = polygon.containsPoint(circle.position); + if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly) + return result; + var mtv; + if (circleCenterInsidePoly) { + mtv = Vector2.multiply(result.normal, new Vector2(Math.sqrt(distanceSquared) - circle.radius, Math.sqrt(distanceSquared) - circle.radius)); + } + else { + if (distanceSquared == 0) { + mtv = Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius)); + } + else { + var distance = Math.sqrt(distanceSquared); + } + } + }; ShapeCollisions.circleToRect = function (circle, box) { var result = new CollisionResult(); var closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res; @@ -3062,12 +3175,33 @@ var Particle = (function () { var SpatialHash = (function () { function SpatialHash(cellSize) { if (cellSize === void 0) { cellSize = 100; } + this.gridBounds = new Rectangle(); this._tempHashSet = []; this._cellDict = new NumberDictionary(); this._cellSize = cellSize; this._inverseCellSize = 1 / this._cellSize; this._raycastParser = new RaycastResultParser(); } + SpatialHash.prototype.remove = function (collider) { + var bounds = collider.registeredPhysicsBounds; + var p1 = this.cellCoords(bounds.x, bounds.y); + var p2 = this.cellCoords(bounds.right, bounds.bottom); + for (var x = p1.x; x <= p2.x; x++) { + for (var y = p1.y; y <= p2.y; y++) { + var cell = this.cellAtPosition(x, y); + if (!cell) + console.error("removing Collider [" + collider + "] from a cell that it is not present in"); + else + cell.remove(collider); + } + } + }; + SpatialHash.prototype.register = function (collider) { + var bounds = collider.bounds; + collider.registeredPhysicsBounds = bounds; + var p1 = this.cellCoords(bounds.x, bounds.y); + var p2 = this.cellCoords(bounds.right, bounds.bottom); + }; SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) { var bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2); this._overlapTestCircle.radius = radius; @@ -3150,263 +3284,6 @@ var NumberDictionary = (function () { }; return NumberDictionary; }()); -var VerletWorld = (function () { - function VerletWorld(simulationBounds) { - this.gravity = new Vector2(0, 980); - this.maximumStepIterations = 5; - this.constraintIterations = 3; - this._leftOverTime = 0; - this._iterationSteps = 0; - this._fixedDeltaTime = 1 / 60; - this._composites = []; - this._tempCircle = new Circle(1); - this.simulationBounds = simulationBounds; - this._fixedDeltaTimeSq = Math.pow(this._fixedDeltaTime, 2); - } - VerletWorld.prototype.update = function () { - this.updateTiming(); - for (var iteration = 1; iteration <= this._iterationSteps; iteration++) { - for (var i = this._composites.length - 1; i >= 0; i--) { - var composite = this._composites[i]; - for (var s = 0; s < this.constraintIterations; s++) { - composite.solveConstraints(); - } - composite.updateParticles(this._fixedDeltaTimeSq, this.gravity); - composite.handleConstraintCollisions(); - for (var j = 0; j < composite.particles.length; j++) { - var p = composite.particles[j]; - if (this.simulationBounds) { - this.constrainParticleToBounds(p); - } - if (p.collidesWithColliders) - this.handleCollisions(p, composite.collidesWithLayers); - } - } - } - }; - VerletWorld.prototype.handleCollisions = function (p, collidesWithLayers) { - var collidedCount = Physics.overlapCircleAll(p.position, p.radius, VerletWorld._colliders, collidesWithLayers); - for (var i = 0; i < collidedCount; i++) { - var collider = VerletWorld._colliders[i]; - if (collider.isTrigger) - continue; - if (p.radius < 2) { - var collisionResult = collider.shape.pointCollidesWithShape(p.position); - if (collisionResult) { - p.position = Vector2.subtract(p.position, collisionResult.minimumTranslationVector); - } - } - else { - this._tempCircle.radius = p.radius; - this._tempCircle.position = p.position; - var collisionResult = this._tempCircle.collidesWithShape(collider.shape); - if (collisionResult) { - p.position = Vector2.subtract(p.position, collisionResult.minimumTranslationVector); - } - } - } - }; - VerletWorld.prototype.constrainParticleToBounds = function (p) { - var tempPos = p.position; - var bounds = this.simulationBounds; - if (p.radius == 0) { - if (tempPos.y > bounds.height) { - tempPos.y = bounds.height; - } - else if (tempPos.y < bounds.y) { - tempPos.y = bounds.y; - } - if (tempPos.x < bounds.x) { - tempPos.x = bounds.x; - } - else if (tempPos.x > bounds.width) { - tempPos.x = bounds.width; - } - } - else { - if (tempPos.y < bounds.y + p.radius) { - tempPos.y = 2 * (bounds.y + p.radius) - tempPos.y; - } - if (tempPos.y > bounds.height - p.radius) { - tempPos.y = 2 * (bounds.height - p.radius) - tempPos.y; - } - if (tempPos.x > bounds.width - p.radius) { - tempPos.x = 2 * (bounds.width - p.radius) - tempPos.x; - } - if (tempPos.x < bounds.x + p.radius) - tempPos.x = 2 * (bounds.x + p.radius) - tempPos.x; - } - p.position = tempPos; - }; - VerletWorld.prototype.debugRender = function (displayObject) { - if (!displayObject) - return; - displayObject.stage.removeChildren(); - for (var i = 0; i < this._composites.length; i++) { - var shape = new egret.Shape(); - this._composites[i].debugRender(shape.graphics); - displayObject.stage.addChild(shape); - } - }; - VerletWorld.prototype.addComposite = function (composite) { - this._composites.push(composite); - return composite; - }; - VerletWorld.prototype.updateTiming = function () { - this._leftOverTime += Time.deltaTime; - this._iterationSteps = Math.trunc(this._leftOverTime / this._fixedDeltaTime); - this._leftOverTime -= this._iterationSteps * this._fixedDeltaTime; - this._iterationSteps = Math.min(this._iterationSteps, this.maximumStepIterations); - }; - VerletWorld._colliders = new Array(4); - return VerletWorld; -}()); -var Composite = (function () { - function Composite() { - this._constraints = []; - this.friction = new Vector2(0.98, 1); - this.drawParticles = true; - this.drawConstraints = true; - this.particles = []; - this.collidesWithLayers = Physics.allLayers; - } - Composite.prototype.solveConstraints = function () { - for (var i = this._constraints.length - 1; i >= 0; i--) { - this._constraints[i].solve(); - } - }; - Composite.prototype.addParticle = function (particle) { - this.particles.push(particle); - return particle; - }; - Composite.prototype.addConstraint = function (constraint) { - this._constraints.push(constraint); - constraint.composite = this; - return constraint; - }; - Composite.prototype.removeConstraint = function (constraint) { - this._constraints.remove(constraint); - }; - Composite.prototype.updateParticles = function (deltaTimeSquared, gravity) { - for (var j = 0; j < this.particles.length; j++) { - var p = this.particles[j]; - if (p.isPinned) { - p.position = p.pinnedPosition; - continue; - } - p.applyForce(Vector2.multiply(new Vector2(p.mass, p.mass), gravity)); - var vel = Vector2.multiply(Vector2.subtract(p.position, p.lastPosition), this.friction); - var nextPos = Vector2.add(Vector2.add(p.position, vel), Vector2.multiply(p.acceleration, new Vector2(0.5 * deltaTimeSquared, 0.5 * deltaTimeSquared))); - p.lastPosition = p.position; - p.position = nextPos; - p.acceleration.x = p.acceleration.y = 0; - } - }; - Composite.prototype.handleConstraintCollisions = function () { - for (var i = this._constraints.length - 1; i >= 0; i--) { - if (this._constraints[i].collidesWithColliders) - this._constraints[i].handleCollisions(this.collidesWithLayers); - } - }; - Composite.prototype.debugRender = function (graphics) { - if (this.drawConstraints) { - for (var i = 0; i < this._constraints.length; i++) { - this._constraints[i].debugRender(graphics); - } - } - if (this.drawParticles) { - for (var i = 0; i < this.particles.length; i++) { - var size = this.particles[i].radius ? this.particles[i].radius : 4; - graphics.lineStyle(4, DebugDefaults.verletParticle); - graphics.drawRect(this.particles[i].position.x, this.particles[i].position.y, size, size); - graphics.endFill(); - } - } - }; - return Composite; -}()); -var Box = (function (_super) { - __extends(Box, _super); - function Box(center, width, height, borderStiffness, diagonalStiffness) { - if (borderStiffness === void 0) { borderStiffness = 0.2; } - if (diagonalStiffness === void 0) { diagonalStiffness = 0.5; } - var _this = _super.call(this) || this; - var tl = _this.addParticle(new Particle(Vector2.add(center, new Vector2(-width / 2, -height / 2)))); - var tr = _this.addParticle(new Particle(Vector2.add(center, new Vector2(width / 2, -height / 2)))); - var br = _this.addParticle(new Particle(Vector2.add(center, new Vector2(width / 2, height / 2)))); - var bl = _this.addParticle(new Particle(Vector2.add(center, new Vector2(-width / 2, height / 2)))); - _this.addConstraint(new DistanceConstraint(tl, tr, borderStiffness)); - _this.addConstraint(new DistanceConstraint(tr, br, borderStiffness)); - _this.addConstraint(new DistanceConstraint(br, bl, borderStiffness)); - _this.addConstraint(new DistanceConstraint(bl, tl, borderStiffness)); - _this.addConstraint(new DistanceConstraint(tl, br, diagonalStiffness)).setCollidesWithColliders(false); - _this.addConstraint(new DistanceConstraint(bl, tr, diagonalStiffness)).setCollidesWithColliders(false); - return _this; - } - return Box; -}(Composite)); -var Constraint = (function () { - function Constraint() { - this.collidesWithColliders = true; - } - Constraint.prototype.handleCollisions = function (collidesWithLayers) { - }; - Constraint.prototype.debugRender = function (graphics) { }; - return Constraint; -}()); -var DistanceConstraint = (function (_super) { - __extends(DistanceConstraint, _super); - function DistanceConstraint(first, second, stiffness, distance) { - if (distance === void 0) { distance = -1; } - var _this = _super.call(this) || this; - _this.stiffness = 0; - _this.restingDistance = 0; - _this.tearSensitivity = Number.POSITIVE_INFINITY; - _this._particleOne = first; - _this._particleTwo = second; - _this.stiffness = stiffness; - if (distance > -1) { - _this.restingDistance = distance; - } - else { - _this.restingDistance = Vector2.distance(first.position, second.position); - } - return _this; - } - DistanceConstraint.prototype.setCollidesWithColliders = function (collidesWithColliders) { - this.collidesWithColliders = collidesWithColliders; - return this; - }; - DistanceConstraint.prototype.handleCollisions = function (collidersWithLayers) { - var minX = Math.min(this._particleOne.position.x, this._particleTwo.position.x); - var maxX = Math.max(this._particleOne.position.x, this._particleTwo.position.x); - var minY = Math.min(this._particleOne.position.y, this._particleTwo.position.y); - var maxY = Math.max(this._particleOne.position.y, this._particleTwo.position.y); - }; - DistanceConstraint.prototype.solve = function () { - var diff = Vector2.subtract(this._particleOne.position, this._particleTwo.position); - var d = diff.length(); - var difference = (this.restingDistance - d) / d; - if (d / this.restingDistance > this.tearSensitivity) { - this.composite.removeConstraint(this); - return; - } - var im1 = 1 / this._particleOne.mass; - var im2 = 1 / this._particleTwo.mass; - var scalarP1 = (im1 / (im1 + im2)) * this.stiffness; - var scalarP2 = this.stiffness - scalarP1; - this._particleOne.position = Vector2.add(this._particleOne.position, Vector2.multiply(diff, new Vector2(scalarP1 * difference, scalarP1 * difference))); - this._particleTwo.position = Vector2.subtract(this._particleTwo.position, Vector2.multiply(diff, new Vector2(scalarP2 * difference, scalarP2 * difference))); - }; - DistanceConstraint.prototype.debugRender = function (graphics) { - graphics.lineStyle(1, DebugDefaults.verletConstraintEdge); - graphics.lineTo(this._particleOne.position.x, this._particleOne.position.y); - graphics.lineTo(this._particleTwo.position.x, this._particleTwo.position.y); - graphics.endFill(); - }; - DistanceConstraint._polygon = new Polygon(2, 1); - return DistanceConstraint; -}(Constraint)); var Triangulator = (function () { function Triangulator() { this.triangleIndices = []; @@ -3494,6 +3371,9 @@ var Vector2Ext = (function () { Vector2Ext.cross = function (u, v) { return u.y * v.x - u.x * v.y; }; + Vector2Ext.perpendicular = function (first, second) { + return new Vector2(-1 * (second.y - first.y), second.x - first.x); + }; return Vector2Ext; }()); var WebGLUtils = (function () { diff --git a/demo/libs/framework/framework.min.js b/demo/libs/framework/framework.min.js index 2c37d571..61ac1686 100644 --- a/demo/libs/framework/framework.min.js +++ b/demo/libs/framework/framework.min.js @@ -1 +1 @@ -window.framework={},window.__extends=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])};return function(e,i){function n(){this.constructor=e}t(e,i),e.prototype=null===i?Object.create(i):(n.prototype=i.prototype,new n)}}(),Array.prototype.findIndex=function(t){return function(t,e){for(var i=0,n=t.length;i-1}(this,t)},Array.prototype.firstOrDefault=function(t){return function(t,e){var i=t.findIndex(e);return-1==i?null:t[i]}(this,t)},Array.prototype.find=function(t){return function(t,e){return t.firstOrDefault(e)}(this,t)},Array.prototype.where=function(t){return function(t,e){if("function"==typeof t.reduce)return t.reduce(function(i,n,o){return e.call(arguments[2],n,o,t)&&i.push(n),i},[]);for(var i=[],n=0,o=t.length;n=0&&t.splice(i,1)}while(i>=0)}(this,t)},Array.prototype.remove=function(t){return function(t,e){var i=t.findIndex(function(t){return t===e});return i>=0&&(t.splice(i,1),!0)}(this,t)},Array.prototype.removeAt=function(t){return function(t,e){t.splice(e,1)}(this,t)},Array.prototype.removeRange=function(t,e){return function(t,e,i){t.splice(e,i)}(this,t,e)},Array.prototype.select=function(t){return function(t,e){if("function"==typeof t.reduce)return t.reduce(function(i,n,o){return i.push(e.call(arguments[2],n,o,t)),i},[]);for(var i=[],n=0,o=t.length;nr?1:-1}),t}(this,t,e)},Array.prototype.orderByDescending=function(t,e){return function(t,e,i){return t.sort(function(t,n){var o=e(t),r=e(n);return i?-i(o,r):o0;){if("break"===h())break}return o?this.recontructPath(r,e,i):null},t.hasKey=function(t,e){for(var i,n=t.keys();!(i=n.next()).done;)if(JSON.stringify(i.value)==JSON.stringify(e))return!0;return!1},t.getKey=function(t,e){for(var i,n,o=t.keys(),r=t.values();i=o.next(),n=r.next(),!i.done;)if(JSON.stringify(i.value)==JSON.stringify(e))return n.value;return null},t.recontructPath=function(t,e,i){var n=[],o=i;for(n.push(i);o!=e;)o=this.getKey(t,o),n.push(o);return n.reverse(),n},t}(),AStarNode=function(t){function e(e){var i=t.call(this)||this;return i.data=e,i}return __extends(e,t),e}(PriorityQueueNode),AstarGridGraph=function(){function t(t,e){this.dirs=[new Point(1,0),new Point(0,-1),new Point(-1,0),new Point(0,1)],this.walls=[],this.weightedNodes=[],this.defaultWeight=1,this.weightedNodeWeight=5,this._neighbors=new Array(4),this._width=t,this._height=e}return t.prototype.isNodeInBounds=function(t){return 0<=t.x&&t.x0&&this.hasHigherPriority(t,i)?this.cascadeUp(t):this.cascadeDown(t)},t.prototype.cascadeDown=function(t){for(var e,i=t.queueIndex;;){e=t;var n=2*i;if(n>this._numNodes){t.queueIndex=i,this._nodes[i]=t;break}var o=this._nodes[n];this.hasHigherPriority(o,e)&&(e=o);var r=n+1;if(r<=this._numNodes){var s=this._nodes[r];this.hasHigherPriority(s,e)&&(e=s)}if(e==t){t.queueIndex=i,this._nodes[i]=t;break}this._nodes[i]=e;var a=e.queueIndex;e.queueIndex=i,i=a}},t.prototype.cascadeUp=function(t){for(var e=Math.floor(t.queueIndex/2);e>=1;){var i=this._nodes[e];if(this.hasHigherPriority(i,t))break;this.swap(t,i),e=Math.floor(t.queueIndex/2)}},t.prototype.swap=function(t,e){this._nodes[t.queueIndex]=e,this._nodes[e.queueIndex]=t;var i=t.queueIndex;t.queueIndex=e.queueIndex,e.queueIndex=i},t.prototype.hasHigherPriority=function(t,e){return t.priority0;){if("break"===a())break}return o?AStarPathfinder.recontructPath(s,e,i):null},t.hasKey=function(t,e){for(var i,n=t.keys();!(i=n.next()).done;)if(JSON.stringify(i.value)==JSON.stringify(e))return!0;return!1},t}(),UnweightedGraph=function(){function t(){this.edges=new Map}return t.prototype.addEdgesForNode=function(t,e){return this.edges.set(t,e),this},t.prototype.getNeighbors=function(t){return this.edges.get(t)},t}(),Point=function(){return function(t,e){this.x=t,this.y=e}}(),UnweightedGridGraph=function(){function t(e,i,n){void 0===n&&(n=!1),this.walls=[],this._neighbors=new Array(4),this._width=e,this._hegiht=i,this._dirs=n?t.COMPASS_DIRS:t.CARDINAL_DIRS}return t.prototype.isNodeInBounds=function(t){return 0<=t.x&&t.x0;){if("break"===h())break}return o?this.recontructPath(r,e,i):null},t.hasKey=function(t,e){for(var i,n=t.keys();!(i=n.next()).done;)if(JSON.stringify(i.value)==JSON.stringify(e))return!0;return!1},t.getKey=function(t,e){for(var i,n,o=t.keys(),r=t.values();i=o.next(),n=r.next(),!i.done;)if(JSON.stringify(i.value)==JSON.stringify(e))return n.value;return null},t.recontructPath=function(t,e,i){var n=[],o=i;for(n.push(i);o!=e;)o=this.getKey(t,o),n.push(o);return n.reverse(),n},t}(),DebugDefaults=function(){function t(){}return t.verletParticle=14431326,t.verletConstraintEdge=4406838,t}(),Component=function(){function t(){this._enabled=!0,this.updateInterval=1}return Object.defineProperty(t.prototype,"transform",{get:function(){return this.entity.transform},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"enabled",{get:function(){return this.entity?this.entity.enabled&&this._enabled:this._enabled},set:function(t){this.setEnabled(t)},enumerable:!0,configurable:!0}),t.prototype.setEnabled=function(t){return this._enabled!=t&&(this._enabled=t,this._enabled?this.onEnabled():this.onDisabled()),this},t.prototype.onAddedToEntity=function(){},t.prototype.onRemovedFromEntity=function(){},t.prototype.onEnabled=function(){},t.prototype.onDisabled=function(){},t.prototype.onEntityTransformChanged=function(t){},t.prototype.update=function(){},t.prototype.registerComponent=function(){this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this),!1),this.entity.scene.entityProcessors.onComponentAdded(this.entity)},t.prototype.deregisterComponent=function(){this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this)),this.entity.scene.entityProcessors.onComponentRemoved(this.entity)},t}(),Entity=function(){function t(e){this._updateOrder=0,this._enabled=!0,this._tag=0,this.name=e,this.transform=new Transform(this),this.components=new ComponentList(this),this.id=t._idGenerator++,this.componentBits=new BitSet}return Object.defineProperty(t.prototype,"parent",{get:function(){return this.transform.parent},set:function(t){this.transform.setParent(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"position",{get:function(){return this.transform.position},set:function(t){this.transform.setPosition(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localPosition",{get:function(){return this.transform.localPosition},set:function(t){this.transform.setLocalPosition(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotation",{get:function(){return this.transform.rotation},set:function(t){this.transform.setRotation(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotationDegrees",{get:function(){return this.transform.rotationDegrees},set:function(t){this.transform.setRotationDegrees(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localRotation",{get:function(){return this.transform.localRotation},set:function(t){this.transform.setLocalRotation(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localRotationDegrees",{get:function(){return this.transform.localRotationDegrees},set:function(t){this.transform.setLocalRotationDegrees(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"scale",{get:function(){return this.transform.scale},set:function(t){this.transform.setScale(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localScale",{get:function(){return this.transform.scale},set:function(t){this.transform.setScale(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"worldInverseTransform",{get:function(){return this.transform.worldInverseTransform},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localToWorldTransform",{get:function(){return this.transform.localToWorldTransform},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"worldToLocalTransform",{get:function(){return this.transform.worldToLocalTransform},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"isDestoryed",{get:function(){return this._isDestoryed},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"enabled",{get:function(){return this._enabled},set:function(t){this.setEnabled(t)},enumerable:!0,configurable:!0}),t.prototype.setEnabled=function(t){return this._enabled!=t&&(this._enabled=t),this},Object.defineProperty(t.prototype,"tag",{get:function(){return this._tag},set:function(t){this.setTag(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"updateOrder",{get:function(){return this._updateOrder},set:function(t){this.setUpdateOrder(t)},enumerable:!0,configurable:!0}),t.prototype.setUpdateOrder=function(t){if(this._updateOrder!=t)return this._updateOrder=t,this.scene,this},t.prototype.setTag=function(t){return this._tag!=t&&(this.scene&&this.scene.entities.removeFromTagList(this),this._tag=t,this.scene&&this.scene.entities.addToTagList(this)),this},t.prototype.attachToScene=function(t){this.scene=t,t.entities.add(this),this.components.registerAllComponents();for(var e=0;e=0;t--){this.transform.getChild(t).entity.destory()}},t}(),Scene=function(t){function e(e){var i=t.call(this)||this;return e.stage.addChild(i),i._projectionMatrix=new Matrix2D(0,0,0,0,0,0),i.entityProcessors=new EntityProcessorList,i.entities=new EntityList(i),i.addEventListener(egret.Event.ACTIVATE,i.onActive,i),i.addEventListener(egret.Event.DEACTIVATE,i.onDeactive,i),i.addEventListener(egret.Event.ENTER_FRAME,i.update,i),i}return __extends(e,t),e.prototype.createEntity=function(t){var e=new Entity(t);return e.transform.position=new Vector2(0,0),this.addEntity(e)},e.prototype.addEntity=function(t){this.entities.add(t),t.scene=this;for(var e=0;et&&(this._zoom=t),this._maximumZoom=t,this},e.prototype.setZoom=function(t){var e=MathHelper.clamp(t,-1,1);return this._zoom=0==e?1:e<0?MathHelper.map(e,-1,0,this._minimumZoom,1):MathHelper.map(e,0,1,1,this._maximumZoom),this._areMatrixesDirty=!0,this},e.prototype.initialize=function(){},e.prototype.update=function(){},e.prototype.setPosition=function(t){return this.entity.transform.setPosition(t),this},e.prototype.updateMatrixes=function(){var t;this._areMatrixesDirty&&(this._transformMatrix=Matrix2D.createTranslation(-this.entity.transform.position.x,-this.entity.transform.position.y),1!=this._zoom&&(t=Matrix2D.createScale(this._zoom,this._zoom),this._transformMatrix=Matrix2D.multiply(this._transformMatrix,t)),t=Matrix2D.createTranslation(this._origin.x,this._origin.y,t),this._transformMatrix=Matrix2D.multiply(this._transformMatrix,t),this._inverseTransformMatrix=Matrix2D.invert(this._transformMatrix),this._areBoundsDirty=!0,this._areMatrixesDirty=!1)},e.prototype.screenToWorldPoint=function(t){return this.updateMatrixes(),Vector2.transform(t,this._inverseTransformMatrix)},e.prototype.worldToScreenPoint=function(t){return this.updateMatrixes(),Vector2.transform(t,this._transformMatrix)},e.prototype.destory=function(){},e}(Component),CameraInset=function(){return function(){}}(),Mesh=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return __extends(e,t),e.prototype.initialize=function(){},e.prototype.setVertPosition=function(t){(!this._verts||this._verts.length!=t.length)&&(this._verts=new Array(t.length));for(var e=0;e>6;0!=(e&t.LONG_MASK)&&i++,this._bits=new Array(i)}return t.prototype.and=function(t){for(var e,i=Math.min(this._bits.length,t._bits.length),n=0;n=0;)this._bits[e]&=~t._bits[e]},t.prototype.cardinality=function(){for(var t=0,e=this._bits.length-1;e>=0;e--){var i=this._bits[e];if(0!=i)if(-1!=i){var n=((i=((i=(i>>1&0x5555555555555400)+(0x5555555555555400&i))>>2&0x3333333333333400)+(0x3333333333333400&i))>>32)+i;t+=((n=((n=(n>>4&252645135)+(252645135&n))>>8&16711935)+(16711935&n))>>16&65535)+(65535&n)}else t+=64}return t},t.prototype.clear=function(t){if(null!=t){var e=t>>6;this.ensure(e),this._bits[e]&=~(1<=this._bits.length){var e=new Number[t+1];e=this._bits.copyWithin(0,0,this._bits.length),this._bits=e}},t.prototype.get=function(t){var e=t>>6;return!(e>=this._bits.length)&&0!=(this._bits[e]&1<=0;)if(0!=(this._bits[e]&t._bits[e]))return!0;return!1},t.prototype.isEmpty=function(){for(var t=this._bits.length-1;t>=0;t--)if(this._bits[t])return!1;return!0},t.prototype.nextSetBit=function(t){for(var e=t>>6,i=1<>6;this.ensure(i),this._bits[i]|=1<0){for(var t=0;t0){t=0;for(var e=this._componentsToAdd.length;t0){var e=this._entitiesToRemove;this._entitiesToRemove=this._tempEntityList,this._tempEntityList=e,this._tempEntityList.forEach(function(e){t._entities.remove(e),e.scene=null,t.scene.entityProcessors.onEntityRemoved(e)}),this._tempEntityList.length=0}if(this._entitiesToAdded.length>0){e=this._entitiesToAdded;this._entitiesToAdded=this._tempEntityList,this._tempEntityList=e,this._tempEntityList.forEach(function(e){t._entities.push(e),e.scene=t.scene,t.scene.entityProcessors.onEntityAdded(e)}),this._tempEntityList.forEach(function(t){return t.onAddedToScene()}),this._tempEntityList.length=0}this._unsortedTags.length>0&&(this._unsortedTags.forEach(function(e){t._entityDict.get(e).sort()}),this._unsortedTags.length=0)},t}(),EntityProcessorList=function(){function t(){this._processors=[]}return t.prototype.add=function(t){this._processors.push(t)},t.prototype.remove=function(t){this._processors.remove(t)},t.prototype.onComponentAdded=function(t){this.notifyEntityChanged(t)},t.prototype.onComponentRemoved=function(t){this.notifyEntityChanged(t)},t.prototype.onEntityAdded=function(t){this.notifyEntityChanged(t)},t.prototype.onEntityRemoved=function(t){this.removeFromProcessors(t)},t.prototype.notifyEntityChanged=function(t){for(var e=0;e=0;e=this.allSet.nextSetBit(e+1))if(!t.componentBits.get(e))return!1;return!(!this.exclusionSet.isEmpty()&&this.exclusionSet.intersects(t.componentBits))&&!(!this.oneSet.isEmpty()&&!this.oneSet.intersects(t.componentBits))},t}(),Time=function(){function t(){}return t.update=function(t){var e=(t-this._lastTime)/1e3;this.deltaTime=e*this.timeScale,this.unscaledDeltaTime=e,this.frameCount++,this._lastTime=t},t.deltaTime=0,t.timeScale=1,t.frameCount=0,t._lastTime=0,t}(),Flags=function(){function t(){}return t.isFlagSet=function(t,e){return 0!=(t&e)},t.isUnshiftedFlagSet=function(t,e){return 0!=(t&(e=1<i?i:t},t.minOf=function(t,e,i,n){return Math.min(t,Math.min(e,Math.min(i,n)))},t.maxOf=function(t,e,i,n){return Math.max(t,Math.max(e,Math.max(i,n)))},t}(),Matrix2D=function(){function t(t,e,i,n,o,r){this.m11=0,this.m12=0,this.m21=0,this.m22=0,this.m31=0,this.m32=0,this.m11=t,this.m12=e,this.m21=i,this.m22=n,this.m31=o,this.m32=r}return Object.defineProperty(t,"identity",{get:function(){return t._identity},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"translation",{get:function(){return new Vector2(this.m31,this.m32)},set:function(t){this.m31=t.x,this.m32=t.y},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotation",{get:function(){return Math.atan2(this.m21,this.m11)},set:function(t){var e=Math.cos(t),i=Math.sin(t);this.m11=e,this.m12=i,this.m21=-i,this.m22=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotationDegrees",{get:function(){return MathHelper.toDegrees(this.rotation)},set:function(t){this.rotation=MathHelper.toRadians(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"scale",{get:function(){return new Vector2(this.m11,this.m22)},set:function(t){this.m11=t.x,this.m12=t.y},enumerable:!0,configurable:!0}),t.add=function(t,e){return t.m11+=e.m11,t.m12+=e.m12,t.m21+=e.m21,t.m22+=e.m22,t.m31+=e.m31,t.m32+=e.m32,t},t.divide=function(t,e){return t.m11/=e.m11,t.m12/=e.m12,t.m21/=e.m21,t.m22/=e.m22,t.m31/=e.m31,t.m32/=e.m32,t},t.multiply=function(t,e){var i=t.m11*e.m11+t.m12*e.m21,n=t.m11*e.m12+t.m12*e.m22,o=t.m21*e.m11+t.m22*e.m21,r=t.m21*e.m12+t.m22*e.m22,s=t.m31*e.m11+t.m32*e.m21+e.m31,a=t.m31*e.m12+t.m32*e.m22+e.m32;return t.m11=i,t.m12=n,t.m21=o,t.m22=r,t.m31=s,t.m32=a,t},t.multiplyTranslation=function(e,i,n){var o=t.createTranslation(i,n);return t.multiply(e,o)},t.prototype.determinant=function(){return this.m11*this.m22-this.m12*this.m21},t.invert=function(e,i){void 0===i&&(i=t.identity);var n=1/e.determinant();return i.m11=e.m22*n,i.m12=-e.m12*n,i.m21=-e.m21*n,i.m22=e.m11*n,i.m31=(e.m32*e.m21-e.m31*e.m22)*n,i.m32=-(e.m32*e.m11-e.m31*e.m12)*n,i},t.createTranslation=function(e,i,n){return void 0===n&&(n=t.identity),n.m11=1,n.m12=0,n.m21=0,n.m22=1,n.m31=e,n.m32=i,n},t.createRotation=function(e,i){i=t.identity;var n=Math.cos(e),o=Math.sin(e);return i.m11=n,i.m12=o,i.m21=-o,i.m22=n,i},t.createScale=function(e,i,n){return void 0===n&&(n=t.identity),n.m11=e,n.m12=0,n.m21=0,n.m22=i,n.m31=0,n.m32=0,n},t._identity=new t(1,0,0,1,0,0),t}(),Rectangle=function(){function t(t,e,i,n){this.x=t,this.y=e,this.width=i,this.height=n}return Object.defineProperty(t.prototype,"left",{get:function(){return this.x},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"right",{get:function(){return this.x+this.width},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"top",{get:function(){return this.y},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"bottom",{get:function(){return this.y+this.height},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"location",{get:function(){return new Vector2(this.x,this.y)},set:function(t){this.x=t.x,this.y=t.y},enumerable:!0,configurable:!0}),t.prototype.intersects=function(t){return t.left1)return!1;var c=(a.x*o.y-a.y*o.x)/s;return!(c<0||c>1)},t.lineToLineIntersection=function(t,e,i,n){var o=new Vector2(0,0),r=Vector2.subtract(e,t),s=Vector2.subtract(n,i),a=r.x*s.y-r.y*s.x;if(0==a)return o;var h=Vector2.subtract(i,t),c=(h.x*s.y-h.y*s.x)/a;if(c<0||c>1)return o;var u=(h.x*r.y-h.y*r.x)/a;return u<0||u>1?o:o=Vector2.add(t,new Vector2(c*r.x,c*r.y))},t.closestPointOnLine=function(t,e,i){var n=Vector2.subtract(e,t),o=Vector2.subtract(i,t),r=Vector2.dot(o,n)/Vector2.dot(n,n);return r=MathHelper.clamp(r,0,1),Vector2.add(t,new Vector2(n.x*r,n.y*r))},t.isCircleToCircle=function(t,e,i,n){return Vector2.distanceSquared(t,i)<(e+n)*(e+n)},t.isCircleToLine=function(t,e,i,n){return Vector2.distanceSquared(t,this.closestPointOnLine(i,n,t))=t&&o.y>=e&&o.x=t+i&&(r|=PointSectors.right),o.y=e+n&&(r|=PointSectors.bottom),r},t}(),Physics=function(){function t(){}return t.overlapCircleAll=function(t,e,i,n){return void 0===n&&(n=-1),this._spatialHash.overlapCircle(t,e,i,n)},t.allLayers=-1,t}(),Shape=function(){return function(){}}(),Circle=function(t){function e(e){var i=t.call(this)||this;return i.radius=e,i._originalRadius=e,i}return __extends(e,t),e.prototype.pointCollidesWithShape=function(t){return ShapeCollisions.pointToCicle(t,this)},e.prototype.collidesWithShape=function(t){if(t instanceof Rect&&t.isUnrotated)return ShapeCollisions.circleToRect(this,t);throw new Error("Collisions of Circle to "+t+" are not supported")},e}(Shape),CollisionResult=function(){return function(){}}(),Polygon=function(t){function e(i,n){var o=t.call(this)||this;return o.isUnrotated=!0,o._areEdgeNormalsDirty=!0,o.setPoints(e.buildSymmertricalPolygon(i,n)),o}return __extends(e,t),e.prototype.setPoints=function(t){this.points=t,this.recalculateCenterAndEdgeNormals(),this._originalPoint=new Vector2[t.length],this._originalPoint=t},e.prototype.recalculateCenterAndEdgeNormals=function(){this._polygonCenter=e.findPolygonCenter(this.points),this._areEdgeNormalsDirty=!0},e.findPolygonCenter=function(t){for(var e=0,i=0,n=0;nt.y!=this.points[n].y>t.y&&t.x<(this.points[n].x-this.points[i].x)*(t.y-this.points[i].y)/(this.points[n].y-this.points[i].y)+this.points[i].x&&(e=!e);return e},e.buildSymmertricalPolygon=function(t,e){for(var i=new Vector2[t],n=0;n=0;e--){for(var i=this._composites[e],n=0;ni.height?e.y=i.height:e.yi.width&&(e.x=i.width)):(e.yi.height-t.radius&&(e.y=2*(i.height-t.radius)-e.y),e.x>i.width-t.radius&&(e.x=2*(i.width-t.radius)-e.x),e.x=0;t--)this._constraints[t].solve()},t.prototype.addParticle=function(t){return this.particles.push(t),t},t.prototype.addConstraint=function(t){return this._constraints.push(t),t.composite=this,t},t.prototype.removeConstraint=function(t){this._constraints.remove(t)},t.prototype.updateParticles=function(t,e){for(var i=0;i=0;t--)this._constraints[t].collidesWithColliders&&this._constraints[t].handleCollisions(this.collidesWithLayers)},t.prototype.debugRender=function(t){if(this.drawConstraints)for(var e=0;e-1?o:Vector2.distance(e.position,i.position),r}return __extends(e,t),e.prototype.setCollidesWithColliders=function(t){return this.collidesWithColliders=t,this},e.prototype.handleCollisions=function(t){Math.min(this._particleOne.position.x,this._particleTwo.position.x),Math.max(this._particleOne.position.x,this._particleTwo.position.x),Math.min(this._particleOne.position.y,this._particleTwo.position.y),Math.max(this._particleOne.position.y,this._particleTwo.position.y)},e.prototype.solve=function(){var t=Vector2.subtract(this._particleOne.position,this._particleTwo.position),e=t.length(),i=(this.restingDistance-e)/e;if(e/this.restingDistance>this.tearSensitivity)this.composite.removeConstraint(this);else{var n=1/this._particleOne.mass,o=n/(n+1/this._particleTwo.mass)*this.stiffness,r=this.stiffness-o;this._particleOne.position=Vector2.add(this._particleOne.position,Vector2.multiply(t,new Vector2(o*i,o*i))),this._particleTwo.position=Vector2.subtract(this._particleTwo.position,Vector2.multiply(t,new Vector2(r*i,r*i)))}},e.prototype.debugRender=function(t){t.lineStyle(1,DebugDefaults.verletConstraintEdge),t.lineTo(this._particleOne.position.x,this._particleOne.position.y),t.lineTo(this._particleTwo.position.x,this._particleTwo.position.y),t.endFill()},e._polygon=new Polygon(2,1),e}(Constraint),Triangulator=function(){function t(){this.triangleIndices=[],this._triPrev=new Array(12),this._triNext=new Array(12)}return t.prototype.triangulate=function(e,i){void 0===i&&(i=!0);var n=e.length;this.initialize(n);for(var o=0,r=0;n>3&&o<500;){o++;var s=!0,a=e[this._triPrev[r]],h=e[r],c=e[this._triNext[r]];if(Vector2Ext.isTriangleCCW(a,h,c)){var u=this._triNext[this._triNext[r]];do{if(t.testPointTriangle(e[u],a,h,c)){s=!1;break}u=this._triNext[u]}while(u!=this._triPrev[r])}else s=!1;s?(this.triangleIndices.push(this._triPrev[r]),this.triangleIndices.push(r),this.triangleIndices.push(this._triNext[r]),this._triNext[this._triPrev[r]]=this._triNext[r],this._triPrev[this._triNext[r]]=this._triPrev[r],n--,r=this._triPrev[r]):r=this._triNext[r]}this.triangleIndices.push(this._triPrev[r]),this.triangleIndices.push(r),this.triangleIndices.push(this._triNext[r]),i||this.triangleIndices.reverse()},t.prototype.initialize=function(t){this.triangleIndices.length=0,this._triNext.length-1}(this,t)},Array.prototype.firstOrDefault=function(t){return function(t,e){var n=t.findIndex(e);return-1==n?null:t[n]}(this,t)},Array.prototype.find=function(t){return function(t,e){return t.firstOrDefault(e)}(this,t)},Array.prototype.where=function(t){return function(t,e){if("function"==typeof t.reduce)return t.reduce(function(n,i,o){return e.call(arguments[2],i,o,t)&&n.push(i),n},[]);for(var n=[],i=0,o=t.length;i=0&&t.splice(n,1)}while(n>=0)}(this,t)},Array.prototype.remove=function(t){return function(t,e){var n=t.findIndex(function(t){return t===e});return n>=0&&(t.splice(n,1),!0)}(this,t)},Array.prototype.removeAt=function(t){return function(t,e){t.splice(e,1)}(this,t)},Array.prototype.removeRange=function(t,e){return function(t,e,n){t.splice(e,n)}(this,t,e)},Array.prototype.select=function(t){return function(t,e){if("function"==typeof t.reduce)return t.reduce(function(n,i,o){return n.push(e.call(arguments[2],i,o,t)),n},[]);for(var n=[],i=0,o=t.length;ir?1:-1}),t}(this,t,e)},Array.prototype.orderByDescending=function(t,e){return function(t,e,n){return t.sort(function(t,i){var o=e(t),r=e(i);return n?-n(o,r):o0;){if("break"===h())break}return o?this.recontructPath(r,e,n):null},t.hasKey=function(t,e){for(var n,i=t.keys();!(n=i.next()).done;)if(JSON.stringify(n.value)==JSON.stringify(e))return!0;return!1},t.getKey=function(t,e){for(var n,i,o=t.keys(),r=t.values();n=o.next(),i=r.next(),!n.done;)if(JSON.stringify(n.value)==JSON.stringify(e))return i.value;return null},t.recontructPath=function(t,e,n){var i=[],o=n;for(i.push(n);o!=e;)o=this.getKey(t,o),i.push(o);return i.reverse(),i},t}(),AStarNode=function(t){function e(e){var n=t.call(this)||this;return n.data=e,n}return __extends(e,t),e}(PriorityQueueNode),AstarGridGraph=function(){function t(t,e){this.dirs=[new Point(1,0),new Point(0,-1),new Point(-1,0),new Point(0,1)],this.walls=[],this.weightedNodes=[],this.defaultWeight=1,this.weightedNodeWeight=5,this._neighbors=new Array(4),this._width=t,this._height=e}return t.prototype.isNodeInBounds=function(t){return 0<=t.x&&t.x0&&this.hasHigherPriority(t,n)?this.cascadeUp(t):this.cascadeDown(t)},t.prototype.cascadeDown=function(t){for(var e,n=t.queueIndex;;){e=t;var i=2*n;if(i>this._numNodes){t.queueIndex=n,this._nodes[n]=t;break}var o=this._nodes[i];this.hasHigherPriority(o,e)&&(e=o);var r=i+1;if(r<=this._numNodes){var s=this._nodes[r];this.hasHigherPriority(s,e)&&(e=s)}if(e==t){t.queueIndex=n,this._nodes[n]=t;break}this._nodes[n]=e;var a=e.queueIndex;e.queueIndex=n,n=a}},t.prototype.cascadeUp=function(t){for(var e=Math.floor(t.queueIndex/2);e>=1;){var n=this._nodes[e];if(this.hasHigherPriority(n,t))break;this.swap(t,n),e=Math.floor(t.queueIndex/2)}},t.prototype.swap=function(t,e){this._nodes[t.queueIndex]=e,this._nodes[e.queueIndex]=t;var n=t.queueIndex;t.queueIndex=e.queueIndex,e.queueIndex=n},t.prototype.hasHigherPriority=function(t,e){return t.priority0;){if("break"===a())break}return o?AStarPathfinder.recontructPath(s,e,n):null},t.hasKey=function(t,e){for(var n,i=t.keys();!(n=i.next()).done;)if(JSON.stringify(n.value)==JSON.stringify(e))return!0;return!1},t}(),UnweightedGraph=function(){function t(){this.edges=new Map}return t.prototype.addEdgesForNode=function(t,e){return this.edges.set(t,e),this},t.prototype.getNeighbors=function(t){return this.edges.get(t)},t}(),Point=function(){return function(t,e){this.x=t,this.y=e}}(),UnweightedGridGraph=function(){function t(e,n,i){void 0===i&&(i=!1),this.walls=[],this._neighbors=new Array(4),this._width=e,this._hegiht=n,this._dirs=i?t.COMPASS_DIRS:t.CARDINAL_DIRS}return t.prototype.isNodeInBounds=function(t){return 0<=t.x&&t.x0;){if("break"===h())break}return o?this.recontructPath(r,e,n):null},t.hasKey=function(t,e){for(var n,i=t.keys();!(n=i.next()).done;)if(JSON.stringify(n.value)==JSON.stringify(e))return!0;return!1},t.getKey=function(t,e){for(var n,i,o=t.keys(),r=t.values();n=o.next(),i=r.next(),!n.done;)if(JSON.stringify(n.value)==JSON.stringify(e))return i.value;return null},t.recontructPath=function(t,e,n){var i=[],o=n;for(i.push(n);o!=e;)o=this.getKey(t,o),i.push(o);return i.reverse(),i},t}(),DebugDefaults=function(){function t(){}return t.verletParticle=14431326,t.verletConstraintEdge=4406838,t}(),Component=function(){function t(){this._enabled=!0,this.updateInterval=1}return Object.defineProperty(t.prototype,"transform",{get:function(){return this.entity.transform},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"enabled",{get:function(){return this.entity?this.entity.enabled&&this._enabled:this._enabled},set:function(t){this.setEnabled(t)},enumerable:!0,configurable:!0}),t.prototype.setEnabled=function(t){return this._enabled!=t&&(this._enabled=t,this._enabled?this.onEnabled():this.onDisabled()),this},t.prototype.onAddedToEntity=function(){},t.prototype.onRemovedFromEntity=function(){},t.prototype.onEnabled=function(){},t.prototype.onDisabled=function(){},t.prototype.onEntityTransformChanged=function(t){},t.prototype.update=function(){},t.prototype.registerComponent=function(){this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this),!1),this.entity.scene.entityProcessors.onComponentAdded(this.entity)},t.prototype.deregisterComponent=function(){this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this)),this.entity.scene.entityProcessors.onComponentRemoved(this.entity)},t}(),Entity=function(){function t(e){this._updateOrder=0,this._enabled=!0,this._tag=0,this.name=e,this.transform=new Transform(this),this.components=new ComponentList(this),this.id=t._idGenerator++,this.componentBits=new BitSet}return Object.defineProperty(t.prototype,"parent",{get:function(){return this.transform.parent},set:function(t){this.transform.setParent(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"position",{get:function(){return this.transform.position},set:function(t){this.transform.setPosition(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localPosition",{get:function(){return this.transform.localPosition},set:function(t){this.transform.setLocalPosition(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotation",{get:function(){return this.transform.rotation},set:function(t){this.transform.setRotation(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotationDegrees",{get:function(){return this.transform.rotationDegrees},set:function(t){this.transform.setRotationDegrees(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localRotation",{get:function(){return this.transform.localRotation},set:function(t){this.transform.setLocalRotation(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localRotationDegrees",{get:function(){return this.transform.localRotationDegrees},set:function(t){this.transform.setLocalRotationDegrees(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"scale",{get:function(){return this.transform.scale},set:function(t){this.transform.setScale(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localScale",{get:function(){return this.transform.scale},set:function(t){this.transform.setScale(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"worldInverseTransform",{get:function(){return this.transform.worldInverseTransform},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localToWorldTransform",{get:function(){return this.transform.localToWorldTransform},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"worldToLocalTransform",{get:function(){return this.transform.worldToLocalTransform},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"isDestoryed",{get:function(){return this._isDestoryed},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"enabled",{get:function(){return this._enabled},set:function(t){this.setEnabled(t)},enumerable:!0,configurable:!0}),t.prototype.setEnabled=function(t){return this._enabled!=t&&(this._enabled=t),this},Object.defineProperty(t.prototype,"tag",{get:function(){return this._tag},set:function(t){this.setTag(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"updateOrder",{get:function(){return this._updateOrder},set:function(t){this.setUpdateOrder(t)},enumerable:!0,configurable:!0}),t.prototype.setUpdateOrder=function(t){if(this._updateOrder!=t)return this._updateOrder=t,this.scene,this},t.prototype.setTag=function(t){return this._tag!=t&&(this.scene&&this.scene.entities.removeFromTagList(this),this._tag=t,this.scene&&this.scene.entities.addToTagList(this)),this},t.prototype.attachToScene=function(t){this.scene=t,t.entities.add(this),this.components.registerAllComponents();for(var e=0;e=0;t--){this.transform.getChild(t).entity.destory()}},t}(),Scene=function(t){function e(e){var n=t.call(this)||this;return e.stage.addChild(n),n._projectionMatrix=new Matrix2D(0,0,0,0,0,0),n.entityProcessors=new EntityProcessorList,n.entities=new EntityList(n),n.addEventListener(egret.Event.ACTIVATE,n.onActive,n),n.addEventListener(egret.Event.DEACTIVATE,n.onDeactive,n),n.addEventListener(egret.Event.ENTER_FRAME,n.update,n),n}return __extends(e,t),e.prototype.createEntity=function(t){var e=new Entity(t);return e.transform.position=new Vector2(0,0),this.addEntity(e)},e.prototype.addEntity=function(t){this.entities.add(t),t.scene=this;for(var e=0;et&&(this._zoom=t),this._maximumZoom=t,this},e.prototype.setZoom=function(t){var e=MathHelper.clamp(t,-1,1);return this._zoom=0==e?1:e<0?MathHelper.map(e,-1,0,this._minimumZoom,1):MathHelper.map(e,0,1,1,this._maximumZoom),this._areMatrixesDirty=!0,this},e.prototype.initialize=function(){},e.prototype.update=function(){},e.prototype.setPosition=function(t){return this.entity.transform.setPosition(t),this},e.prototype.updateMatrixes=function(){var t;this._areMatrixesDirty&&(this._transformMatrix=Matrix2D.createTranslation(-this.entity.transform.position.x,-this.entity.transform.position.y),1!=this._zoom&&(t=Matrix2D.createScale(this._zoom,this._zoom),this._transformMatrix=Matrix2D.multiply(this._transformMatrix,t)),t=Matrix2D.createTranslation(this._origin.x,this._origin.y,t),this._transformMatrix=Matrix2D.multiply(this._transformMatrix,t),this._inverseTransformMatrix=Matrix2D.invert(this._transformMatrix),this._areBoundsDirty=!0,this._areMatrixesDirty=!1)},e.prototype.screenToWorldPoint=function(t){return this.updateMatrixes(),Vector2.transform(t,this._inverseTransformMatrix)},e.prototype.worldToScreenPoint=function(t){return this.updateMatrixes(),Vector2.transform(t,this._transformMatrix)},e.prototype.destory=function(){},e}(Component),CameraInset=function(){return function(){}}(),Mesh=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return __extends(e,t),e.prototype.initialize=function(){},e.prototype.setVertPosition=function(t){(!this._verts||this._verts.length!=t.length)&&(this._verts=new Array(t.length));for(var e=0;e>6;0!=(e&t.LONG_MASK)&&n++,this._bits=new Array(n)}return t.prototype.and=function(t){for(var e,n=Math.min(this._bits.length,t._bits.length),i=0;i=0;)this._bits[e]&=~t._bits[e]},t.prototype.cardinality=function(){for(var t=0,e=this._bits.length-1;e>=0;e--){var n=this._bits[e];if(0!=n)if(-1!=n){var i=((n=((n=(n>>1&0x5555555555555400)+(0x5555555555555400&n))>>2&0x3333333333333400)+(0x3333333333333400&n))>>32)+n;t+=((i=((i=(i>>4&252645135)+(252645135&i))>>8&16711935)+(16711935&i))>>16&65535)+(65535&i)}else t+=64}return t},t.prototype.clear=function(t){if(null!=t){var e=t>>6;this.ensure(e),this._bits[e]&=~(1<=this._bits.length){var e=new Number[t+1];e=this._bits.copyWithin(0,0,this._bits.length),this._bits=e}},t.prototype.get=function(t){var e=t>>6;return!(e>=this._bits.length)&&0!=(this._bits[e]&1<=0;)if(0!=(this._bits[e]&t._bits[e]))return!0;return!1},t.prototype.isEmpty=function(){for(var t=this._bits.length-1;t>=0;t--)if(this._bits[t])return!1;return!0},t.prototype.nextSetBit=function(t){for(var e=t>>6,n=1<>6;this.ensure(n),this._bits[n]|=1<0){for(var t=0;t0){t=0;for(var e=this._componentsToAdd.length;t0){var e=this._entitiesToRemove;this._entitiesToRemove=this._tempEntityList,this._tempEntityList=e,this._tempEntityList.forEach(function(e){t._entities.remove(e),e.scene=null,t.scene.entityProcessors.onEntityRemoved(e)}),this._tempEntityList.length=0}if(this._entitiesToAdded.length>0){e=this._entitiesToAdded;this._entitiesToAdded=this._tempEntityList,this._tempEntityList=e,this._tempEntityList.forEach(function(e){t._entities.push(e),e.scene=t.scene,t.scene.entityProcessors.onEntityAdded(e)}),this._tempEntityList.forEach(function(t){return t.onAddedToScene()}),this._tempEntityList.length=0}this._unsortedTags.length>0&&(this._unsortedTags.forEach(function(e){t._entityDict.get(e).sort()}),this._unsortedTags.length=0)},t}(),EntityProcessorList=function(){function t(){this._processors=[]}return t.prototype.add=function(t){this._processors.push(t)},t.prototype.remove=function(t){this._processors.remove(t)},t.prototype.onComponentAdded=function(t){this.notifyEntityChanged(t)},t.prototype.onComponentRemoved=function(t){this.notifyEntityChanged(t)},t.prototype.onEntityAdded=function(t){this.notifyEntityChanged(t)},t.prototype.onEntityRemoved=function(t){this.removeFromProcessors(t)},t.prototype.notifyEntityChanged=function(t){for(var e=0;e=0;e=this.allSet.nextSetBit(e+1))if(!t.componentBits.get(e))return!1;return!(!this.exclusionSet.isEmpty()&&this.exclusionSet.intersects(t.componentBits))&&!(!this.oneSet.isEmpty()&&!this.oneSet.intersects(t.componentBits))},t}(),Time=function(){function t(){}return t.update=function(t){var e=(t-this._lastTime)/1e3;this.deltaTime=e*this.timeScale,this.unscaledDeltaTime=e,this.frameCount++,this._lastTime=t},t.deltaTime=0,t.timeScale=1,t.frameCount=0,t._lastTime=0,t}(),Flags=function(){function t(){}return t.isFlagSet=function(t,e){return 0!=(t&e)},t.isUnshiftedFlagSet=function(t,e){return 0!=(t&(e=1<n?n:t},t.minOf=function(t,e,n,i){return Math.min(t,Math.min(e,Math.min(n,i)))},t.maxOf=function(t,e,n,i){return Math.max(t,Math.max(e,Math.max(n,i)))},t}(),Matrix2D=function(){function t(t,e,n,i,o,r){this.m11=0,this.m12=0,this.m21=0,this.m22=0,this.m31=0,this.m32=0,this.m11=t,this.m12=e,this.m21=n,this.m22=i,this.m31=o,this.m32=r}return Object.defineProperty(t,"identity",{get:function(){return t._identity},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"translation",{get:function(){return new Vector2(this.m31,this.m32)},set:function(t){this.m31=t.x,this.m32=t.y},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotation",{get:function(){return Math.atan2(this.m21,this.m11)},set:function(t){var e=Math.cos(t),n=Math.sin(t);this.m11=e,this.m12=n,this.m21=-n,this.m22=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotationDegrees",{get:function(){return MathHelper.toDegrees(this.rotation)},set:function(t){this.rotation=MathHelper.toRadians(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"scale",{get:function(){return new Vector2(this.m11,this.m22)},set:function(t){this.m11=t.x,this.m12=t.y},enumerable:!0,configurable:!0}),t.add=function(t,e){return t.m11+=e.m11,t.m12+=e.m12,t.m21+=e.m21,t.m22+=e.m22,t.m31+=e.m31,t.m32+=e.m32,t},t.divide=function(t,e){return t.m11/=e.m11,t.m12/=e.m12,t.m21/=e.m21,t.m22/=e.m22,t.m31/=e.m31,t.m32/=e.m32,t},t.multiply=function(t,e){var n=t.m11*e.m11+t.m12*e.m21,i=t.m11*e.m12+t.m12*e.m22,o=t.m21*e.m11+t.m22*e.m21,r=t.m21*e.m12+t.m22*e.m22,s=t.m31*e.m11+t.m32*e.m21+e.m31,a=t.m31*e.m12+t.m32*e.m22+e.m32;return t.m11=n,t.m12=i,t.m21=o,t.m22=r,t.m31=s,t.m32=a,t},t.multiplyTranslation=function(e,n,i){var o=t.createTranslation(n,i);return t.multiply(e,o)},t.prototype.determinant=function(){return this.m11*this.m22-this.m12*this.m21},t.invert=function(e,n){void 0===n&&(n=t.identity);var i=1/e.determinant();return n.m11=e.m22*i,n.m12=-e.m12*i,n.m21=-e.m21*i,n.m22=e.m11*i,n.m31=(e.m32*e.m21-e.m31*e.m22)*i,n.m32=-(e.m32*e.m11-e.m31*e.m12)*i,n},t.createTranslation=function(e,n,i){return void 0===i&&(i=t.identity),i.m11=1,i.m12=0,i.m21=0,i.m22=1,i.m31=e,i.m32=n,i},t.createRotation=function(e,n){n=t.identity;var i=Math.cos(e),o=Math.sin(e);return n.m11=i,n.m12=o,n.m21=-o,n.m22=i,n},t.createScale=function(e,n,i){return void 0===i&&(i=t.identity),i.m11=e,i.m12=0,i.m21=0,i.m22=n,i.m31=0,i.m32=0,i},t._identity=new t(1,0,0,1,0,0),t}(),Rectangle=function(){function t(t,e,n,i){this.x=t||0,this.y=e||0,this.width=n||0,this.height=i||0}return Object.defineProperty(t.prototype,"left",{get:function(){return this.x},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"right",{get:function(){return this.x+this.width},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"top",{get:function(){return this.y},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"bottom",{get:function(){return this.y+this.height},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"location",{get:function(){return new Vector2(this.x,this.y)},set:function(t){this.x=t.x,this.y=t.y},enumerable:!0,configurable:!0}),t.prototype.intersects=function(t){return t.left1)return!1;var c=(a.x*o.y-a.y*o.x)/s;return!(c<0||c>1)},t.lineToLineIntersection=function(t,e,n,i){var o=new Vector2(0,0),r=Vector2.subtract(e,t),s=Vector2.subtract(i,n),a=r.x*s.y-r.y*s.x;if(0==a)return o;var h=Vector2.subtract(n,t),c=(h.x*s.y-h.y*s.x)/a;if(c<0||c>1)return o;var u=(h.x*r.y-h.y*r.x)/a;return u<0||u>1?o:o=Vector2.add(t,new Vector2(c*r.x,c*r.y))},t.closestPointOnLine=function(t,e,n){var i=Vector2.subtract(e,t),o=Vector2.subtract(n,t),r=Vector2.dot(o,i)/Vector2.dot(i,i);return r=MathHelper.clamp(r,0,1),Vector2.add(t,new Vector2(i.x*r,i.y*r))},t.isCircleToCircle=function(t,e,n,i){return Vector2.distanceSquared(t,n)<(e+i)*(e+i)},t.isCircleToLine=function(t,e,n,i){return Vector2.distanceSquared(t,this.closestPointOnLine(n,i,t))=t&&o.y>=e&&o.x=t+n&&(r|=PointSectors.right),o.y=e+i&&(r|=PointSectors.bottom),r},t}(),Physics=function(){function t(){}return t.overlapCircleAll=function(t,e,n,i){return void 0===i&&(i=-1),this._spatialHash.overlapCircle(t,e,n,i)},t.boxcastBroadphase=function(t,e){return void 0===e&&(e=this.allLayers),this._spatialHash.aabbBroadphase(t,null,e)},t.updateCollider=function(t){this._spatialHash.remove(t),this._spatialHash.register(t)},t.allLayers=-1,t}(),Shape=function(){return function(){}}(),Polygon=function(t){function e(n,i){var o=t.call(this)||this;return o.isUnrotated=!0,o._areEdgeNormalsDirty=!0,o.setPoints(e.buildSymmertricalPolygon(n,i)),o}return __extends(e,t),Object.defineProperty(e.prototype,"edgeNormals",{get:function(){return this._areEdgeNormalsDirty&&this.buildEdgeNormals(),this._edgeNormals},enumerable:!0,configurable:!0}),e.prototype.buildEdgeNormals=function(){var t,e=this.isBox?2:this.points.length;null!=this._edgeNormals&&this._edgeNormals.length==e||(this._edgeNormals=new Vector2[e]);for(var n=0;n=this.points.length?this.points[0]:this.points[n+1];var o=Vector2Ext.perpendicular(i,t);o=Vector2.normalize(o),this._edgeNormals[n]=o}},e.prototype.setPoints=function(t){this.points=t,this.recalculateCenterAndEdgeNormals(),this._originalPoints=new Vector2[t.length],this._originalPoints=t},e.prototype.collidesWithShape=function(t){if(t instanceof e)return ShapeCollisions.polygonToPolygon(this,t)},e.prototype.recalculateCenterAndEdgeNormals=function(){this._polygonCenter=e.findPolygonCenter(this.points),this._areEdgeNormalsDirty=!0},e.findPolygonCenter=function(t){for(var e=0,n=0,i=0;it.y!=this.points[i].y>t.y&&t.x<(this.points[i].x-this.points[n].x)*(t.y-this.points[n].y)/(this.points[i].y-this.points[n].y)+this.points[n].x&&(e=!e);return e},e.buildSymmertricalPolygon=function(t,e){for(var n=new Vector2[t],i=0;it.radius*t.radius&&!s)return n;if(s)Vector2.multiply(n.normal,new Vector2(Math.sqrt(r)-t.radius,Math.sqrt(r)-t.radius));else if(0==r)Vector2.multiply(n.normal,new Vector2(t.radius,t.radius));else Math.sqrt(r)},t.circleToRect=function(t,e){var n=new CollisionResult,i=e.bounds.getClosestPointOnRectangleBorderToPoint(t.position).res;if(e.containsPoint(t.position)){n.point=i;Vector2.add(i,Vector2.subtract(n.normal,new Vector2(t.radius,t.radius)))}return n},t.pointToCicle=function(t,e){var n=new CollisionResult,i=Vector2.distanceSquared(t,e.position),o=1+e.radius;if(i3&&o<500;){o++;var s=!0,a=e[this._triPrev[r]],h=e[r],c=e[this._triNext[r]];if(Vector2Ext.isTriangleCCW(a,h,c)){var u=this._triNext[this._triNext[r]];do{if(t.testPointTriangle(e[u],a,h,c)){s=!1;break}u=this._triNext[u]}while(u!=this._triPrev[r])}else s=!1;s?(this.triangleIndices.push(this._triPrev[r]),this.triangleIndices.push(r),this.triangleIndices.push(this._triNext[r]),this._triNext[this._triPrev[r]]=this._triNext[r],this._triPrev[this._triNext[r]]=this._triPrev[r],i--,r=this._triPrev[r]):r=this._triNext[r]}this.triangleIndices.push(this._triPrev[r]),this.triangleIndices.push(r),this.triangleIndices.push(this._triNext[r]),n||this.triangleIndices.reverse()},t.prototype.initialize=function(t){this.triangleIndices.length=0,this._triNext.length(composite: T): T; - private updateTiming; -} -declare class Composite { - private _constraints; - friction: Vector2; - drawParticles: boolean; - drawConstraints: boolean; - particles: Particle[]; - collidesWithLayers: number; - solveConstraints(): void; - addParticle(particle: Particle): Particle; - addConstraint(constraint: T): T; - removeConstraint(constraint: Constraint): void; - updateParticles(deltaTimeSquared: number, gravity: Vector2): void; - handleConstraintCollisions(): void; - debugRender(graphics: egret.Graphics): void; -} -declare class Box extends Composite { - constructor(center: Vector2, width: number, height: number, borderStiffness?: number, diagonalStiffness?: number); -} -declare abstract class Constraint { - composite: Composite; - collidesWithColliders: boolean; - abstract solve(): any; - handleCollisions(collidesWithLayers: number): void; - debugRender(graphics: egret.Graphics): void; -} -declare class DistanceConstraint extends Constraint { - stiffness: number; - restingDistance: number; - tearSensitivity: number; - private _particleOne; - private _particleTwo; - private static _polygon; - constructor(first: Particle, second: Particle, stiffness: number, distance?: number); - setCollidesWithColliders(collidesWithColliders: boolean): this; - handleCollisions(collidersWithLayers: any): void; - solve(): void; - debugRender(graphics: egret.Graphics): void; -} declare class Triangulator { triangleIndices: number[]; private _triPrev; @@ -757,6 +725,7 @@ declare class Triangulator { declare class Vector2Ext { static isTriangleCCW(a: Vector2, center: Vector2, c: Vector2): boolean; static cross(u: Vector2, v: Vector2): number; + static perpendicular(first: Vector2, second: Vector2): Vector2; } declare class WebGLUtils { static getWebGL(): WebGLRenderingContext; diff --git a/source/bin/framework.js b/source/bin/framework.js index 7e9e9b53..3f081713 100644 --- a/source/bin/framework.js +++ b/source/bin/framework.js @@ -1786,6 +1786,7 @@ var Collider = (function (_super) { function Collider() { var _this = _super !== null && _super.apply(this, arguments) || this; _this.physicsLayer = 1 << 0; + _this._isPositionDirty = true; return _this; } Object.defineProperty(Collider.prototype, "bounds", { @@ -1795,8 +1796,38 @@ var Collider = (function (_super) { enumerable: true, configurable: true }); + Collider.prototype.initialize = function () { + }; return Collider; }(Component)); +var BoxCollider = (function (_super) { + __extends(BoxCollider, _super); + function BoxCollider() { + return _super !== null && _super.apply(this, arguments) || this; + } + Object.defineProperty(BoxCollider.prototype, "width", { + get: function () { + return this.shape.width; + }, + set: function (value) { + this.setWidth(value); + }, + enumerable: true, + configurable: true + }); + BoxCollider.prototype.setWidth = function (width) { + this._colliderRequiresAutoSizing = false; + var box = this.shape; + if (width != box.width) { + box.updateBox(width, box.height); + this._isPositionDirty = true; + if (this.entity && this._isParentEntityAddedToScene) + Physics.updateCollider(this); + } + return this; + }; + return BoxCollider; +}(Collider)); var EntitySystem = (function () { function EntitySystem(matcher) { this._entities = []; @@ -2373,6 +2404,9 @@ var MathHelper = (function () { MathHelper.map = function (value, leftMin, leftMax, rightMin, rightMax) { return rightMin + (value - leftMin) * (rightMax - rightMin) / (leftMax - leftMin); }; + MathHelper.lerp = function (value1, value2, amount) { + return value1 + (value2 - value1) * amount; + }; MathHelper.clamp = function (value, min, max) { if (value < min) return min; @@ -2543,10 +2577,10 @@ var Matrix2D = (function () { }()); var Rectangle = (function () { function Rectangle(x, y, width, height) { - this.x = x; - this.y = y; - this.width = width; - this.height = height; + this.x = x ? x : 0; + this.y = y ? y : 0; + this.width = width ? width : 0; + this.height = height ? height : 0; } Object.defineProperty(Rectangle.prototype, "left", { get: function () { @@ -2686,7 +2720,7 @@ var Vector2 = (function () { this.x = 0; this.y = 0; this.x = x; - this.y = y; + this.y = y ? y : x; } Vector2.add = function (value1, value2) { var result = new Vector2(0, 0); @@ -2733,6 +2767,9 @@ var Vector2 = (function () { var v1 = value1.x - value2.x, v2 = value1.y - value2.y; return (v1 * v1) + (v2 * v2); }; + Vector2.lerp = function (value1, value2, amount) { + return new Vector2(MathHelper.lerp(value1.x, value2.x, amount), MathHelper.lerp(value1.y, value2.y, amount)); + }; Vector2.transform = function (position, matrix) { return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22)); }; @@ -2876,6 +2913,14 @@ var Physics = (function () { if (layerMask === void 0) { layerMask = -1; } return this._spatialHash.overlapCircle(center, randius, results, layerMask); }; + Physics.boxcastBroadphase = function (rect, layerMask) { + if (layerMask === void 0) { layerMask = this.allLayers; } + return this._spatialHash.aabbBroadphase(rect, null, layerMask); + }; + Physics.updateCollider = function (collider) { + this._spatialHash.remove(collider); + this._spatialHash.register(collider); + }; Physics.allLayers = -1; return Physics; }()); @@ -2884,30 +2929,6 @@ var Shape = (function () { } return Shape; }()); -var Circle = (function (_super) { - __extends(Circle, _super); - function Circle(radius) { - var _this = _super.call(this) || this; - _this.radius = radius; - _this._originalRadius = radius; - return _this; - } - Circle.prototype.pointCollidesWithShape = function (point) { - return ShapeCollisions.pointToCicle(point, this); - }; - Circle.prototype.collidesWithShape = function (other) { - if (other instanceof Rect && other.isUnrotated) { - return ShapeCollisions.circleToRect(this, other); - } - throw new Error("Collisions of Circle to " + other + " are not supported"); - }; - return Circle; -}(Shape)); -var CollisionResult = (function () { - function CollisionResult() { - } - return CollisionResult; -}()); var Polygon = (function (_super) { __extends(Polygon, _super); function Polygon(vertCount, radius) { @@ -2917,11 +2938,40 @@ var Polygon = (function (_super) { _this.setPoints(Polygon.buildSymmertricalPolygon(vertCount, radius)); return _this; } + Object.defineProperty(Polygon.prototype, "edgeNormals", { + get: function () { + if (this._areEdgeNormalsDirty) + this.buildEdgeNormals(); + return this._edgeNormals; + }, + enumerable: true, + configurable: true + }); + Polygon.prototype.buildEdgeNormals = function () { + var totalEdges = this.isBox ? 2 : this.points.length; + if (this._edgeNormals == null || this._edgeNormals.length != totalEdges) + this._edgeNormals = new Vector2[totalEdges]; + var p2; + for (var i = 0; i < totalEdges; i++) { + var p1 = this.points[i]; + if (i + 1 >= this.points.length) + p2 = this.points[0]; + else + p2 = this.points[i + 1]; + var perp = Vector2Ext.perpendicular(p1, p2); + perp = Vector2.normalize(perp); + this._edgeNormals[i] = perp; + } + }; Polygon.prototype.setPoints = function (points) { this.points = points; this.recalculateCenterAndEdgeNormals(); - this._originalPoint = new Vector2[points.length]; - this._originalPoint = points; + this._originalPoints = new Vector2[points.length]; + this._originalPoints = points; + }; + Polygon.prototype.collidesWithShape = function (other) { + if (other instanceof Polygon) + return ShapeCollisions.polygonToPolygon(this, other); }; Polygon.prototype.recalculateCenterAndEdgeNormals = function () { this._polygonCenter = Polygon.findPolygonCenter(this.points); @@ -2982,21 +3032,84 @@ var Polygon = (function (_super) { }; return Polygon; }(Shape)); -var Rect = (function (_super) { - __extends(Rect, _super); - function Rect() { +var Box = (function (_super) { + __extends(Box, _super); + function Box() { return _super !== null && _super.apply(this, arguments) || this; } - Rect.prototype.containsPoint = function (point) { + Box.prototype.updateBox = function (width, height) { + this.width = width; + this.height = height; + var halfWidth = width / 2; + var halfHeight = height / 2; + this.points[0] = new Vector2(-halfWidth, -halfHeight); + this.points[1] = new Vector2(halfWidth, -halfHeight); + this.points[2] = new Vector2(halfWidth, halfHeight); + this.points[3] = new Vector2(-halfWidth, halfHeight); + for (var i = 0; i < this.points.length; i++) + this._originalPoints[i] = this.points[i]; + }; + Box.prototype.containsPoint = function (point) { if (this.isUnrotated) return this.bounds.contains(point); return _super.prototype.containsPoint.call(this, point); }; - return Rect; + return Box; }(Polygon)); +var Circle = (function (_super) { + __extends(Circle, _super); + function Circle(radius) { + var _this = _super.call(this) || this; + _this.radius = radius; + _this._originalRadius = radius; + return _this; + } + Circle.prototype.pointCollidesWithShape = function (point) { + return ShapeCollisions.pointToCicle(point, this); + }; + Circle.prototype.collidesWithShape = function (other) { + if (other instanceof Box && other.isUnrotated) { + return ShapeCollisions.circleToRect(this, other); + } + throw new Error("Collisions of Circle to " + other + " are not supported"); + }; + return Circle; +}(Shape)); +var CollisionResult = (function () { + function CollisionResult() { + } + return CollisionResult; +}()); var ShapeCollisions = (function () { function ShapeCollisions() { } + ShapeCollisions.polygonToPolygon = function (first, second) { + var result = new CollisionResult(); + var isIntersecting = true; + }; + ShapeCollisions.circleToPolygon = function (circle, polygon) { + var result = new CollisionResult(); + var poly2Circle = Vector2.subtract(circle.position, polygon.position); + var gpp = Polygon.getClosestPointOnPolygonToPoint(polygon.points, poly2Circle); + var closestPoint = gpp.closestPoint; + var distanceSquared = gpp.distanceSquared; + result.normal = gpp.edgeNormal; + var circleCenterInsidePoly = polygon.containsPoint(circle.position); + if (distanceSquared > circle.radius * circle.radius && !circleCenterInsidePoly) + return result; + var mtv; + if (circleCenterInsidePoly) { + mtv = Vector2.multiply(result.normal, new Vector2(Math.sqrt(distanceSquared) - circle.radius, Math.sqrt(distanceSquared) - circle.radius)); + } + else { + if (distanceSquared == 0) { + mtv = Vector2.multiply(result.normal, new Vector2(circle.radius, circle.radius)); + } + else { + var distance = Math.sqrt(distanceSquared); + } + } + }; ShapeCollisions.circleToRect = function (circle, box) { var result = new CollisionResult(); var closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res; @@ -3062,12 +3175,33 @@ var Particle = (function () { var SpatialHash = (function () { function SpatialHash(cellSize) { if (cellSize === void 0) { cellSize = 100; } + this.gridBounds = new Rectangle(); this._tempHashSet = []; this._cellDict = new NumberDictionary(); this._cellSize = cellSize; this._inverseCellSize = 1 / this._cellSize; this._raycastParser = new RaycastResultParser(); } + SpatialHash.prototype.remove = function (collider) { + var bounds = collider.registeredPhysicsBounds; + var p1 = this.cellCoords(bounds.x, bounds.y); + var p2 = this.cellCoords(bounds.right, bounds.bottom); + for (var x = p1.x; x <= p2.x; x++) { + for (var y = p1.y; y <= p2.y; y++) { + var cell = this.cellAtPosition(x, y); + if (!cell) + console.error("removing Collider [" + collider + "] from a cell that it is not present in"); + else + cell.remove(collider); + } + } + }; + SpatialHash.prototype.register = function (collider) { + var bounds = collider.bounds; + collider.registeredPhysicsBounds = bounds; + var p1 = this.cellCoords(bounds.x, bounds.y); + var p2 = this.cellCoords(bounds.right, bounds.bottom); + }; SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) { var bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2); this._overlapTestCircle.radius = radius; @@ -3150,263 +3284,6 @@ var NumberDictionary = (function () { }; return NumberDictionary; }()); -var VerletWorld = (function () { - function VerletWorld(simulationBounds) { - this.gravity = new Vector2(0, 980); - this.maximumStepIterations = 5; - this.constraintIterations = 3; - this._leftOverTime = 0; - this._iterationSteps = 0; - this._fixedDeltaTime = 1 / 60; - this._composites = []; - this._tempCircle = new Circle(1); - this.simulationBounds = simulationBounds; - this._fixedDeltaTimeSq = Math.pow(this._fixedDeltaTime, 2); - } - VerletWorld.prototype.update = function () { - this.updateTiming(); - for (var iteration = 1; iteration <= this._iterationSteps; iteration++) { - for (var i = this._composites.length - 1; i >= 0; i--) { - var composite = this._composites[i]; - for (var s = 0; s < this.constraintIterations; s++) { - composite.solveConstraints(); - } - composite.updateParticles(this._fixedDeltaTimeSq, this.gravity); - composite.handleConstraintCollisions(); - for (var j = 0; j < composite.particles.length; j++) { - var p = composite.particles[j]; - if (this.simulationBounds) { - this.constrainParticleToBounds(p); - } - if (p.collidesWithColliders) - this.handleCollisions(p, composite.collidesWithLayers); - } - } - } - }; - VerletWorld.prototype.handleCollisions = function (p, collidesWithLayers) { - var collidedCount = Physics.overlapCircleAll(p.position, p.radius, VerletWorld._colliders, collidesWithLayers); - for (var i = 0; i < collidedCount; i++) { - var collider = VerletWorld._colliders[i]; - if (collider.isTrigger) - continue; - if (p.radius < 2) { - var collisionResult = collider.shape.pointCollidesWithShape(p.position); - if (collisionResult) { - p.position = Vector2.subtract(p.position, collisionResult.minimumTranslationVector); - } - } - else { - this._tempCircle.radius = p.radius; - this._tempCircle.position = p.position; - var collisionResult = this._tempCircle.collidesWithShape(collider.shape); - if (collisionResult) { - p.position = Vector2.subtract(p.position, collisionResult.minimumTranslationVector); - } - } - } - }; - VerletWorld.prototype.constrainParticleToBounds = function (p) { - var tempPos = p.position; - var bounds = this.simulationBounds; - if (p.radius == 0) { - if (tempPos.y > bounds.height) { - tempPos.y = bounds.height; - } - else if (tempPos.y < bounds.y) { - tempPos.y = bounds.y; - } - if (tempPos.x < bounds.x) { - tempPos.x = bounds.x; - } - else if (tempPos.x > bounds.width) { - tempPos.x = bounds.width; - } - } - else { - if (tempPos.y < bounds.y + p.radius) { - tempPos.y = 2 * (bounds.y + p.radius) - tempPos.y; - } - if (tempPos.y > bounds.height - p.radius) { - tempPos.y = 2 * (bounds.height - p.radius) - tempPos.y; - } - if (tempPos.x > bounds.width - p.radius) { - tempPos.x = 2 * (bounds.width - p.radius) - tempPos.x; - } - if (tempPos.x < bounds.x + p.radius) - tempPos.x = 2 * (bounds.x + p.radius) - tempPos.x; - } - p.position = tempPos; - }; - VerletWorld.prototype.debugRender = function (displayObject) { - if (!displayObject) - return; - displayObject.stage.removeChildren(); - for (var i = 0; i < this._composites.length; i++) { - var shape = new egret.Shape(); - this._composites[i].debugRender(shape.graphics); - displayObject.stage.addChild(shape); - } - }; - VerletWorld.prototype.addComposite = function (composite) { - this._composites.push(composite); - return composite; - }; - VerletWorld.prototype.updateTiming = function () { - this._leftOverTime += Time.deltaTime; - this._iterationSteps = Math.trunc(this._leftOverTime / this._fixedDeltaTime); - this._leftOverTime -= this._iterationSteps * this._fixedDeltaTime; - this._iterationSteps = Math.min(this._iterationSteps, this.maximumStepIterations); - }; - VerletWorld._colliders = new Array(4); - return VerletWorld; -}()); -var Composite = (function () { - function Composite() { - this._constraints = []; - this.friction = new Vector2(0.98, 1); - this.drawParticles = true; - this.drawConstraints = true; - this.particles = []; - this.collidesWithLayers = Physics.allLayers; - } - Composite.prototype.solveConstraints = function () { - for (var i = this._constraints.length - 1; i >= 0; i--) { - this._constraints[i].solve(); - } - }; - Composite.prototype.addParticle = function (particle) { - this.particles.push(particle); - return particle; - }; - Composite.prototype.addConstraint = function (constraint) { - this._constraints.push(constraint); - constraint.composite = this; - return constraint; - }; - Composite.prototype.removeConstraint = function (constraint) { - this._constraints.remove(constraint); - }; - Composite.prototype.updateParticles = function (deltaTimeSquared, gravity) { - for (var j = 0; j < this.particles.length; j++) { - var p = this.particles[j]; - if (p.isPinned) { - p.position = p.pinnedPosition; - continue; - } - p.applyForce(Vector2.multiply(new Vector2(p.mass, p.mass), gravity)); - var vel = Vector2.multiply(Vector2.subtract(p.position, p.lastPosition), this.friction); - var nextPos = Vector2.add(Vector2.add(p.position, vel), Vector2.multiply(p.acceleration, new Vector2(0.5 * deltaTimeSquared, 0.5 * deltaTimeSquared))); - p.lastPosition = p.position; - p.position = nextPos; - p.acceleration.x = p.acceleration.y = 0; - } - }; - Composite.prototype.handleConstraintCollisions = function () { - for (var i = this._constraints.length - 1; i >= 0; i--) { - if (this._constraints[i].collidesWithColliders) - this._constraints[i].handleCollisions(this.collidesWithLayers); - } - }; - Composite.prototype.debugRender = function (graphics) { - if (this.drawConstraints) { - for (var i = 0; i < this._constraints.length; i++) { - this._constraints[i].debugRender(graphics); - } - } - if (this.drawParticles) { - for (var i = 0; i < this.particles.length; i++) { - var size = this.particles[i].radius ? this.particles[i].radius : 4; - graphics.lineStyle(4, DebugDefaults.verletParticle); - graphics.drawRect(this.particles[i].position.x, this.particles[i].position.y, size, size); - graphics.endFill(); - } - } - }; - return Composite; -}()); -var Box = (function (_super) { - __extends(Box, _super); - function Box(center, width, height, borderStiffness, diagonalStiffness) { - if (borderStiffness === void 0) { borderStiffness = 0.2; } - if (diagonalStiffness === void 0) { diagonalStiffness = 0.5; } - var _this = _super.call(this) || this; - var tl = _this.addParticle(new Particle(Vector2.add(center, new Vector2(-width / 2, -height / 2)))); - var tr = _this.addParticle(new Particle(Vector2.add(center, new Vector2(width / 2, -height / 2)))); - var br = _this.addParticle(new Particle(Vector2.add(center, new Vector2(width / 2, height / 2)))); - var bl = _this.addParticle(new Particle(Vector2.add(center, new Vector2(-width / 2, height / 2)))); - _this.addConstraint(new DistanceConstraint(tl, tr, borderStiffness)); - _this.addConstraint(new DistanceConstraint(tr, br, borderStiffness)); - _this.addConstraint(new DistanceConstraint(br, bl, borderStiffness)); - _this.addConstraint(new DistanceConstraint(bl, tl, borderStiffness)); - _this.addConstraint(new DistanceConstraint(tl, br, diagonalStiffness)).setCollidesWithColliders(false); - _this.addConstraint(new DistanceConstraint(bl, tr, diagonalStiffness)).setCollidesWithColliders(false); - return _this; - } - return Box; -}(Composite)); -var Constraint = (function () { - function Constraint() { - this.collidesWithColliders = true; - } - Constraint.prototype.handleCollisions = function (collidesWithLayers) { - }; - Constraint.prototype.debugRender = function (graphics) { }; - return Constraint; -}()); -var DistanceConstraint = (function (_super) { - __extends(DistanceConstraint, _super); - function DistanceConstraint(first, second, stiffness, distance) { - if (distance === void 0) { distance = -1; } - var _this = _super.call(this) || this; - _this.stiffness = 0; - _this.restingDistance = 0; - _this.tearSensitivity = Number.POSITIVE_INFINITY; - _this._particleOne = first; - _this._particleTwo = second; - _this.stiffness = stiffness; - if (distance > -1) { - _this.restingDistance = distance; - } - else { - _this.restingDistance = Vector2.distance(first.position, second.position); - } - return _this; - } - DistanceConstraint.prototype.setCollidesWithColliders = function (collidesWithColliders) { - this.collidesWithColliders = collidesWithColliders; - return this; - }; - DistanceConstraint.prototype.handleCollisions = function (collidersWithLayers) { - var minX = Math.min(this._particleOne.position.x, this._particleTwo.position.x); - var maxX = Math.max(this._particleOne.position.x, this._particleTwo.position.x); - var minY = Math.min(this._particleOne.position.y, this._particleTwo.position.y); - var maxY = Math.max(this._particleOne.position.y, this._particleTwo.position.y); - }; - DistanceConstraint.prototype.solve = function () { - var diff = Vector2.subtract(this._particleOne.position, this._particleTwo.position); - var d = diff.length(); - var difference = (this.restingDistance - d) / d; - if (d / this.restingDistance > this.tearSensitivity) { - this.composite.removeConstraint(this); - return; - } - var im1 = 1 / this._particleOne.mass; - var im2 = 1 / this._particleTwo.mass; - var scalarP1 = (im1 / (im1 + im2)) * this.stiffness; - var scalarP2 = this.stiffness - scalarP1; - this._particleOne.position = Vector2.add(this._particleOne.position, Vector2.multiply(diff, new Vector2(scalarP1 * difference, scalarP1 * difference))); - this._particleTwo.position = Vector2.subtract(this._particleTwo.position, Vector2.multiply(diff, new Vector2(scalarP2 * difference, scalarP2 * difference))); - }; - DistanceConstraint.prototype.debugRender = function (graphics) { - graphics.lineStyle(1, DebugDefaults.verletConstraintEdge); - graphics.lineTo(this._particleOne.position.x, this._particleOne.position.y); - graphics.lineTo(this._particleTwo.position.x, this._particleTwo.position.y); - graphics.endFill(); - }; - DistanceConstraint._polygon = new Polygon(2, 1); - return DistanceConstraint; -}(Constraint)); var Triangulator = (function () { function Triangulator() { this.triangleIndices = []; @@ -3494,6 +3371,9 @@ var Vector2Ext = (function () { Vector2Ext.cross = function (u, v) { return u.y * v.x - u.x * v.y; }; + Vector2Ext.perpendicular = function (first, second) { + return new Vector2(-1 * (second.y - first.y), second.x - first.x); + }; return Vector2Ext; }()); var WebGLUtils = (function () { diff --git a/source/bin/framework.min.js b/source/bin/framework.min.js index 2c37d571..61ac1686 100644 --- a/source/bin/framework.min.js +++ b/source/bin/framework.min.js @@ -1 +1 @@ -window.framework={},window.__extends=this&&this.__extends||function(){var t=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var i in e)e.hasOwnProperty(i)&&(t[i]=e[i])};return function(e,i){function n(){this.constructor=e}t(e,i),e.prototype=null===i?Object.create(i):(n.prototype=i.prototype,new n)}}(),Array.prototype.findIndex=function(t){return function(t,e){for(var i=0,n=t.length;i-1}(this,t)},Array.prototype.firstOrDefault=function(t){return function(t,e){var i=t.findIndex(e);return-1==i?null:t[i]}(this,t)},Array.prototype.find=function(t){return function(t,e){return t.firstOrDefault(e)}(this,t)},Array.prototype.where=function(t){return function(t,e){if("function"==typeof t.reduce)return t.reduce(function(i,n,o){return e.call(arguments[2],n,o,t)&&i.push(n),i},[]);for(var i=[],n=0,o=t.length;n=0&&t.splice(i,1)}while(i>=0)}(this,t)},Array.prototype.remove=function(t){return function(t,e){var i=t.findIndex(function(t){return t===e});return i>=0&&(t.splice(i,1),!0)}(this,t)},Array.prototype.removeAt=function(t){return function(t,e){t.splice(e,1)}(this,t)},Array.prototype.removeRange=function(t,e){return function(t,e,i){t.splice(e,i)}(this,t,e)},Array.prototype.select=function(t){return function(t,e){if("function"==typeof t.reduce)return t.reduce(function(i,n,o){return i.push(e.call(arguments[2],n,o,t)),i},[]);for(var i=[],n=0,o=t.length;nr?1:-1}),t}(this,t,e)},Array.prototype.orderByDescending=function(t,e){return function(t,e,i){return t.sort(function(t,n){var o=e(t),r=e(n);return i?-i(o,r):o0;){if("break"===h())break}return o?this.recontructPath(r,e,i):null},t.hasKey=function(t,e){for(var i,n=t.keys();!(i=n.next()).done;)if(JSON.stringify(i.value)==JSON.stringify(e))return!0;return!1},t.getKey=function(t,e){for(var i,n,o=t.keys(),r=t.values();i=o.next(),n=r.next(),!i.done;)if(JSON.stringify(i.value)==JSON.stringify(e))return n.value;return null},t.recontructPath=function(t,e,i){var n=[],o=i;for(n.push(i);o!=e;)o=this.getKey(t,o),n.push(o);return n.reverse(),n},t}(),AStarNode=function(t){function e(e){var i=t.call(this)||this;return i.data=e,i}return __extends(e,t),e}(PriorityQueueNode),AstarGridGraph=function(){function t(t,e){this.dirs=[new Point(1,0),new Point(0,-1),new Point(-1,0),new Point(0,1)],this.walls=[],this.weightedNodes=[],this.defaultWeight=1,this.weightedNodeWeight=5,this._neighbors=new Array(4),this._width=t,this._height=e}return t.prototype.isNodeInBounds=function(t){return 0<=t.x&&t.x0&&this.hasHigherPriority(t,i)?this.cascadeUp(t):this.cascadeDown(t)},t.prototype.cascadeDown=function(t){for(var e,i=t.queueIndex;;){e=t;var n=2*i;if(n>this._numNodes){t.queueIndex=i,this._nodes[i]=t;break}var o=this._nodes[n];this.hasHigherPriority(o,e)&&(e=o);var r=n+1;if(r<=this._numNodes){var s=this._nodes[r];this.hasHigherPriority(s,e)&&(e=s)}if(e==t){t.queueIndex=i,this._nodes[i]=t;break}this._nodes[i]=e;var a=e.queueIndex;e.queueIndex=i,i=a}},t.prototype.cascadeUp=function(t){for(var e=Math.floor(t.queueIndex/2);e>=1;){var i=this._nodes[e];if(this.hasHigherPriority(i,t))break;this.swap(t,i),e=Math.floor(t.queueIndex/2)}},t.prototype.swap=function(t,e){this._nodes[t.queueIndex]=e,this._nodes[e.queueIndex]=t;var i=t.queueIndex;t.queueIndex=e.queueIndex,e.queueIndex=i},t.prototype.hasHigherPriority=function(t,e){return t.priority0;){if("break"===a())break}return o?AStarPathfinder.recontructPath(s,e,i):null},t.hasKey=function(t,e){for(var i,n=t.keys();!(i=n.next()).done;)if(JSON.stringify(i.value)==JSON.stringify(e))return!0;return!1},t}(),UnweightedGraph=function(){function t(){this.edges=new Map}return t.prototype.addEdgesForNode=function(t,e){return this.edges.set(t,e),this},t.prototype.getNeighbors=function(t){return this.edges.get(t)},t}(),Point=function(){return function(t,e){this.x=t,this.y=e}}(),UnweightedGridGraph=function(){function t(e,i,n){void 0===n&&(n=!1),this.walls=[],this._neighbors=new Array(4),this._width=e,this._hegiht=i,this._dirs=n?t.COMPASS_DIRS:t.CARDINAL_DIRS}return t.prototype.isNodeInBounds=function(t){return 0<=t.x&&t.x0;){if("break"===h())break}return o?this.recontructPath(r,e,i):null},t.hasKey=function(t,e){for(var i,n=t.keys();!(i=n.next()).done;)if(JSON.stringify(i.value)==JSON.stringify(e))return!0;return!1},t.getKey=function(t,e){for(var i,n,o=t.keys(),r=t.values();i=o.next(),n=r.next(),!i.done;)if(JSON.stringify(i.value)==JSON.stringify(e))return n.value;return null},t.recontructPath=function(t,e,i){var n=[],o=i;for(n.push(i);o!=e;)o=this.getKey(t,o),n.push(o);return n.reverse(),n},t}(),DebugDefaults=function(){function t(){}return t.verletParticle=14431326,t.verletConstraintEdge=4406838,t}(),Component=function(){function t(){this._enabled=!0,this.updateInterval=1}return Object.defineProperty(t.prototype,"transform",{get:function(){return this.entity.transform},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"enabled",{get:function(){return this.entity?this.entity.enabled&&this._enabled:this._enabled},set:function(t){this.setEnabled(t)},enumerable:!0,configurable:!0}),t.prototype.setEnabled=function(t){return this._enabled!=t&&(this._enabled=t,this._enabled?this.onEnabled():this.onDisabled()),this},t.prototype.onAddedToEntity=function(){},t.prototype.onRemovedFromEntity=function(){},t.prototype.onEnabled=function(){},t.prototype.onDisabled=function(){},t.prototype.onEntityTransformChanged=function(t){},t.prototype.update=function(){},t.prototype.registerComponent=function(){this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this),!1),this.entity.scene.entityProcessors.onComponentAdded(this.entity)},t.prototype.deregisterComponent=function(){this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this)),this.entity.scene.entityProcessors.onComponentRemoved(this.entity)},t}(),Entity=function(){function t(e){this._updateOrder=0,this._enabled=!0,this._tag=0,this.name=e,this.transform=new Transform(this),this.components=new ComponentList(this),this.id=t._idGenerator++,this.componentBits=new BitSet}return Object.defineProperty(t.prototype,"parent",{get:function(){return this.transform.parent},set:function(t){this.transform.setParent(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"position",{get:function(){return this.transform.position},set:function(t){this.transform.setPosition(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localPosition",{get:function(){return this.transform.localPosition},set:function(t){this.transform.setLocalPosition(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotation",{get:function(){return this.transform.rotation},set:function(t){this.transform.setRotation(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotationDegrees",{get:function(){return this.transform.rotationDegrees},set:function(t){this.transform.setRotationDegrees(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localRotation",{get:function(){return this.transform.localRotation},set:function(t){this.transform.setLocalRotation(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localRotationDegrees",{get:function(){return this.transform.localRotationDegrees},set:function(t){this.transform.setLocalRotationDegrees(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"scale",{get:function(){return this.transform.scale},set:function(t){this.transform.setScale(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localScale",{get:function(){return this.transform.scale},set:function(t){this.transform.setScale(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"worldInverseTransform",{get:function(){return this.transform.worldInverseTransform},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localToWorldTransform",{get:function(){return this.transform.localToWorldTransform},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"worldToLocalTransform",{get:function(){return this.transform.worldToLocalTransform},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"isDestoryed",{get:function(){return this._isDestoryed},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"enabled",{get:function(){return this._enabled},set:function(t){this.setEnabled(t)},enumerable:!0,configurable:!0}),t.prototype.setEnabled=function(t){return this._enabled!=t&&(this._enabled=t),this},Object.defineProperty(t.prototype,"tag",{get:function(){return this._tag},set:function(t){this.setTag(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"updateOrder",{get:function(){return this._updateOrder},set:function(t){this.setUpdateOrder(t)},enumerable:!0,configurable:!0}),t.prototype.setUpdateOrder=function(t){if(this._updateOrder!=t)return this._updateOrder=t,this.scene,this},t.prototype.setTag=function(t){return this._tag!=t&&(this.scene&&this.scene.entities.removeFromTagList(this),this._tag=t,this.scene&&this.scene.entities.addToTagList(this)),this},t.prototype.attachToScene=function(t){this.scene=t,t.entities.add(this),this.components.registerAllComponents();for(var e=0;e=0;t--){this.transform.getChild(t).entity.destory()}},t}(),Scene=function(t){function e(e){var i=t.call(this)||this;return e.stage.addChild(i),i._projectionMatrix=new Matrix2D(0,0,0,0,0,0),i.entityProcessors=new EntityProcessorList,i.entities=new EntityList(i),i.addEventListener(egret.Event.ACTIVATE,i.onActive,i),i.addEventListener(egret.Event.DEACTIVATE,i.onDeactive,i),i.addEventListener(egret.Event.ENTER_FRAME,i.update,i),i}return __extends(e,t),e.prototype.createEntity=function(t){var e=new Entity(t);return e.transform.position=new Vector2(0,0),this.addEntity(e)},e.prototype.addEntity=function(t){this.entities.add(t),t.scene=this;for(var e=0;et&&(this._zoom=t),this._maximumZoom=t,this},e.prototype.setZoom=function(t){var e=MathHelper.clamp(t,-1,1);return this._zoom=0==e?1:e<0?MathHelper.map(e,-1,0,this._minimumZoom,1):MathHelper.map(e,0,1,1,this._maximumZoom),this._areMatrixesDirty=!0,this},e.prototype.initialize=function(){},e.prototype.update=function(){},e.prototype.setPosition=function(t){return this.entity.transform.setPosition(t),this},e.prototype.updateMatrixes=function(){var t;this._areMatrixesDirty&&(this._transformMatrix=Matrix2D.createTranslation(-this.entity.transform.position.x,-this.entity.transform.position.y),1!=this._zoom&&(t=Matrix2D.createScale(this._zoom,this._zoom),this._transformMatrix=Matrix2D.multiply(this._transformMatrix,t)),t=Matrix2D.createTranslation(this._origin.x,this._origin.y,t),this._transformMatrix=Matrix2D.multiply(this._transformMatrix,t),this._inverseTransformMatrix=Matrix2D.invert(this._transformMatrix),this._areBoundsDirty=!0,this._areMatrixesDirty=!1)},e.prototype.screenToWorldPoint=function(t){return this.updateMatrixes(),Vector2.transform(t,this._inverseTransformMatrix)},e.prototype.worldToScreenPoint=function(t){return this.updateMatrixes(),Vector2.transform(t,this._transformMatrix)},e.prototype.destory=function(){},e}(Component),CameraInset=function(){return function(){}}(),Mesh=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return __extends(e,t),e.prototype.initialize=function(){},e.prototype.setVertPosition=function(t){(!this._verts||this._verts.length!=t.length)&&(this._verts=new Array(t.length));for(var e=0;e>6;0!=(e&t.LONG_MASK)&&i++,this._bits=new Array(i)}return t.prototype.and=function(t){for(var e,i=Math.min(this._bits.length,t._bits.length),n=0;n=0;)this._bits[e]&=~t._bits[e]},t.prototype.cardinality=function(){for(var t=0,e=this._bits.length-1;e>=0;e--){var i=this._bits[e];if(0!=i)if(-1!=i){var n=((i=((i=(i>>1&0x5555555555555400)+(0x5555555555555400&i))>>2&0x3333333333333400)+(0x3333333333333400&i))>>32)+i;t+=((n=((n=(n>>4&252645135)+(252645135&n))>>8&16711935)+(16711935&n))>>16&65535)+(65535&n)}else t+=64}return t},t.prototype.clear=function(t){if(null!=t){var e=t>>6;this.ensure(e),this._bits[e]&=~(1<=this._bits.length){var e=new Number[t+1];e=this._bits.copyWithin(0,0,this._bits.length),this._bits=e}},t.prototype.get=function(t){var e=t>>6;return!(e>=this._bits.length)&&0!=(this._bits[e]&1<=0;)if(0!=(this._bits[e]&t._bits[e]))return!0;return!1},t.prototype.isEmpty=function(){for(var t=this._bits.length-1;t>=0;t--)if(this._bits[t])return!1;return!0},t.prototype.nextSetBit=function(t){for(var e=t>>6,i=1<>6;this.ensure(i),this._bits[i]|=1<0){for(var t=0;t0){t=0;for(var e=this._componentsToAdd.length;t0){var e=this._entitiesToRemove;this._entitiesToRemove=this._tempEntityList,this._tempEntityList=e,this._tempEntityList.forEach(function(e){t._entities.remove(e),e.scene=null,t.scene.entityProcessors.onEntityRemoved(e)}),this._tempEntityList.length=0}if(this._entitiesToAdded.length>0){e=this._entitiesToAdded;this._entitiesToAdded=this._tempEntityList,this._tempEntityList=e,this._tempEntityList.forEach(function(e){t._entities.push(e),e.scene=t.scene,t.scene.entityProcessors.onEntityAdded(e)}),this._tempEntityList.forEach(function(t){return t.onAddedToScene()}),this._tempEntityList.length=0}this._unsortedTags.length>0&&(this._unsortedTags.forEach(function(e){t._entityDict.get(e).sort()}),this._unsortedTags.length=0)},t}(),EntityProcessorList=function(){function t(){this._processors=[]}return t.prototype.add=function(t){this._processors.push(t)},t.prototype.remove=function(t){this._processors.remove(t)},t.prototype.onComponentAdded=function(t){this.notifyEntityChanged(t)},t.prototype.onComponentRemoved=function(t){this.notifyEntityChanged(t)},t.prototype.onEntityAdded=function(t){this.notifyEntityChanged(t)},t.prototype.onEntityRemoved=function(t){this.removeFromProcessors(t)},t.prototype.notifyEntityChanged=function(t){for(var e=0;e=0;e=this.allSet.nextSetBit(e+1))if(!t.componentBits.get(e))return!1;return!(!this.exclusionSet.isEmpty()&&this.exclusionSet.intersects(t.componentBits))&&!(!this.oneSet.isEmpty()&&!this.oneSet.intersects(t.componentBits))},t}(),Time=function(){function t(){}return t.update=function(t){var e=(t-this._lastTime)/1e3;this.deltaTime=e*this.timeScale,this.unscaledDeltaTime=e,this.frameCount++,this._lastTime=t},t.deltaTime=0,t.timeScale=1,t.frameCount=0,t._lastTime=0,t}(),Flags=function(){function t(){}return t.isFlagSet=function(t,e){return 0!=(t&e)},t.isUnshiftedFlagSet=function(t,e){return 0!=(t&(e=1<i?i:t},t.minOf=function(t,e,i,n){return Math.min(t,Math.min(e,Math.min(i,n)))},t.maxOf=function(t,e,i,n){return Math.max(t,Math.max(e,Math.max(i,n)))},t}(),Matrix2D=function(){function t(t,e,i,n,o,r){this.m11=0,this.m12=0,this.m21=0,this.m22=0,this.m31=0,this.m32=0,this.m11=t,this.m12=e,this.m21=i,this.m22=n,this.m31=o,this.m32=r}return Object.defineProperty(t,"identity",{get:function(){return t._identity},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"translation",{get:function(){return new Vector2(this.m31,this.m32)},set:function(t){this.m31=t.x,this.m32=t.y},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotation",{get:function(){return Math.atan2(this.m21,this.m11)},set:function(t){var e=Math.cos(t),i=Math.sin(t);this.m11=e,this.m12=i,this.m21=-i,this.m22=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotationDegrees",{get:function(){return MathHelper.toDegrees(this.rotation)},set:function(t){this.rotation=MathHelper.toRadians(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"scale",{get:function(){return new Vector2(this.m11,this.m22)},set:function(t){this.m11=t.x,this.m12=t.y},enumerable:!0,configurable:!0}),t.add=function(t,e){return t.m11+=e.m11,t.m12+=e.m12,t.m21+=e.m21,t.m22+=e.m22,t.m31+=e.m31,t.m32+=e.m32,t},t.divide=function(t,e){return t.m11/=e.m11,t.m12/=e.m12,t.m21/=e.m21,t.m22/=e.m22,t.m31/=e.m31,t.m32/=e.m32,t},t.multiply=function(t,e){var i=t.m11*e.m11+t.m12*e.m21,n=t.m11*e.m12+t.m12*e.m22,o=t.m21*e.m11+t.m22*e.m21,r=t.m21*e.m12+t.m22*e.m22,s=t.m31*e.m11+t.m32*e.m21+e.m31,a=t.m31*e.m12+t.m32*e.m22+e.m32;return t.m11=i,t.m12=n,t.m21=o,t.m22=r,t.m31=s,t.m32=a,t},t.multiplyTranslation=function(e,i,n){var o=t.createTranslation(i,n);return t.multiply(e,o)},t.prototype.determinant=function(){return this.m11*this.m22-this.m12*this.m21},t.invert=function(e,i){void 0===i&&(i=t.identity);var n=1/e.determinant();return i.m11=e.m22*n,i.m12=-e.m12*n,i.m21=-e.m21*n,i.m22=e.m11*n,i.m31=(e.m32*e.m21-e.m31*e.m22)*n,i.m32=-(e.m32*e.m11-e.m31*e.m12)*n,i},t.createTranslation=function(e,i,n){return void 0===n&&(n=t.identity),n.m11=1,n.m12=0,n.m21=0,n.m22=1,n.m31=e,n.m32=i,n},t.createRotation=function(e,i){i=t.identity;var n=Math.cos(e),o=Math.sin(e);return i.m11=n,i.m12=o,i.m21=-o,i.m22=n,i},t.createScale=function(e,i,n){return void 0===n&&(n=t.identity),n.m11=e,n.m12=0,n.m21=0,n.m22=i,n.m31=0,n.m32=0,n},t._identity=new t(1,0,0,1,0,0),t}(),Rectangle=function(){function t(t,e,i,n){this.x=t,this.y=e,this.width=i,this.height=n}return Object.defineProperty(t.prototype,"left",{get:function(){return this.x},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"right",{get:function(){return this.x+this.width},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"top",{get:function(){return this.y},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"bottom",{get:function(){return this.y+this.height},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"location",{get:function(){return new Vector2(this.x,this.y)},set:function(t){this.x=t.x,this.y=t.y},enumerable:!0,configurable:!0}),t.prototype.intersects=function(t){return t.left1)return!1;var c=(a.x*o.y-a.y*o.x)/s;return!(c<0||c>1)},t.lineToLineIntersection=function(t,e,i,n){var o=new Vector2(0,0),r=Vector2.subtract(e,t),s=Vector2.subtract(n,i),a=r.x*s.y-r.y*s.x;if(0==a)return o;var h=Vector2.subtract(i,t),c=(h.x*s.y-h.y*s.x)/a;if(c<0||c>1)return o;var u=(h.x*r.y-h.y*r.x)/a;return u<0||u>1?o:o=Vector2.add(t,new Vector2(c*r.x,c*r.y))},t.closestPointOnLine=function(t,e,i){var n=Vector2.subtract(e,t),o=Vector2.subtract(i,t),r=Vector2.dot(o,n)/Vector2.dot(n,n);return r=MathHelper.clamp(r,0,1),Vector2.add(t,new Vector2(n.x*r,n.y*r))},t.isCircleToCircle=function(t,e,i,n){return Vector2.distanceSquared(t,i)<(e+n)*(e+n)},t.isCircleToLine=function(t,e,i,n){return Vector2.distanceSquared(t,this.closestPointOnLine(i,n,t))=t&&o.y>=e&&o.x=t+i&&(r|=PointSectors.right),o.y=e+n&&(r|=PointSectors.bottom),r},t}(),Physics=function(){function t(){}return t.overlapCircleAll=function(t,e,i,n){return void 0===n&&(n=-1),this._spatialHash.overlapCircle(t,e,i,n)},t.allLayers=-1,t}(),Shape=function(){return function(){}}(),Circle=function(t){function e(e){var i=t.call(this)||this;return i.radius=e,i._originalRadius=e,i}return __extends(e,t),e.prototype.pointCollidesWithShape=function(t){return ShapeCollisions.pointToCicle(t,this)},e.prototype.collidesWithShape=function(t){if(t instanceof Rect&&t.isUnrotated)return ShapeCollisions.circleToRect(this,t);throw new Error("Collisions of Circle to "+t+" are not supported")},e}(Shape),CollisionResult=function(){return function(){}}(),Polygon=function(t){function e(i,n){var o=t.call(this)||this;return o.isUnrotated=!0,o._areEdgeNormalsDirty=!0,o.setPoints(e.buildSymmertricalPolygon(i,n)),o}return __extends(e,t),e.prototype.setPoints=function(t){this.points=t,this.recalculateCenterAndEdgeNormals(),this._originalPoint=new Vector2[t.length],this._originalPoint=t},e.prototype.recalculateCenterAndEdgeNormals=function(){this._polygonCenter=e.findPolygonCenter(this.points),this._areEdgeNormalsDirty=!0},e.findPolygonCenter=function(t){for(var e=0,i=0,n=0;nt.y!=this.points[n].y>t.y&&t.x<(this.points[n].x-this.points[i].x)*(t.y-this.points[i].y)/(this.points[n].y-this.points[i].y)+this.points[i].x&&(e=!e);return e},e.buildSymmertricalPolygon=function(t,e){for(var i=new Vector2[t],n=0;n=0;e--){for(var i=this._composites[e],n=0;ni.height?e.y=i.height:e.yi.width&&(e.x=i.width)):(e.yi.height-t.radius&&(e.y=2*(i.height-t.radius)-e.y),e.x>i.width-t.radius&&(e.x=2*(i.width-t.radius)-e.x),e.x=0;t--)this._constraints[t].solve()},t.prototype.addParticle=function(t){return this.particles.push(t),t},t.prototype.addConstraint=function(t){return this._constraints.push(t),t.composite=this,t},t.prototype.removeConstraint=function(t){this._constraints.remove(t)},t.prototype.updateParticles=function(t,e){for(var i=0;i=0;t--)this._constraints[t].collidesWithColliders&&this._constraints[t].handleCollisions(this.collidesWithLayers)},t.prototype.debugRender=function(t){if(this.drawConstraints)for(var e=0;e-1?o:Vector2.distance(e.position,i.position),r}return __extends(e,t),e.prototype.setCollidesWithColliders=function(t){return this.collidesWithColliders=t,this},e.prototype.handleCollisions=function(t){Math.min(this._particleOne.position.x,this._particleTwo.position.x),Math.max(this._particleOne.position.x,this._particleTwo.position.x),Math.min(this._particleOne.position.y,this._particleTwo.position.y),Math.max(this._particleOne.position.y,this._particleTwo.position.y)},e.prototype.solve=function(){var t=Vector2.subtract(this._particleOne.position,this._particleTwo.position),e=t.length(),i=(this.restingDistance-e)/e;if(e/this.restingDistance>this.tearSensitivity)this.composite.removeConstraint(this);else{var n=1/this._particleOne.mass,o=n/(n+1/this._particleTwo.mass)*this.stiffness,r=this.stiffness-o;this._particleOne.position=Vector2.add(this._particleOne.position,Vector2.multiply(t,new Vector2(o*i,o*i))),this._particleTwo.position=Vector2.subtract(this._particleTwo.position,Vector2.multiply(t,new Vector2(r*i,r*i)))}},e.prototype.debugRender=function(t){t.lineStyle(1,DebugDefaults.verletConstraintEdge),t.lineTo(this._particleOne.position.x,this._particleOne.position.y),t.lineTo(this._particleTwo.position.x,this._particleTwo.position.y),t.endFill()},e._polygon=new Polygon(2,1),e}(Constraint),Triangulator=function(){function t(){this.triangleIndices=[],this._triPrev=new Array(12),this._triNext=new Array(12)}return t.prototype.triangulate=function(e,i){void 0===i&&(i=!0);var n=e.length;this.initialize(n);for(var o=0,r=0;n>3&&o<500;){o++;var s=!0,a=e[this._triPrev[r]],h=e[r],c=e[this._triNext[r]];if(Vector2Ext.isTriangleCCW(a,h,c)){var u=this._triNext[this._triNext[r]];do{if(t.testPointTriangle(e[u],a,h,c)){s=!1;break}u=this._triNext[u]}while(u!=this._triPrev[r])}else s=!1;s?(this.triangleIndices.push(this._triPrev[r]),this.triangleIndices.push(r),this.triangleIndices.push(this._triNext[r]),this._triNext[this._triPrev[r]]=this._triNext[r],this._triPrev[this._triNext[r]]=this._triPrev[r],n--,r=this._triPrev[r]):r=this._triNext[r]}this.triangleIndices.push(this._triPrev[r]),this.triangleIndices.push(r),this.triangleIndices.push(this._triNext[r]),i||this.triangleIndices.reverse()},t.prototype.initialize=function(t){this.triangleIndices.length=0,this._triNext.length-1}(this,t)},Array.prototype.firstOrDefault=function(t){return function(t,e){var n=t.findIndex(e);return-1==n?null:t[n]}(this,t)},Array.prototype.find=function(t){return function(t,e){return t.firstOrDefault(e)}(this,t)},Array.prototype.where=function(t){return function(t,e){if("function"==typeof t.reduce)return t.reduce(function(n,i,o){return e.call(arguments[2],i,o,t)&&n.push(i),n},[]);for(var n=[],i=0,o=t.length;i=0&&t.splice(n,1)}while(n>=0)}(this,t)},Array.prototype.remove=function(t){return function(t,e){var n=t.findIndex(function(t){return t===e});return n>=0&&(t.splice(n,1),!0)}(this,t)},Array.prototype.removeAt=function(t){return function(t,e){t.splice(e,1)}(this,t)},Array.prototype.removeRange=function(t,e){return function(t,e,n){t.splice(e,n)}(this,t,e)},Array.prototype.select=function(t){return function(t,e){if("function"==typeof t.reduce)return t.reduce(function(n,i,o){return n.push(e.call(arguments[2],i,o,t)),n},[]);for(var n=[],i=0,o=t.length;ir?1:-1}),t}(this,t,e)},Array.prototype.orderByDescending=function(t,e){return function(t,e,n){return t.sort(function(t,i){var o=e(t),r=e(i);return n?-n(o,r):o0;){if("break"===h())break}return o?this.recontructPath(r,e,n):null},t.hasKey=function(t,e){for(var n,i=t.keys();!(n=i.next()).done;)if(JSON.stringify(n.value)==JSON.stringify(e))return!0;return!1},t.getKey=function(t,e){for(var n,i,o=t.keys(),r=t.values();n=o.next(),i=r.next(),!n.done;)if(JSON.stringify(n.value)==JSON.stringify(e))return i.value;return null},t.recontructPath=function(t,e,n){var i=[],o=n;for(i.push(n);o!=e;)o=this.getKey(t,o),i.push(o);return i.reverse(),i},t}(),AStarNode=function(t){function e(e){var n=t.call(this)||this;return n.data=e,n}return __extends(e,t),e}(PriorityQueueNode),AstarGridGraph=function(){function t(t,e){this.dirs=[new Point(1,0),new Point(0,-1),new Point(-1,0),new Point(0,1)],this.walls=[],this.weightedNodes=[],this.defaultWeight=1,this.weightedNodeWeight=5,this._neighbors=new Array(4),this._width=t,this._height=e}return t.prototype.isNodeInBounds=function(t){return 0<=t.x&&t.x0&&this.hasHigherPriority(t,n)?this.cascadeUp(t):this.cascadeDown(t)},t.prototype.cascadeDown=function(t){for(var e,n=t.queueIndex;;){e=t;var i=2*n;if(i>this._numNodes){t.queueIndex=n,this._nodes[n]=t;break}var o=this._nodes[i];this.hasHigherPriority(o,e)&&(e=o);var r=i+1;if(r<=this._numNodes){var s=this._nodes[r];this.hasHigherPriority(s,e)&&(e=s)}if(e==t){t.queueIndex=n,this._nodes[n]=t;break}this._nodes[n]=e;var a=e.queueIndex;e.queueIndex=n,n=a}},t.prototype.cascadeUp=function(t){for(var e=Math.floor(t.queueIndex/2);e>=1;){var n=this._nodes[e];if(this.hasHigherPriority(n,t))break;this.swap(t,n),e=Math.floor(t.queueIndex/2)}},t.prototype.swap=function(t,e){this._nodes[t.queueIndex]=e,this._nodes[e.queueIndex]=t;var n=t.queueIndex;t.queueIndex=e.queueIndex,e.queueIndex=n},t.prototype.hasHigherPriority=function(t,e){return t.priority0;){if("break"===a())break}return o?AStarPathfinder.recontructPath(s,e,n):null},t.hasKey=function(t,e){for(var n,i=t.keys();!(n=i.next()).done;)if(JSON.stringify(n.value)==JSON.stringify(e))return!0;return!1},t}(),UnweightedGraph=function(){function t(){this.edges=new Map}return t.prototype.addEdgesForNode=function(t,e){return this.edges.set(t,e),this},t.prototype.getNeighbors=function(t){return this.edges.get(t)},t}(),Point=function(){return function(t,e){this.x=t,this.y=e}}(),UnweightedGridGraph=function(){function t(e,n,i){void 0===i&&(i=!1),this.walls=[],this._neighbors=new Array(4),this._width=e,this._hegiht=n,this._dirs=i?t.COMPASS_DIRS:t.CARDINAL_DIRS}return t.prototype.isNodeInBounds=function(t){return 0<=t.x&&t.x0;){if("break"===h())break}return o?this.recontructPath(r,e,n):null},t.hasKey=function(t,e){for(var n,i=t.keys();!(n=i.next()).done;)if(JSON.stringify(n.value)==JSON.stringify(e))return!0;return!1},t.getKey=function(t,e){for(var n,i,o=t.keys(),r=t.values();n=o.next(),i=r.next(),!n.done;)if(JSON.stringify(n.value)==JSON.stringify(e))return i.value;return null},t.recontructPath=function(t,e,n){var i=[],o=n;for(i.push(n);o!=e;)o=this.getKey(t,o),i.push(o);return i.reverse(),i},t}(),DebugDefaults=function(){function t(){}return t.verletParticle=14431326,t.verletConstraintEdge=4406838,t}(),Component=function(){function t(){this._enabled=!0,this.updateInterval=1}return Object.defineProperty(t.prototype,"transform",{get:function(){return this.entity.transform},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"enabled",{get:function(){return this.entity?this.entity.enabled&&this._enabled:this._enabled},set:function(t){this.setEnabled(t)},enumerable:!0,configurable:!0}),t.prototype.setEnabled=function(t){return this._enabled!=t&&(this._enabled=t,this._enabled?this.onEnabled():this.onDisabled()),this},t.prototype.onAddedToEntity=function(){},t.prototype.onRemovedFromEntity=function(){},t.prototype.onEnabled=function(){},t.prototype.onDisabled=function(){},t.prototype.onEntityTransformChanged=function(t){},t.prototype.update=function(){},t.prototype.registerComponent=function(){this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this),!1),this.entity.scene.entityProcessors.onComponentAdded(this.entity)},t.prototype.deregisterComponent=function(){this.entity.componentBits.set(ComponentTypeManager.getIndexFor(this)),this.entity.scene.entityProcessors.onComponentRemoved(this.entity)},t}(),Entity=function(){function t(e){this._updateOrder=0,this._enabled=!0,this._tag=0,this.name=e,this.transform=new Transform(this),this.components=new ComponentList(this),this.id=t._idGenerator++,this.componentBits=new BitSet}return Object.defineProperty(t.prototype,"parent",{get:function(){return this.transform.parent},set:function(t){this.transform.setParent(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"position",{get:function(){return this.transform.position},set:function(t){this.transform.setPosition(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localPosition",{get:function(){return this.transform.localPosition},set:function(t){this.transform.setLocalPosition(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotation",{get:function(){return this.transform.rotation},set:function(t){this.transform.setRotation(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotationDegrees",{get:function(){return this.transform.rotationDegrees},set:function(t){this.transform.setRotationDegrees(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localRotation",{get:function(){return this.transform.localRotation},set:function(t){this.transform.setLocalRotation(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localRotationDegrees",{get:function(){return this.transform.localRotationDegrees},set:function(t){this.transform.setLocalRotationDegrees(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"scale",{get:function(){return this.transform.scale},set:function(t){this.transform.setScale(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localScale",{get:function(){return this.transform.scale},set:function(t){this.transform.setScale(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"worldInverseTransform",{get:function(){return this.transform.worldInverseTransform},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"localToWorldTransform",{get:function(){return this.transform.localToWorldTransform},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"worldToLocalTransform",{get:function(){return this.transform.worldToLocalTransform},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"isDestoryed",{get:function(){return this._isDestoryed},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"enabled",{get:function(){return this._enabled},set:function(t){this.setEnabled(t)},enumerable:!0,configurable:!0}),t.prototype.setEnabled=function(t){return this._enabled!=t&&(this._enabled=t),this},Object.defineProperty(t.prototype,"tag",{get:function(){return this._tag},set:function(t){this.setTag(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"updateOrder",{get:function(){return this._updateOrder},set:function(t){this.setUpdateOrder(t)},enumerable:!0,configurable:!0}),t.prototype.setUpdateOrder=function(t){if(this._updateOrder!=t)return this._updateOrder=t,this.scene,this},t.prototype.setTag=function(t){return this._tag!=t&&(this.scene&&this.scene.entities.removeFromTagList(this),this._tag=t,this.scene&&this.scene.entities.addToTagList(this)),this},t.prototype.attachToScene=function(t){this.scene=t,t.entities.add(this),this.components.registerAllComponents();for(var e=0;e=0;t--){this.transform.getChild(t).entity.destory()}},t}(),Scene=function(t){function e(e){var n=t.call(this)||this;return e.stage.addChild(n),n._projectionMatrix=new Matrix2D(0,0,0,0,0,0),n.entityProcessors=new EntityProcessorList,n.entities=new EntityList(n),n.addEventListener(egret.Event.ACTIVATE,n.onActive,n),n.addEventListener(egret.Event.DEACTIVATE,n.onDeactive,n),n.addEventListener(egret.Event.ENTER_FRAME,n.update,n),n}return __extends(e,t),e.prototype.createEntity=function(t){var e=new Entity(t);return e.transform.position=new Vector2(0,0),this.addEntity(e)},e.prototype.addEntity=function(t){this.entities.add(t),t.scene=this;for(var e=0;et&&(this._zoom=t),this._maximumZoom=t,this},e.prototype.setZoom=function(t){var e=MathHelper.clamp(t,-1,1);return this._zoom=0==e?1:e<0?MathHelper.map(e,-1,0,this._minimumZoom,1):MathHelper.map(e,0,1,1,this._maximumZoom),this._areMatrixesDirty=!0,this},e.prototype.initialize=function(){},e.prototype.update=function(){},e.prototype.setPosition=function(t){return this.entity.transform.setPosition(t),this},e.prototype.updateMatrixes=function(){var t;this._areMatrixesDirty&&(this._transformMatrix=Matrix2D.createTranslation(-this.entity.transform.position.x,-this.entity.transform.position.y),1!=this._zoom&&(t=Matrix2D.createScale(this._zoom,this._zoom),this._transformMatrix=Matrix2D.multiply(this._transformMatrix,t)),t=Matrix2D.createTranslation(this._origin.x,this._origin.y,t),this._transformMatrix=Matrix2D.multiply(this._transformMatrix,t),this._inverseTransformMatrix=Matrix2D.invert(this._transformMatrix),this._areBoundsDirty=!0,this._areMatrixesDirty=!1)},e.prototype.screenToWorldPoint=function(t){return this.updateMatrixes(),Vector2.transform(t,this._inverseTransformMatrix)},e.prototype.worldToScreenPoint=function(t){return this.updateMatrixes(),Vector2.transform(t,this._transformMatrix)},e.prototype.destory=function(){},e}(Component),CameraInset=function(){return function(){}}(),Mesh=function(t){function e(){return null!==t&&t.apply(this,arguments)||this}return __extends(e,t),e.prototype.initialize=function(){},e.prototype.setVertPosition=function(t){(!this._verts||this._verts.length!=t.length)&&(this._verts=new Array(t.length));for(var e=0;e>6;0!=(e&t.LONG_MASK)&&n++,this._bits=new Array(n)}return t.prototype.and=function(t){for(var e,n=Math.min(this._bits.length,t._bits.length),i=0;i=0;)this._bits[e]&=~t._bits[e]},t.prototype.cardinality=function(){for(var t=0,e=this._bits.length-1;e>=0;e--){var n=this._bits[e];if(0!=n)if(-1!=n){var i=((n=((n=(n>>1&0x5555555555555400)+(0x5555555555555400&n))>>2&0x3333333333333400)+(0x3333333333333400&n))>>32)+n;t+=((i=((i=(i>>4&252645135)+(252645135&i))>>8&16711935)+(16711935&i))>>16&65535)+(65535&i)}else t+=64}return t},t.prototype.clear=function(t){if(null!=t){var e=t>>6;this.ensure(e),this._bits[e]&=~(1<=this._bits.length){var e=new Number[t+1];e=this._bits.copyWithin(0,0,this._bits.length),this._bits=e}},t.prototype.get=function(t){var e=t>>6;return!(e>=this._bits.length)&&0!=(this._bits[e]&1<=0;)if(0!=(this._bits[e]&t._bits[e]))return!0;return!1},t.prototype.isEmpty=function(){for(var t=this._bits.length-1;t>=0;t--)if(this._bits[t])return!1;return!0},t.prototype.nextSetBit=function(t){for(var e=t>>6,n=1<>6;this.ensure(n),this._bits[n]|=1<0){for(var t=0;t0){t=0;for(var e=this._componentsToAdd.length;t0){var e=this._entitiesToRemove;this._entitiesToRemove=this._tempEntityList,this._tempEntityList=e,this._tempEntityList.forEach(function(e){t._entities.remove(e),e.scene=null,t.scene.entityProcessors.onEntityRemoved(e)}),this._tempEntityList.length=0}if(this._entitiesToAdded.length>0){e=this._entitiesToAdded;this._entitiesToAdded=this._tempEntityList,this._tempEntityList=e,this._tempEntityList.forEach(function(e){t._entities.push(e),e.scene=t.scene,t.scene.entityProcessors.onEntityAdded(e)}),this._tempEntityList.forEach(function(t){return t.onAddedToScene()}),this._tempEntityList.length=0}this._unsortedTags.length>0&&(this._unsortedTags.forEach(function(e){t._entityDict.get(e).sort()}),this._unsortedTags.length=0)},t}(),EntityProcessorList=function(){function t(){this._processors=[]}return t.prototype.add=function(t){this._processors.push(t)},t.prototype.remove=function(t){this._processors.remove(t)},t.prototype.onComponentAdded=function(t){this.notifyEntityChanged(t)},t.prototype.onComponentRemoved=function(t){this.notifyEntityChanged(t)},t.prototype.onEntityAdded=function(t){this.notifyEntityChanged(t)},t.prototype.onEntityRemoved=function(t){this.removeFromProcessors(t)},t.prototype.notifyEntityChanged=function(t){for(var e=0;e=0;e=this.allSet.nextSetBit(e+1))if(!t.componentBits.get(e))return!1;return!(!this.exclusionSet.isEmpty()&&this.exclusionSet.intersects(t.componentBits))&&!(!this.oneSet.isEmpty()&&!this.oneSet.intersects(t.componentBits))},t}(),Time=function(){function t(){}return t.update=function(t){var e=(t-this._lastTime)/1e3;this.deltaTime=e*this.timeScale,this.unscaledDeltaTime=e,this.frameCount++,this._lastTime=t},t.deltaTime=0,t.timeScale=1,t.frameCount=0,t._lastTime=0,t}(),Flags=function(){function t(){}return t.isFlagSet=function(t,e){return 0!=(t&e)},t.isUnshiftedFlagSet=function(t,e){return 0!=(t&(e=1<n?n:t},t.minOf=function(t,e,n,i){return Math.min(t,Math.min(e,Math.min(n,i)))},t.maxOf=function(t,e,n,i){return Math.max(t,Math.max(e,Math.max(n,i)))},t}(),Matrix2D=function(){function t(t,e,n,i,o,r){this.m11=0,this.m12=0,this.m21=0,this.m22=0,this.m31=0,this.m32=0,this.m11=t,this.m12=e,this.m21=n,this.m22=i,this.m31=o,this.m32=r}return Object.defineProperty(t,"identity",{get:function(){return t._identity},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"translation",{get:function(){return new Vector2(this.m31,this.m32)},set:function(t){this.m31=t.x,this.m32=t.y},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotation",{get:function(){return Math.atan2(this.m21,this.m11)},set:function(t){var e=Math.cos(t),n=Math.sin(t);this.m11=e,this.m12=n,this.m21=-n,this.m22=e},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"rotationDegrees",{get:function(){return MathHelper.toDegrees(this.rotation)},set:function(t){this.rotation=MathHelper.toRadians(t)},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"scale",{get:function(){return new Vector2(this.m11,this.m22)},set:function(t){this.m11=t.x,this.m12=t.y},enumerable:!0,configurable:!0}),t.add=function(t,e){return t.m11+=e.m11,t.m12+=e.m12,t.m21+=e.m21,t.m22+=e.m22,t.m31+=e.m31,t.m32+=e.m32,t},t.divide=function(t,e){return t.m11/=e.m11,t.m12/=e.m12,t.m21/=e.m21,t.m22/=e.m22,t.m31/=e.m31,t.m32/=e.m32,t},t.multiply=function(t,e){var n=t.m11*e.m11+t.m12*e.m21,i=t.m11*e.m12+t.m12*e.m22,o=t.m21*e.m11+t.m22*e.m21,r=t.m21*e.m12+t.m22*e.m22,s=t.m31*e.m11+t.m32*e.m21+e.m31,a=t.m31*e.m12+t.m32*e.m22+e.m32;return t.m11=n,t.m12=i,t.m21=o,t.m22=r,t.m31=s,t.m32=a,t},t.multiplyTranslation=function(e,n,i){var o=t.createTranslation(n,i);return t.multiply(e,o)},t.prototype.determinant=function(){return this.m11*this.m22-this.m12*this.m21},t.invert=function(e,n){void 0===n&&(n=t.identity);var i=1/e.determinant();return n.m11=e.m22*i,n.m12=-e.m12*i,n.m21=-e.m21*i,n.m22=e.m11*i,n.m31=(e.m32*e.m21-e.m31*e.m22)*i,n.m32=-(e.m32*e.m11-e.m31*e.m12)*i,n},t.createTranslation=function(e,n,i){return void 0===i&&(i=t.identity),i.m11=1,i.m12=0,i.m21=0,i.m22=1,i.m31=e,i.m32=n,i},t.createRotation=function(e,n){n=t.identity;var i=Math.cos(e),o=Math.sin(e);return n.m11=i,n.m12=o,n.m21=-o,n.m22=i,n},t.createScale=function(e,n,i){return void 0===i&&(i=t.identity),i.m11=e,i.m12=0,i.m21=0,i.m22=n,i.m31=0,i.m32=0,i},t._identity=new t(1,0,0,1,0,0),t}(),Rectangle=function(){function t(t,e,n,i){this.x=t||0,this.y=e||0,this.width=n||0,this.height=i||0}return Object.defineProperty(t.prototype,"left",{get:function(){return this.x},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"right",{get:function(){return this.x+this.width},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"top",{get:function(){return this.y},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"bottom",{get:function(){return this.y+this.height},enumerable:!0,configurable:!0}),Object.defineProperty(t.prototype,"location",{get:function(){return new Vector2(this.x,this.y)},set:function(t){this.x=t.x,this.y=t.y},enumerable:!0,configurable:!0}),t.prototype.intersects=function(t){return t.left1)return!1;var c=(a.x*o.y-a.y*o.x)/s;return!(c<0||c>1)},t.lineToLineIntersection=function(t,e,n,i){var o=new Vector2(0,0),r=Vector2.subtract(e,t),s=Vector2.subtract(i,n),a=r.x*s.y-r.y*s.x;if(0==a)return o;var h=Vector2.subtract(n,t),c=(h.x*s.y-h.y*s.x)/a;if(c<0||c>1)return o;var u=(h.x*r.y-h.y*r.x)/a;return u<0||u>1?o:o=Vector2.add(t,new Vector2(c*r.x,c*r.y))},t.closestPointOnLine=function(t,e,n){var i=Vector2.subtract(e,t),o=Vector2.subtract(n,t),r=Vector2.dot(o,i)/Vector2.dot(i,i);return r=MathHelper.clamp(r,0,1),Vector2.add(t,new Vector2(i.x*r,i.y*r))},t.isCircleToCircle=function(t,e,n,i){return Vector2.distanceSquared(t,n)<(e+i)*(e+i)},t.isCircleToLine=function(t,e,n,i){return Vector2.distanceSquared(t,this.closestPointOnLine(n,i,t))=t&&o.y>=e&&o.x=t+n&&(r|=PointSectors.right),o.y=e+i&&(r|=PointSectors.bottom),r},t}(),Physics=function(){function t(){}return t.overlapCircleAll=function(t,e,n,i){return void 0===i&&(i=-1),this._spatialHash.overlapCircle(t,e,n,i)},t.boxcastBroadphase=function(t,e){return void 0===e&&(e=this.allLayers),this._spatialHash.aabbBroadphase(t,null,e)},t.updateCollider=function(t){this._spatialHash.remove(t),this._spatialHash.register(t)},t.allLayers=-1,t}(),Shape=function(){return function(){}}(),Polygon=function(t){function e(n,i){var o=t.call(this)||this;return o.isUnrotated=!0,o._areEdgeNormalsDirty=!0,o.setPoints(e.buildSymmertricalPolygon(n,i)),o}return __extends(e,t),Object.defineProperty(e.prototype,"edgeNormals",{get:function(){return this._areEdgeNormalsDirty&&this.buildEdgeNormals(),this._edgeNormals},enumerable:!0,configurable:!0}),e.prototype.buildEdgeNormals=function(){var t,e=this.isBox?2:this.points.length;null!=this._edgeNormals&&this._edgeNormals.length==e||(this._edgeNormals=new Vector2[e]);for(var n=0;n=this.points.length?this.points[0]:this.points[n+1];var o=Vector2Ext.perpendicular(i,t);o=Vector2.normalize(o),this._edgeNormals[n]=o}},e.prototype.setPoints=function(t){this.points=t,this.recalculateCenterAndEdgeNormals(),this._originalPoints=new Vector2[t.length],this._originalPoints=t},e.prototype.collidesWithShape=function(t){if(t instanceof e)return ShapeCollisions.polygonToPolygon(this,t)},e.prototype.recalculateCenterAndEdgeNormals=function(){this._polygonCenter=e.findPolygonCenter(this.points),this._areEdgeNormalsDirty=!0},e.findPolygonCenter=function(t){for(var e=0,n=0,i=0;it.y!=this.points[i].y>t.y&&t.x<(this.points[i].x-this.points[n].x)*(t.y-this.points[n].y)/(this.points[i].y-this.points[n].y)+this.points[n].x&&(e=!e);return e},e.buildSymmertricalPolygon=function(t,e){for(var n=new Vector2[t],i=0;it.radius*t.radius&&!s)return n;if(s)Vector2.multiply(n.normal,new Vector2(Math.sqrt(r)-t.radius,Math.sqrt(r)-t.radius));else if(0==r)Vector2.multiply(n.normal,new Vector2(t.radius,t.radius));else Math.sqrt(r)},t.circleToRect=function(t,e){var n=new CollisionResult,i=e.bounds.getClosestPointOnRectangleBorderToPoint(t.position).res;if(e.containsPoint(t.position)){n.point=i;Vector2.add(i,Vector2.subtract(n.normal,new Vector2(t.radius,t.radius)))}return n},t.pointToCicle=function(t,e){var n=new CollisionResult,i=Vector2.distanceSquared(t,e.position),o=1+e.radius;if(i3&&o<500;){o++;var s=!0,a=e[this._triPrev[r]],h=e[r],c=e[this._triNext[r]];if(Vector2Ext.isTriangleCCW(a,h,c)){var u=this._triNext[this._triNext[r]];do{if(t.testPointTriangle(e[u],a,h,c)){s=!1;break}u=this._triNext[u]}while(u!=this._triPrev[r])}else s=!1;s?(this.triangleIndices.push(this._triPrev[r]),this.triangleIndices.push(r),this.triangleIndices.push(this._triNext[r]),this._triNext[this._triPrev[r]]=this._triNext[r],this._triPrev[this._triNext[r]]=this._triPrev[r],i--,r=this._triPrev[r]):r=this._triNext[r]}this.triangleIndices.push(this._triPrev[r]),this.triangleIndices.push(r),this.triangleIndices.push(this._triNext[r]),n||this.triangleIndices.reverse()},t.prototype.initialize=function(t){this.triangleIndices.length=0,this._triNext.length +class BoxCollider extends Collider { + + public get width(){ + return (this.shape as Box).width; + } + + public set width(value: number){ + this.setWidth(value); + } + + public setWidth(width: number): BoxCollider{ + this._colliderRequiresAutoSizing = false; + let box = this.shape as Box; + if (width != box.width){ + box.updateBox(width, box.height); + this._isPositionDirty = true; + if (this.entity && this._isParentEntityAddedToScene) + Physics.updateCollider(this); + } + + return this; + } +} \ No newline at end of file diff --git a/source/src/ECS/Components/Physics/Collider.ts b/source/src/ECS/Components/Physics/Colliders/Collider.ts similarity index 50% rename from source/src/ECS/Components/Physics/Collider.ts rename to source/src/ECS/Components/Physics/Colliders/Collider.ts index 3cf61056..0c2df2d9 100644 --- a/source/src/ECS/Components/Physics/Collider.ts +++ b/source/src/ECS/Components/Physics/Colliders/Collider.ts @@ -2,8 +2,16 @@ abstract class Collider extends Component{ public shape: Shape; public physicsLayer = 1 << 0; public isTrigger: boolean; + public registeredPhysicsBounds: Rectangle; + + protected _isParentEntityAddedToScene; + protected _isPositionDirty = true; + protected _colliderRequiresAutoSizing; public get bounds(): Rectangle { return this.shape.bounds; } + + public initialize() { + } } \ No newline at end of file diff --git a/source/src/Math/Rectangle.ts b/source/src/Math/Rectangle.ts index a0cba2ad..961cf1e1 100644 --- a/source/src/Math/Rectangle.ts +++ b/source/src/Math/Rectangle.ts @@ -32,11 +32,11 @@ class Rectangle { this.y = value.y; } - constructor(x: number, y: number, width: number, height: number) { - this.x = x; - this.y = y; - this.width = width; - this.height = height; + constructor(x?: number, y?: number, width?: number, height?: number) { + this.x = x ? x : 0; + this.y = y ? y : 0; + this.width = width ? width : 0; + this.height = height ? height : 0; } public intersects(value: Rectangle) { diff --git a/source/src/Physics/Physics.ts b/source/src/Physics/Physics.ts index 237332f8..68c4b9e5 100644 --- a/source/src/Physics/Physics.ts +++ b/source/src/Physics/Physics.ts @@ -10,4 +10,9 @@ class Physics { public static boxcastBroadphase(rect: Rectangle, layerMask: number = this.allLayers){ return this._spatialHash.aabbBroadphase(rect, null, layerMask); } + + public static updateCollider(collider: Collider){ + this._spatialHash.remove(collider); + this._spatialHash.register(collider); + } } \ No newline at end of file diff --git a/source/src/Physics/Shapes/Box.ts b/source/src/Physics/Shapes/Box.ts new file mode 100644 index 00000000..c98f35d8 --- /dev/null +++ b/source/src/Physics/Shapes/Box.ts @@ -0,0 +1,28 @@ +/// +class Box extends Polygon { + public width: number; + public height: number; + + public updateBox(width: number, height: number){ + this.width = width; + this.height = height; + + let halfWidth = width / 2; + let halfHeight = height / 2; + + this.points[0] = new Vector2(-halfWidth, -halfHeight); + this.points[1] = new Vector2(halfWidth, -halfHeight); + this.points[2] = new Vector2(halfWidth, halfHeight); + this.points[3] = new Vector2(-halfWidth, halfHeight); + + for (let i = 0; i < this.points.length; i ++) + this._originalPoints[i] = this.points[i]; + } + + public containsPoint(point: Vector2){ + if (this.isUnrotated) + return this.bounds.contains(point); + + return super.containsPoint(point); + } +} \ No newline at end of file diff --git a/source/src/Physics/Shapes/Circle.ts b/source/src/Physics/Shapes/Circle.ts index 133838b1..5a501eff 100644 --- a/source/src/Physics/Shapes/Circle.ts +++ b/source/src/Physics/Shapes/Circle.ts @@ -14,8 +14,8 @@ class Circle extends Shape { } public collidesWithShape(other: Shape): CollisionResult{ - if (other instanceof Rect && (other as Rect).isUnrotated){ - return ShapeCollisions.circleToRect(this, other as Rect); + if (other instanceof Box && (other as Box).isUnrotated){ + return ShapeCollisions.circleToRect(this, other as Box); } throw new Error(`Collisions of Circle to ${other} are not supported`); diff --git a/source/src/Physics/Shapes/Polygon.ts b/source/src/Physics/Shapes/Polygon.ts index b164105c..7f8d4717 100644 --- a/source/src/Physics/Shapes/Polygon.ts +++ b/source/src/Physics/Shapes/Polygon.ts @@ -4,7 +4,7 @@ class Polygon extends Shape { public isUnrotated: boolean = true; private _polygonCenter: Vector2; private _areEdgeNormalsDirty = true; - private _originalPoint: Vector2[]; + protected _originalPoints: Vector2[]; public _edgeNormals: Vector2[]; public get edgeNormals(){ @@ -42,8 +42,8 @@ class Polygon extends Shape { this.points = points; this.recalculateCenterAndEdgeNormals(); - this._originalPoint = new Vector2[points.length]; - this._originalPoint = points; + this._originalPoints = new Vector2[points.length]; + this._originalPoints = points; } public collidesWithShape(other: Shape){ diff --git a/source/src/Physics/Shapes/Rect.ts b/source/src/Physics/Shapes/Rect.ts deleted file mode 100644 index 4c21b59a..00000000 --- a/source/src/Physics/Shapes/Rect.ts +++ /dev/null @@ -1,8 +0,0 @@ -class Rect extends Polygon { - public containsPoint(point: Vector2){ - if (this.isUnrotated) - return this.bounds.contains(point); - - return super.containsPoint(point); - } -} \ No newline at end of file diff --git a/source/src/Physics/Shapes/ShapeCollisions/ShapeCollisions.ts b/source/src/Physics/Shapes/ShapeCollisions/ShapeCollisions.ts index 02918df3..33f7412b 100644 --- a/source/src/Physics/Shapes/ShapeCollisions/ShapeCollisions.ts +++ b/source/src/Physics/Shapes/ShapeCollisions/ShapeCollisions.ts @@ -32,7 +32,7 @@ class ShapeCollisions { } } - public static circleToRect(circle: Circle, box: Rect): CollisionResult{ + public static circleToRect(circle: Circle, box: Box): CollisionResult{ let result = new CollisionResult(); let closestPointOnBounds = box.bounds.getClosestPointOnRectangleBorderToPoint(circle.position).res; diff --git a/source/src/Physics/Verlet/Composites/Box.ts b/source/src/Physics/Verlet/Composites/Box.ts deleted file mode 100644 index 16b0b0fb..00000000 --- a/source/src/Physics/Verlet/Composites/Box.ts +++ /dev/null @@ -1,19 +0,0 @@ -/// -class Box extends Composite{ - constructor(center: Vector2, width: number, height: number, borderStiffness = 0.2, diagonalStiffness = 0.5){ - super(); - - let tl = this.addParticle(new Particle(Vector2.add(center, new Vector2(-width / 2, -height / 2)))); - let tr = this.addParticle(new Particle(Vector2.add(center, new Vector2(width / 2, -height / 2)))); - let br = this.addParticle(new Particle(Vector2.add(center, new Vector2(width / 2, height / 2)))); - let bl = this.addParticle(new Particle(Vector2.add(center, new Vector2(-width / 2, height / 2)))); - - this.addConstraint(new DistanceConstraint(tl, tr, borderStiffness)); - this.addConstraint(new DistanceConstraint(tr, br, borderStiffness)); - this.addConstraint(new DistanceConstraint(br, bl, borderStiffness)); - this.addConstraint(new DistanceConstraint(bl, tl, borderStiffness)); - - this.addConstraint(new DistanceConstraint(tl, br, diagonalStiffness)).setCollidesWithColliders(false); - this.addConstraint(new DistanceConstraint(bl, tr, diagonalStiffness)).setCollidesWithColliders(false); - } -} \ No newline at end of file diff --git a/source/src/Physics/Verlet/Composites/Composite.ts b/source/src/Physics/Verlet/Composites/Composite.ts deleted file mode 100644 index df507d22..00000000 --- a/source/src/Physics/Verlet/Composites/Composite.ts +++ /dev/null @@ -1,76 +0,0 @@ -class Composite { - private _constraints: Constraint[] = []; - - public friction = new Vector2(0.98, 1); - public drawParticles: boolean = true; - public drawConstraints: boolean = true; - public particles: Particle[] = []; - public collidesWithLayers = Physics.allLayers; - - /** - * 处理解决所有约束条件 - */ - public solveConstraints(){ - for (let i = this._constraints.length - 1; i >= 0; i --){ - this._constraints[i].solve(); - } - } - - public addParticle(particle: Particle){ - this.particles.push(particle); - return particle; - } - - public addConstraint(constraint: T): T{ - this._constraints.push(constraint); - constraint.composite = this; - return constraint; - } - - public removeConstraint(constraint: Constraint){ - this._constraints.remove(constraint); - } - - public updateParticles(deltaTimeSquared: number, gravity: Vector2){ - for (let j = 0; j < this.particles.length; j ++){ - let p = this.particles[j]; - if (p.isPinned){ - p.position = p.pinnedPosition; - continue; - } - - p.applyForce(Vector2.multiply(new Vector2(p.mass, p.mass), gravity)); - - let vel = Vector2.multiply(Vector2.subtract(p.position, p.lastPosition), this.friction); - let nextPos = Vector2.add(Vector2.add(p.position, vel), Vector2.multiply(p.acceleration, new Vector2(0.5 * deltaTimeSquared, 0.5 * deltaTimeSquared))); - - p.lastPosition = p.position; - p.position = nextPos; - p.acceleration.x = p.acceleration.y = 0; - } - } - - public handleConstraintCollisions(){ - for (let i = this._constraints.length - 1; i >= 0; i --){ - if (this._constraints[i].collidesWithColliders) - this._constraints[i].handleCollisions(this.collidesWithLayers); - } - } - - public debugRender(graphics: egret.Graphics){ - if (this.drawConstraints){ - for (let i = 0; i < this._constraints.length; i ++){ - this._constraints[i].debugRender(graphics); - } - } - - if (this.drawParticles){ - for (let i = 0; i < this.particles.length; i ++){ - let size = this.particles[i].radius ? this.particles[i].radius : 4; - graphics.lineStyle(4, DebugDefaults.verletParticle); - graphics.drawRect(this.particles[i].position.x, this.particles[i].position.y, size, size); - graphics.endFill(); - } - } - } -} \ No newline at end of file diff --git a/source/src/Physics/Verlet/Constraint/Constraint.ts b/source/src/Physics/Verlet/Constraint/Constraint.ts deleted file mode 100644 index 0130403f..00000000 --- a/source/src/Physics/Verlet/Constraint/Constraint.ts +++ /dev/null @@ -1,12 +0,0 @@ -abstract class Constraint { - public composite: Composite; - public collidesWithColliders = true; - - public abstract solve(); - - public handleCollisions(collidesWithLayers: number){ - - } - - public debugRender(graphics: egret.Graphics) {} -} \ No newline at end of file diff --git a/source/src/Physics/Verlet/Constraint/DistanceConstraint.ts b/source/src/Physics/Verlet/Constraint/DistanceConstraint.ts deleted file mode 100644 index d119d6dc..00000000 --- a/source/src/Physics/Verlet/Constraint/DistanceConstraint.ts +++ /dev/null @@ -1,103 +0,0 @@ -/// -class DistanceConstraint extends Constraint { - public stiffness: number = 0; - public restingDistance: number = 0; - public tearSensitivity = Number.POSITIVE_INFINITY; - public shouldApproximateCollisionWithPoints: boolean; - public totalPointsToApproximateCollisionsWith = 5; - - private _particleOne: Particle; - private _particleTwo: Particle; - private static _polygon = new Polygon(2, 1); - - constructor(first: Particle, second: Particle, stiffness: number, distance = -1){ - super(); - - this._particleOne = first; - this._particleTwo = second; - this.stiffness = stiffness; - - if (distance > -1){ - this.restingDistance = distance; - }else{ - this.restingDistance = Vector2.distance(first.position, second.position); - } - } - - public setCollidesWithColliders(collidesWithColliders: boolean){ - this.collidesWithColliders = collidesWithColliders; - return this; - } - - public handleCollisions(collidersWithLayers){ - if (this.shouldApproximateCollisionWithPoints){ - this.approximateCollisionWithPoints(collidersWithLayers) - return; - } - - let minX = Math.min(this._particleOne.position.x, this._particleTwo.position.x); - let maxX = Math.max(this._particleOne.position.x, this._particleTwo.position.x); - let minY = Math.min(this._particleOne.position.y, this._particleTwo.position.y); - let maxY = Math.max(this._particleOne.position.y, this._particleTwo.position.y); - DistanceConstraint._polygon.bounds = Rectangle.fromMinMax(minX, minY, maxX, maxY); - - let midPoint: Vector2 = this.preparePolygonForCollisionChecks(); - let colliders = Physics.boxcastBroadphase(DistanceConstraint._polygon.bounds, collidersWithLayers); - colliders.forEach(collider => { - - }); - } - - private preparePolygonForCollisionChecks(){ - let midPoint = Vector2.lerp(this._particleOne.position, this._particleTwo.position, 0.5); - DistanceConstraint._polygon.position = midPoint; - DistanceConstraint._polygon.points[0] = Vector2.subtract(this._particleOne.position, DistanceConstraint._polygon.position); - DistanceConstraint._polygon.points[1] = Vector2.subtract(this._particleTwo.position, DistanceConstraint._polygon.position); - DistanceConstraint._polygon.recalculateCenterAndEdgeNormals(); - - return midPoint; - } - - private approximateCollisionWithPoints(collidersWithLayers: number){ - let pt; - for (let j = 0; j < this.totalPointsToApproximateCollisionsWith - 1; j ++){ - pt = Vector2.lerp(this._particleOne.position, this._particleTwo.position, (j + 1) / this.totalPointsToApproximateCollisionsWith); - let collidedCount = Physics.overlapCircleAll(pt, 3, VerletWorld.colliders, collidersWithLayers); - for (let i = 0; i < collidedCount; i ++){ - let collider = VerletWorld.colliders[i]; - let collisionResult: CollisionResult = collider.shape.pointCollidesWithShape(pt); - if (collisionResult){ - this._particleOne.position = Vector2.subtract(this._particleOne.position, collisionResult.minimumTranslationVector); - this._particleTwo.position = Vector2.subtract(this._particleTwo.position, collisionResult.minimumTranslationVector); - } - } - } - } - - public solve() { - let diff = Vector2.subtract(this._particleOne.position, this._particleTwo.position); - let d = diff.length(); - - let difference = (this.restingDistance - d) / d; - - if (d / this.restingDistance > this.tearSensitivity){ - this.composite.removeConstraint(this); - return; - } - - let im1 = 1 / this._particleOne.mass; - let im2 = 1 / this._particleTwo.mass; - let scalarP1 = (im1 / (im1 + im2)) * this.stiffness; - let scalarP2 = this.stiffness - scalarP1; - - this._particleOne.position = Vector2.add(this._particleOne.position, Vector2.multiply(diff, new Vector2(scalarP1 * difference, scalarP1 * difference))); - this._particleTwo.position = Vector2.subtract(this._particleTwo.position, Vector2.multiply(diff, new Vector2(scalarP2 * difference, scalarP2 * difference))) - } - - public debugRender(graphics: egret.Graphics){ - graphics.lineStyle(1, DebugDefaults.verletConstraintEdge); - graphics.lineTo(this._particleOne.position.x, this._particleOne.position.y); - graphics.lineTo(this._particleTwo.position.x, this._particleTwo.position.y); - graphics.endFill(); - } -} \ No newline at end of file diff --git a/source/src/Physics/Verlet/SpatialHash.ts b/source/src/Physics/Verlet/SpatialHash.ts index cda2a97e..bb2f2071 100644 --- a/source/src/Physics/Verlet/SpatialHash.ts +++ b/source/src/Physics/Verlet/SpatialHash.ts @@ -1,4 +1,6 @@ class SpatialHash { + public gridBounds: Rectangle = new Rectangle(); + private _raycastParser: RaycastResultParser; private _cellSize: number; private _inverseCellSize: number; @@ -12,6 +14,29 @@ class SpatialHash { this._raycastParser = new RaycastResultParser(); } + public remove(collider: Collider){ + let bounds = collider.registeredPhysicsBounds; + let p1 = this.cellCoords(bounds.x, bounds.y); + let p2 = this.cellCoords(bounds.right, bounds.bottom); + + for (let x = p1.x; x <= p2.x; x ++){ + for (let y = p1.y; y <= p2.y; y ++){ + let cell = this.cellAtPosition(x, y); + if (!cell) + console.error(`removing Collider [${collider}] from a cell that it is not present in`); + else + cell.remove(collider); + } + } + } + + public register(collider: Collider){ + let bounds = collider.bounds; + collider.registeredPhysicsBounds = bounds; + let p1 = this.cellCoords(bounds.x, bounds.y); + let p2 = this.cellCoords(bounds.right, bounds.bottom); + } + public overlapCircle(circleCenter: Vector2, radius: number, results: Collider[], layerMask){ let bounds = new Rectangle(circleCenter.x - radius, circleCenter.y - radius, radius * 2, radius * 2); diff --git a/source/src/Physics/Verlet/VerletWorld.ts b/source/src/Physics/Verlet/VerletWorld.ts deleted file mode 100644 index 90d62291..00000000 --- a/source/src/Physics/Verlet/VerletWorld.ts +++ /dev/null @@ -1,135 +0,0 @@ -/** - * 基于verlet进行改造的物理引擎 ts重写 - * https://github.com/subprotocol/verlet-js - */ -class VerletWorld { - public gravity: Vector2 = new Vector2(0, 980); - public maximumStepIterations = 5; - public constraintIterations = 3; - public simulationBounds: Rectangle; - - private _leftOverTime: number = 0; - private _iterationSteps: number = 0; - private _fixedDeltaTime = 1 / 60; - private _composites: Composite[] = []; - private _fixedDeltaTimeSq: number; - - public static colliders: Collider[] = new Array(4); - private _tempCircle: Circle = new Circle(1); - - constructor(simulationBounds?: Rectangle){ - this.simulationBounds = simulationBounds; - this._fixedDeltaTimeSq = Math.pow(this._fixedDeltaTime, 2); - } - - public update(){ - this.updateTiming(); - - for (let iteration = 1; iteration <= this._iterationSteps; iteration ++){ - for (let i = this._composites.length - 1; i >= 0; i --){ - let composite = this._composites[i]; - - for (let s = 0; s < this.constraintIterations; s++){ - composite.solveConstraints(); - } - - composite.updateParticles(this._fixedDeltaTimeSq, this.gravity); - - composite.handleConstraintCollisions(); - - for (let j = 0; j < composite.particles.length; j ++){ - let p = composite.particles[j]; - - if (this.simulationBounds){ - this.constrainParticleToBounds(p); - } - - if (p.collidesWithColliders) - this.handleCollisions(p, composite.collidesWithLayers); - } - } - } - } - - private handleCollisions(p: Particle, collidesWithLayers: number){ - let collidedCount = Physics.overlapCircleAll(p.position, p.radius, VerletWorld.colliders, collidesWithLayers); - for (let i = 0; i < collidedCount; i ++){ - let collider = VerletWorld.colliders[i]; - if (collider.isTrigger) - continue; - - if (p.radius < 2){ - let collisionResult = collider.shape.pointCollidesWithShape(p.position); - if (collisionResult){ - p.position = Vector2.subtract(p.position, collisionResult.minimumTranslationVector); - } - }else{ - this._tempCircle.radius = p.radius; - this._tempCircle.position = p.position; - - let collisionResult = this._tempCircle.collidesWithShape(collider.shape); - if (collisionResult){ - p.position = Vector2.subtract(p.position, collisionResult.minimumTranslationVector); - } - } - } - } - - private constrainParticleToBounds(p: Particle){ - let tempPos = p.position; - let bounds = this.simulationBounds; - - if (p.radius == 0){ - if (tempPos.y > bounds.height){ - tempPos.y = bounds.height; - } else if(tempPos.y < bounds.y){ - tempPos.y = bounds.y; - } - - if (tempPos.x < bounds.x){ - tempPos.x = bounds.x; - }else if(tempPos.x > bounds.width){ - tempPos.x = bounds.width; - } - }else{ - if (tempPos.y < bounds.y + p.radius){ - tempPos.y = 2 * (bounds.y + p.radius) - tempPos.y; - } - if (tempPos.y > bounds.height - p.radius){ - tempPos.y = 2 * (bounds.height - p.radius) - tempPos.y; - } - if (tempPos.x > bounds.width - p.radius){ - tempPos.x = 2 * (bounds.width - p.radius) - tempPos.x; - } - if (tempPos.x < bounds.x + p.radius) - tempPos.x = 2 * (bounds.x + p.radius) - tempPos.x; - } - - p.position = tempPos; - } - - public debugRender(displayObject: egret.DisplayObject){ - if (!displayObject) - return; - - displayObject.stage.removeChildren(); - for (let i = 0; i < this._composites.length; i ++){ - let shape = new egret.Shape(); - this._composites[i].debugRender(shape.graphics); - displayObject.stage.addChild(shape); - } - } - - public addComposite(composite: T): T{ - this._composites.push(composite); - return composite; - } - - private updateTiming(){ - this._leftOverTime += Time.deltaTime; - this._iterationSteps = Math.trunc(this._leftOverTime / this._fixedDeltaTime); - this._leftOverTime -= this._iterationSteps * this._fixedDeltaTime; - - this._iterationSteps = Math.min(this._iterationSteps, this.maximumStepIterations); - } -} \ No newline at end of file