Phaser.Plugin.PathManager = function (game, parent){ Phaser.Plugin.call(this, game, parent); this._list = [] ; this._followers = [] ; this._branchRegistry = { } ; } ; Phaser.Plugin.PathManager.prototype = Object.create(Phaser.Plugin.prototype); Phaser.Plugin.PathManager.prototype.constructor = Phaser.Plugin.PathManager; Phaser.Plugin.PathManager.prototype.createPathsFromJSON = function (jsonKey){ var parse = this.game.cache.getJSON(jsonKey); var path; var createdPaths = [] ; var branchList = [] ; parse.paths.forEach(function (config){ path = new Phaser.Path(config.coordinateSystem, config.loops); path.name = config.name; this.addPoints(path, config.pointList, config.speed); this._list.push(path); createdPaths.push(path); config.pointList.reduce(function (list, pnt, index){ if (pnt.branchType === Phaser.Path.BranchTypes.ATTACHED) { list.push({ path: path.name, branchPath: pnt.branchPath, pointIndex: index, type: pnt.branchType} ); } else if (pnt.branchType === Phaser.Path.BranchTypes.JOINED) { list.push({ path: pnt.branchPath, branchPath: path.name, pointIndex: pnt.branchPointIndex, type: pnt.branchType} ); } return list; } , branchList); } , this); branchList.forEach(function (branch){ var mainPath = this.findPathByName(branch.path); var branchPath = this.findPathByName(branch.branchPath); var mainPathPointIndex = branch.pointIndex; if (branch.type === Phaser.Path.BranchTypes.ATTACHED) { this.attachBranch(branchPath, mainPath, mainPathPointIndex); } else if (branch.type === Phaser.Path.BranchTypes.JOINED) { this.joinBranch(branchPath, mainPath, mainPathPointIndex, false ); } } , this); return createdPaths; } ; Phaser.Plugin.PathManager.prototype.addPath = function (path){ this._list.push(path); return path; } ; Phaser.Plugin.PathManager.prototype.attachBranch = function (branchPath, mainPath, mainPathPointIndex, count){ if (typeof mainPath === 'string') { mainPath = this.findPathByName(mainPath); } var branchFromPoint = new Phaser.PathPoint(); if (mainPath.getPathPoint(mainPathPointIndex, branchFromPoint)) { branchPath.origin = branchFromPoint; var branchToPoint = branchPath.getPathPointReference(0); var branchFromPointRef = mainPath.getPathPointReference(mainPathPointIndex); this._branchAttach(branchFromPointRef, branchPath, 0); branchFromPointRef.branchType = Phaser.Path.BranchTypes.ATTACHED; branchToPoint.branchPath = mainPath; branchToPoint.branchPointIndex = mainPathPointIndex; branchPath.coordinateSystem = Phaser.Path.CoordinateSystems.OFFSET; branchPath.type = Phaser.Path.PathTypes.BRANCH; if (count !== undefined) { branchFromPointRef.data = { type: Phaser.PathPoint.DATA_COUNTER, value: count} ; } if (this._branchRegistry[branchPath.name]) { this._branchRegistry[branchPath.name].push(branchFromPointRef); } else { this._branchRegistry[branchPath.name] = [branchFromPointRef] ; } } } ; Phaser.Plugin.PathManager.prototype.joinBranch = function (branchPath, mainPath, mainPathPointIndex, addPoint){ if (typeof addPoint === 'undefined') { addPoint = true ; } if (typeof mainPath === 'string') { mainPath = this.findPathByName(mainPath); } var mainPathJoinPoint, branchLastPoint; mainPathJoinPoint = new Phaser.PathPoint(); mainPath.getPathPoint(mainPathPointIndex, mainPathJoinPoint); if (mainPathJoinPoint) { if (addPoint) { var newBranchPoint = new Phaser.PathPoint(); if (branchPath.getPathPoint(0, newBranchPoint)) { branchLastPoint = branchPath.addPathPoint(mainPathJoinPoint.x - newBranchPoint.x, mainPathJoinPoint.y - newBranchPoint.y, mainPathJoinPoint.vx, mainPathJoinPoint.vy, 1); this._branchAttach(branchLastPoint, mainPath, mainPathPointIndex); } } else { branchLastPoint = branchPath.getPathPointReference(_AN_Read_length('length', branchPath) - 1); this._branchAttach(branchLastPoint, mainPath, mainPathPointIndex); } branchLastPoint.branchType = Phaser.Path.BranchTypes.JOINED; } if (this._branchRegistry[branchPath.name]) { this._branchRegistry[branchPath.name].push(branchLastPoint); } else { this._branchRegistry[branchPath.name] = [branchLastPoint] ; } } ; Phaser.Plugin.PathManager.prototype._branchAttach = function (attachPoint, branchingPath, branchToPointIndex){ attachPoint.branchPath = branchingPath; attachPoint.branchPointIndex = branchToPointIndex; } ; Phaser.Plugin.PathManager.prototype._branchDetach = function (attachedPoint){ attachedPoint.branchPath = null ; attachedPoint.branchPointIndex = null ; } ; Phaser.Plugin.PathManager.prototype.removeBranch = function (branch){ if (typeof branch === 'string') { branch = this.findPathByName(branch); } this._branchRegistry[branch.name].forEach(function (point){ this._branchDetach(point); } , this); this._branchRegistry[branch.name] = null ; this.removePath(this.pathIndex(branch)); } ; Phaser.Plugin.PathManager.prototype.getPath = function (index){ return this._list[index]; } ; Phaser.Plugin.PathManager.prototype.addPoints = function (path, pointList, speed){ if (speed === undefined) speed = 1; for (var i = 0; i < _AN_Read_length('length', pointList); i++ ){ path.addPathPoint(pointList[i].x, pointList[i].y, pointList[i].vx, pointList[i].vy, speed, pointList[i].data); } return path.numPoints(); } ; Phaser.Plugin.PathManager.prototype.findPathByName = function (name){ for (var i = 0; i < _AN_Read_length('length', this._list); i++ ){ if (this._list[i].name == name) { return this._list[i]; } } return null ; } ; Phaser.Plugin.PathManager.prototype.findPathByPoint = function (point){ var l = _AN_Read_length('length', this._list); for (var i = 0; i < l; i++ ){ if (this._list[i].pointIndex(point) > -1) { return this._list[i]; } } } ; Phaser.Plugin.PathManager.prototype.addFollower = function (path, follower, speed, rotationOffset, angularOffset, callbackAtEnd, physicsAdjustTime){ var f = new Phaser.PathFollower(path, follower, speed, rotationOffset, angularOffset, callbackAtEnd, physicsAdjustTime); this._followers.push(f); return f; } ; Phaser.Plugin.PathManager.prototype.update = function (){ for (var i = _AN_Read_length('length', this._followers) - 1; i >= 0; --i){ var f = this._followers[i]; if (!f.update(this.game.time.elpased)) { if (f.callbackAtEnd) { f.callbackAtEnd(f.follower); } f.destroy(); this._followers.splice(i, 1); } } } ; Phaser.Plugin.PathManager.prototype.removeAllFollowers = function (path){ for (var i = _AN_Read_length('length', this._followers) - 1; i >= 0; --i){ var f = this._followers[i]; if (f.path == path) { if (f.callbackAtEnd) { f.callbackAtEnd(f.follower); } f.destroy(); this._followers.splice(i, 1); } } } ; Phaser.Plugin.PathManager.prototype.pathIndex = function (path){ return this._list.indexOf(path); } ; Phaser.Plugin.PathManager.prototype.removePath = function (pathIndex){ this.removeAllFollowers(this.getPath(pathIndex)); if (pathIndex < _AN_Read_length('length', this._list)) { return this._list.splice(pathIndex, 1); } else { throw new Error("ERROR: Cannot remove non-existent path") } } ; Phaser.Plugin.PathManager.prototype.drawPaths = function (graphics){ for (var i = 0; i < _AN_Read_length("length", this._list); i++ ){ this._list[i].debug(graphics); } } ;