Index.html
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 strict//en"
"HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-STRICT.DTD" >
Scene.js
Crafty.scene (' main ', function () {//Console.log ("Main scene is load ...");
var walls = new Array ();
var wallnodes = new Array ();
var pathcubes = new Array (); for (var i = 0; i < Game.tilex. i++) {for (var j = 0; J < Game.tiley; J +) {if Crafty.math.randomNumber (1, game.t
ileX-1) <9 && i>0 && i<game.tilex-1 && j>0 && j<game.tiley-1) {//floor
var wall = CRAFTY.E ("2d, Canvas, Color, Solid"). Color ("#A52A2A"). attr ({w:game.tilewidth, h:game.tileheight,
X:game.tilewidth*i, y:game.tileheight*j});
Walls.push (wall);
Wallnodes.push (New Node (I, j)); }else{//floor crafty.e ("2d, Canvas, Color, Mouse"). Color ("#778899"). attr ({w:game.tilewidth, h:game.tilehe ight, X:game.tilewidth*i, y:game.tileheight*j}). bind (' Click ', function (e) {//alert (Math.floor
Entx/game.tilewidth) + "," +math.floor (E.clienty/game.tileheight));
pathcubes.length = 0; /* for (El in Pathcubes) {El.destroy ();
*/var Path = Game.findpath (new node (player.x, PLAYER.Y), new node (E.clientx, E.clienty), wallnodes);
Console.log (path); for (var k=0 k<path.length; k++) {var pathcube = CRAFTY.E ("2d, Canvas, Color"). Color ("#00FF00"). attr ({w: Game.tilewidth, H:game.tileheight, x:game.tilewidth*path[k].x, y:game.tileheight*path[k].y}). Timeout (func
tion () {This.destroy ();
}, 3000);
Pathcubes.push (Pathcube);
var dest = Path.shift ();
player.x = Dest.x * game.tilewidth;
PLAYER.Y = Dest.y * game.tileheight;
});
}}//var player;
var flag = true;
while (flag) {var Playerx = Crafty.math.randomInt (1,game.tilex-1);
var playery = Crafty.math.randomInt (1,game.tiley-1);
Console.log ("Playerx:" +playerx+ ", Playery:" +playery); for (var i=0 i<walls.length; i++) {if (walls[i].x!= playerx && walls[i].y!= playery) {player = CrAFTY.E ("2d, Canvas, Color"). Color ("Orange"). attr ({w:game.tilewidth, h:game.tileheight, X:playerx*game.tilewi
DTH, Y:playery*game.tileheight, z:99});
Flag = false;
Break
}
}
}
});
Game.js
game={tilewidth:16, Tileheight:16, width:1280, height:480, tilex:80, tiley:30, Start:function () {Crafty.i
NIT (Game.width, game.height);
Crafty.background (' RGB (87, 109, 20) ');
Crafty.scene (' Main '); }, Findpath:function (Start, dest, Walls) {if (Start.x = = Undefined | | start.y = undefined | | dest.x = undefined | | de
St.y = = undefined) {return undefined;
} start.x = Math.floor (start.x/game.tilewidth);
Start.y = Math.floor (start.y/game.tileheight);
Dest.x = Math.floor (dest.x/game.tilewidth);
Dest.y = Math.floor (dest.y/game.tileheight);
Console.log ("Start:" +start.x+ "," +start.y+ "/dest:" +dest.x+ "," +dest.y ");
var openlist = new Array ();
var closelist = new Array ();
Start.costfromstart = 0;
Start.costtoobject = Start.getcost (dest);
Openlist.push (start); while (Openlist.length > 0) {var firstnode = Openlist.shift ();//Return and delete the first element if (firstnode.x = = Dest.x && fi Rstnode.y = = Dest.y) {return Game.makepath (theNode);
}else{Closelist.push (Firstnode);
var neighbors = Firstnode.getneighbornodes ();
for (var i=0 i<neighbors.length; i++) {var neighbornode = neighbors[i];
Whether to open the list var isopended = neighbornode.isposcollided (openlist);
Whether to close the list var isclosed = neighbornode.isposcollided (closelist);
Whether and obstacle collision var Iscollidewithwall = neighbornode.isposcollided (Walls); if (!isopended &&!isclosed &&!iscollidewithwall) {Neighbornode.costfromstart = Firstnode.costfromst
Art + 1;
Neighbornode.costtoobject = Neighbornode.getcost (dest);
Neighbornode.parentnode = Firstnode;
Openlist.push (Neighbornode); CRAFTY.E ("2d, Canvas, color, Solid"). Color ("LightBlue"). attr ({w:game.tilewidth, h:game.tileheight, X:gam
E.tilewidth*neighbornode.x, Y:game.tileheight*neighbornode.y}). Timeout (function () {This.destroy ();}, 1500); Press near-far sort openlist.sort (function (Node1, Node2) {return (Node1.costfromstart + node1.costtoobject)-(Node2.costfromstart + node2.costtoobject);
});
}}} openlist = undefined;
Closelist = undefined;
return undefined;
}, Makepath:function (node) {var path = new Array ();
while (Node.parentnode!= undefined) {Path.push (node);
node = Node.parentnode;
//add last node Path.push (node);
return path;
},
};
function Node (POSX, posy) {this.x = Posx;
This.y = Posy;
This.costfromstart = undefined;
This.costtoobject = undefined;
This.parentnode = undefined;
};
Node.prototype = {Constructor:node, getcost:function (node) {if (node = = undefined) {return undefined;
Return Crafty.math.distance (node.x, Node.y, this.x, THIS.Y);
}, Getneighbornodes:function () {var neighbors = new Array ();
Neighbors.push (New Node (this.x + 1, this.y));
Neighbors.push (New Node (this.x, This.y + 1));
Neighbors.push (New Node (This.x-1, this.y)); Neighbors.push New Node (this.x, this.y-1));
return neighbors; }, Isposcollided:function (arr) {for (var i=0; i<arr.length; i++) {if (arr[i)!= undefined && arr[i].x = t
His.x && arr[i].y = = This.y) {return true;
return false;
}
};
By the way, spit it out Firefox 32.0.3, switch to 360 browser 7.1 Nice