From eefb2a8203e59827017591ec77946f2b7a1fcec8 Mon Sep 17 00:00:00 2001 From: MalphasWats Date: Wed, 2 Jan 2013 16:27:17 +0000 Subject: [PATCH] tests for TileMap.findPath() added, findPath refactored and invert mode added. --- jaws-min.js | 47 +++++++++++++++++++++++-------------------- jaws.js | 11 +++++----- src/tile_map.js | 46 +++++++++++++++++++----------------------- test/tile_map.js | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 54 deletions(-) diff --git a/jaws-min.js b/jaws-min.js index 00cfb0d..b1445b4 100644 --- a/jaws-min.js +++ b/jaws-min.js @@ -1,25 +1,25 @@ var jaws=function(c){function b(b){c.mouse_x=b.pageX||b.clientX;c.mouse_y=b.pageY||b.clientX;b=c.canvas?c.canvas:c.dom;c.mouse_x-=b.offsetLeft;c.mouse_y-=b.offsetTop}var d,e;c.title=function(b){return b?d.innerHTML=b:d.innerHTML};c.unpack=function(){"Sprite SpriteList Animation Viewport SpriteSheet Parallax TileMap Rect pressed".split(" ").forEach(function(b){window[b]?c.log(b+"already exists in global namespace"):window[b]=c[b]})};c.log=function(b,c){e&&(b+="
",e.innerHTML=c?e.innerHTML.toString()+ b:b)};c.init=function(f){d=document.getElementsByTagName("title")[0];c.url_parameters=c.getUrlParameters();e=document.getElementById("jaws-log");c.url_parameters.debug&&!e&&(e=document.createElement("div"),e.id="jaws-log",e.style.cssText="overflow: auto; color: #aaaaaa; width: 300px; height: 150px; margin: 40px auto 0px auto; padding: 5px; border: #444444 1px solid; clear: both; font: 10px verdana; text-align: left;",document.body.appendChild(e));c.canvas=document.getElementsByTagName("canvas")[0]; -c.canvas||(c.dom=document.getElementById("canvas"));c.canvas?c.context=c.canvas.getContext("2d"):c.dom?c.dom.style.position="relative":(c.canvas=document.createElement("canvas"),c.canvas.width=f.width,c.canvas.height=f.height,c.context=c.canvas.getContext("2d"),document.body.appendChild(c.canvas));c.width=c.canvas?c.canvas.width:c.dom.offsetWidth;c.height=c.canvas?c.canvas.height:c.dom.offsetHeight;c.mouse_x=0;c.mouse_y=0;window.addEventListener("mousemove",b)};c.start=function(b,d,e){function j(b){c.context&& -d.loading_screen&&(c.context.save(),c.context.fillStyle="black",c.context.fillRect(0,0,c.width,c.height),c.context.textAlign="center",c.context.fillStyle="white",c.context.font="15px terminal",c.context.fillText("Loading",c.width/2,c.height/2-30),c.context.font="bold 30px terminal",c.context.fillText(b+"%",c.width/2,c.height/2),c.context.restore())}function k(b,d){c.log(d+"%: "+b,!0);j(d)}function p(b){c.log("Error loading: "+b,!0)}function m(){c.log("all assets loaded",!0);c.switchGameState(b||window, -{fps:n},e)}d||(d={});var n=d.fps||60;void 0===d.loading_screen&&(d.loading_screen=!0);d.width||(d.width=500);d.height||(d.height=300);c.init(d);j(0);c.log("setupInput()",!0);c.setupInput();c.log("assets.loadAll()",!0);0b.x||0>b.y||b.x>c.width||b.y>c.height};c.forceInsideCanvas=function(b){0>b.x&&(b.x=0);b.x>c.width&&(b.x=c.width);0>b.y&&(b.y=0);b.y>c.height&& -(b.y=c.height)};c.getUrlParameters=function(){for(var b=[],c,d=window.location.href.slice(window.location.href.indexOf("?")+1).split("&"),e=0;ethis.size){this.values.splice(0,1);for(var c=this.value=0;this.values[c];c++)this.value+=this.values[c];this.value/=this.size}this.values.push(b);return this};this.get=function(){return parseInt(this.value)}}window.requestAnimFrame=window.requestAnimationFrame||window.webkitRequestAnimationFrame||window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(b){window.setTimeout(b,16.666)};c.GameLoop=function(d,e, -f){if(!(this instanceof arguments.callee))return new arguments.callee(d,e);this.fps=this.tick_duration=this.ticks=0;var g,h=!1,j=!1,k=this,p=new b(20);this.runtime=function(){return this.last_tick-this.first_tick};this.start=function(){c.log("game loop start",!0);this.first_tick=(new Date).getTime();this.current_tick=(new Date).getTime();this.last_tick=(new Date).getTime();d.setup&&d.setup(f);step_delay=1E3/e.fps;60==e.fps?requestAnimFrame(this.loop):g=setInterval(this.loop,step_delay);c.log("game loop loop", -!0)};this.loop=function(){k.current_tick=(new Date).getTime();k.tick_duration=k.current_tick-k.last_tick;k.fps=p.add(1E3/k.tick_duration).get();!j&&!h&&(d.update&&d.update(),d.draw&&d.draw(),k.ticks++);60==e.fps&&!j&&requestAnimFrame(k.loop);k.last_tick=k.current_tick};this.pause=function(){h=!0};this.unpause=function(){h=!1};this.stop=function(){g&&clearInterval(g);j=!0}};return c}(jaws||{}),jaws=function(c){c.Rect=function(b,c,e,f){if(!(this instanceof arguments.callee))return new arguments.callee(b, +f){if(!(this instanceof arguments.callee))return new arguments.callee(d,e);this.ticks=this.fps=this.tick_duration=0;var h,k=!1,g=!1,j=this,r=new b(20);this.runtime=function(){return this.last_tick-this.first_tick};this.start=function(){c.log("game loop start",!0);this.first_tick=(new Date).getTime();this.current_tick=(new Date).getTime();this.last_tick=(new Date).getTime();d.setup&&d.setup(f);step_delay=1E3/e.fps;60==e.fps?requestAnimFrame(this.loop):h=setInterval(this.loop,step_delay);c.log("game loop loop", +!0)};this.loop=function(){j.current_tick=(new Date).getTime();j.tick_duration=j.current_tick-j.last_tick;j.fps=r.add(1E3/j.tick_duration).get();!g&&!k&&(d.update&&d.update(),d.draw&&d.draw(),j.ticks++);60==e.fps&&!g&&requestAnimFrame(j.loop);j.last_tick=j.current_tick};this.pause=function(){k=!0};this.unpause=function(){k=!1};this.stop=function(){h&&clearInterval(h);g=!0}};return c}(jaws||{}),jaws=function(c){c.Rect=function(b,c,e,f){if(!(this instanceof arguments.callee))return new arguments.callee(b, c,e,f);this.x=b;this.y=c;this.width=e;this.height=f;this.right=b+e;this.bottom=c+f};c.Rect.prototype.getPosition=function(){return[this.x,this.y]};c.Rect.prototype.move=function(b,c){this.x+=b;this.y+=c;this.right+=b;this.bottom+=c;return this};c.Rect.prototype.moveTo=function(b,c){this.x=b;this.y=c;this.right=this.x+this.width;this.bottom=this.y+this.height;return this};c.Rect.prototype.resize=function(b,c){this.width+=b;this.height+=c;this.right=this.x+this.width;this.bottom=this.y+this.height; return this};c.Rect.prototype.resizeTo=function(b,c){this.width=b;this.height=c;this.right=this.x+this.width;this.bottom=this.y+this.height;return this};c.Rect.prototype.draw=function(){c.context.strokeStyle="red";c.context.strokeRect(this.x,this.y,this.width,this.height);return this};c.Rect.prototype.collidePoint=function(b,c){return b>=this.x&&b<=this.right&&c>=this.y&&c<=this.bottom};c.Rect.prototype.collideRect=function(b){return(this.x>=b.x&&this.x<=b.right||b.x>=this.x&&b.x<=this.right)&&(this.y>= b.y&&this.y<=b.bottom||b.y>=this.y&&b.y<=this.bottom)};c.Rect.prototype.toString=function(){return"[Rect "+this.x+", "+this.y+", "+this.width+", "+this.height+"]"};return c}(jaws||{});"undefined"!==typeof module&&"exports"in module&&(module.exports=jaws.Rect); @@ -43,7 +43,7 @@ function(){return this.sprites.filter.apply(this.sprites,arguments)};c.SpriteLis function(){return this.sprites.reduceRight.apply(this.sprites,arguments)};c.SpriteList.prototype.some=function(){return this.sprites.some.apply(this.sprites,arguments)};c.SpriteList.prototype.isSpriteList=function(){return!0};c.SpriteList.prototype.load=function(b){function d(b){b.forEach(function(b){var d=b._constructor?eval(b._constructor):b.constructor;c.isFunction(d)&&(c.log("Creating "+b._constructor+"("+b.toString()+")",!0),d=new d(b),d._constructor=b._constructor||b.constructor.name,e.push(d))})} var e=this;c.isArray(b)?b.every(function(b){return b._constructor})?d(b):this.sprites=b:c.isString(b)&&(d(JSON.parse(b)),console.log(b));this.updateLength()};c.SpriteList.prototype.remove=function(b){b=this.indexOf(b);-1e&&(e=0);var f=parseInt(b.right/this.cell_size[0]);f>=this.size[0]&&(f=this.size[0]-1);var g=parseInt(b.y/this.cell_size[1]);0>g&&(g=0);var h=parseInt(b.bottom/this.cell_size[1]);h>=this.size[1]&&(h=this.size[1]-1);for(b=e;b<=f;b++)for(e=g;e<=h;e++)this.cells[b][e].forEach(function(b){-1==c.indexOf(b)&&c.push(b)})}catch(j){}return c}; -c.TileMap.prototype.all=function(){for(var b=[],c=0;cj;j++)h[j]=j;for(var j=1,k=b.length;0<=j;k=b.length){for(g.push(h.map(f));0<=j&&h[j]==k- -1;)j--,k--;if(0<=j){h[j]+=1;for(j+=1;2>j;j++)h[j]=h[j-1]+1;j=1}}g.forEach(function(b){c.collideOneWithOne(b[0],b[1])&&e.push([b[0],b[1]])})}else b.forEach(function(b){d.forEach(function(d){c.collideOneWithOne(b,d)&&e.push([b,d])})});return e};c.collideCircles=function(b,d){return c.distanceBetween(b,d)=c.x&&b.x<=c.right||c.x>=b.x&&c.x<=b.right)&&(b.y>=c.y&&b.y<=c.bottom||c.y>=b.y&&c.y<=b.bottom)};c.distanceBetween=function(b,c){return Math.sqrt(Math.pow(b.x- +e=parseInt(b.y/this.cell_size[1]);return this.pushToCell(c,e,b)};c.TileMap.prototype.pushAsRect=function(b,c){for(var e=parseInt(c.x/this.cell_size[0]),f=parseInt((c.right-1)/this.cell_size[0]);e<=f;e++)for(var h=parseInt(c.y/this.cell_size[1]),k=parseInt((c.bottom-1)/this.cell_size[1]);h<=k;h++)this.pushToCell(e,h,b);return b};c.TileMap.prototype.pushToCell=function(b,c,e){this.cells[b][c].push(e);this.sortFunction&&this.cells[b][c].sort(this.sortFunction);return this};c.TileMap.prototype.at=function(b, +c){var e=parseInt(b/this.cell_size[0]),f=parseInt(c/this.cell_size[1]);return this.cells[e][f]};c.TileMap.prototype.atRect=function(b){var c=[];try{var e=parseInt(b.x/this.cell_size[0]);0>e&&(e=0);var f=parseInt(b.right/this.cell_size[0]);f>=this.size[0]&&(f=this.size[0]-1);var h=parseInt(b.y/this.cell_size[1]);0>h&&(h=0);var k=parseInt(b.bottom/this.cell_size[1]);k>=this.size[1]&&(k=this.size[1]-1);for(b=e;b<=f;b++)for(e=h;e<=k;e++)this.cells[b][e].forEach(function(b){-1==c.indexOf(b)&&c.push(b)})}catch(g){}return c}; +c.TileMap.prototype.all=function(){for(var b=[],c=0;cn)p[t][u]={parent:[g,j],G:r+1,score:n}}}j=[];t=[];g=s;for(m=r=0;mg;g++)k[g]=g;for(var g=1,j=b.length;0<=g;j=b.length){for(h.push(k.map(f));0<=g&&k[g]==j- +1;)g--,j--;if(0<=g){k[g]+=1;for(g+=1;2>g;g++)k[g]=k[g-1]+1;g=1}}h.forEach(function(b){c.collideOneWithOne(b[0],b[1])&&e.push([b[0],b[1]])})}else b.forEach(function(b){d.forEach(function(d){c.collideOneWithOne(b,d)&&e.push([b,d])})});return e};c.collideCircles=function(b,d){return c.distanceBetween(b,d)=c.x&&b.x<=c.right||c.x>=b.x&&c.x<=b.right)&&(b.y>=c.y&&b.y<=c.bottom||c.y>=b.y&&c.y<=b.bottom)};c.distanceBetween=function(b,c){return Math.sqrt(Math.pow(b.x- c.x,2)+Math.pow(b.y-c.y,2))};return c}(jaws||{}); -jaws=function(c){c.gfx={};c.gfx.retroScaleImage=function(b,d){var e=c.isImage(b)?c.imageToCanvas(b):b,e=e.getContext("2d").getImageData(0,0,e.width,e.height).data,f=document.createElement("canvas");f.width=b.width*d;f.height=b.height*d;for(var g=f.getContext("2d"),h=g.createImageData(f.width,f.height),j=h.width,k=h.height,p=0;p score)) { - open_nodes[col-1][row] = {parent: [col, row], G: step+1, score: score} - } - } - - if (this.cell(col+1, row).length === 0 && !findInClosed(col+1, row)) { - score = step+1+crowFlies([col+1,row] , [end_col, end_row]) - if (!open_nodes[col+1][row] || (open_nodes[col+1][row] && open_nodes[col+1][row].score > score)) { - open_nodes[col+1][row] = {parent: [col, row], G: step+1, score: score} - } - } + var left_right_up_down = [] + if (col > 0) { left_right_up_down.push([col-1, row]) } + if (col < this.size[0]-1) { left_right_up_down.push([col+1, row]) } + if (row > 0) {left_right_up_down.push([col, row-1])} + if (row < this.size[1]-1) { left_right_up_down.push([col, row+1]) } - if (this.cell(col, row-1).length === 0 && !findInClosed(col, row-1)) { - score = step+1+crowFlies([col,row-1] , [end_col, end_row]) - if (!open_nodes[col][row-1] || (open_nodes[col][row-1] && open_nodes[col][row-1].score > score)) { - open_nodes[col][row-1] = {parent: [col, row], G: step+1, score: score} - } - } - - if (this.cell(col, row+1).length === 0 && !findInClosed(col, row+1)) { - score = step+1+crowFlies([col,row+1] , [end_col, end_row]) - if (!open_nodes[col][row+1] || (open_nodes[col][row+1] && open_nodes[col][row+1].score > score)) { - open_nodes[col][row+1] = {parent: [col, row], G: step+1, score: score} - } + for (var i=0 ; i 0 && inverted) ) && + !findInClosed(c, r) ) + { + score = step+1+crowFlies([c, r] , [end_col, end_row]) + if (!open_nodes[c][r] || (open_nodes[c][r] && open_nodes[c][r].score > score)) { + open_nodes[c][r] = {parent: [col, row], G: step+1, score: score} + } + } } /** diff --git a/test/tile_map.js b/test/tile_map.js index 344327a..8ea26a8 100644 --- a/test/tile_map.js +++ b/test/tile_map.js @@ -50,3 +50,55 @@ test("TileMap simple usage", function () { var bottom_right = new jaws.Rect(5 * 32, 5 * 32, 5 * 32, 5 * 32); ok(tile_map.atRect(bottom_right).length >= 25, "There should be at least 25 tiles showing") }) + +test("TileMap findPath usage", function () { + var cell_size = 32 + var tile_map = new jaws.TileMap({size: [10, 10], cell_size: [cell_size, cell_size]}) + + var start_position = [0, 0] + var end_position = [0, 64] //2 squares down. + + var path_one = tile_map.findPath(start_position, end_position) + ok(path_one.length >= 3, "There should be at least 3 nodes in path") + ok( ( path_one[0][0] === start_position[0]/cell_size && + path_one[0][1] === start_position[1]/cell_size ), "The first node in path should be the start_position") + var last = path_one.length - 1 + ok( ( path_one[last][0] === end_position[0]/cell_size && + path_one[last][1] === end_position[1]/cell_size ), "The last node in path should be the end_position") + + tile_map.push(new jaws.Sprite({image: "rect.png", x:0, y:32})) + + var path_two = tile_map.findPath(start_position, end_position) + ok(path_two.length > path_one.length, "Wall added should force new path to be longer than first path") + + var wall_in_path = false + for(var i=0 ; i