Compare commits
6 Commits
vite
...
20e6174658
Author | SHA1 | Date | |
---|---|---|---|
20e6174658 | |||
239f1ac766 | |||
d82a6aaf4f | |||
eb680c470f | |||
9587ce5ae6 | |||
01081706b1 |
3
.gitignore
vendored
@@ -23,3 +23,6 @@ dist-ssr
|
|||||||
*.njsproj
|
*.njsproj
|
||||||
*.sln
|
*.sln
|
||||||
*.sw?
|
*.sw?
|
||||||
|
|
||||||
|
# Packed devtools
|
||||||
|
devtools.zip
|
||||||
|
@@ -3,7 +3,8 @@
|
|||||||
"dev": "deno run -A --node-modules-dir npm:vite",
|
"dev": "deno run -A --node-modules-dir npm:vite",
|
||||||
"build": "deno run -A --node-modules-dir npm:vite build",
|
"build": "deno run -A --node-modules-dir npm:vite build",
|
||||||
"preview": "deno run -A --node-modules-dir npm:vite preview",
|
"preview": "deno run -A --node-modules-dir npm:vite preview",
|
||||||
"serve": "deno run --allow-net --allow-read jsr:@std/http@1/file-server dist/"
|
"serve": "deno run --allow-net --allow-read jsr:@std/http@1/file-server dist/",
|
||||||
|
"pack-devtools": "rm -rf devtools.zip && deno run -A npm:web-ext build -o --source-dir devtools --artifacts-dir devtools.zip"
|
||||||
},
|
},
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"lib": [
|
"lib": [
|
||||||
@@ -13,7 +14,7 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
"imports": {
|
"imports": {
|
||||||
"@bearmetal/doodler": "jsr:@bearmetal/doodler@0.0.5-b",
|
"@bearmetal/doodler": "jsr:@bearmetal/doodler@0.0.5-c",
|
||||||
"@deno/vite-plugin": "npm:@deno/vite-plugin@^1.0.4",
|
"@deno/vite-plugin": "npm:@deno/vite-plugin@^1.0.4",
|
||||||
"vite": "npm:vite@^6.0.1"
|
"vite": "npm:vite@^6.0.1"
|
||||||
}
|
}
|
||||||
|
@@ -1,8 +1,9 @@
|
|||||||
{
|
{
|
||||||
"manifest_version": 2,
|
"manifest_version": 2,
|
||||||
"name": "Context Stack DevTools",
|
"name": "SNR DevTools",
|
||||||
"version": "1.0",
|
"version": "1.0",
|
||||||
"description": "A devtools panel to view and edit context stack values.",
|
"description": "A devtools panel to view and edit context stack values.",
|
||||||
|
"author": "Emmaline Autumn",
|
||||||
"devtools_page": "devtools.html",
|
"devtools_page": "devtools.html",
|
||||||
"background": {
|
"background": {
|
||||||
"scripts": [
|
"scripts": [
|
||||||
@@ -24,5 +25,13 @@
|
|||||||
"devtools",
|
"devtools",
|
||||||
"tabs",
|
"tabs",
|
||||||
"*://*/*"
|
"*://*/*"
|
||||||
]
|
],
|
||||||
|
"icons": {
|
||||||
|
"48": "train icon.png"
|
||||||
|
},
|
||||||
|
"browser_specific_settings": {
|
||||||
|
"gecko": {
|
||||||
|
"id": "snrdt@cyborggrizzly.com"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 1.3 KiB After Width: | Height: | Size: 1.3 KiB |
BIN
public/blobs/snr/sprite/LargeLady.png
Normal file
After Width: | Height: | Size: 2.1 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 1.5 KiB After Width: | Height: | Size: 1.5 KiB |
Before Width: | Height: | Size: 22 KiB After Width: | Height: | Size: 22 KiB |
@@ -1,16 +1,54 @@
|
|||||||
|
// namespace:type/location
|
||||||
|
type sprite = "sprite";
|
||||||
|
type img = "img";
|
||||||
|
type audio = "audio";
|
||||||
|
type ResourceType = keyof ResourceMap;
|
||||||
|
type SpriteId = `${string}:${sprite}/${string}`;
|
||||||
|
|
||||||
|
interface ResourceMap {
|
||||||
|
sprite: HTMLImageElement;
|
||||||
|
img: HTMLImageElement;
|
||||||
|
audio: HTMLAudioElement;
|
||||||
|
}
|
||||||
|
type NamespacedId<T extends keyof ResourceMap> = `${string}:${T}/${string}`;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resources are stored in namespaces, and can be accessed by their namespaced id.
|
||||||
|
* Sprites are located in blob storage as a single png file.
|
||||||
|
* Audio is located in blob storage as a single mp3 file.
|
||||||
|
*
|
||||||
|
* Custom resources can be loaded via the public API, however they will not be loaded on other clients.
|
||||||
|
* Ideally, engine and car definitions should be stored in the resource manager so that custom cars can be created.
|
||||||
|
*/
|
||||||
export class ResourceManager {
|
export class ResourceManager {
|
||||||
private resources: Map<string, unknown> = new Map();
|
private resources: Map<string, unknown> = new Map();
|
||||||
private statuses: Map<string, Promise<boolean>> = new Map();
|
private statuses: Map<string, Promise<boolean>> = new Map();
|
||||||
|
|
||||||
get<T>(name: string): T {
|
get<K extends ResourceType>(name: NamespacedId<K>): ResourceMap[K];
|
||||||
|
get<T>(name: string): T;
|
||||||
|
get<T>(
|
||||||
|
name: string,
|
||||||
|
): T {
|
||||||
if (!this.resources.has(name)) {
|
if (!this.resources.has(name)) {
|
||||||
throw new Error(`Resource ${name} not found`);
|
throw new Error(`Resource ${name} not found`);
|
||||||
}
|
}
|
||||||
return this.resources.get(name) as T;
|
return this.resources.get(name) as T;
|
||||||
}
|
}
|
||||||
|
|
||||||
set(name: string, value: unknown) {
|
set<T extends keyof ResourceMap>(
|
||||||
|
name: NamespacedId<T>,
|
||||||
|
value: unknown,
|
||||||
|
) {
|
||||||
|
const identifier = parseNamespacedId(name);
|
||||||
if (typeof (value as EventSource).addEventListener === "function") {
|
if (typeof (value as EventSource).addEventListener === "function") {
|
||||||
|
if (value instanceof Image) {
|
||||||
|
// During development, we can use the local file system
|
||||||
|
value.src =
|
||||||
|
`/blobs/${identifier.namespace}/${identifier.type}/${identifier.name}${
|
||||||
|
extensionByType(identifier.type)
|
||||||
|
}`;
|
||||||
|
console.log(value.src);
|
||||||
|
}
|
||||||
this.statuses.set(
|
this.statuses.set(
|
||||||
name,
|
name,
|
||||||
new Promise((resolve) => {
|
new Promise((resolve) => {
|
||||||
@@ -36,3 +74,28 @@ export class ResourceManager {
|
|||||||
return Promise.all(Array.from(this.statuses.values()));
|
return Promise.all(Array.from(this.statuses.values()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function extensionByType(type: ResourceType) {
|
||||||
|
switch (type) {
|
||||||
|
case "img":
|
||||||
|
return ".png";
|
||||||
|
case "audio":
|
||||||
|
return ".mp3";
|
||||||
|
case "sprite":
|
||||||
|
return ".png";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type NamespaceIdentifier<T extends ResourceType> = {
|
||||||
|
namespace: string;
|
||||||
|
type: T;
|
||||||
|
name: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
function parseNamespacedId<T extends ResourceType>(
|
||||||
|
id: NamespacedId<T>,
|
||||||
|
): NamespaceIdentifier<T> {
|
||||||
|
const [namespace, location] = id.split(":");
|
||||||
|
const [type, ...name] = location.split("/");
|
||||||
|
return { namespace, type: type as T, name: name.join("/") };
|
||||||
|
}
|
||||||
|
19
src/main.ts
@@ -20,9 +20,12 @@ const resources = new ResourceManager();
|
|||||||
const doodler = new ZoomableDoodler({
|
const doodler = new ZoomableDoodler({
|
||||||
fillScreen: true,
|
fillScreen: true,
|
||||||
bg: "#302040",
|
bg: "#302040",
|
||||||
});
|
noSmooth: true,
|
||||||
(doodler as any as { ctx: CanvasRenderingContext2D }).ctx
|
}, () => {});
|
||||||
|
setTimeout(() => {
|
||||||
|
(doodler as any as { ctx: CanvasRenderingContext2D }).ctx
|
||||||
.imageSmoothingEnabled = false;
|
.imageSmoothingEnabled = false;
|
||||||
|
}, 0);
|
||||||
// doodler.minScale = 0.1;
|
// doodler.minScale = 0.1;
|
||||||
(doodler as any).scale = 3.14;
|
(doodler as any).scale = 3.14;
|
||||||
|
|
||||||
@@ -37,13 +40,18 @@ const colors = [
|
|||||||
"violet",
|
"violet",
|
||||||
];
|
];
|
||||||
|
|
||||||
const _debug: Debug = JSON.parse(localStorage.getItem("debug") || "0") || {
|
const _fullDebug: Debug = {
|
||||||
track: false,
|
track: false,
|
||||||
train: false,
|
train: false,
|
||||||
path: false,
|
path: false,
|
||||||
car: false,
|
car: false,
|
||||||
|
bogies: false,
|
||||||
|
angles: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const storedDebug = JSON.parse(localStorage.getItem("debug") || "0");
|
||||||
|
const _debug: Debug = Object.assign({}, _fullDebug, storedDebug);
|
||||||
|
|
||||||
const debug = new Proxy(_debug, {
|
const debug = new Proxy(_debug, {
|
||||||
get: (_, prop: string) => {
|
get: (_, prop: string) => {
|
||||||
// if (prop !in _debug) {
|
// if (prop !in _debug) {
|
||||||
@@ -107,3 +115,8 @@ gameLoop.start(state);
|
|||||||
if (import.meta.env.DEV) {
|
if (import.meta.env.DEV) {
|
||||||
console.log("Running in development mode");
|
console.log("Running in development mode");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
globalThis.TWO_PI = Math.PI * 2;
|
||||||
|
declare global {
|
||||||
|
var TWO_PI: number;
|
||||||
|
}
|
||||||
|
@@ -9,3 +9,19 @@ export const map = (
|
|||||||
x2: number,
|
x2: number,
|
||||||
y2: number,
|
y2: number,
|
||||||
) => (value - x1) * (y2 - x2) / (y1 - x1) + x2;
|
) => (value - x1) * (y2 - x2) / (y1 - x1) + x2;
|
||||||
|
|
||||||
|
export function lerpAngle(a: number, b: number, t: number) {
|
||||||
|
let diff = b - a;
|
||||||
|
// Wrap difference to [-PI, PI]
|
||||||
|
while (diff < -Math.PI) diff += 2 * Math.PI;
|
||||||
|
while (diff > Math.PI) diff -= 2 * Math.PI;
|
||||||
|
return a + diff * t;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function averageAngles(angle1: number, angle2: number) {
|
||||||
|
// Convert angles to unit vectors
|
||||||
|
const x = Math.cos(angle1) + Math.cos(angle2);
|
||||||
|
const y = Math.sin(angle1) + Math.sin(angle2);
|
||||||
|
// Compute the angle of the resulting vector
|
||||||
|
return Math.atan2(y, x);
|
||||||
|
}
|
||||||
|
@@ -34,9 +34,11 @@ export class LoadState extends State<States> {
|
|||||||
|
|
||||||
bootstrapInputs();
|
bootstrapInputs();
|
||||||
|
|
||||||
resources.set("engine-sprites", new Image());
|
// This should be driven by a manifest
|
||||||
resources.get<HTMLImageElement>("engine-sprites")!.src =
|
resources.set("snr:sprite/engine", new Image());
|
||||||
"/sprites/EngineSprites.png";
|
resources.set("snr:sprite/LargeLady", new Image());
|
||||||
|
// resources.get<HTMLImageElement>("snr:sprite/engine")!.src =
|
||||||
|
// "/sprites/EngineSprites.png";
|
||||||
resources.ready().then(() => {
|
resources.ready().then(() => {
|
||||||
this.stateMachine.transitionTo(States.RUNNING);
|
this.stateMachine.transitionTo(States.RUNNING);
|
||||||
});
|
});
|
||||||
|
@@ -8,6 +8,7 @@ import { DotFollower } from "../../train/newTrain.ts";
|
|||||||
import { Train } from "../../train/train.ts";
|
import { Train } from "../../train/train.ts";
|
||||||
import { State } from "../machine.ts";
|
import { State } from "../machine.ts";
|
||||||
import { States } from "./index.ts";
|
import { States } from "./index.ts";
|
||||||
|
import { LargeLady, LargeLadyTender } from "../../train/LargeLady.ts";
|
||||||
|
|
||||||
export class RunningState extends State<States> {
|
export class RunningState extends State<States> {
|
||||||
override name: States = States.RUNNING;
|
override name: States = States.RUNNING;
|
||||||
@@ -58,12 +59,17 @@ export class RunningState extends State<States> {
|
|||||||
// const path = track.path;
|
// const path = track.path;
|
||||||
// const follower = new DotFollower(path, path.points[0].copy());
|
// const follower = new DotFollower(path, path.points[0].copy());
|
||||||
// ctx.trains.push(follower);
|
// ctx.trains.push(follower);
|
||||||
const train = new Train(track.path, [new RedEngine(), new Tender()]);
|
// const train = new Train(track.path, [new LargeLady(), new Tender()]);
|
||||||
ctx.trains.push(train);
|
// ctx.trains.push(train);
|
||||||
});
|
});
|
||||||
|
const train = new Train(track.path, [
|
||||||
|
new LargeLady(),
|
||||||
|
new LargeLadyTender(),
|
||||||
|
]);
|
||||||
|
ctx.trains.push(train);
|
||||||
// const trainCount = 1000;
|
// const trainCount = 1000;
|
||||||
// for (let i = 0; i < trainCount; i++) {
|
// for (let i = 0; i < trainCount; i++) {
|
||||||
// const train = new Train(track.path, [new RedEngine(), new Tender()]);
|
// const train = new Train(track.path, [new LargeLady(), new Tender()]);
|
||||||
// ctx.trains.push(train);
|
// ctx.trains.push(train);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
32
src/test/angleMathBench.test.ts
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
/// <reference no-default-lib="true" />
|
||||||
|
/// <reference lib="esnext" />
|
||||||
|
/// <reference lib="dom" />
|
||||||
|
/// <reference lib="dom.iterable" />
|
||||||
|
/// <reference lib="dom.asynciterable" />
|
||||||
|
/// <reference lib="deno.ns" />
|
||||||
|
|
||||||
|
import { averageAngles, lerpAngle } from "../math/lerp.ts";
|
||||||
|
import { testPerformance } from "./bench.ts";
|
||||||
|
|
||||||
|
Deno.test("angle math", () => {
|
||||||
|
console.log("Average angles");
|
||||||
|
testPerformance(
|
||||||
|
() => {
|
||||||
|
const a = Math.random() * Math.PI * 2;
|
||||||
|
const b = Math.random() * Math.PI * 2;
|
||||||
|
const avg = averageAngles(a, b);
|
||||||
|
},
|
||||||
|
10000,
|
||||||
|
60,
|
||||||
|
);
|
||||||
|
console.log("Lerp angles");
|
||||||
|
testPerformance(
|
||||||
|
() => {
|
||||||
|
const a = Math.random() * Math.PI * 2;
|
||||||
|
const b = Math.random() * Math.PI * 2;
|
||||||
|
const avg = lerpAngle(a, b, .5);
|
||||||
|
},
|
||||||
|
10000,
|
||||||
|
60,
|
||||||
|
);
|
||||||
|
});
|
@@ -44,7 +44,7 @@ export class BankLeft extends TrackSegment {
|
|||||||
const p2 = start.copy();
|
const p2 = start.copy();
|
||||||
const p3 = start.copy();
|
const p3 = start.copy();
|
||||||
const p4 = start.copy();
|
const p4 = start.copy();
|
||||||
const scale = 33;
|
const scale = 66;
|
||||||
|
|
||||||
p2.add(new Vector(1, 0).mult(scale));
|
p2.add(new Vector(1, 0).mult(scale));
|
||||||
p3.set(p2);
|
p3.set(p2);
|
||||||
@@ -70,7 +70,7 @@ export class BankRight extends TrackSegment {
|
|||||||
const p2 = start.copy();
|
const p2 = start.copy();
|
||||||
const p3 = start.copy();
|
const p3 = start.copy();
|
||||||
const p4 = start.copy();
|
const p4 = start.copy();
|
||||||
const scale = 33;
|
const scale = 66;
|
||||||
|
|
||||||
p2.add(new Vector(1, 0).mult(scale));
|
p2.add(new Vector(1, 0).mult(scale));
|
||||||
p3.set(p2);
|
p3.set(p2);
|
||||||
|
@@ -499,7 +499,14 @@ export class Spline<T extends PathSegment = PathSegment> {
|
|||||||
ctx?: CanvasRenderingContext2D;
|
ctx?: CanvasRenderingContext2D;
|
||||||
|
|
||||||
evenPoints: PathPoint[];
|
evenPoints: PathPoint[];
|
||||||
pointSpacing: number;
|
_pointSpacing: number;
|
||||||
|
get pointSpacing() {
|
||||||
|
return this._pointSpacing;
|
||||||
|
}
|
||||||
|
set pointSpacing(value: number) {
|
||||||
|
this._pointSpacing = value;
|
||||||
|
this.evenPoints = this.calculateEvenlySpacedPoints(value);
|
||||||
|
}
|
||||||
|
|
||||||
get points() {
|
get points() {
|
||||||
return Array.from(new Set(this.segments.flatMap((s) => s.points)));
|
return Array.from(new Set(this.segments.flatMap((s) => s.points)));
|
||||||
@@ -514,8 +521,8 @@ export class Spline<T extends PathSegment = PathSegment> {
|
|||||||
if (this.segments.at(-1)?.next === this.segments[0]) {
|
if (this.segments.at(-1)?.next === this.segments[0]) {
|
||||||
this.looped = true;
|
this.looped = true;
|
||||||
}
|
}
|
||||||
this.pointSpacing = 1;
|
this._pointSpacing = 1;
|
||||||
this.evenPoints = this.calculateEvenlySpacedPoints(1);
|
this.evenPoints = this.calculateEvenlySpacedPoints(this._pointSpacing);
|
||||||
this.nodes = [];
|
this.nodes = [];
|
||||||
// for (let i = 0; i < this.points.length; i += 3) {
|
// for (let i = 0; i < this.points.length; i += 3) {
|
||||||
// const node: IControlNode = {
|
// const node: IControlNode = {
|
||||||
@@ -549,7 +556,7 @@ export class Spline<T extends PathSegment = PathSegment> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
calculateEvenlySpacedPoints(spacing: number, resolution = 1): PathPoint[] {
|
calculateEvenlySpacedPoints(spacing: number, resolution = 1): PathPoint[] {
|
||||||
this.pointSpacing = 1;
|
// this._pointSpacing = 1;
|
||||||
// return this.segments.flatMap(s => s.calculateEvenlySpacedPoints(spacing, resolution));
|
// return this.segments.flatMap(s => s.calculateEvenlySpacedPoints(spacing, resolution));
|
||||||
const points: PathPoint[] = [];
|
const points: PathPoint[] = [];
|
||||||
|
|
||||||
@@ -589,7 +596,7 @@ export class Spline<T extends PathSegment = PathSegment> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
this.evenPoints = points;
|
// this.evenPoints = points;
|
||||||
|
|
||||||
return points;
|
return points;
|
||||||
}
|
}
|
||||||
|
188
src/train/LargeLady.ts
Normal file
@@ -0,0 +1,188 @@
|
|||||||
|
import { Doodler, Vector } from "@bearmetal/doodler";
|
||||||
|
import { Train, TrainCar } from "./train.ts";
|
||||||
|
import { getContextItem } from "../lib/context.ts";
|
||||||
|
import { ResourceManager } from "../lib/resources.ts";
|
||||||
|
import { debug } from "node:console";
|
||||||
|
import { averageAngles } from "../math/lerp.ts";
|
||||||
|
|
||||||
|
export class LargeLady extends TrainCar {
|
||||||
|
scale = 1;
|
||||||
|
constructor() {
|
||||||
|
const resources = getContextItem<ResourceManager>("resources");
|
||||||
|
const img = resources.get<HTMLImageElement>("snr:sprite/LargeLady")!;
|
||||||
|
super(50, 10, img, 132, 23, {
|
||||||
|
at: new Vector(0, 0),
|
||||||
|
width: 132,
|
||||||
|
height: 23,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.bogies = [
|
||||||
|
{
|
||||||
|
pos: new Vector(0, 0),
|
||||||
|
angle: 0,
|
||||||
|
length: 35 * this.scale,
|
||||||
|
sprite: {
|
||||||
|
at: new Vector(0, 24),
|
||||||
|
width: 35,
|
||||||
|
height: 23,
|
||||||
|
offset: new Vector(-23, -11.5),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pos: new Vector(0, 0),
|
||||||
|
angle: 0,
|
||||||
|
length: 64 * this.scale,
|
||||||
|
// sprite: {
|
||||||
|
// at: new Vector(0, 23),
|
||||||
|
// width: 33,
|
||||||
|
// height: 19,
|
||||||
|
// offset: new Vector(-19, -9.5),
|
||||||
|
// },
|
||||||
|
sprite: {
|
||||||
|
at: new Vector(36, 24),
|
||||||
|
width: 60,
|
||||||
|
height: 23,
|
||||||
|
offset: new Vector(-35, -11.5),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pos: new Vector(0, 0),
|
||||||
|
angle: 0,
|
||||||
|
length: 35 * this.scale,
|
||||||
|
sprite: {
|
||||||
|
at: new Vector(36, 24),
|
||||||
|
width: 60,
|
||||||
|
height: 23,
|
||||||
|
offset: new Vector(-35, -11.5),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pos: new Vector(0, 0),
|
||||||
|
angle: 0,
|
||||||
|
length: 28,
|
||||||
|
sprite: {
|
||||||
|
at: new Vector(97, 24),
|
||||||
|
width: 22,
|
||||||
|
height: 23,
|
||||||
|
offset: new Vector(-11, -11.5),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
override draw(): void {
|
||||||
|
const doodler = getContextItem<Doodler>("doodler");
|
||||||
|
for (const b of this.bogies) {
|
||||||
|
if (!b.sprite) continue;
|
||||||
|
doodler.drawRotated(b.pos, b.angle + (b.rotate ? 0 : Math.PI), () => {
|
||||||
|
doodler.drawSprite(
|
||||||
|
this.img,
|
||||||
|
b.sprite!.at,
|
||||||
|
b.sprite!.width,
|
||||||
|
b.sprite!.height,
|
||||||
|
b.pos.copy().add(b.sprite!.offset ?? new Vector(0, 0)),
|
||||||
|
b.sprite!.width,
|
||||||
|
b.sprite!.height,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const b = this.bogies[2];
|
||||||
|
const a = this.bogies[1];
|
||||||
|
// const origin = Vector.add(Vector.sub(a.pos, b.pos).div(2), b.pos);
|
||||||
|
// const angle = Vector.sub(b.pos, a.pos).heading();
|
||||||
|
const debug = getContextItem<Debug>("debug");
|
||||||
|
if (debug.bogies) return;
|
||||||
|
|
||||||
|
const difAngle = Vector.sub(a.pos, b.pos).heading();
|
||||||
|
const origin = b.pos.copy().add(new Vector(33, 0).rotate(difAngle));
|
||||||
|
const angle = b.angle;
|
||||||
|
const avgAngle = averageAngles(difAngle, angle) + Math.PI;
|
||||||
|
|
||||||
|
doodler.drawRotated(origin, avgAngle, () => {
|
||||||
|
this.sprite
|
||||||
|
? doodler.drawSprite(
|
||||||
|
this.img,
|
||||||
|
this.sprite.at,
|
||||||
|
this.sprite.width,
|
||||||
|
this.sprite.height,
|
||||||
|
origin.copy().sub(
|
||||||
|
this.imgWidth * this.scale / 2,
|
||||||
|
this.imgHeight * this.scale / 2,
|
||||||
|
),
|
||||||
|
this.imgWidth * this.scale,
|
||||||
|
this.imgHeight * this.scale,
|
||||||
|
)
|
||||||
|
: doodler.drawImage(
|
||||||
|
this.img,
|
||||||
|
origin.copy().sub(this.imgWidth / 2, this.imgHeight / 2),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
// doodler.drawCircle(origin, 4, { color: "blue" });
|
||||||
|
|
||||||
|
doodler.deferDrawing(() => {
|
||||||
|
doodler.drawRotated(origin, avgAngle + Math.PI, () => {
|
||||||
|
doodler.drawSprite(
|
||||||
|
this.img,
|
||||||
|
new Vector(133, 0),
|
||||||
|
28,
|
||||||
|
23,
|
||||||
|
origin.copy().sub(93, this.imgHeight / 2),
|
||||||
|
28,
|
||||||
|
23,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export class LargeLadyTender extends TrainCar {
|
||||||
|
constructor() {
|
||||||
|
const resources = getContextItem<ResourceManager>("resources");
|
||||||
|
const sprite = resources.get("snr:sprite/LargeLady");
|
||||||
|
super(40, 39, sprite, 98, 23, {
|
||||||
|
at: new Vector(0, 48),
|
||||||
|
width: 98,
|
||||||
|
height: 23,
|
||||||
|
});
|
||||||
|
|
||||||
|
this.leading = 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
override draw(): void {
|
||||||
|
const doodler = getContextItem<Doodler>("doodler");
|
||||||
|
const b = this.bogies[0];
|
||||||
|
doodler.drawRotated(b.pos, b.angle, () => {
|
||||||
|
doodler.drawSprite(
|
||||||
|
this.img,
|
||||||
|
new Vector(97, 24),
|
||||||
|
22,
|
||||||
|
23,
|
||||||
|
b.pos.copy().sub(11, 11.5),
|
||||||
|
22,
|
||||||
|
23,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
const angle = Vector.sub(this.bogies[1].pos, this.bogies[0].pos).heading();
|
||||||
|
const origin = this.bogies[1].pos.copy().add(
|
||||||
|
new Vector(-11, 0).rotate(angle),
|
||||||
|
);
|
||||||
|
doodler.drawRotated(origin, angle, () => {
|
||||||
|
this.sprite
|
||||||
|
? doodler.drawSprite(
|
||||||
|
this.img,
|
||||||
|
this.sprite.at,
|
||||||
|
this.sprite.width,
|
||||||
|
this.sprite.height,
|
||||||
|
origin.copy().sub(this.imgWidth / 2, this.imgHeight / 2),
|
||||||
|
this.imgWidth,
|
||||||
|
this.imgHeight,
|
||||||
|
)
|
||||||
|
: doodler.drawImage(
|
||||||
|
this.img,
|
||||||
|
origin.copy().sub(this.imgWidth / 2, this.imgHeight / 2),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@@ -6,50 +6,85 @@ import { getContextItem } from "../lib/context.ts";
|
|||||||
export class Tender extends TrainCar {
|
export class Tender extends TrainCar {
|
||||||
constructor() {
|
constructor() {
|
||||||
const resources = getContextItem<ResourceManager>("resources");
|
const resources = getContextItem<ResourceManager>("resources");
|
||||||
super(25, resources.get<HTMLImageElement>("engine-sprites")!, 40, 20, {
|
super(
|
||||||
|
25,
|
||||||
|
10,
|
||||||
|
resources.get<HTMLImageElement>("snr:sprite/engine")!,
|
||||||
|
40,
|
||||||
|
20,
|
||||||
|
{
|
||||||
at: new Vector(80, 0),
|
at: new Vector(80, 0),
|
||||||
width: 40,
|
width: 40,
|
||||||
height: 20,
|
height: 20,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export class Tank extends TrainCar {
|
export class Tank extends TrainCar {
|
||||||
constructor() {
|
constructor() {
|
||||||
const resources = getContextItem<ResourceManager>("resources");
|
const resources = getContextItem<ResourceManager>("resources");
|
||||||
super(50, resources.get<HTMLImageElement>("engine-sprites")!, 70, 20, {
|
super(
|
||||||
|
50,
|
||||||
|
10,
|
||||||
|
resources.get<HTMLImageElement>("snr:sprite/engine")!,
|
||||||
|
70,
|
||||||
|
20,
|
||||||
|
{
|
||||||
at: new Vector(80, 20),
|
at: new Vector(80, 20),
|
||||||
width: 70,
|
width: 70,
|
||||||
height: 20,
|
height: 20,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export class YellowDumpCar extends TrainCar {
|
export class YellowDumpCar extends TrainCar {
|
||||||
constructor() {
|
constructor() {
|
||||||
const resources = getContextItem<ResourceManager>("resources");
|
const resources = getContextItem<ResourceManager>("resources");
|
||||||
super(50, resources.get<HTMLImageElement>("engine-sprites")!, 70, 20, {
|
super(
|
||||||
|
50,
|
||||||
|
10,
|
||||||
|
resources.get<HTMLImageElement>("snr:sprite/engine")!,
|
||||||
|
70,
|
||||||
|
20,
|
||||||
|
{
|
||||||
at: new Vector(80, 40),
|
at: new Vector(80, 40),
|
||||||
width: 70,
|
width: 70,
|
||||||
height: 20,
|
height: 20,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export class GrayDumpCar extends TrainCar {
|
export class GrayDumpCar extends TrainCar {
|
||||||
constructor() {
|
constructor() {
|
||||||
const resources = getContextItem<ResourceManager>("resources");
|
const resources = getContextItem<ResourceManager>("resources");
|
||||||
super(50, resources.get<HTMLImageElement>("engine-sprites")!, 70, 20, {
|
super(
|
||||||
|
50,
|
||||||
|
10,
|
||||||
|
resources.get<HTMLImageElement>("snr:sprite/engine")!,
|
||||||
|
70,
|
||||||
|
20,
|
||||||
|
{
|
||||||
at: new Vector(80, 60),
|
at: new Vector(80, 60),
|
||||||
width: 70,
|
width: 70,
|
||||||
height: 20,
|
height: 20,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export class NullCar extends TrainCar {
|
export class NullCar extends TrainCar {
|
||||||
constructor() {
|
constructor() {
|
||||||
const resources = getContextItem<ResourceManager>("resources");
|
const resources = getContextItem<ResourceManager>("resources");
|
||||||
super(50, resources.get<HTMLImageElement>("engine-sprites")!, 70, 20, {
|
super(
|
||||||
|
50,
|
||||||
|
10,
|
||||||
|
resources.get<HTMLImageElement>("snr:sprite/engine")!,
|
||||||
|
70,
|
||||||
|
20,
|
||||||
|
{
|
||||||
at: new Vector(80, 80),
|
at: new Vector(80, 80),
|
||||||
width: 70,
|
width: 70,
|
||||||
height: 20,
|
height: 20,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -6,50 +6,85 @@ import { ResourceManager } from "../lib/resources.ts";
|
|||||||
export class RedEngine extends TrainCar {
|
export class RedEngine extends TrainCar {
|
||||||
constructor() {
|
constructor() {
|
||||||
const resources = getContextItem<ResourceManager>("resources");
|
const resources = getContextItem<ResourceManager>("resources");
|
||||||
super(55, resources.get<HTMLImageElement>("engine-sprites")!, 80, 20, {
|
super(
|
||||||
|
55,
|
||||||
|
10,
|
||||||
|
resources.get<HTMLImageElement>("snr:sprite/engine")!,
|
||||||
|
80,
|
||||||
|
20,
|
||||||
|
{
|
||||||
at: new Vector(0, 60),
|
at: new Vector(0, 60),
|
||||||
width: 80,
|
width: 80,
|
||||||
height: 20,
|
height: 20,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export class PurpleEngine extends TrainCar {
|
export class PurpleEngine extends TrainCar {
|
||||||
constructor() {
|
constructor() {
|
||||||
const resources = getContextItem<ResourceManager>("resources");
|
const resources = getContextItem<ResourceManager>("resources");
|
||||||
super(55, resources.get<HTMLImageElement>("engine-sprites")!, 80, 20, {
|
super(
|
||||||
|
55,
|
||||||
|
10,
|
||||||
|
resources.get<HTMLImageElement>("snr:sprite/engine")!,
|
||||||
|
80,
|
||||||
|
20,
|
||||||
|
{
|
||||||
at: new Vector(0, 60),
|
at: new Vector(0, 60),
|
||||||
width: 80,
|
width: 80,
|
||||||
height: 20,
|
height: 20,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export class GreenEngine extends TrainCar {
|
export class GreenEngine extends TrainCar {
|
||||||
constructor() {
|
constructor() {
|
||||||
const resources = getContextItem<ResourceManager>("resources");
|
const resources = getContextItem<ResourceManager>("resources");
|
||||||
super(55, resources.get<HTMLImageElement>("engine-sprites")!, 80, 20, {
|
super(
|
||||||
|
55,
|
||||||
|
10,
|
||||||
|
resources.get<HTMLImageElement>("snr:sprite/engine")!,
|
||||||
|
80,
|
||||||
|
20,
|
||||||
|
{
|
||||||
at: new Vector(0, 40),
|
at: new Vector(0, 40),
|
||||||
width: 80,
|
width: 80,
|
||||||
height: 20,
|
height: 20,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export class GrayEngine extends TrainCar {
|
export class GrayEngine extends TrainCar {
|
||||||
constructor() {
|
constructor() {
|
||||||
const resources = getContextItem<ResourceManager>("resources");
|
const resources = getContextItem<ResourceManager>("resources");
|
||||||
super(55, resources.get<HTMLImageElement>("engine-sprites")!, 80, 20, {
|
super(
|
||||||
|
55,
|
||||||
|
10,
|
||||||
|
resources.get<HTMLImageElement>("snr:sprite/engine")!,
|
||||||
|
80,
|
||||||
|
20,
|
||||||
|
{
|
||||||
at: new Vector(0, 20),
|
at: new Vector(0, 20),
|
||||||
width: 80,
|
width: 80,
|
||||||
height: 20,
|
height: 20,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
export class BlueEngine extends TrainCar {
|
export class BlueEngine extends TrainCar {
|
||||||
constructor() {
|
constructor() {
|
||||||
const resources = getContextItem<ResourceManager>("resources");
|
const resources = getContextItem<ResourceManager>("resources");
|
||||||
super(55, resources.get<HTMLImageElement>("engine-sprites")!, 80, 20, {
|
super(
|
||||||
|
55,
|
||||||
|
10,
|
||||||
|
resources.get<HTMLImageElement>("snr:sprite/engine")!,
|
||||||
|
80,
|
||||||
|
20,
|
||||||
|
{
|
||||||
at: new Vector(0, 0),
|
at: new Vector(0, 0),
|
||||||
width: 80,
|
width: 80,
|
||||||
height: 20,
|
height: 20,
|
||||||
});
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,7 +2,7 @@ import { getContextItem } from "../lib/context.ts";
|
|||||||
import { Doodler, Vector } from "@bearmetal/doodler";
|
import { Doodler, Vector } from "@bearmetal/doodler";
|
||||||
import { Spline, TrackSegment, TrackSystem } from "../track/system.ts";
|
import { Spline, TrackSegment, TrackSystem } from "../track/system.ts";
|
||||||
import { Debuggable } from "../lib/debuggable.ts";
|
import { Debuggable } from "../lib/debuggable.ts";
|
||||||
import { map } from "../math/lerp.ts";
|
import { lerp, lerpAngle, map } from "../math/lerp.ts";
|
||||||
|
|
||||||
export class Train extends Debuggable {
|
export class Train extends Debuggable {
|
||||||
nodes: Vector[] = [];
|
nodes: Vector[] = [];
|
||||||
@@ -12,62 +12,68 @@ export class Train extends Debuggable {
|
|||||||
path: Spline<TrackSegment>;
|
path: Spline<TrackSegment>;
|
||||||
t: number;
|
t: number;
|
||||||
|
|
||||||
spacing = 20;
|
spacing = 5;
|
||||||
|
|
||||||
speed = 10;
|
speed = 0;
|
||||||
|
|
||||||
get segments() {
|
get segments() {
|
||||||
return Array.from(new Set(this.cars.flatMap((c) => c.segments)));
|
return Array.from(
|
||||||
|
new Set(this.cars.flatMap((c) => c.segments.values().toArray())),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(track: Spline<TrackSegment>, cars: TrainCar[]) {
|
constructor(track: Spline<TrackSegment>, cars: TrainCar[], t = 0) {
|
||||||
super("train", "path");
|
super("train", "path");
|
||||||
this.path = track;
|
this.path = track;
|
||||||
this.t = 0;
|
this.path.pointSpacing = 5;
|
||||||
this.cars = cars;
|
this.cars = cars;
|
||||||
|
this.t = this.cars.reduce((acc, c) => acc + c.length, 0) +
|
||||||
|
(this.cars.length - 1) * this.spacing;
|
||||||
|
this.t = this.t / this.path.pointSpacing;
|
||||||
|
|
||||||
let currentOffset = 0;
|
let currentOffset = 0;
|
||||||
try {
|
// try {
|
||||||
for (const car of this.cars) {
|
for (const car of this.cars) {
|
||||||
currentOffset += this.spacing;
|
car.train = this;
|
||||||
const a = this.path.followEvenPoints(this.t - currentOffset);
|
currentOffset += car.moveAlongPath(this.t - currentOffset, true) +
|
||||||
currentOffset += car.length;
|
this.spacing / this.path.pointSpacing;
|
||||||
const b = this.path.followEvenPoints(this.t - currentOffset);
|
|
||||||
car.points = [a.p, b.p];
|
|
||||||
this.nodes.push(a.p, b.p);
|
|
||||||
car.segments = [a.segmentId, b.segmentId];
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
currentOffset = 0;
|
|
||||||
for (const car of this.cars.toReversed()) {
|
|
||||||
currentOffset += this.spacing;
|
|
||||||
const a = this.path.followEvenPoints(this.t - currentOffset);
|
|
||||||
currentOffset += car.length;
|
|
||||||
const b = this.path.followEvenPoints(this.t - currentOffset);
|
|
||||||
car.points = [a.p, b.p];
|
|
||||||
this.nodes.push(a.p, b.p);
|
|
||||||
car.segments = [a.segmentId, b.segmentId];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// } catch {
|
||||||
|
// currentOffset = 0;
|
||||||
|
// console.log("Reversed");
|
||||||
|
// for (const car of this.cars.toReversed()) {
|
||||||
|
// for (const [i, bogie] of car.bogies.entries().toArray().reverse()) {
|
||||||
|
// currentOffset += bogie.length;
|
||||||
|
// const a = this.path.followEvenPoints(this.t - currentOffset);
|
||||||
|
// car.setBogiePosition(a.p, i);
|
||||||
|
// this.nodes.push(a.p);
|
||||||
|
// car.segments.add(a.segmentId);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
move(dTime: number) {
|
move(dTime: number) {
|
||||||
if (!this.speed) return;
|
if (!this.speed) return;
|
||||||
this.t = this.t + this.speed * dTime * 10;
|
this.t = this.t + (this.speed / this.path.pointSpacing) * dTime * 10;
|
||||||
// % this.path.evenPoints.length; // This should probably be on the track system
|
// % this.path.evenPoints.length; // This should probably be on the track system
|
||||||
let currentOffset = 0;
|
let currentOffset = 0;
|
||||||
for (const car of this.cars) {
|
for (const car of this.cars) {
|
||||||
// This needs to be moved to the car itself
|
// This needs to be moved to the car itself
|
||||||
if (!car.points) return;
|
// if (!car.points) return;
|
||||||
const [a, b] = car.points;
|
// const [a, b] = car.points;
|
||||||
const nA = this.path.followEvenPoints(this.t - currentOffset);
|
// const nA = this.path.followEvenPoints(this.t - currentOffset);
|
||||||
a.set(nA.p);
|
// a.set(nA.p);
|
||||||
currentOffset += car.length;
|
// currentOffset += car.length;
|
||||||
const nB = this.path.followEvenPoints(this.t - currentOffset);
|
// const nB = this.path.followEvenPoints(this.t - currentOffset);
|
||||||
b.set(nB.p);
|
// b.set(nB.p);
|
||||||
currentOffset += this.spacing;
|
// currentOffset += this.spacing;
|
||||||
car.segments = [nA.segmentId, nB.segmentId];
|
// car.segments = [nA.segmentId, nB.segmentId];
|
||||||
// car.draw();
|
// car.draw();
|
||||||
|
|
||||||
|
currentOffset += car.moveAlongPath(this.t - currentOffset) +
|
||||||
|
this.spacing / this.path.pointSpacing +
|
||||||
|
car.leading / this.path.pointSpacing;
|
||||||
}
|
}
|
||||||
// this.draw();
|
// this.draw();
|
||||||
}
|
}
|
||||||
@@ -118,7 +124,7 @@ export class Train extends Debuggable {
|
|||||||
colors.push(colors.shift()!);
|
colors.push(colors.shift()!);
|
||||||
colors.push(colors.shift()!);
|
colors.push(colors.shift()!);
|
||||||
colors.push(colors.shift()!);
|
colors.push(colors.shift()!);
|
||||||
for (const [i, segmentId] of this.segments.entries()) {
|
for (const [i, segmentId] of this.segments.entries().toArray()) {
|
||||||
const segment = track.getSegment(segmentId);
|
const segment = track.getSegment(segmentId);
|
||||||
segment &&
|
segment &&
|
||||||
doodler.drawBezier(...segment.points, {
|
doodler.drawBezier(...segment.points, {
|
||||||
@@ -134,6 +140,14 @@ export class Train extends Debuggable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface Bogie {
|
||||||
|
pos: Vector;
|
||||||
|
angle: number;
|
||||||
|
length: number;
|
||||||
|
sprite?: ISprite & { offset?: Vector };
|
||||||
|
rotate?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export class TrainCar extends Debuggable {
|
export class TrainCar extends Debuggable {
|
||||||
img: HTMLImageElement;
|
img: HTMLImageElement;
|
||||||
imgWidth: number;
|
imgWidth: number;
|
||||||
@@ -141,31 +155,83 @@ export class TrainCar extends Debuggable {
|
|||||||
sprite?: ISprite;
|
sprite?: ISprite;
|
||||||
|
|
||||||
points?: [Vector, Vector, ...Vector[]];
|
points?: [Vector, Vector, ...Vector[]];
|
||||||
length: number;
|
_length: number;
|
||||||
|
leading: number = 0;
|
||||||
|
|
||||||
segments: string[] = [];
|
bogies: Bogie[] = [];
|
||||||
|
|
||||||
|
segments: Set<string> = new Set();
|
||||||
|
|
||||||
|
train?: Train;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
length: number,
|
length: number,
|
||||||
|
trailing: number,
|
||||||
img: HTMLImageElement,
|
img: HTMLImageElement,
|
||||||
w: number,
|
w: number,
|
||||||
h: number,
|
h: number,
|
||||||
sprite?: ISprite,
|
sprite?: ISprite,
|
||||||
) {
|
) {
|
||||||
super(true, "car");
|
super(true, "car", "bogies", "angles");
|
||||||
this.img = img;
|
this.img = img;
|
||||||
this.sprite = sprite;
|
this.sprite = sprite;
|
||||||
this.imgWidth = w;
|
this.imgWidth = w;
|
||||||
this.imgHeight = h;
|
this.imgHeight = h;
|
||||||
this.length = length;
|
this._length = length;
|
||||||
|
|
||||||
|
this.bogies = [
|
||||||
|
{
|
||||||
|
pos: new Vector(0, 0),
|
||||||
|
angle: 0,
|
||||||
|
length: length,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
pos: new Vector(0, 0),
|
||||||
|
angle: 0,
|
||||||
|
length: trailing,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
get length() {
|
||||||
|
return this.bogies.reduce((acc, b) => acc + b.length, 0) + this.leading;
|
||||||
|
}
|
||||||
|
|
||||||
|
setBogiePosition(pos: Vector, idx: number) {
|
||||||
|
this.bogies[idx].pos.set(pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
update(dTime: number, t: number) {
|
||||||
|
if (this.train) {
|
||||||
|
for (const [i, bogie] of this.bogies.entries()) {
|
||||||
|
const a = this.train.path.followEvenPoints(t - this._length * i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
moveAlongPath(t: number, initial = false): number {
|
||||||
|
if (!this.train) return 0;
|
||||||
|
let offset = this.leading / this.train.path.pointSpacing;
|
||||||
|
this.segments.clear();
|
||||||
|
for (const [i, bogie] of this.bogies.entries()) {
|
||||||
|
const a = this.train.path.followEvenPoints(t - offset);
|
||||||
|
a.tangent.rotate(TWO_PI);
|
||||||
|
offset += bogie.length / this.train.path.pointSpacing;
|
||||||
|
this.setBogiePosition(a.p, i);
|
||||||
|
if (initial) bogie.angle = a.tangent.heading();
|
||||||
|
else {
|
||||||
|
bogie.angle = lerpAngle(a.tangent.heading(), bogie.angle, .1);
|
||||||
|
}
|
||||||
|
this.segments.add(a.segmentId);
|
||||||
|
}
|
||||||
|
return offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
draw() {
|
draw() {
|
||||||
if (!this.points) return;
|
|
||||||
const doodler = getContextItem<Doodler>("doodler");
|
const doodler = getContextItem<Doodler>("doodler");
|
||||||
const [a, b] = this.points;
|
const [a, b] = this.bogies;
|
||||||
const origin = Vector.add(Vector.sub(a, b).div(2), b);
|
const origin = Vector.add(Vector.sub(a.pos, b.pos).div(2), b.pos);
|
||||||
const angle = Vector.sub(b, a).heading();
|
const angle = Vector.sub(b.pos, a.pos).heading();
|
||||||
|
|
||||||
doodler.drawCircle(origin, 4, { color: "blue" });
|
doodler.drawCircle(origin, 4, { color: "blue" });
|
||||||
|
|
||||||
@@ -187,13 +253,74 @@ export class TrainCar extends Debuggable {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
override debugDraw(...args: unknown[]): void {
|
override debugDraw(...args: unknown[]): void {
|
||||||
if (!this.points) return;
|
|
||||||
const doodler = getContextItem<Doodler>("doodler");
|
const doodler = getContextItem<Doodler>("doodler");
|
||||||
doodler.drawLine(this.points, {
|
const debug = getContextItem<Debug>("debug");
|
||||||
color: "blue",
|
if (debug.bogies) {
|
||||||
weight: 3,
|
doodler.deferDrawing(() => {
|
||||||
|
for (const [i, b] of this.bogies.entries()) {
|
||||||
|
const next = this.bogies[i + 1];
|
||||||
|
if (!next) continue;
|
||||||
|
const dist = Vector.dist(b.pos, next.pos);
|
||||||
|
doodler.drawCircle(b.pos, 5, { color: "red" });
|
||||||
|
doodler.fillText(
|
||||||
|
dist.toFixed(1).toString(),
|
||||||
|
b.pos.copy().add(10, 10),
|
||||||
|
100,
|
||||||
|
{
|
||||||
|
color: "white",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
if (debug.car) {
|
||||||
|
doodler.deferDrawing(() => {
|
||||||
|
doodler.drawLine(this.bogies.map((b) => b.pos), {
|
||||||
|
color: "blue",
|
||||||
|
weight: 2,
|
||||||
|
});
|
||||||
|
doodler.deferDrawing(() => {
|
||||||
|
const colors = getContextItem<string[]>("colors");
|
||||||
|
for (const [i, b] of this.bogies.entries()) {
|
||||||
|
doodler.drawCircle(b.pos, 5, { color: colors[i % colors.length] });
|
||||||
|
doodler.fillText(
|
||||||
|
b.length.toString(),
|
||||||
|
b.pos.copy().add(10, 0),
|
||||||
|
100,
|
||||||
|
{
|
||||||
|
color: "white",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (debug.angles) {
|
||||||
|
doodler.deferDrawing(() => {
|
||||||
|
const ps: { pos: Vector; angle: number }[] = [];
|
||||||
|
for (const [i, b] of this.bogies.entries()) {
|
||||||
|
ps.push({ pos: b.pos, angle: b.angle });
|
||||||
|
const next = this.bogies[i + 1];
|
||||||
|
if (!next) continue;
|
||||||
|
const heading = Vector.sub(next.pos, b.pos);
|
||||||
|
const p = b.pos.copy().add(heading.mult(.5));
|
||||||
|
ps.push({ pos: p, angle: heading.heading() });
|
||||||
|
}
|
||||||
|
for (const p of ps) {
|
||||||
|
doodler.dot(p.pos, { color: "green" });
|
||||||
|
doodler.fillText(
|
||||||
|
p.angle.toFixed(2).toString(),
|
||||||
|
p.pos.copy().add(0, 20),
|
||||||
|
100,
|
||||||
|
{
|
||||||
|
color: "white",
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ISprite {
|
interface ISprite {
|
||||||
|
@@ -21,5 +21,7 @@ declare global {
|
|||||||
train: boolean;
|
train: boolean;
|
||||||
car: boolean;
|
car: boolean;
|
||||||
path: boolean;
|
path: boolean;
|
||||||
|
bogies: boolean;
|
||||||
|
angles: boolean;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
1
temp.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
[{"p":[[633.178123792903,100.31612523465073,0],[699.1781237929027,100.31612523465073,0],[762.9292283279814,117.39818221141712,0],[820.0869049777544,150.39818221141715,0]],"id":"83b9fc8c-778e-4e5e-a3db-1199863a2e13","bNeighbors":["a149432a-e04e-481a-ada2-c05a30ffb4c0"],"fNeighbors":["dc3ec17d-f28e-40fe-ba68-8a4ae6b2c117"]},{"p":[[820.0869049777544,150.39818221141715,0],[877.2445816275272,183.398182211417,0],[923.9136291858395,230.06722976972924,0],[956.9136291858396,287.2249064195022,0]],"id":"dc3ec17d-f28e-40fe-ba68-8a4ae6b2c117","bNeighbors":["83b9fc8c-778e-4e5e-a3db-1199863a2e13"],"fNeighbors":["1b6409da-5c9d-4b1d-b460-fcc5e40eaee4"]},{"p":[[956.9136291858396,287.2249064195022,0],[989.9136291858396,344.3825830692749,0],[1006.9956861626059,408.13368760435344,0],[1006.9956861626064,474.1336876043541,0]],"id":"1b6409da-5c9d-4b1d-b460-fcc5e40eaee4","bNeighbors":["dc3ec17d-f28e-40fe-ba68-8a4ae6b2c117"],"fNeighbors":["24861245-ca99-4f97-9e0a-5828773fcf7a"]},{"p":[[1006.9956861626064,474.1336876043541,0],[1006.9956861626068,540.1336876043545,0],[989.9136291858408,603.884792139433,0],[956.9136291858412,661.042468789207,0]],"id":"24861245-ca99-4f97-9e0a-5828773fcf7a","bNeighbors":["1b6409da-5c9d-4b1d-b460-fcc5e40eaee4"],"fNeighbors":["98ad61a1-f2e7-4c53-9caa-022faa15ce68"]},{"p":[[956.9136291858412,661.042468789207,0],[923.9136291858417,718.2001454389806,0],[877.2445816275301,764.8691929972932,0],[820.0869049777573,797.8691929972947,0]],"id":"98ad61a1-f2e7-4c53-9caa-022faa15ce68","bNeighbors":["24861245-ca99-4f97-9e0a-5828773fcf7a"],"fNeighbors":["7f11837f-f043-468f-b1cb-254236501199"]},{"p":[[820.0869049777573,797.8691929972947,0],[762.9292283279847,830.8691929972961,0],[699.1781237929068,847.9512499740634,0],[633.1781237929063,847.9512499740653,0]],"id":"7f11837f-f043-468f-b1cb-254236501199","bNeighbors":["98ad61a1-f2e7-4c53-9caa-022faa15ce68"],"fNeighbors":["d89abe0b-7d84-4921-b690-23cdb6ec7c4a"]},{"p":[[633.1781237929063,847.9512499740653,0],[567.1781237929059,847.9512499740671,0],[503.4270192578273,830.8691929973024,0],[446.26934260805274,797.869192997304,0]],"id":"d89abe0b-7d84-4921-b690-23cdb6ec7c4a","bNeighbors":["7f11837f-f043-468f-b1cb-254236501199"],"fNeighbors":["3fb143a5-3d6e-409c-88b1-c20e9005a14f"]},{"p":[[446.26934260805274,797.869192997304,0],[389.1116659582787,764.869192997306,0],[342.44261839996534,718.2001454389954,0],[309.4426183999627,661.0424687892232,0]],"id":"3fb143a5-3d6e-409c-88b1-c20e9005a14f","bNeighbors":["d89abe0b-7d84-4921-b690-23cdb6ec7c4a"],"fNeighbors":["de299c76-bdbd-4b59-9514-722e3292ed49"]},{"p":[[309.4426183999627,661.0424687892232,0],[276.4426183999604,603.8847921394515,0],[259.36056142319165,540.1336876043738,0],[259.36056142318864,474.13368760437345,0]],"id":"de299c76-bdbd-4b59-9514-722e3292ed49","bNeighbors":["3fb143a5-3d6e-409c-88b1-c20e9005a14f"],"fNeighbors":["e573e278-4039-425f-89f5-245f96fe9096"]},{"p":[[259.36056142318864,474.13368760437345,0],[259.36056142318563,408.13368760437334,0],[276.4426183999492,344.38258306929436,0],[309.44261839994635,287.22490641951936,0]],"id":"e573e278-4039-425f-89f5-245f96fe9096","bNeighbors":["de299c76-bdbd-4b59-9514-722e3292ed49"],"fNeighbors":["87a06884-75e3-4802-ba38-3be366bac467"]},{"p":[[309.44261839994635,287.22490641951936,0],[342.4426183999434,230.06722976974456,0],[389.111665958253,183.39818221143025,0],[446.26934260802403,150.39818221142656,0]],"id":"87a06884-75e3-4802-ba38-3be366bac467","bNeighbors":["e573e278-4039-425f-89f5-245f96fe9096"],"fNeighbors":["a149432a-e04e-481a-ada2-c05a30ffb4c0"]},{"p":[[446.26934260802403,150.39818221142656,0],[503.42701925779517,117.3981822114228,0],[567.1781237928724,100.31612523465262,0],[633.1781237928726,100.31612523464773,0]],"id":"a149432a-e04e-481a-ada2-c05a30ffb4c0","bNeighbors":["87a06884-75e3-4802-ba38-3be366bac467"],"fNeighbors":["83b9fc8c-778e-4e5e-a3db-1199863a2e13"]}]
|