add game manager to handle game life cycle, add onEnd callback to Game class
This commit is contained in:
parent
5aae54fe65
commit
4ffe8a162b
7 changed files with 82 additions and 8 deletions
|
|
@ -22,3 +22,7 @@ export function getIntersection(a, b, out = new Rectangle()) {
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getRandomInt(max) {
|
||||||
|
return Math.floor(Math.random() * max);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
export { backgroundLayout } from "./layouts";
|
export { backgroundLayout } from "./layouts";
|
||||||
export { makeDragable, makeUnDragable } from "./dragAndDrop";
|
export { makeDragable, makeUnDragable } from "./dragAndDrop";
|
||||||
export { getIntersection } from "./helpers";
|
export { getIntersection } from "./helpers";
|
||||||
|
export { getRandomInt } from "./helpers";
|
||||||
export { KeyBoardListener } from "./keyBoardEvents";
|
export { KeyBoardListener } from "./keyBoardEvents";
|
||||||
|
|
|
||||||
65
src/gameManager.js
Normal file
65
src/gameManager.js
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
import { getRandomInt } from "./common";
|
||||||
|
import { Fridge, Opinator, SmartMonday, CleanupHS } from "./games";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
console.log("next !");
|
||||||
|
this.nextGame();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -4,6 +4,7 @@ export class Game {
|
||||||
constructor() {
|
constructor() {
|
||||||
this._difficulty = 1;
|
this._difficulty = 1;
|
||||||
this.won = false;
|
this.won = false;
|
||||||
|
this.onEnd = () => {};
|
||||||
|
|
||||||
this.gameContainer = new Container(); // main container, to add the game to a node, add this container as a child
|
this.gameContainer = new Container(); // main container, to add the game to a node, add this container as a child
|
||||||
this.gameContainer.layout = true;
|
this.gameContainer.layout = true;
|
||||||
|
|
@ -56,9 +57,9 @@ export class Game {
|
||||||
|
|
||||||
/* status indicates if its a win or a loss */
|
/* status indicates if its a win or a loss */
|
||||||
end(status) {
|
end(status) {
|
||||||
this.win = status;
|
this.won = status;
|
||||||
console.log(status ? "game won" : "game lost");
|
console.log(status ? "game won" : "game lost");
|
||||||
// we probably want to call a callback here
|
this.onEnd(status);
|
||||||
}
|
}
|
||||||
|
|
||||||
_addGameObjectChild(node) {
|
_addGameObjectChild(node) {
|
||||||
|
|
|
||||||
|
|
@ -61,6 +61,6 @@ export class TimedGame extends Game {
|
||||||
|
|
||||||
_onTimeout() {
|
_onTimeout() {
|
||||||
console.log("time end");
|
console.log("time end");
|
||||||
this.end(this._win);
|
this.end(this.won);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
10
src/main.js
10
src/main.js
|
|
@ -2,6 +2,7 @@ import "@pixi/layout";
|
||||||
import { Application, Assets, Container, Sprite } from "pixi.js";
|
import { Application, Assets, Container, Sprite } from "pixi.js";
|
||||||
import { Fridge, Opinator, SmartMonday, CleanupHS } from "./games";
|
import { Fridge, Opinator, SmartMonday, CleanupHS } from "./games";
|
||||||
import { backgroundLayout, KeyBoardListener } from "./common";
|
import { backgroundLayout, KeyBoardListener } from "./common";
|
||||||
|
import { GameManager } from "./gameManager";
|
||||||
|
|
||||||
const app = new Application();
|
const app = new Application();
|
||||||
let root = new Container();
|
let root = new Container();
|
||||||
|
|
@ -96,6 +97,12 @@ function switchToGame(gameContainer, newGame) {
|
||||||
maxHeight: "100%",
|
maxHeight: "100%",
|
||||||
margin: 0,
|
margin: 0,
|
||||||
};
|
};
|
||||||
|
root.addChild(gameContainter);
|
||||||
|
|
||||||
|
const manager = new GameManager(app, gameContainter);
|
||||||
|
manager.start();
|
||||||
|
|
||||||
|
/*
|
||||||
const opinator = new Opinator(app.ticker);
|
const opinator = new Opinator(app.ticker);
|
||||||
const fridge = new Fridge(app.ticker);
|
const fridge = new Fridge(app.ticker);
|
||||||
const smartMonday = new SmartMonday(app.ticker);
|
const smartMonday = new SmartMonday(app.ticker);
|
||||||
|
|
@ -103,7 +110,6 @@ function switchToGame(gameContainer, newGame) {
|
||||||
let currentGame = cleanupHS;
|
let currentGame = cleanupHS;
|
||||||
gameContainter.addChild(currentGame.gameContainer);
|
gameContainter.addChild(currentGame.gameContainer);
|
||||||
|
|
||||||
root.addChild(gameContainter);
|
|
||||||
|
|
||||||
currentGame.start();
|
currentGame.start();
|
||||||
|
*/
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
3
todo.md
3
todo.md
|
|
@ -2,6 +2,3 @@
|
||||||
|
|
||||||
- diplay info
|
- diplay info
|
||||||
- game result
|
- game result
|
||||||
- game
|
|
||||||
- game name
|
|
||||||
- play instruction
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue