Commits

Pedro Ferreira committed 982a306

- add physics body to EnemyMoveHorizontally
- use a .moveTo() function to move an enemy (instead of changing directly the x/y)
- rename Ship's .position() to .moveTo()

Comments (0)

Files changed (5)

js/endless_mode.js

     COUNT_TICKS_NEXT_ENEMY = NEXT_ENEMY_TICKS;
     
   
-    var enemy = new ENEMY_TYPES[ getRandomInt(0, ENEMY_TYPES.length - 1 ) ]();
-    
+    //var enemy = new ENEMY_TYPES[ getRandomInt(0, ENEMY_TYPES.length - 1 ) ]();
+    var enemy = new EnemyMoveHorizontally();
+
     var x = getRandomInt( 0, GAME_WIDTH );
     var y = getRandomInt( 0, GAME_HEIGHT );
     
    
     //var enemy = new EnemyMoveHorizontally();
     //var enemy = new EnemyRocks();
-    
-    enemy.x = x;
-    enemy.y = y;
-    
+
+    enemy.moveTo( x, y );
+
     addNewEnemy( enemy );
     }
 
     --- to the prototype ---
     
         .makeShape()
+        .setupPhysics()
         .shipBehaviour()
+        .updateShape()          (optional)
         .damageTaken()          (optional)
         .beforeAddToStage()     (optional)
         .spawnTick_function()   (optional)
     Add reference of the drawn element to:
     
         .shape
+
+    Physics body
+
+        .body
         
  */
 
     // draw the shape (spawn phase animation first)
 this.makeShape();
 
+this.setupPhysics();
 
     // add to Container()
 this.addChild( this.shape );
 
 
 
+p.setupPhysics = function()
+{
+    // do this
+};
+
+
+/*
+    Updates the shape position to match the physic body
+ */
+
+p.updateShape = function()
+{
+this.shape.rotation = this.body.GetAngle() * (180 / Math.PI);
+
+this.shape.x = this.body.GetWorldCenter().x * SCALE;
+this.shape.y = this.body.GetWorldCenter().y * SCALE;
+};
+
+
+p.moveTo = function( x, y )
+{
+this.shape.x = x;
+this.shape.y = y;
+
+var position = new b2Vec2(x / SCALE, y / SCALE);
+
+this.body.SetPosition( position );
+};
+
 
 
 
 {
 this.shipBehaviour();
 
+this.updateShape();
 
     // the limits of the canvas
 this.checkLimits();

js/enemy_move_horizontally.js

 
 
 
+EnemyMoveHorizontally.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;
+};
+
+
+
+
 
 EnemyMoveHorizontally.prototype.shipBehaviour = function()
 {
-this.x += this.velocity;
+var nextX = this.shape.x + this.velocity;
+var nextY = this.shape.y;
+
+this.moveTo( nextX, nextY );
 };
 
 
     
     var i;
     var howMany = parseInt( phase.howMany );
-    
+
+    var x, y;
+
     for (i = 0 ; i < howMany ; i++)
         {
         var enemy = new enemyType();
             // random x position
         if (phase.x < 0)
             {
-            enemy.x = getRandomInt( 0, GAME_WIDTH );
+            x = getRandomInt( 0, GAME_WIDTH );
             }
         
         else
             {
-            enemy.x = phase.x;
+            x = phase.x;
             }
         
             // random y position
         if (phase.y < 0)
             {
-            enemy.y = getRandomInt( 0, GAME_HEIGHT );
+            y = getRandomInt( 0, GAME_HEIGHT );
             }
         
         else
             {
-            enemy.y = phase.y;
+            y = phase.y;
             }
+
+        enemy.moveTo( x, y );
         
         addNewEnemy( enemy );
         }
-    
 
     
         // advance to the next phase of the level
 
 Ship.all.push( this );
 
-this.position( GAME_WIDTH / 2, GAME_HEIGHT / 2 );
+this.moveTo( GAME_WIDTH / 2, GAME_HEIGHT / 2 );
 
 STAGE.addChild( this.shape );
 
 
 fixDef.shape = new b2CircleShape( width / 2 / SCALE );
 
-//fixDef.shape
-
 //fixDef.shape.SetAsOrientedBox( width/2 / SCALE, height/2 / SCALE, new b2Vec2( width/2 / SCALE , height/2 / SCALE ) );
 
-
 var body = WORLD.CreateBody( bodyDef );
 
 body.CreateFixture( fixDef );
 
 this.body = body;
 
-//body.SetUserData( MainCharacter );
-
-//MainCharacter.actorObject = new ActorObject( body, MainCharacter.shape );
-
-//return body;
+body.SetUserData( this );
 };
 
 /*
 };
 
 
-Ship.prototype.position = function( x, y )
+Ship.prototype.moveTo = function( x, y )
 {
 //this.shape.x = x;
 //this.shape.y = y;
         nextX = this.shape.x;
         }
 
-    this.position( nextX, nextY );
+    this.moveTo( nextX, nextY );
     }
     
     // bottom left
         nextY = this.shape.y;
         }
 
-    this.position( nextX, nextY );
+    this.moveTo( nextX, nextY );
     }
     
     // top right
         nextY = this.shape.y;
         }
 
-    this.position( nextX, nextY );
+    this.moveTo( nextX, nextY );
     }
     
     // bottom right
         nextY = this.shape.y;
         }
 
-    this.position( nextX, nextY );
+    this.moveTo( nextX, nextY );
     }
 
     // left
         nextX = this.shape.x;
         }
 
-    this.position( nextX, this.shape.y );
+    this.moveTo( nextX, this.shape.y );
     }
     
     // right
         nextX = this.shape.x;
         }
 
-    this.position( nextX, this.shape.y );
+    this.moveTo( nextX, this.shape.y );
     }
     
     // top
         nextY = this.shape.y;
         }
 
-    this.position( this.shape.x, nextY );
+    this.moveTo( this.shape.x, nextY );
     }
     
     // bottom
         nextY = this.shape.y;
         }
 
-    this.position( this.shape.x, nextY );
+    this.moveTo( this.shape.x, nextY );
     }