88 lines
3 KiB
JavaScript
88 lines
3 KiB
JavaScript
import { getRandomInt } from "./common";
|
|
import { Fridge, Opinator, SmartMonday, CleanupHS } from "./games";
|
|
import { GameResultDisplay, AnnounceNextGameDisplay } from "./ui";
|
|
|
|
/**
|
|
* manages the game lifecyle, starting a game, handeling game end and starting a new one
|
|
* lvl up etc.
|
|
*/
|
|
export class GameManager {
|
|
constructor(app, gameContainter) {
|
|
this._app = app;
|
|
this._gameContainer = gameContainter;
|
|
this._difficulty = 1;
|
|
this.gameResultTime = 2500; // milliseconds
|
|
this.nextGameAnnounceTime = 2000; // ...
|
|
|
|
const opinator = new Opinator(app.ticker);
|
|
const fridge = new Fridge(app.ticker);
|
|
const smartMonday = new SmartMonday(app.ticker);
|
|
const cleanupHS = new CleanupHS(
|
|
app.stage.width,
|
|
app.stage.height,
|
|
app.ticker,
|
|
);
|
|
|
|
this._games = [opinator, fridge, smartMonday, cleanupHS];
|
|
|
|
this._playedGamesIds = [];
|
|
this._unPlayedGamesIds = [];
|
|
for (var i = 0; i < this._games.length; i++) {
|
|
this._unPlayedGamesIds.push(i);
|
|
}
|
|
this._currentGame = undefined;
|
|
|
|
// in between game display
|
|
this._resultScreen = new GameResultDisplay();
|
|
this._nextGameAnnounceScreen = new AnnounceNextGameDisplay();
|
|
|
|
this._gameContainer.addChild(this._resultScreen.node);
|
|
this._gameContainer.addChild(this._nextGameAnnounceScreen.node);
|
|
this._resultScreen.node.visible = false;
|
|
this._nextGameAnnounceScreen.node.visible = false;
|
|
}
|
|
|
|
start() {
|
|
this.nextGame();
|
|
}
|
|
|
|
nextGame() {
|
|
const unplayedLength = this._unPlayedGamesIds.length;
|
|
if (unplayedLength == 0) {
|
|
console.log("level up");
|
|
this._difficulty += 1;
|
|
this._unPlayedGamesIds = this._playedGamesIds;
|
|
this._playedGamesIds = [];
|
|
}
|
|
|
|
const nextIndex = getRandomInt(unplayedLength);
|
|
const nextId = this._unPlayedGamesIds[nextIndex];
|
|
console.log(nextIndex, this._playedGamesIds, this._unPlayedGamesIds);
|
|
this._playedGamesIds.push(this._unPlayedGamesIds[nextIndex]);
|
|
this._unPlayedGamesIds.splice(nextIndex, 1);
|
|
this._currentGame = this._games[nextId];
|
|
this._currentGame.onEnd = (status) => {
|
|
this._onGameEnd(status);
|
|
};
|
|
this._gameContainer.addChild(this._currentGame.gameContainer);
|
|
this._currentGame.start();
|
|
}
|
|
|
|
_onGameEnd(status) {
|
|
console.log("game end", status);
|
|
this._gameContainer.removeChild(this._currentGame.gameContainer);
|
|
|
|
this._resultScreen.setStatus(status);
|
|
this._resultScreen.node.visible = true;
|
|
setTimeout(() => {
|
|
this._resultScreen.node.visible = false;
|
|
this._nextGameAnnounceScreen.node.visible = true;
|
|
}, this.gameResultTime);
|
|
|
|
setTimeout(() => {
|
|
this._nextGameAnnounceScreen.node.visible = false;
|
|
console.log("starting next game");
|
|
this.nextGame();
|
|
}, this.gameResultTime + this.nextGameAnnounceTime);
|
|
}
|
|
}
|