Tank and Tower now delegate to Barrel for calculations.
authordsc <david.schoonover@gmail.com>
Mon, 11 Apr 2011 09:01:34 +0000 (02:01 -0700)
committerdsc <david.schoonover@gmail.com>
Mon, 11 Apr 2011 09:01:34 +0000 (02:01 -0700)
src/tanks/fx/barrel.cjs
src/tanks/thing/tank.cjs
src/tanks/thing/tower.cjs

index 339969f..40b732e 100644 (file)
@@ -2,7 +2,7 @@ var Y = require('Y').Y
 ,   Vec  = require('ezl/math/vec').Vec
 ,   Rect = require('ezl/shape').Rect
 
-// ,   ORIGIN = new Vec(0,0)
+,   ORIGIN = new Vec(0,0)
 // ,   FACING_RIGHT = exports['FACING_RIGHT'] = 'right'
 // ,   FACING_LEFT  = exports['FACING_LEFT']  = 'left'
 ,
@@ -56,18 +56,13 @@ Rect.subclass('Barrel', {
      */
     barrelTipLoc : function barrelTipLoc(wiggle, relParent){
         var theta = this.transform.rotate
-        ,   sin = Math.sin(theta),  cos = Math.cos(theta)
         
         // (sqrt(2)/2 * wiggle) is max diagonal to ensure projectile doesn't overlap with firing unit
         ,   len = this.bbox.originRight + (wiggle || 0)*0.707
         
-        // ,   len = this.bbox.originDist(this.facing) + (wiggle || 0)*0.707
-        // ,   olen = (this.facing === FACING_LEFT ? this.bbox.originLeft : this.bbox.originRight)
-        // ,   len  = olen + (wiggle || 0)*0.707
-        
         ,   o = ((relParent && this.parent) ? this.parent.loc : ORIGIN)
-        ,   x = o.x + this.loc.x + len*cos
-        ,   y = o.y + this.loc.y + len*sin
+        ,   x = o.x + len*Math.cos(theta)
+        ,   y = o.y + len*Math.sin(theta)
         ;
         return new Vec(x,y);
     }
index dc3ebc8..85e62a7 100644 (file)
@@ -271,22 +271,8 @@ Unit.subclass('Tank', {
     
     
     getTurretLoc : function getTurretLoc(){
-        var WIGGLE = 2
-        ,   loc    = this.loc
-        ,   barrel = this.barrelShape
-        
-        ,   theta  = barrel.transform.rotate
-        ,   sin = Math.sin(theta),  cos = Math.cos(theta)
-        
-        // sqrt(2)/2 * (P.width + WIGGLE)
-        // is max diagonal to ensure we don't overlap with the firing unit
-        ,   pw  = this.projectile.fn.width
-        ,   len = barrel.bbox.width + 0.707*(pw+WIGGLE)
-        
-        ,   x = loc.x + len*cos
-        ,   y = loc.y + len*sin
-        ;
-        return new Vec(x,y);
+        var WIGGLE = 2, pw = this.projectile.fn.width;
+        return this.barrelShape.barrelTipLoc(WIGGLE+pw, true);
     },
     
     getBarrelAngle : function getBarrelAngle(){
@@ -298,21 +284,14 @@ Unit.subclass('Tank', {
         return this;
     },
     
-    // FIXME: delegate to Barrel
     rotateBarrelRelPage : function rotateBarrelRelPage(pageX, pageY){
-        var shape  = this.shape
-        ,   off = shape.offset()
-        ,   w = this.width, h = this.height
-        ,   x = off.left + w/2 - pageX
-        ,   y = off.top  + h/2 - pageY
-        ,   theta = Math.atan2(-y,-x)
-        ;
-        this.barrelShape.rotate(theta);
+        this.barrelShape.rotateRelPage(pageX, pageY);
         return this;
     }
     
 });
 
+// TODO: Move to Layer
 var NUM_OR_PERCENT = /^\s*[\d\.]+\s*%?\s*$/;
 function calcValue(v){
     var unit = this;
index 15bfa07..5c48ad0 100644 (file)
@@ -168,25 +168,28 @@ Unit.subclass('Tower', {
     },
     
     
+    getTurretLoc : function getTurretLoc(){
+        var WIGGLE = 2, pw = this.projectile.fn.width;
+        return this.barrelShape.barrelTipLoc(WIGGLE+pw, true);
+    },
+    
+    getBarrelAngle : function getBarrelAngle(){
+        return this.barrelShape.transform.rotate;
+    },
+    
     rotateBarrel : function rotateBarrel(x,y){
         this.barrelShape.rotate(this.angleTo(x,y));
         return this;
     },
     
     rotateBarrelRelPage : function rotateBarrelRelPage(pageX, pageY){
-        var shape  = this.shape
-        ,   off = shape.offset()
-        ,   w = this.width, h = this.height
-        ,   x = off.left + w/2 - pageX
-        ,   y = off.top  + h/2 - pageY
-        ,   theta = Math.atan2(-y,-x)
-        ;
-        this.barrelShape.rotate(theta);
+        this.barrelShape.rotateRelPage(pageX, pageY);
         return this;
     }
     
 });
 
+// TODO: Move to Layer
 var NUM_OR_PERCENT = /^\s*[\d\.]+\s*%?\s*$/;
 function calcValue(v){
     var unit = this;