Add: stop watch
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
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 / 10;
|
||||
|
||||
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.enable(this, Phaser.Physics.ARCADE);
|
||||
game.physics.arcade.enable(this);
|
||||
|
||||
this.moveRandomPosition();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Missile.prototype.moveRandomPosition = function() {
|
||||
this.angleFromMissile = game.rnd.integerInRange(0, 360);
|
||||
// console.log(this.angleFromMissile);
|
||||
|
||||
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;
|
||||
// this.x = this.spaceship.x + posX;
|
||||
// this.y = this.spaceship.y + posY;
|
||||
|
||||
|
||||
var MissileAngle = this.getAngle(this.spaceship.x, this.spaceship.y, this.x, this.y);
|
||||
this.angle = this.getSpriteAngle(MissileAngle);
|
||||
|
||||
this.angleFromMissile = MissileAngle;
|
||||
|
||||
this.upgradeLevel();
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
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 = 0xffffff;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
this.tint = 0xffaaaa;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
this.tint = 0xaaffaa;
|
||||
break;
|
||||
|
||||
case 4:
|
||||
this.tint = 0xaaaaff;
|
||||
break;
|
||||
|
||||
case 5:
|
||||
this.tint = 0xaaaaaa;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Missile.DEFAULT_SPEED_DOT_PER_SEC = 150;
|
||||
|
||||
Missile.START_Radius_PX = 600;
|
||||
Missile.START_RANDOM_Radius_PERCENT = 40;
|
||||
Reference in New Issue
Block a user