Änderung: Hinzufügung der MotorSound-Klasse und Integration in das Taxi-Spiel

Änderungen:
- Implementierung eines neuen AudioWorklet-Prozessors für realistische Motorgeräusche.
- Erstellung der MotorSound-Klasse zur Verwaltung von motorgeräuschabhängigen Audioeffekten.
- Integration des MotorSounds in die TaxiGame.vue, einschließlich der Initialisierung und Steuerung basierend auf der Geschwindigkeit des Taxis.
- Anpassung der Audio-Parameter wie Lautstärke und Geschwindigkeit in Abhängigkeit von der Fahrzeuggeschwindigkeit.

Diese Anpassungen verbessern die akustische Benutzererfahrung im Taxi-Minispiel erheblich und tragen zur Immersion bei.
This commit is contained in:
Torsten Schulz (local)
2025-09-17 10:00:00 +02:00
parent a19e255ca7
commit 37174c7237
3 changed files with 300 additions and 3 deletions

View File

@@ -170,6 +170,7 @@
<script>
import streetCoordinates from '../../utils/streetCoordinates.js';
import { MotorSound, NoiseGenerator } from '../../assets/motorSound.js';
import apiClient from '../../utils/axios.js';
import StatusBar from '../../components/falukant/StatusBar.vue';
@@ -224,16 +225,19 @@ export default {
passengers: [],
destinations: [],
obstacles: [],
keys: {}
keys: {},
motorSound: null,
audioContext: null
}
},
mounted() {
async mounted() {
this.initializeGame();
this.initializeMinimap();
this.loadTiles();
this.loadTaxiImage();
this.loadMaps();
this.setupEventListeners();
await this.initializeMotorSound();
},
beforeUnmount() {
this.cleanup();
@@ -304,10 +308,58 @@ export default {
document.addEventListener('keyup', this.handleKeyUp);
},
async initializeMotorSound() {
// AudioContext wird erst bei erster Benutzerinteraktion erstellt
console.log('MotorSound wird bei erster Benutzerinteraktion initialisiert');
},
async initAudioOnUserInteraction() {
if (this.audioContext) return; // Bereits initialisiert
try {
this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
const generator = new NoiseGenerator();
this.motorSound = new MotorSound(this.audioContext, generator);
await this.motorSound.init();
console.log('MotorSound initialisiert');
} catch (error) {
console.warn('MotorSound konnte nicht initialisiert werden:', error);
}
},
updateMotorSound() {
if (!this.motorSound) return;
const speedKmh = this.taxi.speed * 5; // Geschwindigkeit in km/h
const isMoving = speedKmh > 0;
if (isMoving && !this.motorSound.isPlaying) {
this.motorSound.start();
} else if (!isMoving && this.motorSound.isPlaying) {
this.motorSound.stop();
}
if (isMoving) {
// Geschwindigkeitsabhängige Tonhöhe und Lautstärke
const speedFactor = Math.min(speedKmh / 120, 1); // 0-1 basierend auf 0-120 km/h
const motorSpeed = 0.3 + (speedFactor * 1.2); // 0.3 bis 1.5
const volume = 0.1 + (speedFactor * 0.4); // 0.1 bis 0.5
this.motorSound.setSpeed(motorSpeed);
this.motorSound.setVolume(volume);
}
},
cleanup() {
if (this.gameLoop) {
cancelAnimationFrame(this.gameLoop);
}
if (this.motorSound) {
this.motorSound.stop();
}
if (this.audioContext) {
this.audioContext.close();
}
document.removeEventListener('keydown', this.handleKeyDown);
document.removeEventListener('keyup', this.handleKeyUp);
},
@@ -495,6 +547,9 @@ export default {
if (Math.abs(this.taxi.speed) > 0.1) {
this.fuel = Math.max(0, this.fuel - 0.01);
}
// Motorgeräusch aktualisieren
this.updateMotorSound();
},
handlePassengerActions() {
@@ -940,8 +995,12 @@ export default {
// Canvas-Klick-Handling falls benötigt
},
handleKeyDown(event) {
async handleKeyDown(event) {
this.keys[event.key] = true;
// AudioContext bei erster Benutzerinteraktion initialisieren
await this.initAudioOnUserInteraction();
event.preventDefault();
},