Adds support for visualizing multiple ai paths. Reconfigures starting game.
authordsc <david.schoonover@gmail.com>
Fri, 19 Nov 2010 12:04:40 +0000 (04:04 -0800)
committerdsc <david.schoonover@gmail.com>
Fri, 19 Nov 2010 12:04:40 +0000 (04:04 -0800)
src/tanks/config.js
src/tanks/game/game.js
src/tanks/main.js
src/tanks/map/level.js
src/tanks/map/pathmap.js
src/tanks/thing/tank.js

index 46503eb..304ea63 100644 (file)
@@ -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
index ba148a9..7e6d0df 100644 (file)
@@ -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');
index 233dc98..6eccb99 100644 (file)
@@ -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();
     
index ce7602b..1cfd7d8 100644 (file)
@@ -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 );
     },
index e113de3..b57fe9b 100644 (file)
@@ -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 = $('<canvas class="path" style="position:absolute"/>').appendTo(el)[0];
+            canvas = $('<canvas id="path'+id+'" class="path" style="position:absolute"/>').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();
     },
     
index 3b9974c..d3dfca4 100644 (file)
@@ -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]
         ;