Compare commits

..

No commits in common. "2176f6741391b8464e03c6d62abcd50208f20f28" and "03e0b1afcbe419d584f1403ec392d8dc53619388" have entirely different histories.

5 changed files with 30 additions and 86 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: K, prop: string,
): 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

@ -42,6 +42,18 @@ export class EditTrackState extends State<States> {
const track = getContextItem<TrackSystem>("track"); const track = getContextItem<TrackSystem>("track");
const doodler = getContextItem<Doodler>("doodler"); const doodler = getContextItem<Doodler>("doodler");
// For moving a segment, i.e. the currently active one
// const segment = track.lastSegment;
// if (segment) {
// const firstPoint = segment.points[0].copy();
// const { x, y } = inputManager.getMouseLocation();
// segment.points.forEach((p, i) => {
// const relativePoint = Vector.sub(p, firstPoint);
// p.set(x, y);
// p.add(relativePoint);
// });
// }
if (this.selectedSegment) { if (this.selectedSegment) {
const segment = this.selectedSegment; const segment = this.selectedSegment;
const firstPoint = segment.points[0].copy(); const firstPoint = segment.points[0].copy();
@ -51,7 +63,7 @@ export class EditTrackState extends State<States> {
p.set(mousePos); p.set(mousePos);
p.add(relativePoint); p.add(relativePoint);
}); });
segment.update(); segment.recalculateTiePoints();
const ends = track.findEnds(); const ends = track.findEnds();
setContextItem("showEnds", true); setContextItem("showEnds", true);
@ -130,7 +142,7 @@ export class EditTrackState extends State<States> {
break; break;
} }
} }
this.ghostSegment.update(); this.ghostSegment.recalculateTiePoints();
// } else if (closestEnd) { // } else if (closestEnd) {
// this.closestEnd = closestEnd; // this.closestEnd = closestEnd;
@ -245,7 +257,6 @@ 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.update(); segment.recalculateRailPoints();
segment.length = segment.calculateApproxLength(); segment.length = segment.calculateApproxLength();
} }
} }
@ -282,52 +282,12 @@ export class TrackSegment extends PathSegment {
antiNormalPoints: Vector[] = []; antiNormalPoints: Vector[] = [];
evenPoints: [Vector, number][] = []; evenPoints: [Vector, number][] = [];
aabb!: AABB;
private trackGuage = 12;
constructor(p: VectorSet, id?: string) { constructor(p: VectorSet, id?: string) {
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.update(); this.recalculateRailPoints();
} this.recalculateTiePoints();
updateAABB() {
let minX = Infinity;
let maxX = -Infinity;
let minY = Infinity;
let maxY = -Infinity;
[...this.normalPoints, ...this.antiNormalPoints].forEach((p) => {
minX = Math.min(minX, p.x);
maxX = Math.max(maxX, p.x);
minY = Math.min(minY, p.y);
maxY = Math.max(maxY, p.y);
});
const width = maxX - minX;
const height = maxY - minY;
if (width < this.trackGuage) {
const extra = (this.trackGuage - width) / 2;
minX -= extra;
maxX += extra;
}
if (height < this.trackGuage) {
const extra = (this.trackGuage - height) / 2;
minY -= extra;
maxY += extra;
}
this.aabb = {
pos: new Vector(minX, minY),
x: minX,
y: minY,
width: maxX - minX,
height: maxY - minY,
center: new Vector(minX, minY).add(
new Vector(maxX - minX, maxY - minY).div(2),
),
};
} }
recalculateRailPoints(resolution = 60) { recalculateRailPoints(resolution = 60) {
@ -336,7 +296,7 @@ export class TrackSegment extends PathSegment {
for (let i = 0; i <= resolution; i++) { for (let i = 0; i <= resolution; i++) {
const t = i / resolution; const t = i / resolution;
const normal = this.tangent(t).rotate(Math.PI / 2); const normal = this.tangent(t).rotate(Math.PI / 2);
normal.setMag(this.trackGuage / 2); normal.setMag(6);
const p = this.getPointAtT(t); const p = this.getPointAtT(t);
this.normalPoints.push(p.copy().add(normal)); this.normalPoints.push(p.copy().add(normal));
this.antiNormalPoints.push(p.copy().add(normal.rotate(Math.PI))); this.antiNormalPoints.push(p.copy().add(normal.rotate(Math.PI)));
@ -347,12 +307,6 @@ 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;
} }
@ -435,15 +389,6 @@ export class TrackSegment extends PathSegment {
// color: "red", // color: "red",
// weight: 3, // 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",
});
}
} }
serialize(): SerializedTrackSegment { serialize(): SerializedTrackSegment {

View File

@ -4,6 +4,15 @@ import { Spline, TrackSegment, TrackSystem } from "../track/system.ts";
import { Debuggable } from "../lib/debuggable.ts"; import { Debuggable } from "../lib/debuggable.ts";
import { lerp, lerpAngle, map } from "../math/lerp.ts"; import { lerp, lerpAngle, map } from "../math/lerp.ts";
type TrainAABB = {
pos: Vector;
x: number;
y: number;
width: number;
height: number;
center: Vector;
};
export class Train extends Debuggable { export class Train extends Debuggable {
nodes: Vector[] = []; nodes: Vector[] = [];
@ -16,7 +25,7 @@ export class Train extends Debuggable {
speed = 5; speed = 5;
aabb!: AABB; aabb!: TrainAABB;
get segments() { get segments() {
return Array.from( return Array.from(
@ -195,7 +204,7 @@ export class TrainCar extends Debuggable {
train?: Train; train?: Train;
aabb!: AABB; aabb!: TrainAABB;
constructor( constructor(
length: number, length: number,

View File

@ -25,25 +25,4 @@ declare global {
angles: boolean; angles: boolean;
aabb: boolean; aabb: boolean;
}; };
type AABB = {
pos: Vector;
x: number;
y: number;
width: number;
height: number;
center: Vector;
};
}
export function applyMixins(derivedCtor: any, baseCtors: any[]) {
baseCtors.forEach((baseCtor) => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
Object.defineProperty(
derivedCtor.prototype,
name,
Object.getOwnPropertyDescriptor(baseCtor.prototype, name) ?? {},
);
});
});
} }