From ea22b2de6ac2c8f856e1a21a6233d74667a03d28 Mon Sep 17 00:00:00 2001 From: sfja Date: Fri, 10 Oct 2025 18:01:15 +0200 Subject: [PATCH] mv playground src --- {playground => src}/code-runner.js | 0 src/code_stopper.js | 24 ++++++++++ {playground => src}/debounce.js | 0 src/gamelib.js | 54 +++++++++++++++++++++++ {playground => src}/index.js | 0 {playground => src}/playground-console.js | 0 {playground => src}/sprite-editor.js | 0 7 files changed, 78 insertions(+) rename {playground => src}/code-runner.js (100%) create mode 100644 src/code_stopper.js rename {playground => src}/debounce.js (100%) create mode 100644 src/gamelib.js rename {playground => src}/index.js (100%) rename {playground => src}/playground-console.js (100%) rename {playground => src}/sprite-editor.js (100%) diff --git a/playground/code-runner.js b/src/code-runner.js similarity index 100% rename from playground/code-runner.js rename to src/code-runner.js diff --git a/src/code_stopper.js b/src/code_stopper.js new file mode 100644 index 0000000..72d92ab --- /dev/null +++ b/src/code_stopper.js @@ -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(); + }); + } +} diff --git a/playground/debounce.js b/src/debounce.js similarity index 100% rename from playground/debounce.js rename to src/debounce.js diff --git a/src/gamelib.js b/src/gamelib.js new file mode 100644 index 0000000..204753c --- /dev/null +++ b/src/gamelib.js @@ -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); + } +} diff --git a/playground/index.js b/src/index.js similarity index 100% rename from playground/index.js rename to src/index.js diff --git a/playground/playground-console.js b/src/playground-console.js similarity index 100% rename from playground/playground-console.js rename to src/playground-console.js diff --git a/playground/sprite-editor.js b/src/sprite-editor.js similarity index 100% rename from playground/sprite-editor.js rename to src/sprite-editor.js