Files
chocomae/src/game/mouse/space_invaders/game.js
T

139 lines
3.3 KiB
JavaScript

/////////////////////////////
// Space invaders game
class Game {
create() {
this.game.stage.backgroundColor = '#4d4d4d';
sessionStorageManager.isNewBestRecrd = false;
// top
let backButton = new BackButton( () => {
location.href = '../../web/client/menu_app.html';
});
let heartGauge = new HeartGauge(heartManager);
heartManager.addOnChangeGameOverListener(
() => { this.gameOver(); }
);
heartGauge.start();
let fullscreenButton = new FullscreenButton(this.game);
let scoreBoard = new ScoreBoard();
scoreManager.addOnChangeScoreListener( score => {
sessionStorageManager.record = score;
scoreBoard.printScore(NumberUtil.numberWithCommas(score));
});
scoreManager.addOnChangeHighScoreListener( highScore => {
sessionStorageManager.bestRecord = highScore;
sessionStorageManager.isNewBestRecrd = true;
screenBottom.printBottomLeftTextWithBestRecord(sessionStorageManager.bestRecord);
});
// waiting room
this.game.add.graphics()
.beginFill(0xffffff, 0.1)
.drawRect(0, 100, game.world.width, 100);
// contents
this.aliens = [];
for(let i = 0; i < 10; i++) {
let alien = new Alien(150 + 80 * i, 150,
() => { this.activateNextAlien(); }, // onGoOnstage
(sprite) => {
scoreManager.plusScore(this.getScore());
let scoreText = new ScoreText(sprite.x, sprite.y, this.getScore());
}, // this.onAlienFired,
this.onAlienEscaped
);
alien.goWaitingRoom();
alien.setActive(false);
this.aliens.push(alien);
}
this.alienTimer = new AlienTimer(this.aliens);
// bottom
let screenBottom = new ScreenBottom();
screenBottom.makeBottomLine();
screenBottom.printBottomLeftTextWithBestRecord(sessionStorageManager.bestRecord);
screenBottom.printBottomCenterText(sessionStorageManager.playingAppKoreanName);
screenBottom.printBottomRightText(sessionStorageManager.playerName);
this.startGame();
// this.countDown();
}
initListeners() {
}
/*
countDown() {
const style = { font: "bold 200px Arial", fill: "#fff", boundsAlignH: "center", boundsAlignV: "middle" };
this.countDownText = game.add.text(0, 0, "", style);
this.countDownText.setTextBounds(0, 0, game.world.width, game.world.height);
this.countDownText.stroke = "#333";
this.countDownText.strokeThickness = 50;
this.countDownNumber = 3;
if(isDebugMode())
this.countDownNumber = 1;
this.tweenCountDown();
}
tweenCountDown() {
if(this.countDownNumber === 0) {
this.startGame();
return;
}
this.countDownText.text = this.countDownNumber.toString();
this.countDownText.alpha = 1;
let countDownTween = game.add.tween(this.countDownText);
countDownTween.to( { alpha: 0 }, 1000, Phaser.Easing.Linear.None, true);
countDownTween.onComplete.add(this.tweenCountDown, this);
this.countDownNumber--;
}
*/
startGame() {
// activate first alien
this.alienTimer.start();
}
gameOver() {
this.alienTimer.stop();
let gameOverText = new GameOverText();
game.time.events.add(Phaser.Timer.SECOND * 2, this.goResult, this);
}
goResult() {
location.href = '../../web/client/result.html';
}
activateNextAlien() {
this.alienTimer.next();
}
getScore() {
return (this.alienTimer.activatedAlienIndex) * 2;
}
onAlienEscaped() {
heartManager.breakHearts(1);
}
}
Game.GAME_OVER_WAIT_TIME_MS = 2000;