-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSoundManager.cpp
57 lines (48 loc) · 2.07 KB
/
SoundManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Author: Annie Berend (5033782) - Jonathan Verbeek (5058288)
#include "SoundManager.h"
#include <QRandomGenerator>
#include <QDebug>
#include <QSound>
#include <QMediaPlaylist>
CSoundManager::CSoundManager(QObject *parent)
: QObject(parent)
, bgMusicPlayer(nullptr)
, enableSfx(true)
{
// Populate the paths for the sound effects
soundFiles[ESoundEffectType::CardClick] = {":/assets/card_01.wav", ":/assets/card_02.wav", ":/assets/card_03.wav", ":/assets/card_04.wav"};
soundFiles[ESoundEffectType::CardFlip] = {":/assets/card_flip.wav"};
soundFiles[ESoundEffectType::CardStack] = {":/assets/card_stack_01.wav", ":/assets/card_stack_02.wav", ":/assets/card_stack_03.wav", ":/assets/card_stack_04.wav", ":/assets/card_stack_05.wav"};
soundFiles[ESoundEffectType::CardCannotMove] = {":/assets/card_cant_move.wav"};
soundFiles[ESoundEffectType::Win] = {":/assets/win.wav"};
// Create a media playlist containing the background music on a loop
QMediaPlaylist* musicPlaylist = new QMediaPlaylist();
musicPlaylist->addMedia(QUrl("qrc:/assets/background_music.mp3"));
musicPlaylist->setPlaybackMode(QMediaPlaylist::Loop);
// Create the background music player
this->bgMusicPlayer = new QMediaPlayer(this);
this->bgMusicPlayer->setPlaylist(musicPlaylist);
this->bgMusicPlayer->setVolume(20);
this->bgMusicPlayer->play();
}
void CSoundManager::playSoundEffect(ESoundEffectType sfxType)
{
// Don't if SFX are disabled
if (!enableSfx) return;
// Get the sfx files for this type
QVector<QString> effects = soundFiles[sfxType];
// Randomize which sound to use
int soundIndex = effects.length() > 1 ? QRandomGenerator::global()->bounded(0, effects.length() - 1) : 0;
// Play the sound effect
QSound::play(effects[soundIndex]);
}
void CSoundManager::setEnableSoundEffects(bool enabled)
{
// Set the flag
this->enableSfx = enabled;
}
void CSoundManager::setEnableMusic(bool enabled)
{
// Set the music's volume so it won't completely stop when disabled
this->bgMusicPlayer->setVolume(enabled ? 50 : 0);
}