Guest User

Untitled

a guest
Jul 14th, 2014
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. 'use strict';
  3.  
  4. var Missile = require('../prefabs/missile');  
  5. var Obstacle = require('../prefabs/obstacle');
  6. var Protagonist = require('../prefabs/protagonist');
  7.  
  8. function Play() {}
  9. Play.prototype = {
  10.     create: function() {
  11.      
  12.       this.score = 0;
  13.       this.scoreText = this.game.add.text(
  14.           20, 20, '', { font: '20px Arial', fill: '#ffffff' }
  15.       );
  16.       this.scoreText.setText('0');
  17.  
  18.       //The hero of our game
  19.       this.protagonist = new Protagonist(this.game, 20, 20);
  20.       this.game.add.existing(this.protagonist);
  21.  
  22.       //Collect all missiles in this group
  23.       this.missileGroup = this.game.add.group();
  24.      
  25.       //Create an obstacle that missiles can crash against
  26.       this.obstacle = new Obstacle(this.game, this.game.width/2, this.game.height/2);
  27.       this.game.add.existing(this.obstacle);
  28.      
  29.       // Simulate a pointer click/tap input at the center of the stage
  30.       // when the example begins running.
  31.       this.game.input.activePointer.x = this.game.width/2;
  32.       this.game.input.activePointer.y = this.game.height/2 - 100;
  33.     },
  34.     update: function()
  35.     {
  36.       // If there are fewer than MAX_MISSILES, launch a new one
  37.       if ((this.missileGroup.countLiving() < (this.score + 1)/4) && this.protagonist.alive == true)
  38.       {
  39.         // Set the launch point to a random location below the bottom edge of the stage
  40.         this.launchMissile(this.game.rnd.integerInRange(50, this.game.width-50),this.game.height + 50);
  41.       }
  42.  
  43.       // If any missile is within a certain distance of the mouse pointer, blow it up
  44.       this.missileGroup.forEachAlive(function(m)
  45.       {
  46.         this.game.physics.arcade.collide(m, this.obstacle, this.missileDeathHandler, null, this);
  47.         this.game.physics.arcade.collide(m, this.protagonist, this.protagonistDeathHandler, null, this);
  48.         this.game.physics.arcade.collide(this.obstacle, this.protagonist, null, null, this);
  49.  
  50.         /* Expload when close to something e.g. the pointer
  51.         var distance = this.game.math.distance(m.x, m.y,
  52.             this.game.input.activePointer.x, this.game.input.activePointer.y);
  53.         if (distance < 50) {
  54.             m.kill();
  55.             this.getExplosion(m.x, m.y);
  56.         }
  57.         */
  58.        
  59.       }, this);
  60.     },
  61.     protagonistDeathHandler: function(missile, protagonist)
  62.     {
  63.       missile.kill();        
  64.       protagonist.kill();
  65.       //this.missileGroup.destroy();
  66.      
  67.       //TODO: Scoreboard
  68.       //this.scoreboard = new Scoreboard(this.game);
  69.       //this.game.add.existing(this.scoreboard);
  70.       //this.scoreboard.show(this.score);
  71.     },
  72.     missileDeathHandler: function(missile)
  73.     {
  74.       if(missile.alive == true)
  75.       {
  76.         missile.kill();
  77.         this.score++;
  78.         this.scoreText.setText(this.score.toString());
  79.       }
  80.     },
  81.     launchMissile: function(x, y)
  82.     {
  83.       // // Get the first dead missile from the missileGroup
  84.       var missile = this.missileGroup.getFirstDead();
  85.  
  86.       // If there aren't any available, create a new one
  87.       if (missile === null)
  88.       {
  89.           missile = new Missile(this.game);
  90.           this.missileGroup.add(missile);
  91.       }
  92.  
  93.       // Revive the missile (set it's alive property to true)
  94.       // You can also define a onRevived event handler in your explosion objects
  95.       // to do stuff when they are revived.
  96.       missile.revive();
  97.  
  98.       // Move the missile to the given coordinates
  99.       missile.x = x;
  100.       missile.y = y;
  101.  
  102.       return missile;
  103.     }
  104.   };
  105.  
  106.   module.exports = Play;
Advertisement
Add Comment
Please, Sign In to add comment