Missile.prototype = Object.create(Phaser.Sprite.prototype); Missile.constructor = Missile; function Missile(spaceship, onCollisionFunction) { this.spaceship = spaceship; this.onCollisionFunction = onCollisionFunction; this.isActivated = false; this.speed = Missile.DEFAULT_SPEED_DOT_PER_SEC; this.boostSpeed = Missile.DEFAULT_SPEED_DOT_PER_SEC / 5; this.angleFromSpaceship = 0; this.angleFromMissile = 0; this.responRadius = 0; this.speedLevel = 0; Phaser.Sprite.call(this, game, 0, 0, 'missile'); this.anchor.set(0.5); game.add.existing(this); game.physics.arcade.enable(this); this.moveRandomPosition(); } Missile.prototype.moveRandomPosition = function() { // move random position this.angleFromMissile = game.rnd.integerInRange(0, 360); var radians = this.angleFromMissile * Math.PI / 180; this.responRadius = Missile.START_Radius_PX * ( ( (100 + game.rnd.integerInRange(0, Missile.START_RANDOM_Radius_PERCENT) ) / 100) ); var posX = Math.sin(radians) * this.responRadius; var posY = Math.cos(radians) * this.responRadius; this.x = game.world.centerX + posX; this.y = game.world.centerY + posY; // set missile direction var spaceshipAreaX = this.spaceship.x + this.getRandomArea(); var spaceshipAreaY = this.spaceship.y + this.getRandomArea(); var MissileAngle = this.getAngle(spaceshipAreaX, spaceshipAreaY, this.x, this.y); this.angle = this.getSpriteAngle(MissileAngle); this.angleFromMissile = MissileAngle; } Missile.prototype.getRandomArea = function() { var randomValue = Math.random(); var randomArea = Missile.SPACESHIP_RANDOM_AREA_MARGIN * randomValue - Missile.SPACESHIP_RANDOM_AREA_MARGIN / 2; return randomArea; } Missile.prototype.update = function() { if(!this.isActivated) return; // game.physics.arcade.overlap(this.spaceship, this, this.collisionHandler, null, this); game.physics.arcade.collide(this.spaceship, this, this.collisionHandler, null, this); var deltaTime = game.time.elapsed / 1000; var radians = this.angleFromMissile * Math.PI / 180; var moveX = Math.sin(radians) * this.speed * deltaTime; var moveY = Math.cos(radians) * this.speed * deltaTime; this.x -= moveX; this.y -= moveY; var distanceX = this.x - game.world.centerX; var distanceY = this.y - game.world.centerY; var distanceFromCenter = Math.sqrt(Math.abs(distanceX * distanceX) + Math.abs(distanceY * distanceY)); if(distanceFromCenter > this.responRadius) { this.moveRandomPosition(); this.upgradeLevel(); } } Missile.prototype.collisionHandler = function() { this.stop(); this.onCollisionFunction(); } Missile.prototype.stop = function() { this.setActive(false); this.body.enable = false; this.alpha = 0; } Missile.prototype.setActive = function(isActivated) { this.isActivated = isActivated; } Missile.prototype.getSpriteAngle = function (angle){ return 360 - angle; } Missile.prototype.getAngle = function (x1, y1, x2, y2){ var dx = x2 - x1; var dy = y2 - y1; var rad= Math.atan2(dx, dy); var degree = (rad * 180) / Math.PI ; if(degree < 0) degree += 360; return degree; } Missile.prototype.upgradeLevel = function() { if(this.speedLevel == 5) return; var rand = game.rnd.integerInRange(0, 100); var successNumber = 0; switch(this.speedLevel) { case 0: successNumber = 90; break; case 1: successNumber = 80; break; case 2: successNumber = 70; break; case 3: successNumber = 60; break; case 4: successNumber = 50; break; } if(rand < successNumber) this.speedUp(); } Missile.prototype.speedUp = function() { this.speedLevel++; this.speed = Missile.DEFAULT_SPEED_DOT_PER_SEC + (this.boostSpeed * this.speedLevel); switch(this.speedLevel) { case 1: this.tint = 0x88ff88; // green break; case 2: this.tint = 0x8888ff; // purble break; case 3: this.tint = 0xc080c0; // purple break; case 4: this.tint = 0xff8888; // red break; case 5: this.tint = 0xfee101; // gold break; } } Missile.DEFAULT_SPEED_DOT_PER_SEC = 150; Missile.START_Radius_PX = 640; Missile.START_RANDOM_Radius_PERCENT = 40; Missile.SPACESHIP_RANDOM_AREA_MARGIN = 50;