From 2176f6741391b8464e03c6d62abcd50208f20f28 Mon Sep 17 00:00:00 2001 From: Emma Date: Sat, 22 Feb 2025 16:31:53 -0700 Subject: [PATCH] Edit mode track updates --- src/lib/context.ts | 2 +- src/state/states/EditTrackState.ts | 5 +++-- src/track/system.ts | 30 ++++++++++++++++++------------ 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/src/lib/context.ts b/src/lib/context.ts index f20a57e..019a6f6 100644 --- a/src/lib/context.ts +++ b/src/lib/context.ts @@ -58,7 +58,7 @@ export function getContext() { return ctx; } export function getContextItem( - prop: string, + prop: K, ): ContextMap[K]; export function getContextItem(prop: string): T; export function getContextItem(prop: string): T { diff --git a/src/state/states/EditTrackState.ts b/src/state/states/EditTrackState.ts index b6f394a..b347076 100644 --- a/src/state/states/EditTrackState.ts +++ b/src/state/states/EditTrackState.ts @@ -51,7 +51,7 @@ export class EditTrackState extends State { p.set(mousePos); p.add(relativePoint); }); - segment.recalculateTiePoints(); + segment.update(); const ends = track.findEnds(); setContextItem("showEnds", true); @@ -130,7 +130,7 @@ export class EditTrackState extends State { break; } } - this.ghostSegment.recalculateTiePoints(); + this.ghostSegment.update(); // } else if (closestEnd) { // this.closestEnd = closestEnd; @@ -245,6 +245,7 @@ export class EditTrackState extends State { if (translation.x !== 0 || translation.y !== 0) { track.translate(translation); + track.recalculateAll(); } // TODO diff --git a/src/track/system.ts b/src/track/system.ts index d3eda77..7f00f62 100644 --- a/src/track/system.ts +++ b/src/track/system.ts @@ -35,7 +35,7 @@ export class TrackSystem { recalculateAll() { for (const segment of this._segments.values()) { - segment.recalculateRailPoints(); + segment.update(); segment.length = segment.calculateApproxLength(); } } @@ -290,9 +290,7 @@ export class TrackSegment extends PathSegment { super(p); this.doodler = getContextItem("doodler"); this.id = id ?? crypto.randomUUID(); - this.recalculateRailPoints(); - this.recalculateTiePoints(); - this.updateAABB(); + this.update(); } updateAABB() { @@ -301,7 +299,7 @@ export class TrackSegment extends PathSegment { let minY = Infinity; let maxY = -Infinity; - this.points.forEach((p) => { + [...this.normalPoints, ...this.antiNormalPoints].forEach((p) => { minX = Math.min(minX, p.x); maxX = Math.max(maxX, p.x); minY = Math.min(minY, p.y); @@ -349,6 +347,12 @@ export class TrackSegment extends PathSegment { this.evenPoints = this.calculateEvenlySpacedPoints(this.length / spacing); } + update() { + this.recalculateRailPoints(); + this.recalculateTiePoints(); + this.updateAABB(); + } + setTrack(t: TrackSystem) { this.track = t; } @@ -431,13 +435,15 @@ export class TrackSegment extends PathSegment { // color: "red", // weight: 3, // }); - - this.doodler.drawRect(this.aabb.pos, this.aabb.width, this.aabb.height, { - color: "lime", - }); - this.doodler.drawCircle(this.aabb.center, 2, { - color: "cyan", - }); + 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", + }); + } } serialize(): SerializedTrackSegment {