Edit mode track updates

This commit is contained in:
Emmaline Autumn 2025-02-22 16:31:53 -07:00
parent 10d462edaf
commit 2176f67413
3 changed files with 22 additions and 15 deletions

View File

@ -58,7 +58,7 @@ export function getContext() {
return ctx; return ctx;
} }
export function getContextItem<K extends keyof ContextMap>( export function getContextItem<K extends keyof ContextMap>(
prop: string, prop: K,
): ContextMap[K]; ): ContextMap[K];
export function getContextItem<T>(prop: string): T; export function getContextItem<T>(prop: string): T;
export function getContextItem<T>(prop: string): T { export function getContextItem<T>(prop: string): T {

View File

@ -51,7 +51,7 @@ export class EditTrackState extends State<States> {
p.set(mousePos); p.set(mousePos);
p.add(relativePoint); p.add(relativePoint);
}); });
segment.recalculateTiePoints(); segment.update();
const ends = track.findEnds(); const ends = track.findEnds();
setContextItem("showEnds", true); setContextItem("showEnds", true);
@ -130,7 +130,7 @@ export class EditTrackState extends State<States> {
break; break;
} }
} }
this.ghostSegment.recalculateTiePoints(); this.ghostSegment.update();
// } else if (closestEnd) { // } else if (closestEnd) {
// this.closestEnd = closestEnd; // this.closestEnd = closestEnd;
@ -245,6 +245,7 @@ export class EditTrackState extends State<States> {
if (translation.x !== 0 || translation.y !== 0) { if (translation.x !== 0 || translation.y !== 0) {
track.translate(translation); track.translate(translation);
track.recalculateAll();
} }
// TODO // TODO

View File

@ -35,7 +35,7 @@ export class TrackSystem {
recalculateAll() { recalculateAll() {
for (const segment of this._segments.values()) { for (const segment of this._segments.values()) {
segment.recalculateRailPoints(); segment.update();
segment.length = segment.calculateApproxLength(); segment.length = segment.calculateApproxLength();
} }
} }
@ -290,9 +290,7 @@ export class TrackSegment extends PathSegment {
super(p); super(p);
this.doodler = getContextItem<Doodler>("doodler"); this.doodler = getContextItem<Doodler>("doodler");
this.id = id ?? crypto.randomUUID(); this.id = id ?? crypto.randomUUID();
this.recalculateRailPoints(); this.update();
this.recalculateTiePoints();
this.updateAABB();
} }
updateAABB() { updateAABB() {
@ -301,7 +299,7 @@ export class TrackSegment extends PathSegment {
let minY = Infinity; let minY = Infinity;
let maxY = -Infinity; let maxY = -Infinity;
this.points.forEach((p) => { [...this.normalPoints, ...this.antiNormalPoints].forEach((p) => {
minX = Math.min(minX, p.x); minX = Math.min(minX, p.x);
maxX = Math.max(maxX, p.x); maxX = Math.max(maxX, p.x);
minY = Math.min(minY, p.y); minY = Math.min(minY, p.y);
@ -349,6 +347,12 @@ export class TrackSegment extends PathSegment {
this.evenPoints = this.calculateEvenlySpacedPoints(this.length / spacing); this.evenPoints = this.calculateEvenlySpacedPoints(this.length / spacing);
} }
update() {
this.recalculateRailPoints();
this.recalculateTiePoints();
this.updateAABB();
}
setTrack(t: TrackSystem) { setTrack(t: TrackSystem) {
this.track = t; this.track = t;
} }
@ -431,13 +435,15 @@ export class TrackSegment extends PathSegment {
// color: "red", // color: "red",
// weight: 3, // weight: 3,
// }); // });
const debug = getContextItem("debug");
this.doodler.drawRect(this.aabb.pos, this.aabb.width, this.aabb.height, { if (debug.track) {
color: "lime", this.doodler.drawRect(this.aabb.pos, this.aabb.width, this.aabb.height, {
}); color: "lime",
this.doodler.drawCircle(this.aabb.center, 2, { });
color: "cyan", this.doodler.drawCircle(this.aabb.center, 2, {
}); color: "cyan",
});
}
} }
serialize(): SerializedTrackSegment { serialize(): SerializedTrackSegment {