Commits

Pedro Ferreira committed 6bc8089

- fix not being able to shoot in all directions (was hitting the main ship)

  • Participants
  • Parent commits 4231da7

Comments (0)

Files changed (9)

js/endless_mode.js

     
   
     //var enemy = new ENEMY_TYPES[ getRandomInt(0, ENEMY_TYPES.length - 1 ) ]();
-    //var enemy = new EnemyMoveHorizontally();
+    var enemy = new EnemyMoveHorizontally();
     //var enemy = new EnemyRocks();
-    var enemy = new EnemyRotateAround();
+    //var enemy = new EnemyRotateAround();
 
     var x = getRandomInt( 0, GAME_WIDTH );
     var y = getRandomInt( 0, GAME_HEIGHT );
 
 window.EndlessMode = EndlessMode;
 
-}(window));
+}(window));
 };
 
 
-/*
-    Calculates the angle in degrees between the enemy ship and the main ship
- */
-
-p.calculateAngleBetweenShip = function()
-{
-    // make a triangle from the position the ship is in, relative to the enemy position
-var triangleOppositeSide = MAIN_SHIP.shape.y - this.y;
-var triangleAdjacentSide = this.x - MAIN_SHIP.shape.x;
-
-
-    // find the angle, given the two sides (of a right triangle)
-var angleRadians = Math.atan2( triangleOppositeSide, triangleAdjacentSide );
-    
-    // convert to degrees
-var angleDegrees = angleRadians * 180 / Math.PI;
-
-return angleDegrees;
-};
-
 
 
 /*
 
 window.EnemyShip = EnemyShip;
 
-}(window));
+}(window));

js/enemy_kamikaze.js

 EnemyKamikaze.prototype.updateRotation = function()
 {
     // calculate the angle between the enemy and the ship  
-var angleDegrees = this.calculateAngleBetweenShip();
+var angleDegrees = calculateAngleBetweenObjects( MAIN_SHIP, this );
 
     //HERE align the image
 angleDegrees += 90;

js/enemy_rotate_around.js

     {
     this.countTicks = 0;
     
-    var angleRotation = this.calculateAngleBetweenShip();
+    var angleRotation = calculateAngleBetweenObjects( MAIN_SHIP, this );
     
     
         //HERE align the image
         collisions:
 
             - remove bullets when out of bounds and hit stuff
-            - cant shoot bullets to all directions
     
  */
 
 }
 
 
+/*
+    objectA and objectB has to have a .getX() and .getY() method
+ */
+
+function calculateAngleBetweenObjects( objectA, objectB )
+{
+var aX = objectA.getX();
+var aY = objectA.getY();
+
+var bX = objectB.getX();
+var bY = objectB.getY();
+
+calculateAngleBetweenObjects2( aX, aY, bX, bY );
+}
+
+/*
+    Called with the x/y directly
+ */
+
+function calculateAngleBetweenObjects2( aX, aY, bX, bY )
+{
+    // make a triangle from the position the objectA is in, relative to the objectB position
+var triangleOppositeSide = aY - bY;
+var triangleAdjacentSide = bX - aX;
+
+    // find the angle, given the two sides (of a right triangle)
+var angleRadians = Math.atan2( triangleOppositeSide, triangleAdjacentSide );
+
+    // convert to degrees
+var angleDegrees = angleRadians * 180 / Math.PI;
+
+return angleDegrees;
+}
+
+
+
+function toRadians( degrees )
+{
+return degrees * Math.PI / 180;
+}
+
+
+function toDegrees( radians )
+{
+return radians * 180 / Math.PI;
+}

js/weapon1_laser.js

 this.height = 2;
 
 
-if ( !$.isNumeric( angleRotation ) )
-    {
-    angleRotation = shipObject.getRotation();
-    }
-
-
     // inherit from the Weapons class
 Weapons.call( this, shipObject, angleRotation );
 
 this.damage = 10;
 
-applyImpulse( this.body, angleRotation, 10 * this.body.GetMass() );
+applyImpulse( this.body, this.angleRotation, 10 * this.body.GetMass() );
 }
 
 
 laser.rotation = angleRotation;
 
 this.shape = laser;
-};
+};

js/weapon2_sniper.js

 
 //this.countTicks = Weapon2_sniper.numberTicksAnimation;
 
-//Weapon2_sniper.findIfIntercept( shipObject );
-
 this.damage = 40;
 
 applyImpulse( this.body, shipObject.getRotation(), 40 * this.body.GetMass() );
     {
     this.remove();
     }
-};
+};
 
 if ( !$.isNumeric( angleRotation ) )
     {
-    angleRotation = shipObject.getRotation();
+    angleRotation = calculateAngleBetweenObjects2( MAIN_SHIP.getX(), MAIN_SHIP.getY(), STAGE.mouseX, STAGE.mouseY );
     }
 
+angleRotation = -1 * angleRotation; //HERE
+
+this.angleRotation = angleRotation;
 
 this.type = TYPE_BULLET;
 
 
 
 
-if ( typeof x == 'undefined')
+if ( typeof x === 'undefined')
     {
     x = shipObject.getX();
     y = shipObject.getY();
     }
 
-this.moveTo( x, y );
+    // fire from outside the main ship radius (so it doesn't collide immediately with it)
+var shipRadius = MAIN_SHIP.width / 2;
+
+var angle = this.angleRotation;
+
+var radians = toRadians( angle );
+
+var addX = Math.cos( radians ) * shipRadius;
+var addY = Math.sin( radians ) * shipRadius;
+
+this.moveTo( x + addX, y + addY );
 }
 
     // all the bullets from the enemies
     // public stuff
 window.Weapons = Weapons;
 
-}(window));
+}(window));