diff --git a/src/main.ts b/src/main.ts index a3c92b8..bb7c619 100644 --- a/src/main.ts +++ b/src/main.ts @@ -48,6 +48,7 @@ const _fullDebug: Debug = { bogies: false, angles: false, aabb: false, + segment: false, }; const storedDebug = JSON.parse(localStorage.getItem("debug") || "0"); diff --git a/src/state/states/EditTrackState.ts b/src/state/states/EditTrackState.ts index b347076..e828882 100644 --- a/src/state/states/EditTrackState.ts +++ b/src/state/states/EditTrackState.ts @@ -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 { new BankRight(), new WideBankLeft(), new WideBankRight(), + new TightBankLeft(), + new TightBankRight(), ]); const inputManager = getContextItem("inputManager"); diff --git a/src/state/states/RunningState.ts b/src/state/states/RunningState.ts index 341256a..356532f 100644 --- a/src/state/states/RunningState.ts +++ b/src/state/states/RunningState.ts @@ -85,7 +85,10 @@ export class RunningState extends State { // 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); // } diff --git a/src/track/shapes.ts b/src/track/shapes.ts index ec64135..45e5d5a 100644 --- a/src/track/shapes.ts +++ b/src/track/shapes.ts @@ -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); diff --git a/src/track/system.ts b/src/track/system.ts index 7f00f62..51be6d0 100644 --- a/src/track/system.ts +++ b/src/track/system.ts @@ -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 = new Map(); private doodler: Doodler; constructor(segments: TrackSegment[]) { + super("track"); this.doodler = getContextItem("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 = 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 { diff --git a/src/types.ts b/src/types.ts index b2a3f34..f093362 100644 --- a/src/types.ts +++ b/src/types.ts @@ -18,6 +18,7 @@ declare global { type Debug = { track: boolean; + segment: boolean; train: boolean; car: boolean; path: boolean;