From 7914eb444af9082db6d1942f6b3ab293b68a0105 Mon Sep 17 00:00:00 2001 From: Emma Date: Sat, 15 Feb 2025 20:41:47 -0700 Subject: [PATCH] debuggable and code stripping --- src/lib/debuggable.ts | 24 ++++++++++++++++ src/main.ts | 4 +++ src/train/train.ts | 36 ++++++++--------------- vite.config.ts | 9 +++--- vite/plugins/strip.ts | 66 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 111 insertions(+), 28 deletions(-) create mode 100644 src/lib/debuggable.ts create mode 100644 vite/plugins/strip.ts diff --git a/src/lib/debuggable.ts b/src/lib/debuggable.ts new file mode 100644 index 0000000..74f2004 --- /dev/null +++ b/src/lib/debuggable.ts @@ -0,0 +1,24 @@ +import { getContextItem } from "./context.ts"; + +export abstract class Drawable { + abstract draw(...args: unknown[]): void; +} + +export abstract class Debuggable extends Drawable { + constructor(...debugKeys: (keyof Debug)[]) { + super(); + if (import.meta.env.DEV) { + const draw = this.draw.bind(this); + console.log("Adding debug draw"); + this.draw = (...args: unknown[]) => { + draw(...args); + const debug = getContextItem("debug"); + if (debugKeys.some((k) => debug[k])) { + this.debugDraw(...args); + } + }; + } + } + + abstract debugDraw(...args: unknown[]): void; +} diff --git a/src/main.ts b/src/main.ts index 3c94103..f9c68e9 100644 --- a/src/main.ts +++ b/src/main.ts @@ -103,3 +103,7 @@ setInterval(() => { const gameLoop = new GameLoop(); gameLoop.start(state); + +if (import.meta.env.DEV) { + console.log("Running in development mode"); +} diff --git a/src/train/train.ts b/src/train/train.ts index bdfb6d9..6d3470f 100644 --- a/src/train/train.ts +++ b/src/train/train.ts @@ -6,8 +6,9 @@ import { Doodler, Vector } from "@bearmetal/doodler"; import { Spline, TrackSegment, TrackSystem } from "../track/system.ts"; import { ResourceManager } from "../lib/resources.ts"; import { map } from "../math/lerp.ts"; +import { Debuggable } from "../lib/debuggable.ts"; -export class Train { +export class Train extends Debuggable { nodes: Vector[] = []; cars: TrainCar[] = []; @@ -15,8 +16,7 @@ export class Train { path: Spline; t: number; - engineLength = 40; - spacing = 30; + spacing = 20; speed = 10; @@ -25,26 +25,11 @@ export class Train { } constructor(track: Spline, cars: TrainCar[]) { + super("train", "path"); this.path = track; this.t = 0; - const resources = getContextItem("resources"); this.cars = cars; - // this.cars.push( - // new TrainCar( - // 55, - // engineSprites, - // 80, - // 20, - // { at: new Vector(0, 60), width: 80, height: 20 }, - // ), - // new TrainCar( - // 25, - // engineSprites, - // 40, - // 20, - // { at: new Vector(80, 0), width: 40, height: 20 }, - // ), - // ); + let currentOffset = 0; try { for (const car of this.cars) { @@ -108,6 +93,13 @@ export class Train { // } draw() { + for (const car of this.cars) { + car.draw(); + } + } + + override debugDraw(...args: unknown[]): void { + console.log("Train debug draw"); const debug = getContextItem("debug"); const doodler = getContextItem("doodler"); if (debug.path) { @@ -141,10 +133,6 @@ export class Train { }); } } - - for (const car of this.cars) { - car.draw(); - } } real2Track(length: number) { diff --git a/vite.config.ts b/vite.config.ts index bf20a8a..463f414 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,7 +1,8 @@ -import { defineConfig } from 'vite' -import deno from '@deno/vite-plugin' +import { defineConfig } from "vite"; +import deno from "@deno/vite-plugin"; +import { strip } from "./vite/plugins/strip.ts"; // https://vite.dev/config/ export default defineConfig({ - plugins: [deno()], -}) + plugins: [deno(), strip()], +}); diff --git a/vite/plugins/strip.ts b/vite/plugins/strip.ts new file mode 100644 index 0000000..0227c40 --- /dev/null +++ b/vite/plugins/strip.ts @@ -0,0 +1,66 @@ +import { Plugin } from "vite"; + +export function strip(): Plugin { + const p: Plugin = { + name: "debug-strip", + enforce: "pre", + transform(code: string, id: string) { + if (!id.endsWith(".ts")) { + return code; + } + + const keyword = "override debugDraw"; + const results = []; + let currentIndex = 0; + + while (true) { + // Find the next occurrence of the keyword starting from currentIndex. + const startIndex = code.indexOf(keyword, currentIndex); + if (startIndex === -1) { + break; // No more occurrences. + } + + // Find the first opening brace '{' after the keyword. + const braceStart = code.indexOf("{", startIndex); + if (braceStart === -1) { + // No opening brace found; skip this occurrence. + currentIndex = startIndex + keyword.length; + continue; + } + + // Use a counter to find the matching closing brace. + let openBraces = 0; + let endIndex = -1; + for (let i = braceStart; i < code.length; i++) { + if (code[i] === "{") { + openBraces++; + } else if (code[i] === "}") { + openBraces--; + } + + // When openBraces returns to 0, we found the matching closing brace. + if (openBraces === 0) { + endIndex = i; + break; + } + } + + // If a matching closing brace was found, extract the substring. + if (endIndex !== -1) { + results.push(code.substring(startIndex, endIndex + 1)); + // Move the currentIndex past the extracted block. + currentIndex = endIndex + 1; + } else { + // If no matching closing brace is found, skip this occurrence. + currentIndex = startIndex + keyword.length; + } + } + + for (const result of results) { + code = code.replace(result, ""); + } + return code; + }, + }; + return p; +}