Almost at the release, and that persistent sickness that haunted me last week is now gone, which means my productivity increased as well.
This week I’ve mostly been working with the SoundManager (or more accurately: the SoundEntity and MusicEntity), the version we had was working up ti’ll Beta, but in the final, we need a listener for the sound, otherwise the sound of the guards steps would be heard from anywhere on the map, that would just be…. strange.
Now to the problems that needed solving; we needed a position for the sound for the listener to work (I will go deeper into this soon). A minimum distance of the sound as well as the attenuation (the rate at which the sound fall off past the minimum distance). I also needed a way to see if music was muted (the music would play even if it was muted in the menu), so I created a MusicEntity that worked similar to the original SoundEntity. And a listener, being the players ears.
The first problem couldn’t be solved without revising most of the original SoundEntity. As the SoundEntity originally held both the buffer and the sf::Sound, and we would only allow one SoundEntity of the same type to be loaded into memory, this meant a sound could only have one position as well, the result; ”teleporting” sound for EACH step a guard took, it would then jump all over the place. To solve this, I changed it so the SoundEntity held the buffer only, and contained a function to create a new sf::Sound linked to the buffer, so that new steps would create a new sf::Sound with the same buffer. Each holding their own position.
The second problem is the minimum distance and attenuation; a buffer can’t hold these values, so I had it stored in two variables in SoundEntity. This led to the CreateSound() function (originally it just returned the buffer to use on your own), that set those values before passing on the created sf::Sound. It takes one argument, a sf::Vector2f, being the new sounds position.
The third problem was the absence of control over the music, I created a MusicEntity to help with that, it did most things that SoundEntity did before the revision; holding a static bool telling us if music is muted or not. A new static volume variable to give a consistency of the volume. A Play, Stop and Pause function, doing little less than calling the sf:Music’s respective functions, except Play which check if it’s muted before playing. And finally functions to set and get volume as well as switching between muted and un-muted, and a bool check if it is muted.
The last problem was the listener, I had to decide where to put the listener, and finally decided to add it to the SoundManager class, it being closely related to each other. We also pondered wether we should use targeted listener (i.e. directional sound), but decided against it, as it would feel odd to hear the sound coming from the right speaker when the cause of the sound is on the left side of the screen (should you go down). The SoundManager was largely untouched except for the addition of the listener as well as a Toggle/Mute sound and music function, and some minor changes inside the functions (mostly name changes). The listener’s position is changed in the Update() function, now taking an argument, the players position.
I’m probably well over 400 words now, but I still need a picture, I’ll add a (hopefully) descriptive image:
The sounds position is at the footstep (where it’s created), it is heard at full volume until the first circle, and gradually gets weaker as it reaches the second circle (actual distance and rate of fall-off can be adjusted), the sound can’t be heard past the second circle. The listener (the player in this and most cases) is within the second circle, but past the first, which means it would be heard, but not at full volume, giving the impression that it’s far away.