diff --git a/src/game/mouse/space_invaders/alien.js b/src/game/mouse/space_invaders/alien.js new file mode 100644 index 0000000..3d127a2 --- /dev/null +++ b/src/game/mouse/space_invaders/alien.js @@ -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; + diff --git a/src/game/mouse/space_invaders/game.js b/src/game/mouse/space_invaders/game.js index 46ed3ae..d40dea8 100644 --- a/src/game/mouse/space_invaders/game.js +++ b/src/game/mouse/space_invaders/game.js @@ -19,6 +19,13 @@ class Game { // 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'); alien.anchor.set(0.5); alien.scale.set(0); @@ -28,6 +35,8 @@ class Game { // alienTween.yoyo(true, 3000); alienTween.to( { x: 4, y: 4 }, 1000, Phaser.Easing.Linear.None, false); alienTween.start(); + */ + // bottom let screenBottom = new ScreenBottom(game); @@ -76,4 +85,13 @@ class Game { console.log("startGame"); } + onAlienFired(sprite) { + console.log("onAlienFired"); + sprite.moveOnStage(); + } + + onAlienEscaped(sprite) { + console.log("onAlienEscaped"); + } + } \ No newline at end of file diff --git a/src/web/client/space_invaders.html b/src/web/client/space_invaders.html index 1bce0ce..e3ddedf 100644 --- a/src/web/client/space_invaders.html +++ b/src/web/client/space_invaders.html @@ -26,6 +26,7 @@ +