Grafos
Estudo de trajeto e movimentação por um mapa
by edgardleal
JavaScript
function Node(){
this.parent = null;
var childs = [];
this.add = function(node){
this.childs[this.childs.length] = node;
};
this.remove = function(node){
childs.forEach(function(ele, index, array){
if(node==ele){
for(var i = index; i < array.length - 1; i++)
array[i] = array[i+1];
array.pop();
return true;
}
});
return false;
}
}
var blockIdCount = 0;
function Block(x,y,w,h){
this.x = x?x:0;
this.y = y?y:-15;
this.w = w?w:10;
this.h = h?h:10;
var stringElement = "<div id='block_" + (blockIdCount++) +
"' style='position:absolute;border : 1px solid;'></div>";
this.toString = function(){
return "[" + this.x + ", " + this.y + ", " +
this.w + ", " + this.h + "]";
};
this.refresh = function(){
element.css({left : this.x + "px", top : this.y + 'px',
width : this.w + 'px', height : this.h + 'px'});
};
this.goTo = function(x, y, callBack, next){
this.x = x;
this.y = y;
element.animate({left : x + 'px', top : y + 'px'},1000, callBack(next));
};
var element = $(stringElement).appendTo("body");
this.refresh();
}
Block.prototype = new Node();
function Walker(route){
var path = route;
this.prorotype = new Block();
var callBackWalk = function(index){
if(!index || index >= path.length) return;
goTo(path[index][0], path[index][1], callBackWalk, index + 1);
}
this.walk = function(){
goTo(path[0][0], path[0][1],callBackWalk, 1);
}
}
//Walker.prototype = new Block();
function Map(w , h){
var root = new Block(0,0);
this.colsCount = w;
this.rowsCount = h;
}
var b = new Block(10,15);
var w = new Walker([[10,10], [20,10], [20,20]]);
w.walk();
b.goTo(20, 25);
console.log(b.toString());