Commits

Pedro Ferreira committed 4cf7cfd

- fix not being able to click in the canvas (z-index)
- add physics body to bullets (laser)

Comments (0)

Files changed (8)

 
 #mainCanvas
     {
-    z-index: 50;
+    z-index: 100;
     }
 
 #debugCanvas
     {
-    z-index: 100;
+    z-index: 50;
     }
     {
     $( bullets ).each(function( bullet_index, bullet )
         {
-        bulletX = bullet.bulletShape.x;
-        bulletY = bullet.bulletShape.y;
+        bulletX = bullet.shape.x;
+        bulletY = bullet.shape.y;
         
         var bulletHalfWidth = bullet.width / 2;
         var bulletHalfHeight = bullet.height / 2;
 
 Ship.prototype.moveTo = function( x, y )
 {
-//this.shape.x = x;
-//this.shape.y = y;
+this.shape.x = x;
+this.shape.y = y;
 
 var position = new b2Vec2(x / SCALE, y / SCALE);
 
 };
 
 
+Ship.prototype.getX = function()
+{
+return this.shape.x;
+};
+
+
+Ship.prototype.getY = function()
+{
+return this.shape.y;
+};
+
+
+Ship.prototype.getRotation = function()
+{
+return this.shape.rotation;
+};
+
+
 Ship.inTopLimit = function( y )
 {
 if (y < 0)

js/weapon1_laser.js

     animations: {
     
         main :  { 
-            
             frames: [ 0, 1 ],
             next : "main",
             frequency: 10
-            
             }
         },
         
     frames: {
-    
         width: 4,
         height: 2
-        
         },
         
     images: [ "images/weapon1_laser.png" ]
 var shipObject = this.shipObject;
 
 
-laser.x = shipObject.x;
-laser.y = shipObject.y;
+laser.x = shipObject.getX();
+laser.y = shipObject.getY();
 
 if ( $.isNumeric( angleRotation ) )
     {
 
 else
     {
-    laser.rotation = shipObject.rotation;
+    laser.rotation = shipObject.getRotation();
     }
 
 
-this.bulletShape = laser;
+this.shape = laser;
 };

js/weapon2_sniper.js

     
 var shipObject = this.shipObject;
 
-sniper.x = shipObject.x;
-sniper.y = shipObject.y;
-sniper.rotation = shipObject.rotation;
+sniper.x = shipObject.getX();
+sniper.y = shipObject.getY();
+sniper.rotation = shipObject.getRotation();
 
 
-this.bulletShape = sniper;
+this.shape = sniper;
 };
 
 
 var mouseX = STAGE.mouseX;
 var mouseY = STAGE.mouseY;
 
-var shipX = ship.x;
-var shipY = ship.y;
+var shipX = ship.getX();
+var shipY = ship.getY();
 
     // find the line equation: y = slope * x + b
 var slope = (mouseY - shipY) / (mouseX - shipX);

js/weapon3_rocket.js

     animations: {
     
         main: {
-        
             frames: [ 0, 1 ],
             next: "main"
-            
             }
-        
         },
         
     frames: {
-    
         width: 15,
         height: 7
-    
         },
         
     images: [ "images/weapon3_rocket.png" ]
 
 var shipObject = this.shipObject;
 
-rocket.x = shipObject.x;
-rocket.y = shipObject.y;
-rocket.rotation = shipObject.rotation;
+rocket.x = shipObject.getX();
+rocket.y = shipObject.getY();
+rocket.rotation = shipObject.getRotation();
 
-this.bulletShape = rocket;
+this.shape = rocket;
 };  

js/weapon4_mines.js

     animations: {
     
         main: {
-            
             frames: [ 0, 1, 2, 3 ],
             next: "main",
             frequency: 10
-        
             }
         },
         
     frames: {
-    
         width: 15,
         height: 15
-    
         },
         
     images: [ "images/weapon4_mines.png" ]
 
 var shipObject = this.shipObject;
 
-mine.x = shipObject.x;
-mine.y = shipObject.y;
-//mine.rotation = shipObject.rotation; //HERE nao precisa
+mine.x = shipObject.getX();
+mine.y = shipObject.getY();
+//mine.rotation = shipObject.getRotation(); //HERE nao precisa
 
-this.bulletShape = mine;
+this.shape = mine;
 };
 
     Functions to write (in derived class):
     
         .drawBullet()
+        .setupPhysics()         (optional -- the default is a rectangle from the width/height properties)
 
     Properties:
     
 
 function Weapons( shipObject, angleRotation )
 {
-this.bulletShape = null;
+this.shape = null;
 
 this.shipObject = shipObject;
 
     // draw the bullet
 this.drawBullet( angleRotation );
 
+this.setupPhysics();
+
 this.isEnemy = shipObject.isEnemy;
 
-STAGE.addChild( this.bulletShape );
+STAGE.addChild( this.shape );
 
 ZIndex.update();
 
 
 
 
+Weapons.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;
+};
+
+
 
 
 Weapons.prototype.moveForwardBullet = function()
 {
-var shape = this.bulletShape;
+var shape = this.shape;
 
 
     //HERE
 var rotation = shape.rotation - 90;
 
-shape.x += Math.sin( rotation * (Math.PI/-180) ) * this.speed;
-shape.y += Math.cos( rotation * (Math.PI/-180) ) * this.speed;
+var currentX = shape.x;
+var currentY = shape.y;
+
+var x = currentX + Math.sin( rotation * (Math.PI/-180) ) * this.speed;
+var y = currentY + Math.cos( rotation * (Math.PI/-180) ) * this.speed;
+
+
+this.moveTo( x, y );
+
 
     // remove the bullets that are out of the canvas
 if ( this.reachedLimits() )
 };
 
 
+Weapons.prototype.getX = function()
+{
+return this.shape.x;
+};
+
+
+Weapons.prototype.getY = function()
+{
+return this.shape.y;
+};
+
+
+Weapons.prototype.moveTo = function( x, y )
+{
+this.shape.x = x;
+this.shape.y = y;
+
+var position = new b2Vec2(x / SCALE, y / SCALE);
+
+this.body.SetPosition( position );
+};
+
+
+Weapons.prototype.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;
+};
+
 
 /*
     Tells if a bullet has reached the canvas limits
 
 Weapons.prototype.reachedLimits = function()
 {
-var x = this.bulletShape.x;
-var y = this.bulletShape.y;
+var x = this.shape.x;
+var y = this.shape.y;
 
 if (x < 0 || x > GAME_WIDTH || y < 0 || y > GAME_HEIGHT)
     {
     all = Weapons.allyBullets;
     }
     
-STAGE.removeChild( this.bulletShape );
+STAGE.removeChild( this.shape );
 createjs.Ticker.removeListener( this );
 
 
+WORLD.DestroyBody( this.body );
+
 var position = all.indexOf( this );
 
 all.splice( position, 1 );
 {
 this.moveForwardBullet();
 
+this.updateShape();
 
 if (typeof this.tick_function !== "undefined" && this.tick_function !== null)
     {