pinball/assets/_Game/Scripts/Environments/SequenceSound.ts

33 lines
1.0 KiB
TypeScript

import { _decorator, AudioClip, CCFloat, Component, Node } from 'cc';
import AudioManager from '../Manager/AudioManager';
const { ccclass, property } = _decorator;
@ccclass('SequenceSound')
export class SequenceSound extends Component {
@property({ type: CCFloat, visible: true })
private _duration: number = 0.5;
@property({ type: AudioClip, visible: true })
private _audioClips: AudioClip[] = [];
private _currentIndex = 0;
private _timer = 0;
protected update(dt: number): void {
if (this._timer > 0) {
this._timer -= dt;
}
}
public playSound(volume: number = 1) {
if (this._timer > 0) {
AudioManager.playSfx(this._audioClips[this._currentIndex], { volume });
this._currentIndex++;
if (this._currentIndex >= this._audioClips.length) this._currentIndex = 0;
} else {
this._currentIndex = 0;
AudioManager.playSfx(this._audioClips[this._currentIndex], { volume });
}
this._timer = this._duration;
}
}