Add: alien
This commit is contained in:
@@ -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;
|
||||||
|
|
||||||
@@ -19,6 +19,13 @@ class Game {
|
|||||||
|
|
||||||
|
|
||||||
// contents
|
// contents
|
||||||
|
let alien = new Alien(
|
||||||
|
this.game.world.width / 2, this.game.world.height / 2,
|
||||||
|
this.onAlienFired, this.onAlienEscaped
|
||||||
|
);
|
||||||
|
alien.setActive(true);
|
||||||
|
alien.moveOnStage();
|
||||||
|
/*
|
||||||
let alien = game.add.sprite(this.game.world.width / 2, this.game.world.height / 2, 'alien_normal');
|
let alien = game.add.sprite(this.game.world.width / 2, this.game.world.height / 2, 'alien_normal');
|
||||||
alien.anchor.set(0.5);
|
alien.anchor.set(0.5);
|
||||||
alien.scale.set(0);
|
alien.scale.set(0);
|
||||||
@@ -28,6 +35,8 @@ class Game {
|
|||||||
// alienTween.yoyo(true, 3000);
|
// alienTween.yoyo(true, 3000);
|
||||||
alienTween.to( { x: 4, y: 4 }, 1000, Phaser.Easing.Linear.None, false);
|
alienTween.to( { x: 4, y: 4 }, 1000, Phaser.Easing.Linear.None, false);
|
||||||
alienTween.start();
|
alienTween.start();
|
||||||
|
*/
|
||||||
|
|
||||||
|
|
||||||
// bottom
|
// bottom
|
||||||
let screenBottom = new ScreenBottom(game);
|
let screenBottom = new ScreenBottom(game);
|
||||||
@@ -76,4 +85,13 @@ class Game {
|
|||||||
console.log("startGame");
|
console.log("startGame");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onAlienFired(sprite) {
|
||||||
|
console.log("onAlienFired");
|
||||||
|
sprite.moveOnStage();
|
||||||
|
}
|
||||||
|
|
||||||
|
onAlienEscaped(sprite) {
|
||||||
|
console.log("onAlienEscaped");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -26,6 +26,7 @@
|
|||||||
<!-- Space Invaders : source files -->
|
<!-- Space Invaders : source files -->
|
||||||
<script src="../../game/mouse/space_invaders/define_variables.js"></script>
|
<script src="../../game/mouse/space_invaders/define_variables.js"></script>
|
||||||
<script src="../../game/mouse/space_invaders/loading.js"></script>
|
<script src="../../game/mouse/space_invaders/loading.js"></script>
|
||||||
|
<script src="../../game/mouse/space_invaders/alien.js"></script>
|
||||||
<script src="../../game/mouse/space_invaders/game.js"></script>
|
<script src="../../game/mouse/space_invaders/game.js"></script>
|
||||||
<script src="../../game/mouse/space_invaders/main.js"></script>
|
<script src="../../game/mouse/space_invaders/main.js"></script>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user