This repository has been archived by the owner on Sep 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SoundManager.cpp
125 lines (103 loc) · 2.87 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "SoundManager.h"
#include <SFML/Audio.hpp>
using namespace sf;
SoundManager::SoundManager()
{
int constexpr SOUND_NUMBER = 7;
// Load sounds to buffers
_fireBuffer.loadFromFile("sound/fire1.wav");
_fallInFireBuffer.loadFromFile("sound/fallinfire.wav");
_fallInWaterBuffer.loadFromFile("sound/fallinwater.wav");
_jumpBuffer.loadFromFile("sound/jump.wav");
_reachGoalBuffer.loadFromFile("sound/reachgoal.wav");
// Assign buffers to sounds
_fireSound1.setBuffer(_fireBuffer);
_fireSound2.setBuffer(_fireBuffer);
_fireSound3.setBuffer(_fireBuffer);
_fallInFireSound.setBuffer(_fallInFireBuffer);
_fallInWaterSound.setBuffer(_fallInWaterBuffer);
_jumpSound.setBuffer(_jumpBuffer);
_reachGoalSound.setBuffer(_reachGoalBuffer);
/*
const SoundBuffer buffers[SOUND_NUMBER] = {_fireBuffer, _fireBuffer, _fireBuffer,
_fallInFireBuffer, _fallInWaterBuffer, _jumpBuffer, _reachGoalBuffer};
Sound sounds[SOUND_NUMBER] = {_fireSound1,_fireSound2,_fireSound3,
_fallInFireSound,_jumpSound,_reachGoalSound};
for (int x = 0; x < SOUND_NUMBER; x++)
sounds[x].setBuffer(buffers[x]);
*/
const float minDistance = 150;
const float attenuation = 15;
/*
for (int x = 0; x < 3; x++)
{
sounds[x].setAttenuation(attenuation);
sounds[x].setMinDistance(minDistance);
sounds[x].setLoop(true);
}
*/
_fireSound1.setAttenuation(attenuation);
_fireSound1.setMinDistance(minDistance);
_fireSound1.setLoop(true);
_fireSound2.setAttenuation(attenuation);
_fireSound2.setMinDistance(minDistance);
_fireSound2.setLoop(true);
_fireSound3.setAttenuation(attenuation);
_fireSound3.setMinDistance(minDistance);
_fireSound3.setLoop(true);
}
void SoundManager::playFire(Vector2f emitterLocation, Vector2f listenerLocation)
{
Listener::setPosition(listenerLocation.x, listenerLocation.y, 0.0f);
// Move the source of the sounds
switch (_nextSound)
{
case 1:
_fireSound1.setPosition(emitterLocation.x, emitterLocation.y, 0.0f);
if (_fireSound1.getStatus() == Sound::Status::Stopped)
{
_fireSound1.play();
}
break;
case 2:
_fireSound2.setPosition(emitterLocation.x, emitterLocation.y, 0.0f);
if (_fireSound2.getStatus() == Sound::Status::Stopped)
{
_fireSound2.play();
}
break;
case 3:
_fireSound3.setPosition(emitterLocation.x, emitterLocation.y, 0.0f);
if (_fireSound3.getStatus() == Sound::Status::Stopped)
{
_fireSound3.play();
}
break;
default: break;
}
_nextSound++;
if (_nextSound > 3)
{
_nextSound = 1;
}
}
void SoundManager::playFallInFire()
{
_fallInFireSound.setRelativeToListener(true);
_fallInFireSound.play();
}
void SoundManager::playFallInWater()
{
_fallInWaterSound.setRelativeToListener(true);
_fallInWaterSound.play();
}
void SoundManager::playJump()
{
_jumpSound.setRelativeToListener(true);
_jumpSound.play();
}
void SoundManager::playReachGoal()
{
_reachGoalSound.setRelativeToListener(true);
_reachGoalSound.play();
}