debuggable and code stripping
This commit is contained in:
parent
6009818d93
commit
7914eb444a
24
src/lib/debuggable.ts
Normal file
24
src/lib/debuggable.ts
Normal file
@ -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>("debug");
|
||||
if (debugKeys.some((k) => debug[k])) {
|
||||
this.debugDraw(...args);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
abstract debugDraw(...args: unknown[]): void;
|
||||
}
|
@ -103,3 +103,7 @@ setInterval(() => {
|
||||
|
||||
const gameLoop = new GameLoop();
|
||||
gameLoop.start(state);
|
||||
|
||||
if (import.meta.env.DEV) {
|
||||
console.log("Running in development mode");
|
||||
}
|
||||
|
@ -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<TrackSegment>;
|
||||
t: number;
|
||||
|
||||
engineLength = 40;
|
||||
spacing = 30;
|
||||
spacing = 20;
|
||||
|
||||
speed = 10;
|
||||
|
||||
@ -25,26 +25,11 @@ export class Train {
|
||||
}
|
||||
|
||||
constructor(track: Spline<TrackSegment>, cars: TrainCar[]) {
|
||||
super("train", "path");
|
||||
this.path = track;
|
||||
this.t = 0;
|
||||
const resources = getContextItem<ResourceManager>("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>("debug");
|
||||
const doodler = getContextItem<Doodler>("doodler");
|
||||
if (debug.path) {
|
||||
@ -141,10 +133,6 @@ export class Train {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
for (const car of this.cars) {
|
||||
car.draw();
|
||||
}
|
||||
}
|
||||
|
||||
real2Track(length: number) {
|
||||
|
@ -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()],
|
||||
});
|
||||
|
66
vite/plugins/strip.ts
Normal file
66
vite/plugins/strip.ts
Normal file
@ -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;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user