118 lines
2.8 KiB
JavaScript
118 lines
2.8 KiB
JavaScript
Alien.prototype = Object.create(Phaser.Sprite.prototype);
|
|
Alien.constructor = Alien;
|
|
|
|
function Alien(spaceship) {
|
|
this.spaceship = spaceship;
|
|
|
|
this.isActivated = false;
|
|
this.speed = Alien.DEFAULT_SPEED_DOT_PER_SEC;
|
|
this.angleFromSpaceship = 0;
|
|
this.angleFromAlien = 0;
|
|
|
|
Phaser.Sprite.call(this, game, 0, 0, 'alien_normal');
|
|
this.anchor.set(0.5);
|
|
|
|
game.add.existing(this);
|
|
this.moveRandomPosition();
|
|
|
|
// this.setActive(true);
|
|
}
|
|
|
|
|
|
|
|
|
|
Alien.prototype.moveRandomPosition = function() {
|
|
this.angleFromAlien = game.rnd.integerInRange(0, 360);
|
|
// console.log(this.angleFromAlien);
|
|
|
|
var radians = this.angleFromAlien * Math.PI / 180;
|
|
|
|
var radious = Alien.START_RADIOUS_PX * ( ( (100 + game.rnd.integerInRange(0, Alien.START_RANDOM_RADIOUS_PERCENT) ) / 100) );
|
|
var posX = Math.sin(radians) * radious;
|
|
var posY = Math.cos(radians) * radious;
|
|
|
|
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 alienAngle = this.getAngle(this.spaceship.x, this.spaceship.y, this.x, this.y);
|
|
this.angle = this.getSpriteAngle(alienAngle);
|
|
|
|
this.angleFromAlien = alienAngle;
|
|
|
|
// this.angle = this.getSpriteAngle(this.angleFromAlien);
|
|
}
|
|
|
|
Alien.prototype.update = function() {
|
|
if(!this.isActivated)
|
|
return;
|
|
|
|
var deltaTime = game.time.elapsed / 1000;
|
|
|
|
var radians = this.angleFromAlien * 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 > Alien.START_RADIOUS_PX)
|
|
this.moveRandomPosition();
|
|
}
|
|
|
|
|
|
Alien.prototype.stop = function() {
|
|
this.setActive(false);
|
|
}
|
|
|
|
Alien.prototype.setActive = function(isActivated) {
|
|
this.isActivated = isActivated;
|
|
}
|
|
|
|
Alien.prototype.getSpriteAngle = function (angle){
|
|
return 360 - angle;
|
|
}
|
|
|
|
Alien.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;
|
|
}
|
|
|
|
Alien.prototype.goRandomPosition = function() {
|
|
// set random position
|
|
var SPAWN_BOX_LEFT = 80;
|
|
var SPAWN_BOX_WIDTH = game.world.width - 160;
|
|
var SPAWN_BOX_TOP = 280;
|
|
var SPAWN_BOX_HEIGHT = game.world.height - 400;
|
|
|
|
this.x = SPAWN_BOX_LEFT + Math.random() * SPAWN_BOX_WIDTH;
|
|
this.y = SPAWN_BOX_TOP + Math.random() * SPAWN_BOX_HEIGHT;
|
|
|
|
// if(isDebugMode()) {
|
|
// this.game.add.graphics()
|
|
// .beginFill(0xffffff, 0.1)
|
|
// .drawRect(SPAWN_BOX_LEFT, SPAWN_BOX_TOP, SPAWN_BOX_WIDTH, SPAWN_BOX_HEIGHT);
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
Alien.DEFAULT_SPEED_DOT_PER_SEC = 100;
|
|
|
|
Alien.START_RADIOUS_PX = 600;
|
|
Alien.START_RANDOM_RADIOUS_PERCENT = 40;
|