From e9073b18db91643d3f1164436feb5d3c7fede3d3 Mon Sep 17 00:00:00 2001 From: dsc Date: Fri, 19 Nov 2010 04:04:40 -0800 Subject: [PATCH] Adds support for visualizing multiple ai paths. Reconfigures starting game. --- src/tanks/config.js | 4 ++-- src/tanks/game/game.js | 2 +- src/tanks/main.js | 7 +++++-- src/tanks/map/level.js | 3 ++- src/tanks/map/pathmap.js | 40 ++++++++++++++++++++++++++-------------- src/tanks/thing/tank.js | 6 ++---- 6 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/tanks/config.js b/src/tanks/config.js index 46503eb..304ea63 100644 --- a/src/tanks/config.js +++ b/src/tanks/config.js @@ -2,10 +2,10 @@ tanks.config = { pathing : { overlayPathmap : false, - overlayAIPaths : true, + overlayAIPaths : false, traceTrajectories : false }, debug : { - projectiles : 0 + tanks : 1 } }; \ No newline at end of file diff --git a/src/tanks/game/game.js b/src/tanks/game/game.js index ba148a9..7e6d0df 100644 --- a/src/tanks/game/game.js +++ b/src/tanks/game/game.js @@ -52,7 +52,7 @@ tanks.Game = new Y.Class('Game', { SQUARETH = REF_SIZE * SECONDTH if (!tanks.config.pathing.overlayAIPaths) - this.pathmap.clearPath(); + this.pathmap.hidePaths(); this.active.invoke('updateCooldowns', NOW); this.active.invoke('act'); diff --git a/src/tanks/main.js b/src/tanks/main.js index 233dc98..6eccb99 100644 --- a/src/tanks/main.js +++ b/src/tanks/main.js @@ -11,8 +11,11 @@ function main(){ LBT = new tanks.Game(); ctx = LBT.level.ctx; - P = LBT.addUnit(new PlayerTank(1), 8,9); - E = LBT.addUnit(new Tank(2), 1,2); + P = LBT.addUnit(new PlayerTank(1), 6,9); + E = + LBT.addUnit(new Tank(2), 0,1); + LBT.addUnit(new Tank(2), 1,0); + LBT.addUnit(new Tank(2), 8,1); setupUI(); diff --git a/src/tanks/map/level.js b/src/tanks/map/level.js index ce7602b..1cfd7d8 100644 --- a/src/tanks/map/level.js +++ b/src/tanks/map/level.js @@ -13,7 +13,8 @@ Level = Rect.subclass('Level', { Y.map([ [6,1, 1,4], [6,7, 1,2], [1,7, 2,2], - [3,6, 1,1] ], + [3,6, 1,1], + [2,2, 1,1] ], "this.addWall.apply(this, Y.map(_, '*50'))", this ); }, diff --git a/src/tanks/map/pathmap.js b/src/tanks/map/pathmap.js index e113de3..b57fe9b 100644 --- a/src/tanks/map/pathmap.js +++ b/src/tanks/map/pathmap.js @@ -182,7 +182,7 @@ PathMap = new Y.Class('PathMap', QuadTree, { return d1 + d2; }, - path : function path(start, end){ + path : function path(start, end, id){ var size = this.gridSquareSize, floor = Math.floor , grid = this.grid() @@ -198,7 +198,7 @@ PathMap = new Y.Class('PathMap', QuadTree, { ; if (tanks.config.pathing.overlayAIPaths) - this.drawPath(startN, path); + this.drawPath(id, startN, path); return path .invoke('scale', size) @@ -206,16 +206,16 @@ PathMap = new Y.Class('PathMap', QuadTree, { .end(); }, - drawPath : function drawPath(start, path){ - var size = this.gridSquareSize, off = size*0.15 + drawPath : function drawPath(id, start, path){ + var size = this.gridSquareSize, off , w = this.width-2, h = this.height-2 , el = this.game.viewport , grid = this.grid() - , canvas = $('.path', el)[0] + , canvas = $('#path'+id, el)[0] ; if (!canvas) { - canvas = $('').appendTo(el)[0]; + canvas = $('').appendTo(el)[0]; $(canvas).width(w).height(h); canvas.width = w; canvas.height = h; @@ -251,10 +251,11 @@ PathMap = new Y.Class('PathMap', QuadTree, { // Draw path - var r = size/2 - off; + off = size*0.3; - function drawStep(p, i){ - var x = p.x*size + off + r + function drawStep(p){ + var r = size/2 - off + , x = p.x*size + off + r , y = p.y*size + off + r ; ctx.beginPath(); @@ -264,22 +265,33 @@ PathMap = new Y.Class('PathMap', QuadTree, { } this.drawStep = drawStep; - ctx.fillStyle = 'rgba(0,0,0,0.2)'; + ctx.fillStyle = 'rgba(0,0,0,0.1)'; + drawStep( start ); path.forEach(drawStep); // Draw start - ctx.fillStyle = 'rgba(0,255,0,0.2)'; - drawStep(start, 0); + off = size*0.15; + + ctx.fillStyle = 'rgba(0,255,0,0.05)'; + drawStep( start ); // Draw finish - ctx.fillStyle = 'rgba(0,0,255,0.2)'; + ctx.fillStyle = 'rgba(0,0,255,0.05)'; drawStep( path.last() ); $(canvas).show(); }, - clearPath : function clearPath(){ + destroyPath : function destroyPath(id){ + $('#path'+id, this.game.viewport).remove(); + }, + + hidePath : function hidePath(id){ + $('#path'+id, this.game.viewport).hide(); + }, + + hidePaths : function hidePaths(){ $('.path', this.game.viewport).hide(); }, diff --git a/src/tanks/thing/tank.js b/src/tanks/thing/tank.js index 3b9974c..d3dfca4 100644 --- a/src/tanks/thing/tank.js +++ b/src/tanks/thing/tank.js @@ -33,7 +33,7 @@ Tank = Thing.subclass('Tank', { var self = this; this.addEventListener('destroy', function(){ - this.game.pathmap.clearPath(); + this.game.pathmap.destroyPath(this.id); }); }, @@ -86,15 +86,13 @@ Tank = Thing.subclass('Tank', { if (!t) return; - pm.clearPath(); - // console.log(this, 'moving toward', t); var end = t.boundingBox.midpoint() , bb = this.boundingBox , mid = bb.midpoint() , start = mid - , path = this.lastPath = pm.path(start, end) + , path = this.lastPath = pm.path(start, end, this.id) , to = this.currentMove = path[0] ; -- 1.7.0.4