Compare commits
4 Commits
03e0b1afcb
...
main
Author | SHA1 | Date | |
---|---|---|---|
7b6dbb295f | |||
3aea38f9f4 | |||
2176f67413 | |||
10d462edaf |
BIN
public/blobs/snr/audio/ding.mp3
Normal file
BIN
public/blobs/snr/audio/ding.mp3
Normal file
Binary file not shown.
@@ -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 {
|
||||||
|
@@ -41,7 +41,7 @@ export class ResourceManager {
|
|||||||
) {
|
) {
|
||||||
const identifier = parseNamespacedId(name);
|
const identifier = parseNamespacedId(name);
|
||||||
if (typeof (value as EventSource).addEventListener === "function") {
|
if (typeof (value as EventSource).addEventListener === "function") {
|
||||||
if (value instanceof Image) {
|
if (value instanceof Image || value instanceof Audio) {
|
||||||
// During development, we can use the local file system
|
// During development, we can use the local file system
|
||||||
value.src =
|
value.src =
|
||||||
`/blobs/${identifier.namespace}/${identifier.type}/${identifier.name}${
|
`/blobs/${identifier.namespace}/${identifier.type}/${identifier.name}${
|
||||||
@@ -55,8 +55,10 @@ export class ResourceManager {
|
|||||||
const onload = () => {
|
const onload = () => {
|
||||||
this.resources.set(name, value);
|
this.resources.set(name, value);
|
||||||
resolve(true);
|
resolve(true);
|
||||||
|
(value as EventSource).removeEventListener("loadeddata", onload);
|
||||||
(value as EventSource).removeEventListener("load", onload);
|
(value as EventSource).removeEventListener("load", onload);
|
||||||
};
|
};
|
||||||
|
(value as EventSource).addEventListener("loadeddata", onload);
|
||||||
(value as EventSource).addEventListener("load", onload);
|
(value as EventSource).addEventListener("load", onload);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
@@ -48,6 +48,7 @@ const _fullDebug: Debug = {
|
|||||||
bogies: false,
|
bogies: false,
|
||||||
angles: false,
|
angles: false,
|
||||||
aabb: false,
|
aabb: false,
|
||||||
|
segment: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const storedDebug = JSON.parse(localStorage.getItem("debug") || "0");
|
const storedDebug = JSON.parse(localStorage.getItem("debug") || "0");
|
||||||
|
7
src/math/angle.ts
Normal file
7
src/math/angle.ts
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
export function angleToRadians(angle: number) {
|
||||||
|
return angle / 180 * Math.PI;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function angleToDegrees(angle: number) {
|
||||||
|
return angle * 180 / Math.PI;
|
||||||
|
}
|
@@ -14,6 +14,8 @@ import {
|
|||||||
SBendLeft,
|
SBendLeft,
|
||||||
SBendRight,
|
SBendRight,
|
||||||
StraightTrack,
|
StraightTrack,
|
||||||
|
TightBankLeft,
|
||||||
|
TightBankRight,
|
||||||
WideBankLeft,
|
WideBankLeft,
|
||||||
WideBankRight,
|
WideBankRight,
|
||||||
} from "../../track/shapes.ts";
|
} from "../../track/shapes.ts";
|
||||||
@@ -42,18 +44,6 @@ 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();
|
||||||
@@ -63,7 +53,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);
|
||||||
@@ -142,7 +132,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;
|
||||||
@@ -257,6 +247,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
|
||||||
@@ -296,6 +287,8 @@ export class EditTrackState extends State<States> {
|
|||||||
new BankRight(),
|
new BankRight(),
|
||||||
new WideBankLeft(),
|
new WideBankLeft(),
|
||||||
new WideBankRight(),
|
new WideBankRight(),
|
||||||
|
new TightBankLeft(),
|
||||||
|
new TightBankRight(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
const inputManager = getContextItem<InputManager>("inputManager");
|
const inputManager = getContextItem<InputManager>("inputManager");
|
||||||
|
@@ -39,9 +39,11 @@ export class LoadState extends State<States> {
|
|||||||
resources.set("snr:sprite/LargeLady", new Image());
|
resources.set("snr:sprite/LargeLady", new Image());
|
||||||
// resources.get<HTMLImageElement>("snr:sprite/engine")!.src =
|
// resources.get<HTMLImageElement>("snr:sprite/engine")!.src =
|
||||||
// "/sprites/EngineSprites.png";
|
// "/sprites/EngineSprites.png";
|
||||||
|
|
||||||
|
resources.set("snr:audio/ding", new Audio());
|
||||||
resources.ready().then(() => {
|
resources.ready().then(() => {
|
||||||
this.stateMachine.transitionTo(States.RUNNING);
|
this.stateMachine.transitionTo(States.RUNNING);
|
||||||
});
|
}).catch((e) => console.error(e));
|
||||||
|
|
||||||
const doodler = getContextItem<Doodler>("doodler");
|
const doodler = getContextItem<Doodler>("doodler");
|
||||||
// this.layers.push(doodler.createLayer((_, __, dTime) => {
|
// this.layers.push(doodler.createLayer((_, __, dTime) => {
|
||||||
|
@@ -85,7 +85,10 @@ export class RunningState extends State<States> {
|
|||||||
// this.activeTr0ain = train;
|
// this.activeTr0ain = train;
|
||||||
// const trainCount = 1000;
|
// const trainCount = 1000;
|
||||||
// for (let i = 0; i < trainCount; i++) {
|
// for (let i = 0; i < trainCount; i++) {
|
||||||
// const train = new Train(track.path, [new LargeLady(), new Tender()]);
|
// const train = new Train(track.path, [
|
||||||
|
// new LargeLady(),
|
||||||
|
// new LargeLadyTender(),
|
||||||
|
// ]);
|
||||||
// ctx.trains.push(train);
|
// ctx.trains.push(train);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
@@ -88,6 +88,58 @@ export class WideBankLeft extends TrackSegment {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export class TightBankLeft extends TrackSegment {
|
||||||
|
constructor(start?: Vector) {
|
||||||
|
start = start || new Vector(100, 100);
|
||||||
|
|
||||||
|
const p1 = start.copy();
|
||||||
|
const p2 = start.copy();
|
||||||
|
const p3 = start.copy();
|
||||||
|
const p4 = start.copy();
|
||||||
|
const scale = 61.57;
|
||||||
|
|
||||||
|
p2.add(new Vector(1, 0).mult(scale));
|
||||||
|
p3.set(p2);
|
||||||
|
const dirToP3 = Vector.fromAngle(-Math.PI / 12).mult(scale);
|
||||||
|
p3.add(dirToP3);
|
||||||
|
p4.set(p3);
|
||||||
|
const dirToP4 = Vector.fromAngle(-Math.PI / 6).mult(scale);
|
||||||
|
p4.add(dirToP4);
|
||||||
|
|
||||||
|
super([
|
||||||
|
p1,
|
||||||
|
p2,
|
||||||
|
p3,
|
||||||
|
p4,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
export class TightBankRight extends TrackSegment {
|
||||||
|
constructor(start?: Vector) {
|
||||||
|
start = start || new Vector(100, 100);
|
||||||
|
|
||||||
|
const p1 = start.copy();
|
||||||
|
const p2 = start.copy();
|
||||||
|
const p3 = start.copy();
|
||||||
|
const p4 = start.copy();
|
||||||
|
const scale = 61.57;
|
||||||
|
|
||||||
|
p2.add(new Vector(1, 0).mult(scale));
|
||||||
|
p3.set(p2);
|
||||||
|
const dirToP3 = Vector.fromAngle(Math.PI / 12).mult(scale);
|
||||||
|
p3.add(dirToP3);
|
||||||
|
p4.set(p3);
|
||||||
|
const dirToP4 = Vector.fromAngle(Math.PI / 6).mult(scale);
|
||||||
|
p4.add(dirToP4);
|
||||||
|
|
||||||
|
super([
|
||||||
|
p1,
|
||||||
|
p2,
|
||||||
|
p3,
|
||||||
|
p4,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
export class WideBankRight extends TrackSegment {
|
export class WideBankRight extends TrackSegment {
|
||||||
constructor(start?: Vector) {
|
constructor(start?: Vector) {
|
||||||
start = start || new Vector(100, 100);
|
start = start || new Vector(100, 100);
|
||||||
|
@@ -2,12 +2,14 @@ import { Doodler, Point, Vector } from "@bearmetal/doodler";
|
|||||||
import { ComplexPath, PathSegment } from "../math/path.ts";
|
import { ComplexPath, PathSegment } from "../math/path.ts";
|
||||||
import { getContextItem, setDefaultContext } from "../lib/context.ts";
|
import { getContextItem, setDefaultContext } from "../lib/context.ts";
|
||||||
import { clamp } from "../math/clamp.ts";
|
import { clamp } from "../math/clamp.ts";
|
||||||
|
import { Debuggable } from "../lib/debuggable.ts";
|
||||||
|
|
||||||
export class TrackSystem {
|
export class TrackSystem extends Debuggable {
|
||||||
private _segments: Map<string, TrackSegment> = new Map();
|
private _segments: Map<string, TrackSegment> = new Map();
|
||||||
private doodler: Doodler;
|
private doodler: Doodler;
|
||||||
|
|
||||||
constructor(segments: TrackSegment[]) {
|
constructor(segments: TrackSegment[]) {
|
||||||
|
super("track");
|
||||||
this.doodler = getContextItem<Doodler>("doodler");
|
this.doodler = getContextItem<Doodler>("doodler");
|
||||||
for (const segment of segments) {
|
for (const segment of segments) {
|
||||||
this._segments.set(segment.id, segment);
|
this._segments.set(segment.id, segment);
|
||||||
@@ -26,6 +28,19 @@ export class TrackSystem {
|
|||||||
return this._segments.values().toArray().pop();
|
return this._segments.values().toArray().pop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getNearestSegment(pos: Vector) {
|
||||||
|
let minDistance = Infinity;
|
||||||
|
let nearestSegment: TrackSegment | undefined;
|
||||||
|
for (const segment of this._segments.values()) {
|
||||||
|
const distance = segment.getDistanceTo(pos);
|
||||||
|
if (distance < minDistance) {
|
||||||
|
minDistance = distance;
|
||||||
|
nearestSegment = segment;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nearestSegment;
|
||||||
|
}
|
||||||
|
|
||||||
optimize(percent: number) {
|
optimize(percent: number) {
|
||||||
console.log("Optimizing track", percent * 100 / 4);
|
console.log("Optimizing track", percent * 100 / 4);
|
||||||
for (const segment of this._segments.values()) {
|
for (const segment of this._segments.values()) {
|
||||||
@@ -35,7 +50,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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -85,6 +100,15 @@ export class TrackSystem {
|
|||||||
// }
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override debugDraw(): void {
|
||||||
|
const debug = getContextItem("debug");
|
||||||
|
if (debug.track) {
|
||||||
|
for (const segment of this._segments.values()) {
|
||||||
|
segment.drawAABB();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ends: Map<TrackSegment, [End, End]> = new Map();
|
ends: Map<TrackSegment, [End, End]> = new Map();
|
||||||
endArray: End[] = [];
|
endArray: End[] = [];
|
||||||
|
|
||||||
@@ -282,12 +306,56 @@ 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.recalculateRailPoints();
|
this.update();
|
||||||
this.recalculateTiePoints();
|
}
|
||||||
|
|
||||||
|
getDistanceTo(pos: Vector) {
|
||||||
|
return Vector.dist(this.aabb.center, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
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) {
|
||||||
@@ -296,7 +364,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(6);
|
normal.setMag(this.trackGuage / 2);
|
||||||
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)));
|
||||||
@@ -307,6 +375,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;
|
||||||
}
|
}
|
||||||
@@ -391,6 +465,15 @@ export class TrackSegment extends PathSegment {
|
|||||||
// });
|
// });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
drawAABB() {
|
||||||
|
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 {
|
||||||
return {
|
return {
|
||||||
p: this.points.map((p) => p.array()),
|
p: this.points.map((p) => p.array()),
|
||||||
|
@@ -4,15 +4,6 @@ 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[] = [];
|
||||||
|
|
||||||
@@ -25,7 +16,7 @@ export class Train extends Debuggable {
|
|||||||
|
|
||||||
speed = 5;
|
speed = 5;
|
||||||
|
|
||||||
aabb!: TrainAABB;
|
aabb!: AABB;
|
||||||
|
|
||||||
get segments() {
|
get segments() {
|
||||||
return Array.from(
|
return Array.from(
|
||||||
@@ -204,7 +195,7 @@ export class TrainCar extends Debuggable {
|
|||||||
|
|
||||||
train?: Train;
|
train?: Train;
|
||||||
|
|
||||||
aabb!: TrainAABB;
|
aabb!: AABB;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
length: number,
|
length: number,
|
||||||
|
22
src/types.ts
22
src/types.ts
@@ -18,6 +18,7 @@ declare global {
|
|||||||
|
|
||||||
type Debug = {
|
type Debug = {
|
||||||
track: boolean;
|
track: boolean;
|
||||||
|
segment: boolean;
|
||||||
train: boolean;
|
train: boolean;
|
||||||
car: boolean;
|
car: boolean;
|
||||||
path: boolean;
|
path: boolean;
|
||||||
@@ -25,4 +26,25 @@ 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) ?? {},
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user