'use strict'; var Missile = require('../prefabs/missile'); var Obstacle = require('../prefabs/obstacle'); var Protagonist = require('../prefabs/protagonist'); function Play() {} Play.prototype = { create: function() { this.score = 0; this.scoreText = this.game.add.text( 20, 20, '', { font: '20px Arial', fill: '#ffffff' } ); this.scoreText.setText('0'); //The hero of our game this.protagonist = new Protagonist(this.game, 20, 20); this.game.add.existing(this.protagonist); //Collect all missiles in this group this.missileGroup = this.game.add.group(); //Create an obstacle that missiles can crash against this.obstacle = new Obstacle(this.game, this.game.width/2, this.game.height/2); this.game.add.existing(this.obstacle); // Simulate a pointer click/tap input at the center of the stage // when the example begins running. this.game.input.activePointer.x = this.game.width/2; this.game.input.activePointer.y = this.game.height/2 - 100; }, update: function() { // If there are fewer than MAX_MISSILES, launch a new one if ((this.missileGroup.countLiving() < (this.score + 1)/4) && this.protagonist.alive == true) { // Set the launch point to a random location below the bottom edge of the stage this.launchMissile(this.game.rnd.integerInRange(50, this.game.width-50),this.game.height + 50); } // If any missile is within a certain distance of the mouse pointer, blow it up this.missileGroup.forEachAlive(function(m) { this.game.physics.arcade.collide(m, this.obstacle, this.missileDeathHandler, null, this); this.game.physics.arcade.collide(m, this.protagonist, this.protagonistDeathHandler, null, this); this.game.physics.arcade.collide(this.obstacle, this.protagonist, null, null, this); /* Expload when close to something e.g. the pointer var distance = this.game.math.distance(m.x, m.y, this.game.input.activePointer.x, this.game.input.activePointer.y); if (distance < 50) { m.kill(); this.getExplosion(m.x, m.y); } */ }, this); }, protagonistDeathHandler: function(missile, protagonist) { missile.kill(); protagonist.kill(); //this.missileGroup.destroy(); //TODO: Scoreboard //this.scoreboard = new Scoreboard(this.game); //this.game.add.existing(this.scoreboard); //this.scoreboard.show(this.score); }, missileDeathHandler: function(missile) { if(missile.alive == true) { missile.kill(); this.score++; this.scoreText.setText(this.score.toString()); } }, launchMissile: function(x, y) { // // Get the first dead missile from the missileGroup var missile = this.missileGroup.getFirstDead(); // If there aren't any available, create a new one if (missile === null) { missile = new Missile(this.game); this.missileGroup.add(missile); } // Revive the missile (set it's alive property to true) // You can also define a onRevived event handler in your explosion objects // to do stuff when they are revived. missile.revive(); // Move the missile to the given coordinates missile.x = x; missile.y = y; return missile; } }; module.exports = Play;