Remove SwitchPrefabTrack class

This commit is contained in:
dominik-korsa 2025-07-19 13:24:57 +02:00
parent e95a4dddaf
commit 8f94f0f142
No known key found for this signature in database
GPG Key ID: 5A24D76EE0A30974
6 changed files with 81 additions and 68 deletions

View File

@ -1,3 +1,5 @@
import Vector3 from "../model/vector3";
function degToRad(angle) {
return angle * (Math.PI / 180);
}
@ -6,6 +8,22 @@ function radToDeg(angle) {
return angle * (180 / Math.PI);
}
function rotationDegToRad(vector) {
return new Vector3(
degToRad(vector.x),
degToRad(vector.y),
degToRad(vector.z),
);
}
function rotationRadToDeg(vector) {
return new Vector3(
radToDeg(vector.x),
radToDeg(vector.y),
radToDeg(vector.z),
);
}
function normalizeDegAngle(angle) {
if (angle < 0) {
return angle + 360 * Math.ceil(Math.abs(angle) / 360);
@ -19,7 +37,9 @@ function normalizeDegAngle(angle) {
const AngleHelper = {
degToRad,
radToDeg,
rotationDegToRad,
rotationRadToDeg,
normalizeDegAngle,
}
export default AngleHelper;
export default AngleHelper;

View File

@ -0,0 +1,42 @@
import Vector3 from "../model/vector3";
function calculateCurveEnd(startPos, startAngle, radius, curveLength) {
if (radius === 0) {
return {
endPos: startPos.add(Vector3.fromAngleY(startAngle, curveLength)),
endAngle: startAngle,
circleCenter: null,
};
}
const centerToStart = Vector3.fromAngleY(startAngle + Math.sign(radius) * Math.PI / 2, Math.abs(radius));
const circleCenter = startPos.add(centerToStart.negate());
const endAngle = startAngle -curveLength / radius;
const centerToEnd = Vector3.fromAngleY(endAngle + Math.sign(radius) * Math.PI / 2, Math.abs(radius));
const endPos = circleCenter.add(centerToEnd);
return { endPos, endAngle, circleCenter };
}
/**
* Does the same as calculateCurveEnd with `startPos = Vector3.zero()` and `startAngle = 0`.
*/
function calculateCurveEndStandard(radius, curveLength) {
if (radius === 0) {
return {
endPos: new Vector3(0, 0, curveLength),
endAngle: 0,
circleCenter: null,
};
}
const circleCenter = new Vector3(-radius, 0, 0);
const endAngle = -curveLength / radius;
const centerToEnd = Vector3.fromAngleY(endAngle + Math.sign(radius) * Math.PI / 2, Math.abs(radius));
const endPos = circleCenter.add(centerToEnd);
return { endPos, endAngle, circleCenter };
}
const CurveHelper = {
calculateCurveEnd,
calculateCurveEndStandard,
};
export default CurveHelper;

View File

@ -1,12 +1,13 @@
import SwitchPrefab from "../switch-descriptions/switch-prefab";
import Quaternion from "../quaternion";
import Vector3 from "../vector3";
import {SwitchTrackConnectionType} from "../switch-descriptions/switch-prefab-track";
import {TrackConnectionEnd} from "../track-connection";
import MiscHelper from "../../helpers/miscHelper";
const { START, END } = TrackConnectionEnd;
const { INTERNAL, EXTERNAL } = SwitchTrackConnectionType;
const INTERNAL = Symbol('SwitchTrackConnectionType.INTERNAL');
const EXTERNAL = Symbol('SwitchTrackConnectionType.INTERNAL');
export const SwitchTrackConnectionType = { INTERNAL, EXTERNAL };
const exportedSwitches = {
'Crossing': {
@ -839,8 +840,6 @@ const exportedSwitches = {
},
};
const DefinedSwitches = MiscHelper.mapObject(exportedSwitches, (def, prefabName) => {
export const DefinedSwitches = MiscHelper.mapObject(exportedSwitches, (def, prefabName) => {
return SwitchPrefab.parseExported(prefabName, def);
});
export default DefinedSwitches;

View File

@ -1,32 +0,0 @@
export const SwitchTrackConnectionType = {
INTERNAL: Symbol('SwitchTrackConnectionType.INTERNAL'),
EXTERNAL: Symbol('SwitchTrackConnectionType.EXTERNAL'),
};
export default class SwitchPrefabTrack {
startPos;
endPos;
radius;
dataIndex;
connections;
constructor(startPos, endPos, radius, dataIndex, connections = []) {
Object.assign(this, {
startPos,
endPos,
radius,
dataIndex,
connections,
});
}
static point(pos, dataIndex, connections = []) {
return new SwitchPrefabTrack(
pos,
pos,
0,
dataIndex,
connections,
);
}
}

View File

@ -1,43 +1,20 @@
import Vector3 from "../vector3";
import SwitchPrefabTrack from "./switch-prefab-track";
import MiscHelper from "../../helpers/miscHelper";
export default class SwitchPrefab {
tracks = {};
isolation_id_offset;
prefab_name;
constructor(tracks, isolation_id_offset, prefab_name) {
constructor(tracks, isolation_id_offset) {
Object.assign(this, {
tracks,
isolation_id_offset,
prefab_name,
});
}
static _calculateCurveEnd(startPos, startAngle, radius, curveLength) {
if (radius === 0) {
return { endPos: Vector3.fromAngleY(startAngle, curveLength), endAngle: startAngle };
}
const centerToStart = Vector3.fromAngleY(startAngle + Math.sign(radius) * Math.PI / 2, Math.abs(radius));
const circleCenter = startPos.add(centerToStart.negate());
const endAngle = startAngle -curveLength / radius;
const centerToEnd = Vector3.fromAngleY(endAngle + Math.sign(radius) * Math.PI / 2, Math.abs(radius));
const endPos = circleCenter.add(centerToEnd);
return { endPos, endAngle };
}
static parseExported(prefabName, def) {
const tracks = MiscHelper.mapObject(def.tracks, (exportedTrack) => {
const startPos = exportedTrack.pos;
const startToEnd = SwitchPrefab._calculateCurveEnd(Vector3.zero(), 0 , exportedTrack.radius, exportedTrack.length).endPos;
const endPos = startPos.add(startToEnd.rotateByQuaternion(exportedTrack.rot));
return new SwitchPrefabTrack(startPos, endPos, exportedTrack.radius, exportedTrack.dataIndex, exportedTrack.connections);
});
const isolationIdOffset = prefabName.startsWith("Rkp") || prefabName.startsWith("Crossing")
? Vector3.zero() : new Vector3(0, 0, 6);
return new SwitchPrefab(tracks, isolationIdOffset, prefabName);
return new SwitchPrefab(def.tracks, isolationIdOffset);
}
}

View File

@ -2,9 +2,10 @@ import PointTrack from "./tracks/point-track.js";
import SceneryObject from "./scenery-object.js";
import SceneryParserLog from "./scenery-parser-log.js";
import Vector3 from "./vector3.js";
import DefinedSwitches from "./defs/defined-switches.js";
import {SwitchTrackConnectionType} from "./switch-descriptions/switch-prefab-track";
import {DefinedSwitches, SwitchTrackConnectionType} from "./defs/defined-switches.js";
import TrackConnection, {TrackConnectionEnd} from "./track-connection";
import CurveHelper from "../helpers/curveHelper";
import AngleHelper from "../helpers/angleHelper";
export default class Switch extends SceneryObject {
model;
@ -105,7 +106,7 @@ export default class Switch extends SceneryObject {
}
_createSwitchTrackFromDef(scenery, switchDef, trackDef, ids) {
const rotRad = this.rot.multiply(Math.PI / 180);
const rotRad = AngleHelper.rotationDegToRad(this.rot);
if (trackDef.dataIndex >= ids.length) {
SceneryParserLog.warn(
'switchMissingTrackId',
@ -145,10 +146,16 @@ export default class Switch extends SceneryObject {
}
});
const localEndPos = CurveHelper
.calculateCurveEndStandard(trackDef.radius, trackDef.length)
.endPos
.rotateByQuaternion(trackDef.rot)
.add(trackDef.pos);
const trackObj = new PointTrack(
trackId,
this.pos.add(trackDef.startPos.rotate(rotRad)),
this.pos.add(trackDef.endPos.rotate(rotRad)),
this.pos.add(trackDef.pos.rotate(rotRad)),
this.pos.add(localEndPos.rotate(rotRad)),
trackDef.radius,
connections,
this.id_switch,