this.switchBlades = []; this.signals = []; this.blockSignals = []; this.aiTrains = []; this.passengerDoors = []; this.trainCars = []; // Einzelne Fahrzeuge (Lok + Wagen) mit Drehgestellen & Gelenkphysik this.setupLighting(); this.buildCurvedEnvironment(); this.buildStationsAndRailYard(); this.buildPlayerTrain(); this.spawnAITrains(); this.setupControls(); this.lastTime = performance.now(); this.selectTrackLane(1); } ``` ```html:Kurvenreiche 3D-Strecke mit zusätzlichen S-Kurven & Bogenabschnitten:swiss_train_game.html constructor(branchMode = false) { // Hauptstrecke durch die Schweizer Landschaft (Olten -> Viadukt -> Aarau -> Tunnel -> RBL -> Zürich HB) // Mit zusätzlichen geschwungenen S-Kurven, Talbögen und Zufahrtskurven // Im branchMode zweigt nach dem Voralpenbogen eine kurvenreiche Nebenlinie (Aare-Seetal) über Hügel & Landstationen ab! const mainPoints = [ new THREE.Vector3(0, 0, 140), new THREE.Vector3(-45, 0.5, -320), // Sanfter Einlaufbogen new THREE.Vector3(35, 0, -760), // Gegenbogen vor Olten HB new THREE.Vector3(0, 0, -1100), // Olten HB Bereich new THREE.Vector3(55, 1.5, -1580), // Ausfahrts-S-Kurve Richtung Osten new THREE.Vector3(-140, 4.5, -2050), // Weite Linkskurve new THREE.Vector3(-310, 7.5, -2520), // Rechts-S-Schwung zum Viadukt new THREE.Vector3(-440, 11.5, -3050), // Aare-Viadukt auf 11.5m Höhe new THREE.Vector3(-290, 6.5, -3580), // Kurvige Talabfahrt new THREE.Vector3(-150, 2.5, -4120), // Aarau Bahnhof new THREE.Vector3(-65, 3.5, -4620), // Rechtskurve hinter Aarau new THREE.Vector3(-260, 7.5, -5120), // Gegenkurve in die Voralpen new THREE.Vector3(-480, 12.0, -5620), // Weite Bergkurve & Heitersbergtunnel new THREE.Vector3(-570, 14.0, -6120), // Scheitelkurve im Tunnel new THREE.Vector3(-380, 6.0, -6620), // Tunnelausfahrt & S-Kurve ins Limmattal new THREE.Vector3(-240, 0, -7150), // Einfahrt Rangierbahnhof Limmattal RBL new THREE.Vector3(-240, 0, -8350), // RBL-Korridor new THREE.Vector3(-75, 0, -8850), // Rechtskurve aus dem RBL-Vorfeld new THREE.Vector3(95, 0, -9350), // S-Gegenbogen Richtung Zürich new THREE.Vector3(210, 0, -9900), // Zürich HB Vorfeld new THREE.Vector3(240, 0, -10450), // Zürich HB Halle new THREE.Vector3(240, 0, -11100) ]; const branchPoints = [ new THREE.Vector3(0, 0, 140), new THREE.Vector3(-55, 0.5, -420), new THREE.Vector3(0, 0, -1100), // Olten HB new THREE.Vector3(-115, 2.5, -1680), // Linksbogen new THREE.Vector3(-240, 6.0, -2350), // Weichenabzweig zur Nebenlinie Aare-Seetal new THREE.Vector3(-420, 8.5, -2780), // Geschwungene Waldkurve new THREE.Vector3(-590, 10.5, -3180), // Gegenbogen über das Seetal new THREE.Vector3(-690, 12.0, -3520), // Landstation Lenzburg See new THREE.Vector3(-560, 9.5, -3980), // Weite Hügelkurve new THREE.Vector3(-410, 6.5, -4380), // S-Schleife Richtung Seon new THREE.Vector3(-320, 4.0, -4750), // Landstation Seon Dorf new THREE.Vector3(-210, 2.5, -5120), // Endbogen new THREE.Vector3(-150, 2.0, -5450), // Endhaltestelle Seetal-Winkel new THREE.Vector3(-120, 2.0, -5900) ]; const controlPoints = branchMode ? branchPoints : mainPoints; ``` ```html:Präzise Weichengeometrie, Bahnhofssignale & Drehgestelle (Bogies):swiss_train_game.html /** * Hilfsfunktion: Erstellt ein Gleis mit variabler Querverschiebung (für Weichen!) * Nutzt exakte seitliche Kurven-Vektoren, damit Weichenschienen fluchtend an Haupt- & Nebengleis anschließen. */ createSwitchTrackGeometry(sStart, sEnd, offsetStart, offsetEnd, width, heightOffset) { const positions = []; const indices = []; const steps = 40; for (let i = 0; i <= steps; i++) { const t = i / steps; // Sanfter S-förmiger Übergang (Quintic Smoothstep) ohne Knick am Herzstück const smoothT = t * t * t * (t * (t * 6 - 15) + 10); const s = sStart + t * (sEnd - sStart); const lat = offsetStart + (offsetEnd - offsetStart) * smoothT; const f = this.getFrameAt(s, 0); const basePt = f.pos.clone().addScaledVector(f.right, lat).addScaledVector(f.up, heightOffset); const lPt = basePt.clone().addScaledVector(f.right, -width / 2); const rPt = basePt.clone().addScaledVector(f.right, width / 2); positions.push(lPt.x, lPt.y, lPt.z, rPt.x, rPt.y, rPt.z); if (i < steps) { const r = i * 2; indices.push(r, r + 1, r + 2, r + 1, r + 3, r + 2); } } const geo = new THREE.BufferGeometry(); geo.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3)); geo.setIndex(indices); geo.computeVertexNormals(); return geo; } } // ============================================================================ // 4. HAUPT-3D-SIMULATION (SBB3DTrainSimulator) // ============================================================================ ``` ```html:Bahnhofssignale, Weichenzungen & Blocksignale für KI-Schutz:swiss_train_game.html this.stations.forEach((st, idx) => { // Einfahrts-Hauptsignal vor der Einfahrtsweiche (für Spieler- & KI-Regelung) this.createSwissSignal(-4.4, st.switchStartS - 110, st, 'entry'); // Bahnhofssignal am Perronkopf / Einfahrtsbereich des Bahnhofs this.createSwissSignal(-4.4, st.platformStartS - 12, st, 'station'); // Einfahrts-Weichenstraße (Links nach -7.6m und Rechts nach st.rightLaneOffset) [-0.717, 0.717].forEach(rOff => { const swLeft = this.spline.createSwitchTrackGeometry(st.switchStartS, st.switchEndS, -2.4 + rOff, -7.6 + rOff, 0.14, 0.14); const swRight = this.spline.createSwitchTrackGeometry(st.switchStartS, st.switchEndS, -2.4 + rOff, st.rightLaneOffset + rOff, 0.14, 0.14); this.scene.add(new THREE.Mesh(swLeft, railMat), new THREE.Mesh(swRight, railMat)); }); // Weichen-Radlenker / Herzstück-Schwellen im Weichenbereich const swMidS = (st.switchStartS + st.switchEndS) * 0.5; for (let s = st.switchStartS; s <= st.switchEndS; s += 2.2) { const f = this.spline.getFrameAt(s, -2.4); const tie = new THREE.Mesh(new THREE.BoxGeometry(5.6, 0.12, 0.28), new THREE.MeshLambertMaterial({ color: 0x475569 })); tie.position.copy(f.pos); tie.rotation.y = Math.atan2(-f.tan.x, -f.tan.z); this.scene.add(tie); } // Ausfahrts-Weichenstraße zurück aufs Hauptgleis (-2.4m) [-0.717, 0.717].forEach(rOff => { const exLeft = this.spline.createSwitchTrackGeometry(st.exitSwitchStartS, st.exitSwitchEndS, -7.6 + rOff, -2.4 + rOff, 0.14, 0.14); const exRight = this.spline.createSwitchTrackGeometry(st.exitSwitchStartS, st.exitSwitchEndS, st.rightLaneOffset + rOff, -2.4 + rOff, 0.14, 0.14); this.scene.add(new THREE.Mesh(exLeft, railMat), new THREE.Mesh(exRight, railMat)); }); // Bewegliche 3D-Weichenzungen (links & rechts anliegend/abweisend) + Weichenlaterne const swFrame = this.spline.getFrameAt(st.switchStartS, -2.4); const swHeading = Math.atan2(-swFrame.tan.x, -swFrame.tan.z); const bladeGroup = new THREE.Group(); const bladeGeo = new THREE.BoxGeometry(0.14, 0.24, 20); const bladeMat = new THREE.MeshStandardMaterial({ color: 0xF59E0B, emissive: 0x78350F }); const bladeL = new THREE.Mesh(bladeGeo, bladeMat); bladeL.position.set(-0.72, 0.16, -10); const bladeR = new THREE.Mesh(bladeGeo, bladeMat); bladeR.position.set(0.72, 0.16, -10); // Weichenlaterne mit weißer Richtungslampe const lantern = new THREE.Mesh( new THREE.BoxGeometry(0.42, 0.42, 0.42), new THREE.MeshBasicMaterial({ color: 0xFEF08A }) ); lantern.position.set(-1.75, 1.15, 0); const lanternPost = new THREE.Mesh( new THREE.CylinderGeometry(0.06, 0.08, 1.15, 6), new THREE.MeshLambertMaterial({ color: 0x64748B }) ); lanternPost.position.set(-1.75, 0.58, 0); bladeGroup.add(bladeL, bladeR, lantern, lanternPost); bladeGroup.position.copy(swFrame.pos); bladeGroup.rotation.y = swHeading; this.scene.add(bladeGroup); const boxPos = swFrame.pos.clone().addScaledVector(swFrame.right, -2.2); const switchBox = new THREE.Mesh( new THREE.BoxGeometry(0.75, 0.75, 0.75), new THREE.MeshStandardMaterial({ color: 0xF59E0B, emissive: 0xB45309 }) ); switchBox.position.set(boxPos.x, boxPos.y + 0.38, boxPos.z); switchBox.rotation.y = swHeading; this.scene.add(switchBox); this.switchBlades.push({ stationIndex: idx, group: bladeGroup, bladeL, bladeR, lantern, box: switchBox }); ``` ```html:Haupt- & Gegengleis-Signale für Stationsschutz und KI-Zugsicherung:swiss_train_game.html // Grüne Ziel-Stoppzone am Perron / Rangiergleis const targetLat = st.targetLane === 0 ? -7.6 : (st.targetLane === 2 ? st.rightLaneOffset : -2.4); const stopGeo = this.spline.createRibbonGeometry(st.stopTargetS - 14, st.stopTargetS + 14, targetLat, 2.6, 0.08, 2); const stopMarker = new THREE.Mesh(stopGeo, new THREE.MeshBasicMaterial({ color: 0x10B981, transparent: true, opacity: 0.78 })); this.scene.add(stopMarker); // Ausfahrts-Hauptsignal sowie Gegengleis-Einfahrtssignal (bei +4.4m) damit Gegenzüge nie durch Stationen fahren this.createSwissSignal(-4.4, st.platformEndS + 15, null, 'exit'); this.createSwissSignal(4.4, st.exitSwitchEndS + 35, st, 'opposing_entry'); }); // Zusätzliche Strecken-Blocksignale entlang der Strecke für realistische KI-Zugfolgeabstände for (let s = 600; s < this.spline.totalLength - 400; s += 1350) { const nearSt = this.stations.some(st => Math.abs(s - st.switchStartS) < 420 || Math.abs(s - st.exitSwitchEndS) < 420); if (!nearSt) { this.createSwissSignal(4.4, s, null, 'block'); } } } ``` ```html:Detaillierte 3D-Drehgestelle (Bogies) und Signal-Darstellung:swiss_train_game.html createSwissSignal(latOffset, sPos, linkedStation, role = 'block') { const f = this.spline.getFrameAt(sPos, latOffset); const hdg = Math.atan2(-f.tan.x, -f.tan.z); const group = new THREE.Group(); const mast = new THREE.Mesh( new THREE.CylinderGeometry(0.11, 0.13, 4.9, 8), new THREE.MeshStandardMaterial({ color: 0x64748B, metalness: 0.5 }) ); mast.position.y = 2.45; group.add(mast); // Schweizer Signalschirm (schwarz mit weißem Rand) const screenBorder = new THREE.Mesh( new THREE.BoxGeometry(0.84, 1.68, 0.22), new THREE.MeshBasicMaterial({ color: 0xF8FAFC }) ); screenBorder.position.y = 4.45; group.add(screenBorder); const head = new THREE.Mesh( new THREE.BoxGeometry(0.72, 1.56, 0.26), new THREE.MeshBasicMaterial({ color: 0x0F172A }) ); head.position.y = 4.45; group.add(head); const lampG = new THREE.Mesh(new THREE.SphereGeometry(0.15, 10, 10), new THREE.MeshBasicMaterial({ color: 0x10B981 })); lampG.position.set(0, 4.92, 0.14); const lampY = new THREE.Mesh(new THREE.SphereGeometry(0.15, 10, 10), new THREE.MeshBasicMaterial({ color: 0x1E293B })); lampY.position.set(0, 4.46, 0.14); const lampR = new THREE.Mesh(new THREE.SphereGeometry(0.15, 10, 10), new THREE.MeshBasicMaterial({ color: 0x1E293B })); lampR.position.set(0, 4.00, 0.14); group.add(lampG, lampY, lampR); const speedTex = createSpeedSignTexture(linkedStation ? 60 : 140); const signPlate = new THREE.Mesh( new THREE.PlaneGeometry(0.78, 0.78), new THREE.MeshBasicMaterial({ map: speedTex, side: THREE.DoubleSide }) ); signPlate.position.set(0, 2.55, 0.15); group.add(signPlate); group.position.copy(f.pos); group.rotation.y = hdg; this.scene.add(group); this.signals.push({ sPos, linkedStation, role, lampG, lampY, lampR }); } /** * Baut ein detailliertes 3D-Drehgestell (Bogie) mit Rahmen, Achslagern, * Schraubenfedern, Bremsscheiben und Spurkranz-Stahlrädern. */ createBogieMesh(wheelRadius = 0.46, wheelbase = 2.6) { const bogie = new THREE.Group(); const frameMat = new THREE.MeshStandardMaterial({ color: 0x1E293B, metalness: 0.72, roughness: 0.35 }); const steelMat = new THREE.MeshStandardMaterial({ color: 0x94A3B8, metalness: 0.88, roughness: 0.22 }); const springMat = new THREE.MeshStandardMaterial({ color: 0x334155, metalness: 0.6, roughness: 0.4 }); const brakeMat = new THREE.MeshStandardMaterial({ color: 0xB45309, metalness: 0.5, roughness: 0.5 }); // Längsträger (Side Frames) links und rechts [-0.96, 0.96].forEach(sideX => { const sideBeam = new THREE.Mesh(new THREE.BoxGeometry(0.18, 0.28, wheelbase + 0.65), frameMat); sideBeam.position.set(sideX, wheelRadius + 0.08, 0); bogie.add(sideBeam); // Sekundär-Schraubenfedern / Luftfederung [-0.55, 0.55].forEach(zSpr => { const spring = new THREE.Mesh(new THREE.CylinderGeometry(0.15, 0.15, 0.26, 10), springMat); spring.position.set(sideX * 0.85, wheelRadius + 0.32, zSpr); bogie.add(spring); }); }); // Querträger (Bolster / Wiege) in der Drehgestellmitte const bolster = new THREE.Mesh(new THREE.BoxGeometry(2.15, 0.24, 0.65), frameMat); bolster.position.set(0, wheelRadius + 0.14, 0); bogie.add(bolster); // Zwei Radsätze (vordere & hintere Achse) mit Achslagern, Bremsscheiben & Rädern [-wheelbase / 2, wheelbase / 2].forEach(zAxle => { const axle = new THREE.Mesh( new THREE.CylinderGeometry(0.09, 0.09, 1.95, 10), steelMat ); axle.rotation.z = Math.PI / 2; axle.position.set(0, wheelRadius, zAxle); bogie.add(axle); [-0.717, 0.717].forEach(xWheel => { // Stahlrad (Lauffläche) const wheel = new THREE.Mesh( new THREE.CylinderGeometry(wheelRadius, wheelRadius, 0.14, 16), steelMat ); wheel.rotation.z = Math.PI / 2; wheel.position.set(xWheel, wheelRadius, zAxle); bogie.add(wheel); // Spurkranz (Flange) innen const flange = new THREE.Mesh( new THREE.CylinderGeometry(wheelRadius + 0.045, wheelRadius + 0.045, 0.035, 16), steelMat ); flange.rotation.z = Math.PI / 2; flange.position.set(xWheel + (xWheel < 0 ? 0.075 : -0.075), wheelRadius, zAxle); bogie.add(flange); // Bremsscheibe const disc = new THREE.Mesh( new THREE.CylinderGeometry(wheelRadius * 0.72, wheelRadius * 0.72, 0.04, 12), brakeMat ); disc.rotation.z = Math.PI / 2; disc.position.set(xWheel + (xWheel < 0 ? -0.09 : 0.09), wheelRadius, zAxle); bogie.add(disc); // Achslagergehäuse außen const box = new THREE.Mesh( new THREE.BoxGeometry(0.22, 0.24, 0.26), frameMat ); box.position.set(xWheel + (xWheel < 0 ? -0.14 : 0.14), wheelRadius, zAxle); bogie.add(box); }); }); return bogie; } ``` ```html:Drehgestelle unter Re 460, IC2000-Wagen, Güterwagen & KI-Zügen:swiss_train_game.html this.scene.add(loco); this.locoMesh = loco; // Zwei Drehgestelle unter der Re 460 montieren const locoBogieFront = this.createBogieMesh(0.52, 2.8); const locoBogieRear = this.createBogieMesh(0.52, 2.8); this.scene.add(locoBogieFront, locoBogieRear); this.trainCars.push({ mesh: loco, sOffset: locoLen / 2, length: locoLen, bogies: [ { mesh: locoBogieFront, localOffset: 5.4 }, { mesh: locoBogieRear, localOffset: -5.4 } ] }); // 2. ANGEHÄNGTE WAGEN (Entweder 4 IC2000 Doppelstockwagen oder 5 SBB Cargo Güterwagen) if (this.gameMode === 'express') { const coachLen = 24; for (let c = 0; c < 4; c++) { const coach = new THREE.Group(); const cBody = new THREE.Mesh(new THREE.BoxGeometry(2.9, 3.85, coachLen), sbbWhite); cBody.position.set(0, 2.45, 0); cBody.castShadow = true; coach.add(cBody); const winLower = new THREE.Mesh(new THREE.BoxGeometry(2.94, 0.65, coachLen - 2), sbbDark); winLower.position.set(0, 1.75, 0); const winUpper = new THREE.Mesh(new THREE.BoxGeometry(2.94, 0.65, coachLen - 2), sbbDark); winUpper.position.set(0, 3.35, 0); coach.add(winLower, winUpper); [-1.46, 1.46].forEach(sideX => { const door = new THREE.Mesh(new THREE.BoxGeometry(0.08, 2.1, 1.6), sbbRed); door.position.set(sideX, 1.65, -coachLen / 2 + 4.5); coach.add(door); this.passengerDoors.push({ mesh: door, baseZ: -coachLen / 2 + 4.5 }); }); this.scene.add(coach); const bFront = this.createBogieMesh(0.46, 2.5); const bRear = this.createBogieMesh(0.46, 2.5); this.scene.add(bFront, bRear); const sOffset = locoLen + 1.0 + c * (coachLen + 0.9) + coachLen / 2; this.trainCars.push({ mesh: coach, sOffset, length: coachLen, bogies: [ { mesh: bFront, localOffset: 8.2 }, { mesh: bRear, localOffset: -8.2 } ] }); } } else { // SBB Cargo Güterzug-Komposition (5 abwechslungsreiche Güterwagen) const wagonLen = 15.5; for (let c = 0; c < 5; c++) { const wagon = this.createFreightWagonMesh(c % 3); this.scene.add(wagon); const bFront = this.createBogieMesh(0.44, 1.8); const bRear = this.createBogieMesh(0.44, 1.8); this.scene.add(bFront, bRear); const sOffset = locoLen + 1.0 + c * (wagonLen + 0.8) + wagonLen / 2; this.trainCars.push({ mesh: wagon, sOffset, length: wagonLen, bogies: [ { mesh: bFront, localOffset: 5.0 }, { mesh: bRear, localOffset: -5.0 } ] }); } } } // ------------------------------------------------------------------------ // ENTGEGENKOMMENDE KI-ZÜGE AUF DEM GEGENGLEIS (lateralOffset = +2.4 m) // ------------------------------------------------------------------------ spawnAITrains() { // Vorhandene KI-Züge sauber entfernen bei Szenario-/Streckenwechsel this.aiTrains.forEach(ai => { ai.cars.forEach(c => { this.scene.remove(c.mesh); if (c.bogies) c.bogies.forEach(b => this.scene.remove(b.mesh)); }); }); this.aiTrains = []; const sbbRed = new THREE.MeshLambertMaterial({ color: 0xEB0000 }); const sbbWhite = new THREE.MeshLambertMaterial({ color: 0xF8FAFC }); const headlightMat = new THREE.MeshBasicMaterial({ color: 0xFEF08A }); const pantoMat = new THREE.MeshStandardMaterial({ color: 0x94A3B8, metalness: 0.7, roughness: 0.3 }); const trainCount = this.gameMode === 'branch' ? 5 : 8; const spacing = Math.max(1150, (this.spline.totalLength - 1100) / trainCount); for (let i = 0; i < trainCount; i++) { const cars = []; const locoGroup = new THREE.Group(); const loco = new THREE.Mesh(new THREE.BoxGeometry(2.95, 3.45, 17.5), sbbRed); loco.position.y = 2.2; locoGroup.add(loco); // Spitzenlichter & Stromabnehmer (Pantograf) an jedem entgegenkommenden KI-Zug [-1.0, 1.0].forEach(lx => { const hl = new THREE.Mesh(new THREE.SphereGeometry(0.15, 8, 8), headlightMat); hl.position.set(lx, 1.35, -8.8); locoGroup.add(hl); }); const panto = new THREE.Mesh(new THREE.BoxGeometry(1.5, 1.25, 0.14), pantoMat); panto.position.set(0, 4.55, 4.5); locoGroup.add(panto); this.scene.add(locoGroup); const lBogieA = this.createBogieMesh(0.48, 2.5); const lBogieB = this.createBogieMesh(0.48, 2.5); this.scene.add(lBogieA, lBogieB); cars.push({ mesh: locoGroup, sOffset: 0, bogies: [ { mesh: lBogieA, localOffset: 5.2 }, { mesh: lBogieB, localOffset: -5.2 } ] }); const coachCount = this.gameMode === 'branch' ? 2 : 3; for (let w = 1; w <= coachCount; w++) { const carGroup = new THREE.Group(); const car = new THREE.Mesh(new THREE.BoxGeometry(2.85, 3.7, 22), sbbWhite); car.position.y = 2.4; carGroup.add(car); this.scene.add(carGroup); const cBogieA = this.createBogieMesh(0.45, 2.4); const cBogieB = this.createBogieMesh(0.45, 2.4); this.scene.add(cBogieA, cBogieB); cars.push({ mesh: carGroup, sOffset: w * 23, bogies: [ { mesh: cBogieA, localOffset: 7.5 }, { mesh: cBogieB, localOffset: -7.5 } ] }); } this.aiTrains.push({ s: 780 + i * spacing, speedMs: 26 + (i % 3) * 5.0, maxSpeedMs: 26 + (i % 3) * 5.0, stoppedAtSignal: false, cars }); } } ``` ```html:Drehgestell-Ausrichtung, Weichenzungen & KI-Signalschutz im Simulationsloop:swiss_train_game.html // 3. ALLE WAGEN & DREHGESTELLE ENTLANG DER KURVENSPLINE GELENKIG AUSRICHTEN this.trainCars.forEach(car => { const carS = Math.max(2, this.trainS - car.sOffset); const carLat = this.getLateralOffsetAt(carS); const f = this.spline.getFrameAt(carS, carLat); car.mesh.position.copy(f.pos); const heading = Math.atan2(-f.tan.x, -f.tan.z); const pitch = Math.asin(Math.max(-0.5, Math.min(0.5, f.tan.y))); car.mesh.rotation.order = 'YXZ'; car.mesh.rotation.y = heading; car.mesh.rotation.x = pitch; car.mesh.rotation.z = -f.bank; // Kurvenüberhöhung! // Jedes Drehgestell folgt exakt der Gleisrichtung an seiner eigenen Achskoordinate if (car.bogies) { car.bogies.forEach(b => { const bS = Math.max(1, carS + b.localOffset); const bLat = this.getLateralOffsetAt(bS); const bf = this.spline.getFrameAt(bS, bLat); b.mesh.position.copy(bf.pos); b.mesh.rotation.order = 'YXZ'; b.mesh.rotation.y = Math.atan2(-bf.tan.x, -bf.tan.z); b.mesh.rotation.x = Math.asin(Math.max(-0.5, Math.min(0.5, bf.tan.y))); b.mesh.rotation.z = -bf.bank; }); } }); // Cockpithebel & Türen animieren this.cabThrottleLever.rotation.x = -(this.throttle / 100) * 0.7; this.cabBrakeLever.rotation.x = -(this.brake / 100) * 0.7; this.passengerDoors.forEach(d => { const targetZ = this.doorsOpen ? d.baseZ + 0.85 : d.baseZ; d.mesh.position.z += (targetZ - d.mesh.position.z) * Math.min(1, dt * 6); }); // Bewegliche Weichenzungen (Links = nach links abgelenkt, Rechts = nach rechts abgelenkt, Mitte = gerade) this.switchBlades.forEach(sb => { const stItem = this.stations[sb.stationIndex]; const activeLane = sb.stationIndex === this.currentStationIndex ? this.selectedLane : (stItem ? stItem.targetLane : 1); const targetAngle = activeLane === 0 ? 0.14 : (activeLane === 2 ? -0.14 : 0); sb.group.rotation.y += (targetAngle - sb.group.rotation.y) * Math.min(1, dt * 10); if (sb.bladeL && sb.bladeR) { sb.bladeL.position.x = activeLane === 0 ? -0.56 : -0.72; sb.bladeR.position.x = activeLane === 2 ? 0.56 : 0.72; } if (sb.lantern) { sb.lantern.rotation.y = activeLane === 0 ? Math.PI / 4 : (activeLane === 2 ? -Math.PI / 4 : 0); } }); ``` ```html:Bahnhofssignale & strenge KI-Zugsicherung (kein Durchfahren von Stationen):swiss_train_game.html // 5. BAHNHOFSSIGNALE AKTUALISIEREN & KI-GEGENZÜGE SIGNALGERECHT REGELN // Ein Gegenzug darf NIEMALS einen Bahnhof/Bereich durchfahren, wenn der Spieler dort hält/einfährt // oder das Einfahrtssignal Rot zeigt. const playerInStationOrApproach = st ? (this.trainS >= st.switchStartS - 260 && this.trainS <= st.exitSwitchEndS + 140) : false; this.signals.forEach(sig => { let isRed = false; if (sig.role === 'opposing_entry' && sig.linkedStation) { const sObj = sig.linkedStation; const playerNearThisStation = (this.trainS >= sObj.switchStartS - 220 && this.trainS <= sObj.exitSwitchEndS + 120); const anyAiInside = this.aiTrains.some(other => other.s >= sObj.switchStartS - 40 && other.s <= sObj.exitSwitchEndS + 60 ); isRed = playerNearThisStation || anyAiInside; } sig.lampR.material.color.setHex(isRed ? 0xEF4444 : 0x1E293B); sig.lampG.material.color.setHex(isRed ? 0x1E293B : 0x10B981); sig.lampY.material.color.setHex(0x1E293B); }); this.aiTrains.forEach((ai, aiIdx) => { let mustStop = false; let stopTargetS = -Infinity; // Prüfe alle Stationen & Gegengleis-Einfahrtssignale vor dem KI-Zug (KI fährt von hohem s zu niedrigem s) for (const sObj of this.stations) { const entrySignalS = sObj.exitSwitchEndS + 35; const stationZoneEnd = sObj.switchStartS - 30; const playerAtStation = (this.trainS >= sObj.switchStartS - 240 && this.trainS <= sObj.exitSwitchEndS + 140); // Wenn Spieler im Bahnhof/Weichenbereich ist oder noch im Abschnitt steht: Gegenzug hält vor dem Bahnhofssignal! if (playerAtStation && ai.s > entrySignalS && ai.s - entrySignalS < 340) { mustStop = true; stopTargetS = Math.max(stopTargetS, entrySignalS + 18); } } // Sicherheitsabstand zu anderen KI-Zügen auf dem Gegengleis einhalten for (let j = 0; j < this.aiTrains.length; j++) { if (j === aiIdx) continue; const other = this.aiTrains[j]; if (other.s < ai.s && (ai.s - other.s) < 165) { mustStop = true; stopTargetS = Math.max(stopTargetS, other.s + 135); } } if (mustStop && stopTargetS > -Infinity) { const distToStop = ai.s - stopTargetS; if (distToStop <= 2) { ai.speedMs = 0; ai.stoppedAtSignal = true; } else { const desiredSpeed = Math.min(ai.maxSpeedMs, Math.max(0, (distToStop - 2) * 0.45)); ai.speedMs += (desiredSpeed - ai.speedMs) * Math.min(1, dt * 3.5); } } else { ai.stoppedAtSignal = false; ai.speedMs += (ai.maxSpeedMs - ai.speedMs) * Math.min(1, dt * 1.8); } ai.s -= ai.speedMs * dt; if (ai.s < this.trainS - 340) { const resetS = Math.min(this.spline.totalLength - 140, this.trainS + 1650 + Math.random() * 1400); ai.s = resetS > this.trainS + 350 ? resetS : Math.min(this.spline.totalLength - 140, this.spline.totalLength - 220); ai.speedMs = ai.maxSpeedMs; } ai.cars.forEach(c => { const cs = Math.max(10, Math.min(this.spline.totalLength - 10, ai.s + c.sOffset)); const f = this.spline.getFrameAt(cs, 2.4); c.mesh.position.copy(f.pos); c.mesh.rotation.y = Math.atan2(f.tan.x, f.tan.z); if (c.bogies) { c.bogies.forEach(b => { const bS = Math.max(8, Math.min(this.spline.totalLength - 8, cs + b.localOffset)); const bf = this.spline.getFrameAt(bS, 2.4); b.mesh.position.copy(bf.pos); b.mesh.rotation.y = Math.atan2(bf.tan.x, bf.tan.z); }); } }); }); // 6. KAMERA & HUD AKTUALISIEREN ``` ```html:Doppelte startGame-Methode bereinigen:swiss_train_game.html startGame() { audio.init(); this.toggleHelp(false); const modeLabels = { freight: 'SBB Cargo Güterzug', yard_start: 'Rangierbahnhof Limmattal RBL Start', branch: 'S9 Nebenlinie Aare-Seetal', express: 'IC 1 Express' }; const label = (sim && modeLabels[sim.gameMode]) || 'IC 1 Express'; ui.showToast(`🚂 ${label} abfahrbereit! Bremse lösen und mit W Schub geben.`, 'info'); }, showToast(msg, type = 'info') {