Prevents the user from firing a shot into a barrier.
authordsc <david.schoonover@gmail.com>
Thu, 18 Nov 2010 02:46:51 +0000 (18:46 -0800)
committerdsc <david.schoonover@gmail.com>
Thu, 18 Nov 2010 02:46:51 +0000 (18:46 -0800)
src/tanks/main.js
src/tanks/map/trajectory.js
src/tanks/thing/bullet.js
src/tanks/thing/thing.js
src/tanks/ui/player.js

index e715777..7e5e0b8 100644 (file)
@@ -24,6 +24,8 @@ function main(){
     new Player(LBT, T);
     
     setupUI();
+    
+    // barrel = T.barrel;
     // B = bullets.attr(0);
     // R = B.trajectory;
 }
index 01cdaa2..e99fca0 100644 (file)
@@ -1,7 +1,7 @@
 Trajectory = new Y.Class('Trajectory', math.Line, {
+    depth   : 0,
     elapsed : 0,
-    depth : 0,
-    
+    bounces : 0,
     
     init : function initTrajectory(owner, x1,y1, x2,y2, tdist){
         this.owner   = owner;
@@ -64,12 +64,13 @@ Trajectory = new Y.Class('Trajectory', math.Line, {
             
             // Blocked! Reflect trajectory
             if ( test ) {
+                this.bounces++;
                 to       = test.to;
                 side     = test.side;
                 blockers = test.blockers;
                 
                 if (!side) {
-                    console.error('Null side! Something horrible has occurred!');
+                    console.error('Null reflection line!', 'to:', to, 'blockers:', blockers);
                     continue;
                 }
                 
index 3b59033..83041c2 100644 (file)
@@ -1,5 +1,5 @@
 //  -*- mode: JavaScript; tab-width: 4; indent-tabs-mode: nil; -*-
-Bullet = new Y.Class('Bullet', Thing, {
+Bullet = Thing.subclass('Bullet', {
     traceTrajectories : true,
     
     
@@ -31,6 +31,8 @@ Bullet = new Y.Class('Bullet', Thing, {
         range     : 0.1  // attack range (squares)
     },
     
+    bounces : 0,
+    
     fillStats : function(){
         this.stats = Y({},
             Thing.fillStats(this.owner.stats),
@@ -64,7 +66,11 @@ Bullet = new Y.Class('Bullet', Thing, {
     
     move : function move(){
         var to = this.trajectory.step( ELAPSED );
-        this.game.moveAgentTo(this, to.x, to.y);
+        this.bounces = this.trajectory.bounces;
+        if (this.bounces > 1)
+            this.destroy();
+        else
+            this.game.moveAgentTo(this, to.x, to.y);
         return this;
     },
     
index 391627f..20eb553 100644 (file)
@@ -43,8 +43,7 @@ Thing = new Evt.Class('Thing', {
         if (this.dead) return this;
         
         this.dead = true;
-        this.fire('destroy', this);
-        return this;
+        return this.remove().fire('destroy', this);
     },
     
     remove : function remove(){
index 841d7f1..4abed69 100644 (file)
@@ -52,6 +52,72 @@ Player = new Y.Class('Player', {
             ;
     },
     
+    
+    act : function act(){
+        if (this.tank.dead)
+            return this.tank;
+        
+        var action = this.queue.shift();
+        
+        if (action && action.type === 'fire') {
+            // console.log('fire', [action.x, action.y], 'loc', this.tank.loc)
+            this.tank.fireProjectile(action.x, action.y);
+        } else if ( this.activeKeys.size() )
+            this.move();
+        
+        return this.tank;
+    },
+    
+    fire : function fire(){
+        var WIGGLE = 4
+        
+        ,   tank = this.tank
+        ,   barrel = tank.barrel
+        ,   bb = tank.boundingBox
+        ,   w2 = bb.width/2,    h2 = bb.height/2
+        ,   x0 = bb.x1+w2,      y0 = bb.y1+h2
+        
+        ,   theta  = barrel.transform.rotate
+        ,   sin = Math.sin(theta),  cos = Math.cos(theta)
+        
+        ,   x1 = x0 + w2*cos, y1 = y0 + h2*sin
+        ,   sz = (barrel.boundingBox.width - w2)/2 + WIGGLE
+        ,   blockers = this.pathmap.get(x1-sz,y1-sz, x1+sz,y1+sz).remove(tank)
+        ;
+        
+        if ( blockers.size() ) return; // console.log('squelch!', blockers);
+        
+        this.queue.push({
+            type : 'fire',
+            x : x0 + REF_SIZE*cos,
+            y : y0 + REF_SIZE*sin
+        });
+    },
+    
+    move : function move(){
+        var dir = this.activeKeys
+            .unique()
+            .sort()
+            .join(' ');
+        
+        if (!dir) return;
+        
+        var tank  = this.tank
+        ,   toLoc = tank.loc.moveByDir(dir, (tank.stats.move * SQUARETH))
+        
+        ,   x = toLoc.x, y = toLoc.y
+        ,   bb = tank.boundingBox.add(x,y)
+        
+        ,   blockers = this.pathmap.get(bb.x1,bb.y1, bb.x2,bb.y2).remove(tank)
+        ;
+        
+        if ( !blockers.size() )
+            this.game.moveAgentTo(tank, x,y);
+    },
+    
+    
+    
+    
     updateMeta : function updateMeta(evt){
         this.shift = evt.shiftKey;
         this.alt   = evt.altKey;
@@ -63,15 +129,7 @@ Player = new Y.Class('Player', {
         this.updateMeta(evt);
         
         if (evt.which === Key.fire) {
-            var theta = this.tank.barrel.transform.rotate
-            ,   sin = Math.sin(theta), cos = Math.cos(theta)
-            ,   bb = this.tank.boundingBox
-            ;
-            this.queue.push({
-                type : 'fire',
-                x : bb.x1+bb.width/2  + REF_SIZE*cos,
-                y : bb.y1+bb.height/2 + REF_SIZE*sin
-            });
+            this.fire();
             return;
         }
         
@@ -126,44 +184,6 @@ Player = new Y.Class('Player', {
         ,   theta = Math.atan2(-y,-x);
         
         barrel.rotate(theta);
-    },
-    
-    
-    
-    act : function act(){
-        if (this.tank.dead)
-            return this.tank;
-        
-        var action = this.queue.shift();
-        
-        if (action && action.type === 'fire') {
-            // console.log('fire', [action.x, action.y], 'loc', this.tank.loc)
-            this.tank.fireProjectile(action.x, action.y);
-        } else if ( this.activeKeys.size() )
-            this.move();
-        
-        return this.tank;
-    },
-    
-    move : function move(){
-        var dir = this.activeKeys
-            .unique()
-            .sort()
-            .join(' ');
-        
-        if (!dir) return;
-        
-        var tank  = this.tank
-        ,   toLoc = tank.loc.moveByDir(dir, (tank.stats.move * SQUARETH))
-        
-        ,   x = toLoc.x, y = toLoc.y
-        ,   bb = tank.boundingBox.add(x,y)
-        
-        ,   blockers = this.pathmap.get(bb.x1,bb.y1, bb.x2,bb.y2).remove(tank)
-        ;
-        
-        if ( !blockers.size() )
-            this.game.moveAgentTo(tank, x,y);
     }
     
 });
\ No newline at end of file