Phaser.Path = function (game, type, loops){ if (type === undefined) { type = Phaser.Path.CoordinateSystems.WORLD; } if (loops === undefined) { loops = false ; } this.game = game; this.coordinateSystem = type; this.loops = loops; this.cacheKey = ''; this.key = ''; this.name = ''; this.type = Phaser.Path.PathTypes.PATH; this.branches = [] ; this._points = [] ; this._offset = new Phaser.Point(); this._p1 = new Phaser.PathPoint(); this._p2 = new Phaser.PathPoint(); this._origin = new Phaser.Point(); } ; Phaser.Path.prototype.constructor = Phaser.Path; Phaser.Path.PathTypes = { } ; Phaser.Path.BranchTypes = { } ; Phaser.Path.CoordinateSystems = { } ; Phaser.Path.PathTypes.PATH = 0; Phaser.Path.PathTypes.BRANCH = 1; Phaser.Path.BranchTypes.ATTACHED = 0; Phaser.Path.BranchTypes.JOINED = 1; Phaser.Path.CoordinateSystems.WORLD = 1; Phaser.Path.CoordinateSystems.SCREEN = 2; Phaser.Path.CoordinateSystems.OFFSET = 3; Phaser.Path.prototype = { create: function (coordinateSystem, loops){ if (loops === undefined) { loops = false ; } switch (coordinateSystem){ default : { this.coordinateSystem = Phaser.Path.CoordinateSystems.WORLD; break ; } case 2: case 'SCREEN_COORDINATES': this.coordinateSystem = Phaser.Path.CoordinateSystems.SCREEN; break ; case 3: case 'OFFSET_COORDINATES': this.coordinateSystem = Phaser.Path.CoordinateSystems.OFFSET; break ; } this.loops = loops; this._points = [] ; return this; } , clone: function (){ var clone = new Phaser.Path(this.coordinateSystem, this.loops); this.origin.clone(clone.origin); this.points.forEach(function (p){ clone._points.push(p.clone()); } ); return clone; } , addPathPoint: function (x, y, vx, vy, speed, data, index){ if (x === undefined) { x = 0; } if (y === undefined) { y = 0; } if (vx === undefined) { vx = 0; } if (vy === undefined) { vy = 0; } var pp = new Phaser.PathPoint(x - this.origin.x, y - this.origin.y, vx, vy, speed, data); if (index !== null && index !== undefined) { this._points.splice(index, 0, pp); } else { this._points.push(pp); } return pp; } , removePathPoint: function (index){ var p = this.getPathPointReference(index); if (p) { this._points.splice(index, 1); } return p; } , setPathPoint: function (index, x, y, vx, vy){ var p = this.getPathPointReference(index); if (p) { p.setTo(x, y, vx, vy); } return p; } , translatePoints: function (point){ this._points.forEach(function (pnt){ pnt.x += point.x; pnt.y += point.y; } ); return this; } , setOffset: function (x, y){ this._offset.x = x; this._offset.y = y; return this; } , getPointOnThisCurve: function (curve, t){ if (!curve) { return null ; } var pnt = curve.getPoint(t); pnt.x += this._offset.x; pnt.y += this._offset.y; return pnt; } , getControlPointsOnThisCurve: function (curve){ var pnts = Phaser.ArrayUtils.numberArrayStep(0, 1.1, 0.1).map(function (num){ return this.getPointOnThisCurve(curve, num); } , this); return pnts; } , getPathPoint: function (index, point){ var i = this.loops? index % _AN_Read_length('length', this._points): index; if (_AN_Read_length('length', this._points) > i) { point.copy(this._points[i]); switch (this.coordinateSystem){ case Phaser.Path.CoordinateSystems.SCREEN: point.x -= this.game.camera.x; point.y -= this.game.camera.y; break ; case Phaser.Path.CoordinateSystems.OFFSET: point.x += this.origin.x; point.y += this.origin.y; break ; } return true ; } else { return false ; } } , getPathPointReference: function (index){ var i = this.loops? index % _AN_Read_length('length', this._points): index; if (_AN_Read_length('length', this._points) > i) { return this._points[i]; } return null ; } , getCurve: function (index){ if (index === undefined) { index = 0; } if (!this.getPathPoint(index, this._p1)) { return null ; } if (this._p1.curve) { return this._p1.curve; } if (!this.getPathPoint(index + 1, this._p2)) { if (!this._p1.branchPath) { return null ; } var newPath = this._p1.branchPath; var joinIndex = this._p1.branchPointIndex; if (!newPath.getPathPoint(joinIndex + 1, this._p2)) { return null ; } } this._p1.curve = new Phaser.Hermite(this._p1.x, this._p1.y, this._p2.x, this._p2.y, this._p1.vx, this._p1.vy, this._p2.vx, this._p2.vy); this.curvePointIndex = index; return this._p1.curve; } , pointIndex: function (pathPoint){ var l = _AN_Read_length('length', this._points); for (var i = 0; i < l; i++ ){ if (this.coordinateSystem === Phaser.Path.CoordinateSystems.OFFSET && i !== 0) { if (pathPoint.equals(this._points[i], this._points[0].x, this._points[0].y)) { return i; } } else { if (pathPoint.equals(this._points[i])) { return i; } } } return -1; } , atEnd: function (index){ if (this.loops) { return (index === _AN_Read_length('length', this._points)); } return (index === _AN_Read_length('length', this._points) - 1); } , numPoints: function (){ return _AN_Read_length('length', this._points); } , processData: function (follower, pathPointIndex, reversing){ if (this.getPathPoint(pathPointIndex, this._p1)) { if (this._p1.branchPath && !reversing) { follower.dispatchEvent({ type: Phaser.PathFollower.EVENT_BRANCH_CHOICE, target: follower, data: this._p1.clone()} ); } if (this._p1.data && this._p1.data.type) { switch (this._p1.data.type){ case Phaser.PathPoint.DATA_PAUSE: follower.pause(this._p1.data.value); break ; case Phaser.PathPoint.DATA_COUNTER: if (follower.branchCount === 0) { follower.branchCount = this._p1.data.value; } else { follower.branchCount-- ; if (follower.branchCount <= 0) { follower.branchCount = 0; follower.dispatchEvent({ type: Phaser.PathFollower.EVENT_COUNT_FINISH, target: follower, data: this._p1.clone()} ); } } break ; } } follower.dispatchEvent({ type: Phaser.PathFollower.EVENT_REACHED_POINT, target: follower, data: this._p1.clone()} ); } return this._p1; } , smooth: function (){ if (_AN_Read_length('length', this._points) === 0) { return this; } var i; var thisPoint; var p1; var p2; var dx; var dy; for (i = 1; i < _AN_Read_length('length', this._points) - 1; i++ ){ thisPoint = this.getPathPointReference(i); p1 = this.getPathPointReference(i - 1); p2 = this.getPathPointReference(i + 1); dx = p2.x - p1.x; dy = p2.y - p1.y; thisPoint.setTangent(dx, dy); } if (this.loops) { i = _AN_Read_length('length', this._points) - 1; thisPoint = this.getPathPointReference(i); p1 = this.getPathPointReference(i - 1); p2 = this.getPathPointReference(0); dx = p2.x - p1.x; dy = p2.y - p1.y; thisPoint.setTangent(dx, dy); i = 0; thisPoint = this.getPathPointReference(i); p1 = this.getPathPointReference(_AN_Read_length('length', this._points) - 1); p2 = this.getPathPointReference(1); dx = p2.x - p1.x; dy = p2.y - p1.y; thisPoint.setTangent(dx, dy); } return this; } , debug: function (ctx, active){ var lineColor = '#333333'; if (active) { lineColor = '#ffff00'; } if (_AN_Read_length('length', this._points) === 0) { return this; } this._p1.setTo(0, 0); var lastPoint = _AN_Read_length('length', this._points); if (!this.loops) { lastPoint-- ; } var p = new Phaser.PathPoint(); for (var i = 0; i < lastPoint; i++ ){ var curve = this.getCurve(i); this.getPathPoint(i, p); ctx.lineWidth = 2; ctx.strokeStyle = 'rgb(100, 0, 250)'; ctx.save(); var controlPoints = this.getControlPointsOnThisCurve(curve); ctx.beginPath(); controlPoints.forEach(function (pnt, index){ if (!!pnt) { if (index === 0) { ctx.moveTo(pnt.x, pnt.y); } else { ctx.lineTo(pnt.x, pnt.y); } } } ); ctx.stroke(); ctx.closePath(); if (p.active) { ctx.fillStyle = '#ffffff'; ctx.strokeStyle = '#333333'; ctx.lineWidth = 1; this.getPathPointReference(i).controlPoints = controlPoints; controlPoints.forEach(function (pnt){ ctx.beginPath(); ctx.arc(pnt.x, pnt.y, 3, 0, Math.PI * 2); ctx.fill(); ctx.stroke(); ctx.closePath(); } ); } ctx.restore(); } return this; } , toJSON: function (){ return { name: this.name, id: this.id, type: this.type, coordinateSystem: this.coordinateSystem, loops: this.loops, speed: 1, pointList: this._points.map(function (p){ return p.toJSON(); } )} ; } } ; Object.defineProperty(Phaser.Path.prototype, 'points', { get: function (){ return this._points; } } ); Object.defineProperty(Phaser.Path.prototype, 'length', { get: function (){ return _AN_Read_length('length', this._points); } } ); Object.defineProperty(Phaser.Path.prototype, 'origin', { get: function (){ return this._origin; } , set: function (val){ this._origin.setTo(val.x, val.y); } } );