Better track shapes, rotation in track editing

This commit is contained in:
2025-02-19 13:16:09 -07:00
parent 7b244526b9
commit 03e0b1afcb
11 changed files with 154 additions and 34 deletions

View File

@@ -14,6 +14,8 @@ import {
SBendLeft,
SBendRight,
StraightTrack,
WideBankLeft,
WideBankRight,
} from "../../track/shapes.ts";
import { TrackSegment } from "../../track/system.ts";
import { clamp } from "../../math/clamp.ts";
@@ -61,6 +63,7 @@ export class EditTrackState extends State<States> {
p.set(mousePos);
p.add(relativePoint);
});
segment.recalculateTiePoints();
const ends = track.findEnds();
setContextItem("showEnds", true);
@@ -109,18 +112,21 @@ export class EditTrackState extends State<States> {
this.ghostRotated = false;
}
switch (this.closestEnd.frontOrBack) {
case "front":
case "front": {
this.ghostSegment.setPositionByPoint(
this.closestEnd.pos,
this.ghostSegment.points[0],
);
// this.ghostSegment.points[0] = this.closestEnd.pos;
const ghostEndTangent = this.ghostSegment.tangent(0);
!this.ghostRotated && this.ghostSegment.rotateAboutPoint(
this.closestEnd.tangent.heading(),
this.closestEnd.tangent.heading() - ghostEndTangent.heading(),
this.ghostSegment.points[0],
);
this.ghostRotated = true;
break;
}
case "back": {
this.ghostSegment.setPositionByPoint(
this.closestEnd.pos,
@@ -136,6 +142,8 @@ export class EditTrackState extends State<States> {
break;
}
}
this.ghostSegment.recalculateTiePoints();
// } else if (closestEnd) {
// this.closestEnd = closestEnd;
} else if (!this.closestEnd || !closestEnd) {
@@ -286,6 +294,8 @@ export class EditTrackState extends State<States> {
new SBendRight(),
new BankLeft(),
new BankRight(),
new WideBankLeft(),
new WideBankRight(),
]);
const inputManager = getContextItem<InputManager>("inputManager");
@@ -306,14 +316,6 @@ export class EditTrackState extends State<States> {
state.transitionTo(States.RUNNING);
});
inputManager.onKey(" ", () => {
if (this.selectedSegment) {
this.selectedSegment = undefined;
} else {
this.selectedSegment = new StraightTrack();
}
});
inputManager.onMouse("left", () => {
const track = getContextItem<TrackSystem>("track");
if (this.ghostSegment && this.closestEnd) {
@@ -383,6 +385,18 @@ export class EditTrackState extends State<States> {
}
});
inputManager.onKey("r", () => {
if (!this.selectedSegment) return;
const segment = this.selectedSegment;
let angle = Math.PI / 12;
segment.rotate(angle);
});
inputManager.onKey("R", () => {
if (!this.selectedSegment) return;
const segment = this.selectedSegment;
let angle = -Math.PI / 12;
segment.rotate(angle);
});
// TODO
// Cache trains and save
@@ -405,6 +419,8 @@ export class EditTrackState extends State<States> {
inputManager.offKey("e");
inputManager.offKey("w");
inputManager.offKey("z");
inputManager.offKey("r");
inputManager.offKey("R");
inputManager.offKey("Escape");
inputManager.offMouse("left");
if (this.heldEvents.size > 0) {

View File

@@ -71,15 +71,18 @@ export class RunningState extends State<States> {
// const path = track.path;
// const follower = new DotFollower(path, path.points[0].copy());
// ctx.trains.push(follower);
// const train = new Train(track.path, [new LargeLady(), new Tender()]);
// ctx.trains.push(train);
const train = new Train(track.path, [
new LargeLady(),
new LargeLadyTender(),
]);
ctx.trains.push(train);
});
const train = new Train(track.path, [
new LargeLady(),
new LargeLadyTender(),
]);
ctx.trains.push(train);
this.activeTrain = train;
// const train = new Train(track.path, [
// new LargeLady(),
// new LargeLadyTender(),
// ]);
// ctx.trains.push(train);
// this.activeTr0ain = train;
// const trainCount = 1000;
// for (let i = 0; i < trainCount; i++) {
// const train = new Train(track.path, [new LargeLady(), new Tender()]);