Add: alien

This commit is contained in:
2018-05-11 11:20:09 +09:00
parent 5d2de6b7d6
commit 8ec9129c8e
3 changed files with 110 additions and 0 deletions
+91
View File
@@ -0,0 +1,91 @@
class Alien extends Phaser.Sprite {
constructor(initialX, initialY, onFiredFunction, onEscapedFunction) {
super();
this.isActivated = false;
this.speedGrowing = Alien.DEFAULT_SPEED_SEC;
this.tweenScale = null;
this.initialX = initialX;
this.initialY = initialY;
this.onFiredFunction = onFiredFunction;
this.onEscapedFunction = onEscapedFunction;
Phaser.Sprite.call(this, game, this.initialX, this.initialY, 'alien_normal');
this.anchor.set(0.5);
this.scale.set(Alien.MAX_SCALE);
this.inputEnabled = true;
this.events.onInputOut.add(this.onOut, this);
this.events.onInputOver.add(this.onOver, this);
this.events.onInputDown.add(this.onDown, this);
game.add.existing(this);
}
onOut() {
if(!this.isActivated)
return;
// console.log("onOut");
}
onOver() {
if(!this.isActivated)
return;
// console.log("onOver");
}
onDown() {
if(!this.isActivated)
return;
// console.log("onDown");
if(this.onFiredFunction)
this.onFiredFunction(this);
}
setActive(isActivated) {
let beforeState = this.isActivated;
this.isActivated = isActivated;
if(beforeState && !this.isActivated) {
this.shutdown();
}
}
shutdown() {
if(!this.tweenScale) {
this.tweenScale.stop();
}
this.scale.set(0);
}
setOnInitialPosition() {
this.x = this.initialX;
this.y = this.initialY;
this.scale.set(Alien.MAX_SCALE);
}
moveOnStage() {
let SPAWN_BOX_LEFT = 100;
let SPAWN_BOX_WIDTH = game.world.width - 200;
let SPAWN_BOX_TOP = 300;
let 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;
}
}
Alien.DEFAULT_SPEED_SEC = 1000;
Alien.MAX_SCALE = 3;