Commits

Pedro Ferreira committed 15db09e

- add functions to get the x and y position from an Enemy
- add function to rotate an Enemy
- fix enemies not reappearing in the other side of the window (as they pass the limits of the window)
- add physics body to EnemyRocks and EnemyRotateAround

  • Participants
  • Parent commits 982a306

Comments (0)

Files changed (4)

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 x = getRandomInt( 0, GAME_WIDTH );
     var y = getRandomInt( 0, GAME_HEIGHT );
     
    
-    //var enemy = new EnemyMoveHorizontally();
-    //var enemy = new EnemyRocks();
+
 
     enemy.moveTo( x, y );
 
 };
 
 
+p.getPosition = function()
+{
+return {
+    x: this.shape.x,
+    y: this.shape.y
+    };
+};
+
+p.getX = function()
+{
+return this.shape.x;
+};
+
+
+p.getY = function()
+{
+return this.shape.y;
+};
+
+
 p.moveTo = function( x, y )
 {
 this.shape.x = x;
 
 
 
+p.rotate = function( degrees )
+{
+this.shape.rotation = degrees;
+
+this.body.SetAngle( degrees * Math.PI / 180 );
+};
+
+
 
 EnemyShip.prototype.damageGiven = function()
 {
 
 p.checkLimits = function()
 {
-if (this.x < 0)    
+var x = this.getX();
+var y = this.getY();
+
+if (x < 0)
     {
-    this.x = GAME_WIDTH;
+    this.moveTo( GAME_WIDTH, y );
     }
 
-else if (this.x > GAME_WIDTH)
+else if (x > GAME_WIDTH)
     {
-    this.x = 0;
+    this.moveTo( 0, y );
     }
 
-else if (this.y < 0)
+else if (y < 0)
     {
-    this.y = GAME_HEIGHT;
+    this.moveTo( x, GAME_HEIGHT );
     }
 
-else if (this.y > GAME_HEIGHT)
+else if (y > GAME_HEIGHT)
     {
-    this.y = 0;
+    this.moveTo( x, 0 );
     }
 };
 
 
 createjs.Ticker.removeListener( this );
 
+
+WORLD.DestroyBody( this.body );
+
 var position = EnemyShip.all.indexOf( this );
 
 EnemyShip.all.splice( position, 1 );

File js/enemy_rocks.js

 };
 
 
+EnemyRocks.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 b2PolygonShape;
+
+    // arguments: half width, half height
+fixDef.shape.SetAsBox( width / 2 / SCALE, height / 2 / SCALE );
+
+var body = WORLD.CreateBody( bodyDef );
+
+body.CreateFixture( fixDef );
+
+
+body.SetUserData( this );
+
+this.body = body;
+};
+
+
+
 //HERE mudar o nome para enemyBehaviour ou shapeBehaviour
 EnemyRocks.prototype.shipBehaviour = function()
 {
-this.x += Math.sin( this.angleRadians ) * this.velocity;
-this.y += Math.cos( this.angleRadians ) * this.velocity;
+var currentX = this.shape.x;
+var currentY = this.shape.y;
 
-this.rotation++;
+var x = currentX + Math.sin( this.angleRadians ) * this.velocity;
+var y = currentY + Math.cos( this.angleRadians ) * this.velocity;
+
+this.moveTo( x, y );
+
+this.rotate( this.shape.rotation + 1 );
 };
 
 
         var rock = new EnemyRocks( 0.5 );
         
             // spawn from the current position
-        rock.x = this.x;
-        rock.y = this.y;
-        
+        rock.moveTo( this.shape.x, this.shape.y );
+
         addNewEnemy( rock );
         }
     }

File js/enemy_rotate_around.js

 
 
 
+EnemyRotateAround.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 );
+
+// arguments: half width, half height
+//fixDef.shape.SetAsBox( width / 2 / SCALE, height / 2 / SCALE );
+
+var body = WORLD.CreateBody( bodyDef );
+
+body.CreateFixture( fixDef );
+
+
+body.SetUserData( this );
+
+this.body = body;
+};
+
 
 EnemyRotateAround.prototype.shipBehaviour = function()
 {
+var currentX = this.shape.x;
+var currentY = this.shape.y;
+
     // 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;
+var triangleOppositeSide = MAIN_SHIP.shape.y - currentY;
+var triangleAdjacentSide = currentX - MAIN_SHIP.shape.x;
 
 
 
     // find the angle, given the two sides (of a right triangle)
 var angleRadians = Math.atan2( triangleOppositeSide, triangleAdjacentSide );
 
+var x = currentX + Math.sin( angleRadians ) * this.velocity;
+var y = currentY + Math.cos( angleRadians ) * this.velocity;
 
-this.x += Math.sin( angleRadians ) * this.velocity;
-this.y += Math.cos( angleRadians ) * this.velocity;
+this.moveTo( x, y );
 };