From 2eec9a82f964943d61c96bcfd2884b3a5ba0178d Mon Sep 17 00:00:00 2001 From: YHH <359807859@qq.com> Date: Thu, 11 Jun 2020 00:03:26 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9Ebox=E5=A4=8D=E5=90=88?= =?UTF-8?q?=E4=BD=93=20=E4=BF=AE=E5=A4=8Dvector2=E8=BF=90=E7=AE=97?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- demo/libs/framework/framework.d.ts | 85 ++++- demo/libs/framework/framework.js | 342 ++++++++++++++++-- demo/libs/framework/framework.min.js | 2 +- demo/manifest.json | 3 +- demo/src/Main.ts | 1 + demo/src/game/MainScene.ts | 2 +- demo/src/game/VerletDemo.ts | 27 ++ source/bin/framework.d.ts | 85 ++++- source/bin/framework.js | 342 ++++++++++++++++-- source/bin/framework.min.js | 2 +- source/src/Debug/DebugDefaults.ts | 4 + .../src/ECS/Components/RenderableComponent.ts | 18 +- source/src/ECS/Transform.ts | 2 +- source/src/ECS/Utils/ComponentList.ts | 5 + source/src/ECS/Utils/Time.ts | 4 +- source/src/Math/Vector2.ts | 44 +-- source/src/Physics/Collision.ts | 2 +- source/src/Physics/Physics.ts | 7 + source/src/Physics/Verlet/Composites/Box.ts | 19 + .../Physics/Verlet/Composites/Composite.ts | 67 ++++ .../Physics/Verlet/Constraint/Constraint.ts | 8 + .../Verlet/Constraint/DistanceConstraint.ts | 54 +++ .../src/{Utils => Physics}/Verlet/Particle.ts | 34 +- source/src/Physics/Verlet/SpatialHash.ts | 7 + source/src/Physics/Verlet/VerletWorld.ts | 113 ++++++ .../src/Utils/Verlet/Composites/Composite.ts | 25 -- .../src/Utils/Verlet/Constraint/Constraint.ts | 5 - source/src/Utils/Verlet/VerletWorld.ts | 45 --- 28 files changed, 1154 insertions(+), 200 deletions(-) create mode 100644 demo/src/game/VerletDemo.ts create mode 100644 source/src/Debug/DebugDefaults.ts create mode 100644 source/src/Physics/Physics.ts create mode 100644 source/src/Physics/Verlet/Composites/Box.ts create mode 100644 source/src/Physics/Verlet/Composites/Composite.ts create mode 100644 source/src/Physics/Verlet/Constraint/Constraint.ts create mode 100644 source/src/Physics/Verlet/Constraint/DistanceConstraint.ts rename source/src/{Utils => Physics}/Verlet/Particle.ts (62%) create mode 100644 source/src/Physics/Verlet/SpatialHash.ts create mode 100644 source/src/Physics/Verlet/VerletWorld.ts delete mode 100644 source/src/Utils/Verlet/Composites/Composite.ts delete mode 100644 source/src/Utils/Verlet/Constraint/Constraint.ts delete mode 100644 source/src/Utils/Verlet/VerletWorld.ts diff --git a/demo/libs/framework/framework.d.ts b/demo/libs/framework/framework.d.ts index 58cc02a0..091f1ea6 100644 --- a/demo/libs/framework/framework.d.ts +++ b/demo/libs/framework/framework.d.ts @@ -135,6 +135,10 @@ declare class WeightedPathfinder { private static getKey; static recontructPath(cameFrom: Map, start: T, goal: T): T[]; } +declare class DebugDefaults { + static verletParticle: number; + static verletConstraintEdge: number; +} declare abstract class Component { entity: Entity; private _enabled; @@ -358,6 +362,9 @@ declare abstract class RenderableComponent extends Component { readonly height: number; isVisible: boolean; readonly bounds: Rectangle; + protected getWidth(): number; + protected getHeight(): number; + protected getBounds(): Rectangle; protected onBecameVisible(): void; protected onBecameInvisible(): void; isVisibleFromCamera(camera: Camera): boolean; @@ -484,6 +491,7 @@ declare class Time { static unscaledDeltaTime: any; static deltaTime: number; static timeScale: number; + static frameCount: number; private static _lastTime; static update(currentTime: number): void; } @@ -538,19 +546,17 @@ declare class Rectangle { declare class Vector2 { x: number; y: number; - private static readonly zeroVector; - private static readonly unitVector; - static readonly One: Vector2; - static readonly Zero: Vector2; 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; static subtract(value1: Vector2, value2: Vector2): Vector2; normalize(): void; + length(): number; static dot(value1: Vector2, value2: Vector2): number; static distanceSquared(value1: Vector2, value2: Vector2): number; static transform(position: Vector2, matrix: Matrix2D): Vector2; + static distance(value1: Vector2, value2: Vector2): number; } declare enum PointSectors { center = 0, @@ -575,6 +581,77 @@ declare class Collisions { static isRectToPoint(rX: number, rY: number, rW: number, rH: number, point: Vector2): boolean; static getSector(rX: number, rY: number, rW: number, rH: number, point: Vector2): PointSectors; } +declare class Physics { + private static _spatialHash; + static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number; +} +declare class Particle { + position: Vector2; + lastPosition: Vector2; + isPinned: boolean; + pinnedPosition: any; + acceleration: Vector2; + mass: number; + radius: number; + collidesWithColliders: boolean; + constructor(position: Vector2); + applyForce(force: Vector2): void; +} +declare class SpatialHash { + overlapCircle(circleCenter: Vector2, radius: number, results: any[], layerMask: any): number; +} +declare class VerletWorld { + gravity: Vector2; + maximumStepIterations: number; + constraintIterations: number; + simulationBounds: Rectangle; + private _leftOverTime; + private _iterationSteps; + private _fixedDeltaTime; + private _composites; + private _fixedDeltaTimeSq; + private static _colliders; + 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[]; + solveConstraints(): void; + addParticle(particle: Particle): Particle; + addConstraint(constraint: T): T; + removeConstraint(constraint: Constraint): void; + updateParticles(deltaTimeSquared: number, gravity: Vector2): 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; + debugRender(graphics: egret.Graphics): void; +} +declare class DistanceConstraint extends Constraint { + stiffness: number; + restingDistance: number; + tearSensitivity: number; + private _particleOne; + private _particleTwo; + constructor(first: Particle, second: Particle, stiffness: number, distance?: number); + setCollidesWithColliders(collidesWithColliders: boolean): this; + solve(): void; + debugRender(graphics: egret.Graphics): void; +} declare class Triangulator { triangleIndices: number[]; private _triPrev; diff --git a/demo/libs/framework/framework.js b/demo/libs/framework/framework.js index 04b44875..d6d939c3 100644 --- a/demo/libs/framework/framework.js +++ b/demo/libs/framework/framework.js @@ -730,6 +730,13 @@ var WeightedPathfinder = (function () { }; return WeightedPathfinder; }()); +var DebugDefaults = (function () { + function DebugDefaults() { + } + DebugDefaults.verletParticle = 0xDC345E; + DebugDefaults.verletConstraintEdge = 0x433E36; + return DebugDefaults; +}()); var Component = (function () { function Component() { this._enabled = true; @@ -1171,7 +1178,7 @@ var Transform = (function () { this._worldInverseTransform = Matrix2D.identity; this._rotation = 0; this.entity = entity; - this._scale = this._localScale = Vector2.One; + this._scale = this._localScale = new Vector2(0, 0); this._children = []; } Object.defineProperty(Transform.prototype, "childCount", { @@ -1695,14 +1702,14 @@ var RenderableComponent = (function (_super) { } Object.defineProperty(RenderableComponent.prototype, "width", { get: function () { - return this.bounds.width; + return this.getWidth(); }, enumerable: true, configurable: true }); Object.defineProperty(RenderableComponent.prototype, "height", { get: function () { - return this.bounds.height; + return this.getHeight(); }, enumerable: true, configurable: true @@ -1723,15 +1730,24 @@ var RenderableComponent = (function (_super) { }); Object.defineProperty(RenderableComponent.prototype, "bounds", { get: function () { - if (this._areBoundsDirty) { - this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, Vector2.Zero, this.entity.transform.scale, this.entity.transform.rotation, this.width, this.height); - this._areBoundsDirty = false; - } - return this._bounds; + return this.getBounds(); }, enumerable: true, configurable: true }); + RenderableComponent.prototype.getWidth = function () { + return this.bounds.width; + }; + RenderableComponent.prototype.getHeight = function () { + return this.bounds.height; + }; + RenderableComponent.prototype.getBounds = function () { + if (this._areBoundsDirty) { + this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, new Vector2(0, 0), this.entity.transform.scale, this.entity.transform.rotation, this.width, this.height); + this._areBoundsDirty = false; + } + return this._bounds; + }; RenderableComponent.prototype.onBecameVisible = function () { }; RenderableComponent.prototype.onBecameInvisible = function () { }; RenderableComponent.prototype.isVisibleFromCamera = function (camera) { @@ -2058,6 +2074,11 @@ var ComponentList = (function () { }; ComponentList.prototype.update = function () { this.updateLists(); + for (var i = 0; i < this._components.length; i++) { + var component = this._components[i]; + if (component.enabled && (component.updateInterval == 1 || Time.frameCount % component.updateInterval == 0)) + component.update(); + } }; ComponentList.prototype.onEntityTransformChanged = function (comp) { for (var i = 0; i < this._components.length; i++) { @@ -2285,13 +2306,17 @@ var Matcher = (function () { var Time = (function () { function Time() { } + ; Time.update = function (currentTime) { var dt = (currentTime - this._lastTime) / 1000; this.deltaTime = dt * this.timeScale; this.unscaledDeltaTime = dt; + this.frameCount++; this._lastTime = currentTime; }; + Time.deltaTime = 0; Time.timeScale = 1; + Time.frameCount = 0; Time._lastTime = 0; return Time; }()); @@ -2570,38 +2595,28 @@ var Vector2 = (function () { this.x = x; this.y = y; } - Object.defineProperty(Vector2, "One", { - get: function () { - return this.unitVector; - }, - enumerable: true, - configurable: true - }); - Object.defineProperty(Vector2, "Zero", { - get: function () { - return this.zeroVector; - }, - enumerable: true, - configurable: true - }); Vector2.add = function (value1, value2) { - value1.x += value2.x; - value1.y += value2.y; - return value1; + var result = new Vector2(0, 0); + result.x = value1.x + value2.x; + result.y = value1.y + value2.y; + return result; }; Vector2.divide = function (value1, value2) { - value1.x /= value2.x; - value1.y /= value2.y; + var result = new Vector2(0, 0); + result.x = value1.x / value2.x; + result.y = value1.y / value2.y; return value1; }; Vector2.multiply = function (value1, value2) { - value1.x *= value2.x; - value1.y *= value2.y; - return value1; + var result = new Vector2(0, 0); + result.x = value1.x * value2.x; + result.y = value1.y * value2.y; + return result; }; Vector2.subtract = function (value1, value2) { - value1.x -= value2.x; - value1.y -= value2.y; + var result = new Vector2(0, 0); + result.x = value1.x - value2.x; + result.y = value1.y - value2.y; return value1; }; Vector2.prototype.normalize = function () { @@ -2609,6 +2624,9 @@ var Vector2 = (function () { this.x *= val; this.y *= val; }; + Vector2.prototype.length = function () { + return Math.sqrt((this.x * this.x) + (this.y * this.y)); + }; Vector2.dot = function (value1, value2) { return (value1.x * value2.x) + (value1.y * value2.y); }; @@ -2619,8 +2637,10 @@ var Vector2 = (function () { Vector2.transform = function (position, matrix) { return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22)); }; - Vector2.zeroVector = new Vector2(0, 0); - Vector2.unitVector = new Vector2(1, 1); + Vector2.distance = function (value1, value2) { + var v1 = value1.x - value2.x, v2 = value1.y - value2.y; + return Math.sqrt((v1 * v1) + (v2 * v2)); + }; return Vector2; }()); var PointSectors; @@ -2654,7 +2674,7 @@ var Collisions = (function () { return true; }; Collisions.lineToLineIntersection = function (a1, a2, b1, b2) { - var intersection = Vector2.Zero; + var intersection = new Vector2(0, 0); var b = Vector2.subtract(a2, a1); var d = Vector2.subtract(b2, b1); var bDotDPerp = b.x * d.y - b.y * d.x; @@ -2775,6 +2795,258 @@ var Collisions = (function () { }; return Collisions; }()); +var Physics = (function () { + function Physics() { + } + Physics.overlapCircleAll = function (center, randius, results, layerMask) { + if (layerMask === void 0) { layerMask = -1; } + return this._spatialHash.overlapCircle(center, randius, results, layerMask); + }; + return Physics; +}()); +var Particle = (function () { + function Particle(position) { + this.position = new Vector2(0, 0); + this.lastPosition = new Vector2(0, 0); + this.acceleration = new Vector2(0, 0); + this.mass = 1; + this.radius = 0; + this.collidesWithColliders = true; + this.position = position; + this.lastPosition = position; + } + Particle.prototype.applyForce = function (force) { + this.acceleration = Vector2.add(this.acceleration, new Vector2(force.x / this.mass, force.y / this.mass)); + }; + return Particle; +}()); +var SpatialHash = (function () { + function SpatialHash() { + } + SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) { + var resultCounter = 0; + return resultCounter; + }; + return SpatialHash; +}()); +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.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); + for (var j = 0; j < composite.particles.length; j++) { + var p = composite.particles[j]; + if (this.simulationBounds) { + this.constrainParticleToBounds(p); + } + } + } + } + }; + VerletWorld.prototype.handleCollisions = function (p, collidesWithLayers) { + var collidedCount = Physics.overlapCircleAll(p.position, p.radius, VerletWorld._colliders, collidesWithLayers); + }; + 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 = []; + } + 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.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.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.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(); + }; + return DistanceConstraint; +}(Constraint)); var Triangulator = (function () { function Triangulator() { this.triangleIndices = []; diff --git a/demo/libs/framework/framework.min.js b/demo/libs/framework/framework.min.js index b3598ce5..e3b41723 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 n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};return function(e,n){function i(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(i.prototype=n.prototype,new i)}}(),Array.prototype.findIndex=function(t){return function(t,e){for(var n=0,i=t.length;n-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,r){return e.call(arguments[2],i,r,t)&&n.push(i),n},[]);for(var n=[],i=0,r=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,r){return n.push(e.call(arguments[2],i,r,t)),n},[]);for(var n=[],i=0,r=t.length;io?1:-1}),t}(this,t,e)},Array.prototype.orderByDescending=function(t,e){return function(t,e,n){return t.sort(function(t,i){var r=e(t),o=e(i);return n?-n(r,o):r0;){if("break"===h())break}return r?this.recontructPath(o,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,r=t.keys(),o=t.values();n=r.next(),i=o.next(),!n.done;)if(JSON.stringify(n.value)==JSON.stringify(e))return i.value;return null},t.recontructPath=function(t,e,n){var i=[],r=n;for(i.push(n);r!=e;)r=this.getKey(t,r),i.push(r);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 r=this._nodes[i];this.hasHigherPriority(r,e)&&(e=r);var o=i+1;if(o<=this._numNodes){var s=this._nodes[o];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 r?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 r?this.recontructPath(o,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,r=t.keys(),o=t.values();n=r.next(),i=o.next(),!n.done;)if(JSON.stringify(n.value)==JSON.stringify(e))return i.value;return null},t.recontructPath=function(t,e,n){var i=[],r=n;for(i.push(n);r!=e;)r=this.getKey(t,r),i.push(r);return i.reverse(),i},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._lastTime=t},t.timeScale=1,t._lastTime=0,t}(),MathHelper=function(){function t(){}return t.toDegrees=function(t){return 57.29577951308232*t},t.toRadians=function(t){return.017453292519943295*t},t.map=function(t,e,n,i,r){return i+(t-e)*(r-i)/(n-e)},t.clamp=function(t,e,n){return tn?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,r,o){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=r,this.m32=o}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,r=t.m21*e.m11+t.m22*e.m21,o=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=r,t.m22=o,t.m31=s,t.m32=a,t},t.multiplyTranslation=function(e,n,i){var r=t.createTranslation(n,i);return t.multiply(e,r)},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),r=Math.sin(e);return n.m11=i,n.m12=r,n.m21=-r,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,this.y=e,this.width=n,this.height=i}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*r.y-a.y*r.x)/s;return!(c<0||c>1)},t.lineToLineIntersection=function(t,e,n,i){var r=Vector2.Zero,o=Vector2.subtract(e,t),s=Vector2.subtract(i,n),a=o.x*s.y-o.y*s.x;if(0==a)return r;var h=Vector2.subtract(n,t),c=(h.x*s.y-h.y*s.x)/a;if(c<0||c>1)return r;var u=(h.x*o.y-h.y*o.x)/a;return u<0||u>1?r:r=Vector2.add(t,new Vector2(c*o.x,c*o.y))},t.closestPointOnLine=function(t,e,n){var i=Vector2.subtract(e,t),r=Vector2.subtract(n,t),o=Vector2.dot(r,i)/Vector2.dot(i,i);return o=MathHelper.clamp(o,0,1),Vector2.add(t,new Vector2(i.x*o,i.y*o))},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&&r.y>=e&&r.x=t+n&&(o|=PointSectors.right),r.y=e+i&&(o|=PointSectors.bottom),o},t}(),Triangulator=function(){function t(){this.triangleIndices=[],this._triPrev=new Array(12),this._triNext=new Array(12)}return t.prototype.triangulate=function(e,n){void 0===n&&(n=!0);var i=e.length;this.initialize(i);for(var r=0,o=0;i>3&&r<500;){r++;var s=!0,a=e[this._triPrev[o]],h=e[o],c=e[this._triNext[o]];if(Vector2Ext.isTriangleCCW(a,h,c)){var u=this._triNext[this._triNext[o]];do{if(t.testPointTriangle(e[u],a,h,c)){s=!1;break}u=this._triNext[u]}while(u!=this._triPrev[o])}else s=!1;s?(this.triangleIndices.push(this._triPrev[o]),this.triangleIndices.push(o),this.triangleIndices.push(this._triNext[o]),this._triNext[this._triPrev[o]]=this._triNext[o],this._triPrev[this._triNext[o]]=this._triPrev[o],i--,o=this._triPrev[o]):o=this._triNext[o]}this.triangleIndices.push(this._triPrev[o]),this.triangleIndices.push(o),this.triangleIndices.push(this._triNext[o]),n||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 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}(),MathHelper=function(){function t(){}return t.toDegrees=function(t){return 57.29577951308232*t},t.toRadians=function(t){return.017453292519943295*t},t.map=function(t,e,i,n,o){return n+(t-e)*(o-n)/(i-e)},t.clamp=function(t,e,i){return ti?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}(),Particle=function(){function t(t){this.position=new Vector2(0,0),this.lastPosition=new Vector2(0,0),this.acceleration=new Vector2(0,0),this.mass=1,this.radius=0,this.collidesWithColliders=!0,this.position=t,this.lastPosition=t}return t.prototype.applyForce=function(t){this.acceleration=Vector2.add(this.acceleration,new Vector2(t.x/this.mass,t.y/this.mass))},t}(),SpatialHash=function(){function t(){}return t.prototype.overlapCircle=function(t,e,i,n){return 0},t}(),VerletWorld=function(){function t(t){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.simulationBounds=t,this._fixedDeltaTimeSq=Math.pow(this._fixedDeltaTime,2)}return t.prototype.update=function(){this.updateTiming();for(var t=1;t<=this._iterationSteps;t++)for(var e=this._composites.length-1;e>=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-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.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}(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(cameFrom: Map, start: T, goal: T): T[]; } +declare class DebugDefaults { + static verletParticle: number; + static verletConstraintEdge: number; +} declare abstract class Component { entity: Entity; private _enabled; @@ -358,6 +362,9 @@ declare abstract class RenderableComponent extends Component { readonly height: number; isVisible: boolean; readonly bounds: Rectangle; + protected getWidth(): number; + protected getHeight(): number; + protected getBounds(): Rectangle; protected onBecameVisible(): void; protected onBecameInvisible(): void; isVisibleFromCamera(camera: Camera): boolean; @@ -484,6 +491,7 @@ declare class Time { static unscaledDeltaTime: any; static deltaTime: number; static timeScale: number; + static frameCount: number; private static _lastTime; static update(currentTime: number): void; } @@ -538,19 +546,17 @@ declare class Rectangle { declare class Vector2 { x: number; y: number; - private static readonly zeroVector; - private static readonly unitVector; - static readonly One: Vector2; - static readonly Zero: Vector2; 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; static subtract(value1: Vector2, value2: Vector2): Vector2; normalize(): void; + length(): number; static dot(value1: Vector2, value2: Vector2): number; static distanceSquared(value1: Vector2, value2: Vector2): number; static transform(position: Vector2, matrix: Matrix2D): Vector2; + static distance(value1: Vector2, value2: Vector2): number; } declare enum PointSectors { center = 0, @@ -575,6 +581,77 @@ declare class Collisions { static isRectToPoint(rX: number, rY: number, rW: number, rH: number, point: Vector2): boolean; static getSector(rX: number, rY: number, rW: number, rH: number, point: Vector2): PointSectors; } +declare class Physics { + private static _spatialHash; + static overlapCircleAll(center: Vector2, randius: number, results: any[], layerMask?: number): number; +} +declare class Particle { + position: Vector2; + lastPosition: Vector2; + isPinned: boolean; + pinnedPosition: any; + acceleration: Vector2; + mass: number; + radius: number; + collidesWithColliders: boolean; + constructor(position: Vector2); + applyForce(force: Vector2): void; +} +declare class SpatialHash { + overlapCircle(circleCenter: Vector2, radius: number, results: any[], layerMask: any): number; +} +declare class VerletWorld { + gravity: Vector2; + maximumStepIterations: number; + constraintIterations: number; + simulationBounds: Rectangle; + private _leftOverTime; + private _iterationSteps; + private _fixedDeltaTime; + private _composites; + private _fixedDeltaTimeSq; + private static _colliders; + 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[]; + solveConstraints(): void; + addParticle(particle: Particle): Particle; + addConstraint(constraint: T): T; + removeConstraint(constraint: Constraint): void; + updateParticles(deltaTimeSquared: number, gravity: Vector2): 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; + debugRender(graphics: egret.Graphics): void; +} +declare class DistanceConstraint extends Constraint { + stiffness: number; + restingDistance: number; + tearSensitivity: number; + private _particleOne; + private _particleTwo; + constructor(first: Particle, second: Particle, stiffness: number, distance?: number); + setCollidesWithColliders(collidesWithColliders: boolean): this; + solve(): void; + debugRender(graphics: egret.Graphics): void; +} declare class Triangulator { triangleIndices: number[]; private _triPrev; diff --git a/source/bin/framework.js b/source/bin/framework.js index 04b44875..d6d939c3 100644 --- a/source/bin/framework.js +++ b/source/bin/framework.js @@ -730,6 +730,13 @@ var WeightedPathfinder = (function () { }; return WeightedPathfinder; }()); +var DebugDefaults = (function () { + function DebugDefaults() { + } + DebugDefaults.verletParticle = 0xDC345E; + DebugDefaults.verletConstraintEdge = 0x433E36; + return DebugDefaults; +}()); var Component = (function () { function Component() { this._enabled = true; @@ -1171,7 +1178,7 @@ var Transform = (function () { this._worldInverseTransform = Matrix2D.identity; this._rotation = 0; this.entity = entity; - this._scale = this._localScale = Vector2.One; + this._scale = this._localScale = new Vector2(0, 0); this._children = []; } Object.defineProperty(Transform.prototype, "childCount", { @@ -1695,14 +1702,14 @@ var RenderableComponent = (function (_super) { } Object.defineProperty(RenderableComponent.prototype, "width", { get: function () { - return this.bounds.width; + return this.getWidth(); }, enumerable: true, configurable: true }); Object.defineProperty(RenderableComponent.prototype, "height", { get: function () { - return this.bounds.height; + return this.getHeight(); }, enumerable: true, configurable: true @@ -1723,15 +1730,24 @@ var RenderableComponent = (function (_super) { }); Object.defineProperty(RenderableComponent.prototype, "bounds", { get: function () { - if (this._areBoundsDirty) { - this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, Vector2.Zero, this.entity.transform.scale, this.entity.transform.rotation, this.width, this.height); - this._areBoundsDirty = false; - } - return this._bounds; + return this.getBounds(); }, enumerable: true, configurable: true }); + RenderableComponent.prototype.getWidth = function () { + return this.bounds.width; + }; + RenderableComponent.prototype.getHeight = function () { + return this.bounds.height; + }; + RenderableComponent.prototype.getBounds = function () { + if (this._areBoundsDirty) { + this._bounds.calculateBounds(this.entity.transform.position, this._localOffset, new Vector2(0, 0), this.entity.transform.scale, this.entity.transform.rotation, this.width, this.height); + this._areBoundsDirty = false; + } + return this._bounds; + }; RenderableComponent.prototype.onBecameVisible = function () { }; RenderableComponent.prototype.onBecameInvisible = function () { }; RenderableComponent.prototype.isVisibleFromCamera = function (camera) { @@ -2058,6 +2074,11 @@ var ComponentList = (function () { }; ComponentList.prototype.update = function () { this.updateLists(); + for (var i = 0; i < this._components.length; i++) { + var component = this._components[i]; + if (component.enabled && (component.updateInterval == 1 || Time.frameCount % component.updateInterval == 0)) + component.update(); + } }; ComponentList.prototype.onEntityTransformChanged = function (comp) { for (var i = 0; i < this._components.length; i++) { @@ -2285,13 +2306,17 @@ var Matcher = (function () { var Time = (function () { function Time() { } + ; Time.update = function (currentTime) { var dt = (currentTime - this._lastTime) / 1000; this.deltaTime = dt * this.timeScale; this.unscaledDeltaTime = dt; + this.frameCount++; this._lastTime = currentTime; }; + Time.deltaTime = 0; Time.timeScale = 1; + Time.frameCount = 0; Time._lastTime = 0; return Time; }()); @@ -2570,38 +2595,28 @@ var Vector2 = (function () { this.x = x; this.y = y; } - Object.defineProperty(Vector2, "One", { - get: function () { - return this.unitVector; - }, - enumerable: true, - configurable: true - }); - Object.defineProperty(Vector2, "Zero", { - get: function () { - return this.zeroVector; - }, - enumerable: true, - configurable: true - }); Vector2.add = function (value1, value2) { - value1.x += value2.x; - value1.y += value2.y; - return value1; + var result = new Vector2(0, 0); + result.x = value1.x + value2.x; + result.y = value1.y + value2.y; + return result; }; Vector2.divide = function (value1, value2) { - value1.x /= value2.x; - value1.y /= value2.y; + var result = new Vector2(0, 0); + result.x = value1.x / value2.x; + result.y = value1.y / value2.y; return value1; }; Vector2.multiply = function (value1, value2) { - value1.x *= value2.x; - value1.y *= value2.y; - return value1; + var result = new Vector2(0, 0); + result.x = value1.x * value2.x; + result.y = value1.y * value2.y; + return result; }; Vector2.subtract = function (value1, value2) { - value1.x -= value2.x; - value1.y -= value2.y; + var result = new Vector2(0, 0); + result.x = value1.x - value2.x; + result.y = value1.y - value2.y; return value1; }; Vector2.prototype.normalize = function () { @@ -2609,6 +2624,9 @@ var Vector2 = (function () { this.x *= val; this.y *= val; }; + Vector2.prototype.length = function () { + return Math.sqrt((this.x * this.x) + (this.y * this.y)); + }; Vector2.dot = function (value1, value2) { return (value1.x * value2.x) + (value1.y * value2.y); }; @@ -2619,8 +2637,10 @@ var Vector2 = (function () { Vector2.transform = function (position, matrix) { return new Vector2((position.x * matrix.m11) + (position.y * matrix.m21), (position.x * matrix.m12) + (position.y * matrix.m22)); }; - Vector2.zeroVector = new Vector2(0, 0); - Vector2.unitVector = new Vector2(1, 1); + Vector2.distance = function (value1, value2) { + var v1 = value1.x - value2.x, v2 = value1.y - value2.y; + return Math.sqrt((v1 * v1) + (v2 * v2)); + }; return Vector2; }()); var PointSectors; @@ -2654,7 +2674,7 @@ var Collisions = (function () { return true; }; Collisions.lineToLineIntersection = function (a1, a2, b1, b2) { - var intersection = Vector2.Zero; + var intersection = new Vector2(0, 0); var b = Vector2.subtract(a2, a1); var d = Vector2.subtract(b2, b1); var bDotDPerp = b.x * d.y - b.y * d.x; @@ -2775,6 +2795,258 @@ var Collisions = (function () { }; return Collisions; }()); +var Physics = (function () { + function Physics() { + } + Physics.overlapCircleAll = function (center, randius, results, layerMask) { + if (layerMask === void 0) { layerMask = -1; } + return this._spatialHash.overlapCircle(center, randius, results, layerMask); + }; + return Physics; +}()); +var Particle = (function () { + function Particle(position) { + this.position = new Vector2(0, 0); + this.lastPosition = new Vector2(0, 0); + this.acceleration = new Vector2(0, 0); + this.mass = 1; + this.radius = 0; + this.collidesWithColliders = true; + this.position = position; + this.lastPosition = position; + } + Particle.prototype.applyForce = function (force) { + this.acceleration = Vector2.add(this.acceleration, new Vector2(force.x / this.mass, force.y / this.mass)); + }; + return Particle; +}()); +var SpatialHash = (function () { + function SpatialHash() { + } + SpatialHash.prototype.overlapCircle = function (circleCenter, radius, results, layerMask) { + var resultCounter = 0; + return resultCounter; + }; + return SpatialHash; +}()); +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.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); + for (var j = 0; j < composite.particles.length; j++) { + var p = composite.particles[j]; + if (this.simulationBounds) { + this.constrainParticleToBounds(p); + } + } + } + } + }; + VerletWorld.prototype.handleCollisions = function (p, collidesWithLayers) { + var collidedCount = Physics.overlapCircleAll(p.position, p.radius, VerletWorld._colliders, collidesWithLayers); + }; + 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 = []; + } + 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.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.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.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(); + }; + return DistanceConstraint; +}(Constraint)); var Triangulator = (function () { function Triangulator() { this.triangleIndices = []; diff --git a/source/bin/framework.min.js b/source/bin/framework.min.js index b3598ce5..e3b41723 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 n in e)e.hasOwnProperty(n)&&(t[n]=e[n])};return function(e,n){function i(){this.constructor=e}t(e,n),e.prototype=null===n?Object.create(n):(i.prototype=n.prototype,new i)}}(),Array.prototype.findIndex=function(t){return function(t,e){for(var n=0,i=t.length;n-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,r){return e.call(arguments[2],i,r,t)&&n.push(i),n},[]);for(var n=[],i=0,r=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,r){return n.push(e.call(arguments[2],i,r,t)),n},[]);for(var n=[],i=0,r=t.length;io?1:-1}),t}(this,t,e)},Array.prototype.orderByDescending=function(t,e){return function(t,e,n){return t.sort(function(t,i){var r=e(t),o=e(i);return n?-n(r,o):r0;){if("break"===h())break}return r?this.recontructPath(o,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,r=t.keys(),o=t.values();n=r.next(),i=o.next(),!n.done;)if(JSON.stringify(n.value)==JSON.stringify(e))return i.value;return null},t.recontructPath=function(t,e,n){var i=[],r=n;for(i.push(n);r!=e;)r=this.getKey(t,r),i.push(r);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 r=this._nodes[i];this.hasHigherPriority(r,e)&&(e=r);var o=i+1;if(o<=this._numNodes){var s=this._nodes[o];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 r?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 r?this.recontructPath(o,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,r=t.keys(),o=t.values();n=r.next(),i=o.next(),!n.done;)if(JSON.stringify(n.value)==JSON.stringify(e))return i.value;return null},t.recontructPath=function(t,e,n){var i=[],r=n;for(i.push(n);r!=e;)r=this.getKey(t,r),i.push(r);return i.reverse(),i},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._lastTime=t},t.timeScale=1,t._lastTime=0,t}(),MathHelper=function(){function t(){}return t.toDegrees=function(t){return 57.29577951308232*t},t.toRadians=function(t){return.017453292519943295*t},t.map=function(t,e,n,i,r){return i+(t-e)*(r-i)/(n-e)},t.clamp=function(t,e,n){return tn?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,r,o){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=r,this.m32=o}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,r=t.m21*e.m11+t.m22*e.m21,o=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=r,t.m22=o,t.m31=s,t.m32=a,t},t.multiplyTranslation=function(e,n,i){var r=t.createTranslation(n,i);return t.multiply(e,r)},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),r=Math.sin(e);return n.m11=i,n.m12=r,n.m21=-r,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,this.y=e,this.width=n,this.height=i}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*r.y-a.y*r.x)/s;return!(c<0||c>1)},t.lineToLineIntersection=function(t,e,n,i){var r=Vector2.Zero,o=Vector2.subtract(e,t),s=Vector2.subtract(i,n),a=o.x*s.y-o.y*s.x;if(0==a)return r;var h=Vector2.subtract(n,t),c=(h.x*s.y-h.y*s.x)/a;if(c<0||c>1)return r;var u=(h.x*o.y-h.y*o.x)/a;return u<0||u>1?r:r=Vector2.add(t,new Vector2(c*o.x,c*o.y))},t.closestPointOnLine=function(t,e,n){var i=Vector2.subtract(e,t),r=Vector2.subtract(n,t),o=Vector2.dot(r,i)/Vector2.dot(i,i);return o=MathHelper.clamp(o,0,1),Vector2.add(t,new Vector2(i.x*o,i.y*o))},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&&r.y>=e&&r.x=t+n&&(o|=PointSectors.right),r.y=e+i&&(o|=PointSectors.bottom),o},t}(),Triangulator=function(){function t(){this.triangleIndices=[],this._triPrev=new Array(12),this._triNext=new Array(12)}return t.prototype.triangulate=function(e,n){void 0===n&&(n=!0);var i=e.length;this.initialize(i);for(var r=0,o=0;i>3&&r<500;){r++;var s=!0,a=e[this._triPrev[o]],h=e[o],c=e[this._triNext[o]];if(Vector2Ext.isTriangleCCW(a,h,c)){var u=this._triNext[this._triNext[o]];do{if(t.testPointTriangle(e[u],a,h,c)){s=!1;break}u=this._triNext[u]}while(u!=this._triPrev[o])}else s=!1;s?(this.triangleIndices.push(this._triPrev[o]),this.triangleIndices.push(o),this.triangleIndices.push(this._triNext[o]),this._triNext[this._triPrev[o]]=this._triNext[o],this._triPrev[this._triNext[o]]=this._triPrev[o],i--,o=this._triPrev[o]):o=this._triNext[o]}this.triangleIndices.push(this._triPrev[o]),this.triangleIndices.push(o),this.triangleIndices.push(this._triNext[o]),n||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 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}(),MathHelper=function(){function t(){}return t.toDegrees=function(t){return 57.29577951308232*t},t.toRadians=function(t){return.017453292519943295*t},t.map=function(t,e,i,n,o){return n+(t-e)*(o-n)/(i-e)},t.clamp=function(t,e,i){return ti?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}(),Particle=function(){function t(t){this.position=new Vector2(0,0),this.lastPosition=new Vector2(0,0),this.acceleration=new Vector2(0,0),this.mass=1,this.radius=0,this.collidesWithColliders=!0,this.position=t,this.lastPosition=t}return t.prototype.applyForce=function(t){this.acceleration=Vector2.add(this.acceleration,new Vector2(t.x/this.mass,t.y/this.mass))},t}(),SpatialHash=function(){function t(){}return t.prototype.overlapCircle=function(t,e,i,n){return 0},t}(),VerletWorld=function(){function t(t){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.simulationBounds=t,this._fixedDeltaTimeSq=Math.pow(this._fixedDeltaTime,2)}return t.prototype.update=function(){this.updateTiming();for(var t=1;t<=this._iterationSteps;t++)for(var e=this._composites.length-1;e>=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-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.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}(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 +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 new file mode 100644 index 00000000..6de532c1 --- /dev/null +++ b/source/src/Physics/Verlet/Composites/Composite.ts @@ -0,0 +1,67 @@ +class Composite { + private _constraints: Constraint[] = []; + + public friction = new Vector2(0.98, 1); + public drawParticles: boolean = true; + public drawConstraints: boolean = true; + public particles: Particle[] = []; + /** + * 处理解决所有约束条件 + */ + 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 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 new file mode 100644 index 00000000..cb760aaa --- /dev/null +++ b/source/src/Physics/Verlet/Constraint/Constraint.ts @@ -0,0 +1,8 @@ +abstract class Constraint { + public composite: Composite; + public collidesWithColliders = true; + + public abstract solve(); + + 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 new file mode 100644 index 00000000..f9f4ae8a --- /dev/null +++ b/source/src/Physics/Verlet/Constraint/DistanceConstraint.ts @@ -0,0 +1,54 @@ +class DistanceConstraint extends Constraint { + public stiffness: number = 0; + public restingDistance: number = 0; + public tearSensitivity = Number.POSITIVE_INFINITY; + + private _particleOne: Particle; + private _particleTwo: Particle; + + 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 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/Utils/Verlet/Particle.ts b/source/src/Physics/Verlet/Particle.ts similarity index 62% rename from source/src/Utils/Verlet/Particle.ts rename to source/src/Physics/Verlet/Particle.ts index 511a713d..7de39223 100644 --- a/source/src/Utils/Verlet/Particle.ts +++ b/source/src/Physics/Verlet/Particle.ts @@ -1,17 +1,19 @@ -class Particle { - public position: Vector2; - public lastPosition: Vector2; - public isPinned: boolean; - public pinnedPosition; - public acceleration: Vector2; - public mass: number = 1; - - constructor(position: Vector2){ - this.position = position; - this.lastPosition = position; - } - - public applyForce(force: Vector2){ - this.acceleration = Vector2.add(this.acceleration, new Vector2(force.x / this.mass, force.y / this.mass)); - } +class Particle { + public position: Vector2 = new Vector2(0, 0); + public lastPosition: Vector2 = new Vector2(0, 0); + public isPinned: boolean; + public pinnedPosition; + public acceleration: Vector2 = new Vector2(0, 0); + public mass: number = 1; + public radius: number = 0; + public collidesWithColliders = true; + + constructor(position: Vector2){ + this.position = position; + this.lastPosition = position; + } + + public applyForce(force: Vector2){ + this.acceleration = Vector2.add(this.acceleration, new Vector2(force.x / this.mass, force.y / this.mass)); + } } \ No newline at end of file diff --git a/source/src/Physics/Verlet/SpatialHash.ts b/source/src/Physics/Verlet/SpatialHash.ts new file mode 100644 index 00000000..8e2bdf8c --- /dev/null +++ b/source/src/Physics/Verlet/SpatialHash.ts @@ -0,0 +1,7 @@ +class SpatialHash { + public overlapCircle(circleCenter: Vector2, radius: number, results: any[], layerMask){ + let resultCounter = 0; + + return resultCounter; + } +} \ No newline at end of file diff --git a/source/src/Physics/Verlet/VerletWorld.ts b/source/src/Physics/Verlet/VerletWorld.ts new file mode 100644 index 00000000..53bed2bf --- /dev/null +++ b/source/src/Physics/Verlet/VerletWorld.ts @@ -0,0 +1,113 @@ +/** + * 基于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; + + private static _colliders = new Array(4); + + 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); + + 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, -1); + } + } + } + } + + private handleCollisions(p: Particle, collidesWithLayers: number){ + let collidedCount = Physics.overlapCircleAll(p.position, p.radius, VerletWorld._colliders, collidesWithLayers); + // handle + } + + 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 diff --git a/source/src/Utils/Verlet/Composites/Composite.ts b/source/src/Utils/Verlet/Composites/Composite.ts deleted file mode 100644 index f3caeada..00000000 --- a/source/src/Utils/Verlet/Composites/Composite.ts +++ /dev/null @@ -1,25 +0,0 @@ -class Composite { - private _constraints: Constraint[] = []; - - public particles: Particle[] = []; - /** - * 处理解决所有约束条件 - */ - public solveConstraints(){ - for (let i = this._constraints.length - 1; i >= 0; i --){ - this._constraints[i].solve(); - } - } - - 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)); - } - } -} \ No newline at end of file diff --git a/source/src/Utils/Verlet/Constraint/Constraint.ts b/source/src/Utils/Verlet/Constraint/Constraint.ts deleted file mode 100644 index e371f532..00000000 --- a/source/src/Utils/Verlet/Constraint/Constraint.ts +++ /dev/null @@ -1,5 +0,0 @@ -abstract class Constraint { - public composite: Composite; - - public abstract solve(); -} \ No newline at end of file diff --git a/source/src/Utils/Verlet/VerletWorld.ts b/source/src/Utils/Verlet/VerletWorld.ts deleted file mode 100644 index f2be8ff2..00000000 --- a/source/src/Utils/Verlet/VerletWorld.ts +++ /dev/null @@ -1,45 +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; - private _iterationSteps: number; - private _fixedDeltaTime = 1 / 60; - private _composites: Composite[] = []; - private _fixedDeltaTimeSq: number; - - 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); - } - } - } - - 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