Skip to content

Commit

Permalink
feat: add randomSound method to Group class
Browse files Browse the repository at this point in the history
  • Loading branch information
ctoth committed Aug 21, 2024
1 parent 21c28b9 commit 3f6739a
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,19 @@ export class Group implements BaseSound {
this.sounds.push(sound);
}

/**
* Returns a random sound from the group.
* @returns A random Sound object from the group.
* @throws Error if the group is empty.
*/
randomSound(): Sound {
if (this.sounds.length === 0) {
throw new Error("Cannot get a random sound from an empty group");
}
const randomIndex = Math.floor(Math.random() * this.sounds.length);
return this.sounds[randomIndex];
}

preplay(): Playback[] {
return this.sounds.reduce<Playback[]>((playbacks, sound) => {
sound.loop && sound.loop(this.loopCount);
Expand Down

0 comments on commit 3f6739a

Please sign in to comment.