uradibou/src/games/opinator.js
2026-04-18 20:29:19 +02:00

65 lines
2.5 KiB
JavaScript

import { Application, Assets, Container, Sprite } from "pixi.js";
import { backgroundLayout } from "../common";
import { Game } from "./game";
export class Opinator extends Game {
constructor() {
super();
this._inMove = false; // indicates if player has grabed the wire (is mouse down)
this._currentScore = 0;
this._pixelGoalScore = undefined;
this._goalScore = 10 * this._difficulty; // goal to match, measured in play area, 10 = moving the mouse on the equivalent of 10 play areas
this._lastMousePos = undefined; // use to compute the travelled distance
const backgroundSprite = Sprite.from("opinator");
this._pixelGoalScore = backgroundSprite.width * this._goalScore; // beware of resize mid game
backgroundSprite.eventMode = "static";
backgroundSprite.on("pointerdown", (event) => {
console.log("Opinator grabbed");
this._inMove = true;
this._lastMousePos = event.client.clone();
});
backgroundSprite.on("pointerupoutside", () => {
console.log("Opinator released");
this._inMove = false;
this._lastMousePos = undefined;
});
backgroundSprite.on("pointerup", () => {
console.log("Opinator released");
this._inMove = false;
this._lastMousePos = undefined;
});
backgroundSprite.on("mousemove", (event) => {
if (this._inMove && !this.won) {
const newMousePos = event.client;
const progress = Math.sqrt(
(newMousePos.x - this._lastMousePos.x) ** 2 +
(newMousePos.y - this._lastMousePos.y) ** 2,
);
if (progress > 0) {
this._lastMousePos = newMousePos.clone();
this._currentScore += progress;
console.log(
"mouse move, current score",
this._currentScore,
"goal",
this._pixelGoalScore,
);
if (this._pixelGoalScore < this._currentScore) {
console.log("game won");
this.won = true;
}
} //else ignore, not enough move to get some distance
}
});
backgroundSprite.layout = backgroundLayout;
this.gameContainer.addChild(backgroundSprite);
}
reset() {}
}