Commits

Pedro Ferreira  committed 06fdac3

- add physics body to EnemyKamikaze and use box2d to control its behaviour (with linear velocity), following the ship

  • Participants
  • Parent commits 565722f

Comments (0)

Files changed (5)

File 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 EnemyKamikaze();
 
     var x = getRandomInt( 0, GAME_WIDTH );
     var y = getRandomInt( 0, GAME_HEIGHT );
         .beforeAddToStage()     (optional)
         .spawnTick_function()   (optional)
         .tick_function()        (optional)
-        
+
     --- to the class ---
 
         .increaseDifficulty()
     
         // only add now to the enemies list (so, only from now on will the bullets be able to kill it, etc)
     EnemyShip.all.push( this );
-    
+
         // now execute the normal tick function
     this.tick = this.normalTick;
     }

File js/enemy_kamikaze.js

 };
 
 
+EnemyKamikaze.prototype.setupPhysics = function()
+{
+var width = this.width;
+var height = this.height;
+
+    // physics
+var fixDef = new b2FixtureDef;
+
+fixDef.density = 1;
+fixDef.friction = 0.5;
+fixDef.restitution = 0.2;
+
+var bodyDef = new b2BodyDef;
+
+bodyDef.type = b2Body.b2_dynamicBody;
+
+bodyDef.position.x = 0;
+bodyDef.position.y = 0;
+
+fixDef.shape = new b2CircleShape( width / 2 / SCALE );
+
+var body = WORLD.CreateBody( bodyDef );
+
+body.CreateFixture( fixDef );
+
+body.SetUserData( this );
+
+this.body = body;
+};
+
+
 EnemyKamikaze.prototype.shipBehaviour = function()
 {
-var shipX = MAIN_SHIP.x;
-var shipY = MAIN_SHIP.y;
+var angle = calculateAngleBetweenObjects( this, MAIN_SHIP );
 
-var enemyX = this.x;
-var enemyY = this.y;
+angle *= -1;    //HERE
 
-    // sometimes enemyY is NaN.. don't know why, so just return when that happens
-if (isNaN(enemyY) === true)
-    {
-    return;
-    }
+var radians = toRadians( angle );
 
+var velocity = EnemyKamikaze.velocity;
 
-    // y = slope * x + b
-var slope = (enemyY - shipY) / (enemyX - shipX);
-    
-    // sometimes you don't get a number (the division)
-if ( $.isNumeric( slope ) === false )
-    {
-    return;
-    }
+var x = Math.cos( radians ) * velocity;
+var y = Math.sin( radians ) * velocity;
 
-    
-var b = enemyY - slope * enemyX;
-    
-var newEnemyX;
-    
-if ( (enemyX - shipX) > 0 )
-    {
-    newEnemyX = enemyX - this.velocity;
-    }
+this.body.SetLinearVelocity( new b2Vec2(x, y) );
 
-else
-    {
-    newEnemyX = enemyX + this.velocity;
-    }
-
-
-    
-var newEnemyY = slope * newEnemyX + b;
-
-
-
-var limit = newEnemyY - enemyY;
-
-    // slow down the y variation
-if (limit > this.velocity)
-    {
-    newEnemyY = enemyY + this.velocity - 1;
-    }
-
-else if (limit < -this.velocity )
-    {
-    newEnemyY = enemyY - this.velocity - 1;
-    }
-
-    
-this.x = newEnemyX;
-this.y = newEnemyY;
-    
-    
 this.updateRotation();
 };
 
     // calculate the angle between the enemy and the ship  
 var angleDegrees = calculateAngleBetweenObjects( MAIN_SHIP, this );
 
+
     //HERE align the image
 angleDegrees += 90;
 
     // we multiply by -1 because the .rotation property seems to have the angles in the other direction
-this.rotation = -1 * angleDegrees; 
+this.rotate( -1 * angleDegrees );
 };
 
 

File js/utilities.js

 
 function applyImpulse( body, degrees, power )
 {
-var rads = degrees * Math.PI / 180;
-
-//console.log(degrees);
+var rads = toRadians( degrees );
 
 var impulse = new b2Vec2( Math.cos( rads ) * power,
     Math.sin( rads ) * power );
 body.ApplyImpulse( impulse, point );
 }
 
+/*
+    Applies a force to a body (box2d)
+ */
+
+function applyForce( body, degrees, power )
+{
+var rads = toRadians( degrees );
+
+var impulse = new b2Vec2( Math.cos( rads ) * power, Math.sin( rads ) * power );
+
+var point = body.GetWorldCenter();
+
+body.ApplyForce( impulse, point );
+}
+
+
 
 /*
     objectA and objectB has to have a .getX() and .getY() method
 var bX = objectB.getX();
 var bY = objectB.getY();
 
-calculateAngleBetweenObjects2( aX, aY, bX, bY );
+return calculateAngleBetweenObjects2( aX, aY, bX, bY );
 }
 
 /*

File js/weapons.js

 var body = WORLD.CreateBody( bodyDef );
 
 body.CreateFixture( fixDef );
+body.SetBullet( true );
 
 body.SetUserData( this );