add TimedGame class, integrate it to smartmonday game

This commit is contained in:
lagaffe 2026-04-26 00:55:27 +02:00
parent 108fcf3d31
commit 5cb4c299a3
5 changed files with 44 additions and 14 deletions

View file

@ -18,12 +18,10 @@ export class Game {
this.reset(); this.reset();
} }
end() {} /* status indicates if its a win or a loss */
end(status) {
_win() { this.win = status;
this.win = true; console.log(status ? "game won" : "game lost");
console.log("game won");
this.end();
// we probably want to call a callback here // we probably want to call a callback here
} }
} }

View file

@ -2,3 +2,4 @@ export { Fridge } from "./fridge.js";
export { Opinator } from "./opinator.js"; export { Opinator } from "./opinator.js";
export { SmartMonday } from "./smartMonday.js"; export { SmartMonday } from "./smartMonday.js";
export { CleanupHS } from "./cleanupHS.js"; export { CleanupHS } from "./cleanupHS.js";
export { TimedGame } from "./timedGame.js";

View file

@ -1,12 +1,14 @@
import { Application, Assets, Container, Sprite, Rectangle } from "pixi.js"; import { Application, Assets, Container, Sprite, Rectangle } from "pixi.js";
import { Game } from "./game"; import { TimedGame } from "./timedGame";
import { KeyBoardListener } from "../common"; import { KeyBoardListener } from "../common";
export class SmartMonday extends Game { export class SmartMonday extends TimedGame {
constructor() { constructor() {
super(); super(10000); // 10 chars per second
this._goal = 100 * this._difficulty; // number of characters this._goal = 100 * this._difficulty; // number of characters
this.timeout = 1000 * (this._goal / 10); // 10 chars per second
this._currentString = ""; this._currentString = "";
this._keyEventHandle = undefined; this._keyEventHandle = undefined;
} }
@ -22,12 +24,13 @@ export class SmartMonday extends Game {
this._currentString += event.key; this._currentString += event.key;
console.log("current text", this._currentString); console.log("current text", this._currentString);
if (this._currentString.length >= this._goal) { if (this._currentString.length >= this._goal) {
this._win(); this.end(true);
} }
}); });
} }
end() { end(status) {
super.end(status);
KeyBoardListener.offKeyDown(this._keyEventHandle); KeyBoardListener.offKeyDown(this._keyEventHandle);
} }
} }

28
src/games/timedGame.js Normal file
View file

@ -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);
}
}

View file

@ -101,11 +101,11 @@ function switchToGame(gameContainer, newGame) {
const smartMonday = new SmartMonday(); const smartMonday = new SmartMonday();
const cleanupHS = new CleanupHS(root.width, root.height); const cleanupHS = new CleanupHS(root.width, root.height);
//gameContainter.addChild(fridge.gameContainer); //gameContainter.addChild(fridge.gameContainer);
gameContainter.addChild(opinator.gameContainer); //gameContainter.addChild(opinator.gameContainer);
//gameContainter.addChild(smartMonday.gameContainer); gameContainter.addChild(smartMonday.gameContainer);
//gameContainter.addChild(cleanupHS.gameContainer); //gameContainter.addChild(cleanupHS.gameContainer);
root.addChild(gameContainter); root.addChild(gameContainter);
opinator.start(); smartMonday.start();
})(); })();