loading saved track layouts

This commit is contained in:
Emma
2023-02-08 11:53:11 -07:00
parent 9b03e6c2cb
commit 657f228d47
4 changed files with 161 additions and 56 deletions

View File

@@ -90,6 +90,16 @@ export class Track extends PathSegment {
e.drawDot();
}
}
setNext(t: Track) {
this.next = t;
this.next.points[0] = this.points[3];
}
setPrev(t: Track) {
this.prev = t;
this.prev.points[3] = this.points[0];
}
}
export class Spline<T extends PathSegment = PathSegment> {
@@ -99,7 +109,7 @@ export class Spline<T extends PathSegment = PathSegment> {
evenPoints: Vector[];
constructor(segs: T[]) {
this.segments = segs;
this.evenPoints = this.calculateEvenlySpacedPoints(3);
this.evenPoints = this.calculateEvenlySpacedPoints(1);
}
setContext(ctx: CanvasRenderingContext2D) {
@@ -116,39 +126,40 @@ export class Spline<T extends PathSegment = PathSegment> {
}
calculateEvenlySpacedPoints(spacing: number, resolution = 1) {
return this.segments.flatMap(s => s.calculateEvenlySpacedPoints(spacing, resolution));
// const points: Vector[] = []
// return this.segments.flatMap(s => s.calculateEvenlySpacedPoints(spacing, resolution));
const points: Vector[] = []
// points.push(this.segments[0].points[0]);
// let prev = points[0];
// let distSinceLastEvenPoint = 0
// for (const seg of this.segments) {
points.push(this.segments[0].points[0]);
let prev = points[0];
let distSinceLastEvenPoint = 0
for (const seg of this.segments) {
// let t = 0;
let t = 0;
// const div = Math.ceil(seg.length * resolution * 10);
// while (t < 1) {
// t += 1 / div;
// const point = seg.getPointAtT(t);
// distSinceLastEvenPoint += prev.dist(point);
const div = Math.ceil(seg.length * resolution * 10);
while (t < 1) {
t += 1 / div;
const point = seg.getPointAtT(t);
distSinceLastEvenPoint += prev.dist(point);
// if (distSinceLastEvenPoint >= spacing) {
// const overshoot = distSinceLastEvenPoint - spacing;
// const evenPoint = Vector.add(point, Vector.sub(point, prev).normalize().mult(overshoot))
// distSinceLastEvenPoint = overshoot;
// points.push(evenPoint);
// prev = evenPoint;
// }
if (distSinceLastEvenPoint >= spacing) {
const overshoot = distSinceLastEvenPoint - spacing;
const evenPoint = Vector.add(point, Vector.sub(point, prev).normalize().mult(overshoot))
distSinceLastEvenPoint = overshoot;
points.push(evenPoint);
prev = evenPoint;
}
// prev = point
// }
// }
prev = point
}
}
// return points;
return points;
}
followEvenPoints(t: number) {
if (t < 0) t+= this.evenPoints.length
const i = Math.floor(t);
const a = this.evenPoints[i]
const b = this.evenPoints[(i + 1) % this.evenPoints.length]
@@ -192,3 +203,20 @@ export const generateSquareTrack = () => {
return new Spline<Track>([first, second, third, fourth, fifth, sixth, seventh, eighth]);
}
export const loadFromJson = () => {
const json = JSON.parse(localStorage.getItem('railPath') || '');
if (!json) return generateSquareTrack();
const segments: Track[] = [];
for (const {points} of json.segments) {
segments.push(new Track(points.map((p:{x:number,y:number}) => new Vector(p.x, p.y))));
}
for (const [i,s] of segments.entries()) {
s.setNext(segments[(i+1)%segments.length])
s.setPrev(segments.at(i-1)!)
}
return new Spline<Track>(segments);
}