Last year to do a imitation of the defense of the Radish Tower defense game, I wrote, the framework of the game is COCO2D-HTML5
The principle of implementation can refer to http://www.cnblogs.com/technology/archive/2011/05/26/2058842.html
This algorithm project has been placed on GitHub, and friends need to be able to see their own
Https://github.com/caoke90/Algorithm/blob/master/Astar.js
Astar pathfinding algorithm//point type var cc=cc| | Consolevar point=function (x, y) {if (this instanceof point) {this.init (x, y)}else{return new Point (x, y) }}point.prototype={init:function (x, y) {this.x=x; This.y=y; }, Parentpoint:null, f:0,//f=g+h g:0, h:0, x:0, y:0, Calcf:function () {this. F = this. G + this. H }}//maze type var maze=function (Maze) {if (this instanceof Maze) {this.init (Maze); }else{return new Maze (Maze)}}maze.prototype={init:function (Maze) {this. Mazearray=maze}, Oblique:14, Step:10, closelist:[], openlist:[], findpath:function (Start, End, Isigno Recorner) {this. Openlist.push (start); while (this. Openlist.length! = 0) {//Find out the lowest point of the F value var Tempstart = this. MinPoint (this. Openlist); This. Closelist.push (Tempstart); This. Remove (this. Openlist,tempstart); Find it adjacent to the point var surroundpoints = thiS.surrroundpoints (Tempstart, Isignorecorner); for (var i=0;i< surroundpoints.length;i++) {var point=surroundpoints[i] if (t His. Exists (this. Openlist,point) {//Calculate G-value, if larger than the original, do nothing, otherwise set its parent node as the current point, and update the G and F this. Foundpoint (Tempstart, point); } else{//If they are not in the start list, join, set the parent node, and calculate GHF this. Notfoundpoint (Tempstart, end, point); }} if (this. Get (this. Openlist,end) = null) {return this. Get (this. Openlist,end); }} return this. Get (this. Openlist,end); },//The position in the two-dimensional array is not an obstacle canreaches:function (x, y) {return this. Mazearray[this. MAZEARRAY.LENGTH-Y-1][X] = = 0; }, Canreach:function (Start, point, Isignorecorner) {if (!this. Canreaches (Point.x, point.y) | | This. Exists (this. Closelist,point)) return false; else { if ((Math.Abs (point.x-start.x) + Math.Abs (point.y-start.y)) = = 1) {return true; } return false; }}, Notfoundpoint:function (Tempstart, end, point) {point. Parentpoint = Tempstart; Point. G = this. CALCG (Tempstart, point); Point. H = this. Calch (end, point); Point. CALCF (); This. Openlist.push (point); }, Foundpoint:function (Tempstart, point) {var G = this. CALCG (Tempstart, point); if (G < point. G) {point. Parentpoint = Tempstart; Point. g = g; Point. CALCF (); }}, Calcg:function (Start, point) {var G = (math.abs. X-start. X) + Math.Abs (point. Y-start. Y) = = 2? This. Oblique:this. STEP; var PARENTG = point. Parentpoint! = null? Point. parentpoint.g:0; return G + PARENTG; }, Calch:function (end, point) {var step = Math.Abs (point.x-end.x) + math.abs (POINT.Y-END.Y); ReturnStep * this. STEP; },//Gets the points surrroundpoints:function (point, Isignorecorner) {var surroundpoints = [] that can be reached around a point; if (this. Canreach (Point,point (POINT.X-1,POINT.Y), Isignorecorner)) {Surroundpoints.push (point (POINT.X-1,POINT.Y)); } if (this. Canreach (Point,point (point.x,point.y-1), Isignorecorner)) {Surroundpoints.push (point (point.x,point.y-1)); } if (this. Canreach (Point,point (POINT.X+1,POINT.Y), Isignorecorner)) {Surroundpoints.push (point (POINT.X+1,POINT.Y)); } if (this. Canreach (Point,point (point.x,point.y+1), Isignorecorner)) {Surroundpoints.push (point (point.x,point.y+1)); } return surroundpoints; },//some extension methods for list<point>//Determine if there are points exists:function (points, point) {for (k in points) {var p= Points[k] if ((p.x = = point.x) && (p.y = = Point.y)) {return true; }} return false; },//getF min minpoint:function (points) {var min=points[0]; for (Var i=0;i<points.length-1;i++) {if (Points[i]. F<POINTS[I+1]. F) {Min=points[i]}} return min; },//Gets the dot get:function (points, point) {for (Var k in points) {var p=points[k] if (p.x = = Point.x) && (p.y = = point.y)) return p; } return null; },//Delete Point remove:function (points,point) {for (Var i=0;i<points.length;i++) {var p=points[i] if (point.x = = = P.x && Point.y = = = p.y) {return points.splice (i,1); }}}}var arr= [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 0, 0, 0, 0 and 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 ", [1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] , [1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],[1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 0, 0, 0, 1], [1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]];var Map=maze (arr)//Start point whether the end point is angled var parent=map. Findpath (Point (2,3), point (16,2), false) while (parent! = null) {Cc.log (Parent.x + "," + parent.y); Parent = parent. Parentpoint;}
JavaScript's Astar version pathfinding algorithm