Slash-The-Hordes/assets/Scripts/Services/AudioPlayer/AudioPlayer.ts

40 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

2022-12-20 09:00:47 +00:00
import { AudioClip, AudioSource, Component, _decorator } from "cc";
const { ccclass, property } = _decorator;
@ccclass("AudioPlayer")
export class AudioPlayer extends Component {
@property(AudioSource) private soundSource: AudioSource;
@property(AudioSource) private musicSource: AudioSource;
public init(soundVolume: number, musicVolume: number): void {
this.setSoundVolume(soundVolume);
this.setMusicVolume(musicVolume);
}
2022-12-21 13:08:49 +00:00
public get SoundVolume(): number {
return this.soundSource.volume;
}
public get MusicVolume(): number {
return this.musicSource.volume;
}
2022-12-20 09:00:47 +00:00
public setSoundVolume(volume: number): void {
this.soundSource.volume = volume;
}
public setMusicVolume(volume: number): void {
this.musicSource.volume = volume;
}
public playSound(clip: AudioClip): void {
this.soundSource.playOneShot(clip);
}
public playMusic(clip: AudioClip): void {
2022-12-22 13:05:31 +00:00
this.musicSource.stop();
2022-12-20 09:00:47 +00:00
this.musicSource.clip = clip;
this.musicSource.play();
}
}