mv playground src
This commit is contained in:
parent
5ae84ad471
commit
ea22b2de6a
24
src/code_stopper.js
Normal file
24
src/code_stopper.js
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
export class CodeStopper {
|
||||||
|
constructor() {
|
||||||
|
this.abortController = new AbortController();
|
||||||
|
}
|
||||||
|
|
||||||
|
start() {
|
||||||
|
this.abortController = new AbortController();
|
||||||
|
}
|
||||||
|
|
||||||
|
stop() {
|
||||||
|
this.abortController.abort();
|
||||||
|
}
|
||||||
|
|
||||||
|
isStopped() {
|
||||||
|
return this.abortController.signal.aborted;
|
||||||
|
}
|
||||||
|
|
||||||
|
addListener(fn) {
|
||||||
|
this.abortController.signal
|
||||||
|
.addEventListener("abort", () => {
|
||||||
|
fn();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
54
src/gamelib.js
Normal file
54
src/gamelib.js
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
export class Gamelib {
|
||||||
|
constructor(console, codeStopper, canvasElement) {
|
||||||
|
this.console = console;
|
||||||
|
this.codeStopper = codeStopper;
|
||||||
|
|
||||||
|
this.canvas = canvasElement;
|
||||||
|
this.width = 480;
|
||||||
|
this.height = 360;
|
||||||
|
this.canvas.width = this.width;
|
||||||
|
this.canvas.height = this.height;
|
||||||
|
this.cx = this.canvas.getContext("2d");
|
||||||
|
this.cx.imageSmootingEnabled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
println(msg) {
|
||||||
|
this.console.log(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
startGameLoop(loopFunction) {
|
||||||
|
let before = Date.now();
|
||||||
|
const loopInterval = setInterval(() => {
|
||||||
|
const now = Date.now();
|
||||||
|
const deltaT = (now - before) / 1000;
|
||||||
|
before = now;
|
||||||
|
loopFunction(deltaT);
|
||||||
|
}, 16);
|
||||||
|
|
||||||
|
if (this.codeStopper.isStopped()) {
|
||||||
|
clearInterval(loopInterval);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.codeStopper.addListener(() => {
|
||||||
|
clearInterval(loopInterval);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
rgb(red, green, blue) {
|
||||||
|
return `rgb(${red}, ${green}, ${blue})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
clear(color) {
|
||||||
|
const cx = this.cx;
|
||||||
|
|
||||||
|
cx.fillStyle = color;
|
||||||
|
cx.fillRect(0, 0, this.width, this.height);
|
||||||
|
}
|
||||||
|
|
||||||
|
drawRect(x, y, width, height, color) {
|
||||||
|
const cx = this.cx;
|
||||||
|
|
||||||
|
cx.fillStyle = color;
|
||||||
|
cx.fillRect(x, y, width, height);
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user