73 lines
2.8 KiB
JavaScript
73 lines
2.8 KiB
JavaScript
import { Application, Assets, Container, Sprite } from "pixi.js";
|
|
import { TimedGame } from "./timedGame";
|
|
|
|
export class Opinator extends TimedGame {
|
|
constructor(ticker) {
|
|
super(10000, ticker);
|
|
this._inMove = false; // indicates if player has grabed the wire (is mouse down)
|
|
this._currentScore = 0;
|
|
this._pixelGoalScore = undefined;
|
|
this._baseGoalScore = 8;
|
|
this._goalScore = this._baseGoalScore * 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
|
|
|
|
this.timeout = this._baseGoalScore * 1000; // 1s per sprite len
|
|
|
|
this._backgroundSprite = Sprite.from("opinator");
|
|
|
|
this._backgroundSprite.eventMode = "static";
|
|
|
|
this._backgroundSprite.on("pointerdown", (event) => {
|
|
console.log("Opinator grabbed");
|
|
this._inMove = true;
|
|
this._lastMousePos = event.client.clone();
|
|
});
|
|
|
|
this._backgroundSprite.on("pointerupoutside", () => {
|
|
console.log("Opinator released");
|
|
this._inMove = false;
|
|
this._lastMousePos = undefined;
|
|
});
|
|
this._backgroundSprite.on("pointerup", () => {
|
|
console.log("Opinator released");
|
|
this._inMove = false;
|
|
this._lastMousePos = undefined;
|
|
});
|
|
|
|
this._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) {
|
|
this.end(true);
|
|
}
|
|
} //else ignore, not enough move to get some distance
|
|
}
|
|
});
|
|
this._backgroundSprite.layout = { objectFit: "contain" };
|
|
this.gameContainer.layout = { width: "100%", height: "100%" };
|
|
this._addGameObjectChild(this._backgroundSprite);
|
|
}
|
|
|
|
reset() {
|
|
super.reset();
|
|
this._currentScore = 0;
|
|
this._pixelGoalScore = this._backgroundSprite.width * this._goalScore; // beware of resize mid game
|
|
}
|
|
|
|
start() {
|
|
super.start();
|
|
}
|
|
}
|