Compare commits

...

2 commits

Author SHA1 Message Date
lagaffe
8f08f96096 add game smart monday 2026-04-19 13:44:45 +02:00
lagaffe
0a95900c55 add class for keyboard events 2026-04-19 13:26:59 +02:00
9 changed files with 89 additions and 15 deletions

View file

@ -1,3 +1,4 @@
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 { KeyBoardListener } from "./keyBoardEvents";

View file

@ -0,0 +1,46 @@
export class KeyBoardListener {
static _listenersKeyUp = {};
static _listenersKeyDown = {};
static ready = false;
static _listenCount = 0;
static init() {
document.addEventListener("keydown", (event) => {
for (const listener of Object.values(
KeyBoardListener._listenersKeyDown,
)) {
listener(event);
}
});
document.addEventListener("keyup", (event) => {
for (const listener of Object.values(
KeyBoardListener._listenersKeyUp,
)) {
listener(event);
}
});
KeyBoardListener.ready = true;
}
/* return id used to remove the listener */
static onKeyDown(callback) {
const id = KeyBoardListener._listenCount++;
KeyBoardListener._listenersKeyDown[id] = callback;
return id;
}
static offKeyDown(id) {
delete KeyBoardListener._listenersKeyDown[id];
}
/* return id used to remove the listener */
static onKeyUp(callback) {
const id = KeyBoardListener._listenCount++;
KeyBoardListener._listenersKeyUp[id] = callback;
return id;
}
static offKeyUp(id) {
delete KeyBoardListener._listenersKeyUp[id];
}
}

View file

@ -91,7 +91,7 @@ export class Fridge extends Game {
makeDragable(newBottle, (node) => { makeDragable(newBottle, (node) => {
this._onBottleDrop(node); this._onBottleDrop(node);
if (this._bottlePlaced >= this._bottleCount) { if (this._bottlePlaced >= this._bottleCount) {
console.log("fridge game won"); this._win();
} }
}); });
this.gameContainer.addChild(newBottle); this.gameContainer.addChild(newBottle);

View file

@ -10,4 +10,10 @@ export class Game {
/* should reset the game to its initial position*/ /* should reset the game to its initial position*/
reset() {} reset() {}
_win() {
this.win = true;
console.log("game won");
// we probably want to call a callback here
}
} }

View file

@ -1,2 +1,3 @@
export { Fridge } from "./fridge.js"; export { Fridge } from "./fridge.js";
export { Opinator } from "./opinator.js"; export { Opinator } from "./opinator.js";
export { SmartMonday } from "./smartMonday.js";

View file

@ -51,13 +51,13 @@ export class Opinator extends Game {
this._pixelGoalScore, this._pixelGoalScore,
); );
if (this._pixelGoalScore < this._currentScore) { if (this._pixelGoalScore < this._currentScore) {
console.log("game won"); this._win();
this.won = true;
} }
} //else ignore, not enough move to get some distance } //else ignore, not enough move to get some distance
} }
}); });
backgroundSprite.layout = backgroundLayout; backgroundSprite.layout = { objectFit: "contain" };
this.gameContainer.layout = { width: "100%", height: "100%" };
this.gameContainer.addChild(backgroundSprite); this.gameContainer.addChild(backgroundSprite);
} }

22
src/games/smartMonday.js Normal file
View file

@ -0,0 +1,22 @@
import { Application, Assets, Container, Sprite, Rectangle } from "pixi.js";
import { Game } from "./game";
import { KeyBoardListener } from "../common";
export class SmartMonday extends Game {
constructor() {
super();
this._goal = 100 * this._difficulty; // number of characters
this._currentString = "";
const handle = KeyBoardListener.onKeyDown((event) => {
this._currentString += event.key;
console.log("current text", this._currentString);
if (this._currentString.length >= this._goal) {
this._win();
}
});
}
reset() {}
}

View file

@ -1,7 +1,7 @@
import "@pixi/layout"; import "@pixi/layout";
import { Application, Assets, Container, Sprite } from "pixi.js"; import { Application, Assets, Container, Sprite } from "pixi.js";
import { Fridge, Opinator } from "./games"; import { Fridge, Opinator, SmartMonday } from "./games";
import { backgroundLayout } from "./common"; import { backgroundLayout, KeyBoardListener } from "./common";
const app = new Application(); const app = new Application();
let root = new Container(); let root = new Container();
@ -53,6 +53,8 @@ async function preload() {
}, },
]; ];
await Assets.load(assets); await Assets.load(assets);
KeyBoardListener.init();
} }
function switchToGame(gameContainer, newGame) { function switchToGame(gameContainer, newGame) {
@ -80,7 +82,10 @@ function switchToGame(gameContainer, newGame) {
}; };
const opinator = new Opinator(); const opinator = new Opinator();
const fridge = new Fridge(); const fridge = new Fridge();
gameContainter.addChild(fridge.gameContainer); const smartMonday = new SmartMonday();
//gameContainter.addChild(opinator.gameContainer); //gameContainter.addChild(fridge.gameContainer);
gameContainter.addChild(opinator.gameContainer);
gameContainter.addChild(smartMonday.gameContainer);
root.addChild(gameContainter); root.addChild(gameContainter);
})(); })();

7
src/package-lock.json generated
View file

@ -9,7 +9,6 @@
"version": "0.0.0", "version": "0.0.0",
"dependencies": { "dependencies": {
"@pixi/layout": "^3.2.0", "@pixi/layout": "^3.2.0",
"@pixi/math": "^7.4.3",
"pixi.js": "^8.0.0" "pixi.js": "^8.0.0"
}, },
"devDependencies": { "devDependencies": {
@ -102,12 +101,6 @@
"yoga-layout": "^3" "yoga-layout": "^3"
} }
}, },
"node_modules/@pixi/math": {
"version": "7.4.3",
"resolved": "https://registry.npmjs.org/@pixi/math/-/math-7.4.3.tgz",
"integrity": "sha512-/uJOVhR2DOZ+zgdI6Bs/CwcXT4bNRKsS+TqX3ekRIxPCwaLra+Qdm7aDxT5cTToDzdxbKL5+rwiLu3Y1egILDw==",
"license": "MIT"
},
"node_modules/@pixi/react": { "node_modules/@pixi/react": {
"version": "8.0.5", "version": "8.0.5",
"resolved": "https://registry.npmjs.org/@pixi/react/-/react-8.0.5.tgz", "resolved": "https://registry.npmjs.org/@pixi/react/-/react-8.0.5.tgz",