From 5cb4c299a3031f97f655c20593e4810ead73a5f4 Mon Sep 17 00:00:00 2001 From: lagaffe <> Date: Sun, 26 Apr 2026 00:55:27 +0200 Subject: [PATCH] add TimedGame class, integrate it to smartmonday game --- src/games/game.js | 10 ++++------ src/games/index.js | 1 + src/games/smartMonday.js | 13 ++++++++----- src/games/timedGame.js | 28 ++++++++++++++++++++++++++++ src/main.js | 6 +++--- 5 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 src/games/timedGame.js diff --git a/src/games/game.js b/src/games/game.js index 1c384c1..90eea82 100644 --- a/src/games/game.js +++ b/src/games/game.js @@ -18,12 +18,10 @@ export class Game { this.reset(); } - end() {} - - _win() { - this.win = true; - console.log("game won"); - this.end(); + /* status indicates if its a win or a loss */ + end(status) { + this.win = status; + console.log(status ? "game won" : "game lost"); // we probably want to call a callback here } } diff --git a/src/games/index.js b/src/games/index.js index 3d26666..ae13ec4 100644 --- a/src/games/index.js +++ b/src/games/index.js @@ -2,3 +2,4 @@ export { Fridge } from "./fridge.js"; export { Opinator } from "./opinator.js"; export { SmartMonday } from "./smartMonday.js"; export { CleanupHS } from "./cleanupHS.js"; +export { TimedGame } from "./timedGame.js"; diff --git a/src/games/smartMonday.js b/src/games/smartMonday.js index 1abcffb..94527eb 100644 --- a/src/games/smartMonday.js +++ b/src/games/smartMonday.js @@ -1,12 +1,14 @@ import { Application, Assets, Container, Sprite, Rectangle } from "pixi.js"; -import { Game } from "./game"; +import { TimedGame } from "./timedGame"; import { KeyBoardListener } from "../common"; -export class SmartMonday extends Game { +export class SmartMonday extends TimedGame { constructor() { - super(); + super(10000); // 10 chars per second this._goal = 100 * this._difficulty; // number of characters + this.timeout = 1000 * (this._goal / 10); // 10 chars per second + this._currentString = ""; this._keyEventHandle = undefined; } @@ -22,12 +24,13 @@ export class SmartMonday extends Game { this._currentString += event.key; console.log("current text", this._currentString); if (this._currentString.length >= this._goal) { - this._win(); + this.end(true); } }); } - end() { + end(status) { + super.end(status); KeyBoardListener.offKeyDown(this._keyEventHandle); } } diff --git a/src/games/timedGame.js b/src/games/timedGame.js new file mode 100644 index 0000000..864c21c --- /dev/null +++ b/src/games/timedGame.js @@ -0,0 +1,28 @@ +import { Game } from "./game"; + +export class TimedGame extends Game { + constructor(time) { + super(); + this._timeoutId = undefined; + this.timeout = time; + } + + start() { + super.start(); + this._timeoutId = setInterval(() => { + console.log("time end"); + this.end(this._win); + }, this.timeout); + } + + reset() { + super.reset(); + clearTimeout(this._timeoutId); + this._timeoutId = undefined; + } + + end(status) { + super.end(status); + clearTimeout(this._timeoutId); + } +} diff --git a/src/main.js b/src/main.js index c00c6d6..a727536 100644 --- a/src/main.js +++ b/src/main.js @@ -101,11 +101,11 @@ function switchToGame(gameContainer, newGame) { const smartMonday = new SmartMonday(); const cleanupHS = new CleanupHS(root.width, root.height); //gameContainter.addChild(fridge.gameContainer); - gameContainter.addChild(opinator.gameContainer); - //gameContainter.addChild(smartMonday.gameContainer); + //gameContainter.addChild(opinator.gameContainer); + gameContainter.addChild(smartMonday.gameContainer); //gameContainter.addChild(cleanupHS.gameContainer); root.addChild(gameContainter); - opinator.start(); + smartMonday.start(); })();