Compare commits

...

2 Commits

Author SHA1 Message Date
7b6dbb295f some minor tweaks 2025-02-23 14:36:34 -07:00
3aea38f9f4 tight curve, nearest segment 2025-02-23 13:25:40 -07:00
10 changed files with 113 additions and 13 deletions

Binary file not shown.

View File

@@ -41,7 +41,7 @@ export class ResourceManager {
) {
const identifier = parseNamespacedId(name);
if (typeof (value as EventSource).addEventListener === "function") {
if (value instanceof Image) {
if (value instanceof Image || value instanceof Audio) {
// During development, we can use the local file system
value.src =
`/blobs/${identifier.namespace}/${identifier.type}/${identifier.name}${
@@ -55,8 +55,10 @@ export class ResourceManager {
const onload = () => {
this.resources.set(name, value);
resolve(true);
(value as EventSource).removeEventListener("loadeddata", onload);
(value as EventSource).removeEventListener("load", onload);
};
(value as EventSource).addEventListener("loadeddata", onload);
(value as EventSource).addEventListener("load", onload);
}),
);

View File

@@ -48,6 +48,7 @@ const _fullDebug: Debug = {
bogies: false,
angles: false,
aabb: false,
segment: false,
};
const storedDebug = JSON.parse(localStorage.getItem("debug") || "0");

7
src/math/angle.ts Normal file
View File

@@ -0,0 +1,7 @@
export function angleToRadians(angle: number) {
return angle / 180 * Math.PI;
}
export function angleToDegrees(angle: number) {
return angle * 180 / Math.PI;
}

View File

@@ -14,6 +14,8 @@ import {
SBendLeft,
SBendRight,
StraightTrack,
TightBankLeft,
TightBankRight,
WideBankLeft,
WideBankRight,
} from "../../track/shapes.ts";
@@ -285,6 +287,8 @@ export class EditTrackState extends State<States> {
new BankRight(),
new WideBankLeft(),
new WideBankRight(),
new TightBankLeft(),
new TightBankRight(),
]);
const inputManager = getContextItem<InputManager>("inputManager");

View File

@@ -39,9 +39,11 @@ export class LoadState extends State<States> {
resources.set("snr:sprite/LargeLady", new Image());
// resources.get<HTMLImageElement>("snr:sprite/engine")!.src =
// "/sprites/EngineSprites.png";
resources.set("snr:audio/ding", new Audio());
resources.ready().then(() => {
this.stateMachine.transitionTo(States.RUNNING);
});
}).catch((e) => console.error(e));
const doodler = getContextItem<Doodler>("doodler");
// this.layers.push(doodler.createLayer((_, __, dTime) => {

View File

@@ -85,7 +85,10 @@ export class RunningState extends State<States> {
// this.activeTr0ain = train;
// const trainCount = 1000;
// for (let i = 0; i < trainCount; i++) {
// const train = new Train(track.path, [new LargeLady(), new Tender()]);
// const train = new Train(track.path, [
// new LargeLady(),
// new LargeLadyTender(),
// ]);
// ctx.trains.push(train);
// }

View File

@@ -88,6 +88,58 @@ export class WideBankLeft extends TrackSegment {
]);
}
}
export class TightBankLeft extends TrackSegment {
constructor(start?: Vector) {
start = start || new Vector(100, 100);
const p1 = start.copy();
const p2 = start.copy();
const p3 = start.copy();
const p4 = start.copy();
const scale = 61.57;
p2.add(new Vector(1, 0).mult(scale));
p3.set(p2);
const dirToP3 = Vector.fromAngle(-Math.PI / 12).mult(scale);
p3.add(dirToP3);
p4.set(p3);
const dirToP4 = Vector.fromAngle(-Math.PI / 6).mult(scale);
p4.add(dirToP4);
super([
p1,
p2,
p3,
p4,
]);
}
}
export class TightBankRight extends TrackSegment {
constructor(start?: Vector) {
start = start || new Vector(100, 100);
const p1 = start.copy();
const p2 = start.copy();
const p3 = start.copy();
const p4 = start.copy();
const scale = 61.57;
p2.add(new Vector(1, 0).mult(scale));
p3.set(p2);
const dirToP3 = Vector.fromAngle(Math.PI / 12).mult(scale);
p3.add(dirToP3);
p4.set(p3);
const dirToP4 = Vector.fromAngle(Math.PI / 6).mult(scale);
p4.add(dirToP4);
super([
p1,
p2,
p3,
p4,
]);
}
}
export class WideBankRight extends TrackSegment {
constructor(start?: Vector) {
start = start || new Vector(100, 100);

View File

@@ -2,12 +2,14 @@ import { Doodler, Point, Vector } from "@bearmetal/doodler";
import { ComplexPath, PathSegment } from "../math/path.ts";
import { getContextItem, setDefaultContext } from "../lib/context.ts";
import { clamp } from "../math/clamp.ts";
import { Debuggable } from "../lib/debuggable.ts";
export class TrackSystem {
export class TrackSystem extends Debuggable {
private _segments: Map<string, TrackSegment> = new Map();
private doodler: Doodler;
constructor(segments: TrackSegment[]) {
super("track");
this.doodler = getContextItem<Doodler>("doodler");
for (const segment of segments) {
this._segments.set(segment.id, segment);
@@ -26,6 +28,19 @@ export class TrackSystem {
return this._segments.values().toArray().pop();
}
getNearestSegment(pos: Vector) {
let minDistance = Infinity;
let nearestSegment: TrackSegment | undefined;
for (const segment of this._segments.values()) {
const distance = segment.getDistanceTo(pos);
if (distance < minDistance) {
minDistance = distance;
nearestSegment = segment;
}
}
return nearestSegment;
}
optimize(percent: number) {
console.log("Optimizing track", percent * 100 / 4);
for (const segment of this._segments.values()) {
@@ -85,6 +100,15 @@ export class TrackSystem {
// }
}
override debugDraw(): void {
const debug = getContextItem("debug");
if (debug.track) {
for (const segment of this._segments.values()) {
segment.drawAABB();
}
}
}
ends: Map<TrackSegment, [End, End]> = new Map();
endArray: End[] = [];
@@ -293,6 +317,10 @@ export class TrackSegment extends PathSegment {
this.update();
}
getDistanceTo(pos: Vector) {
return Vector.dist(this.aabb.center, pos);
}
updateAABB() {
let minX = Infinity;
let maxX = -Infinity;
@@ -435,15 +463,15 @@ export class TrackSegment extends PathSegment {
// color: "red",
// weight: 3,
// });
const debug = getContextItem("debug");
if (debug.track) {
this.doodler.drawRect(this.aabb.pos, this.aabb.width, this.aabb.height, {
color: "lime",
});
this.doodler.drawCircle(this.aabb.center, 2, {
color: "cyan",
});
}
}
drawAABB() {
this.doodler.drawRect(this.aabb.pos, this.aabb.width, this.aabb.height, {
color: "lime",
});
this.doodler.drawCircle(this.aabb.center, 2, {
color: "cyan",
});
}
serialize(): SerializedTrackSegment {

View File

@@ -18,6 +18,7 @@ declare global {
type Debug = {
track: boolean;
segment: boolean;
train: boolean;
car: boolean;
path: boolean;