The open source repository for the action RPG game in development by Sig Productions titled 'Adventures in Lestoria'!
https://forums.lestoria.net
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
157 lines
6.1 KiB
157 lines
6.1 KiB
#pragma region License
|
|
/*
|
|
License (OLC-3)
|
|
~~~~~~~~~~~~~~~
|
|
|
|
Copyright 2024 Joshua Sigona <sigonasr2@gmail.com>
|
|
|
|
Redistribution and use in source and binary forms, with or without modification,
|
|
are permitted provided that the following conditions are met:
|
|
|
|
1. Redistributions or derivations of source code must retain the above copyright
|
|
notice, this list of conditions and the following disclaimer.
|
|
|
|
2. Redistributions or derivative works in binary form must reproduce the above
|
|
copyright notice. This list of conditions and the following disclaimer must be
|
|
reproduced in the documentation and/or other materials provided with the distribution.
|
|
|
|
3. Neither the name of the copyright holder nor the names of its contributors may
|
|
be used to endorse or promote products derived from this software without specific
|
|
prior written permission.
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
|
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
|
|
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
SUCH DAMAGE.
|
|
|
|
Portions of this software are copyright © 2024 The FreeType
|
|
Project (www.freetype.org). Please see LICENSE_FT.txt for more information.
|
|
All rights reserved.
|
|
*/
|
|
#pragma endregion
|
|
|
|
#include "olcUTIL_DataFile.h"
|
|
#include "SoundEffect.h"
|
|
#include "Audio.h"
|
|
#include "util.h"
|
|
#include "DEFINES.h"
|
|
#include "AdventuresInLestoria.h"
|
|
|
|
INCLUDE_DATA
|
|
INCLUDE_game
|
|
|
|
std::multimap<EventName,SoundEffect>SoundEffect::SOUND_EFFECTS;
|
|
const vf2d SoundEffect::CENTERED={-8419.f,-3289.f};
|
|
std::unordered_set<size_t>RepeatingSoundEffect::playingSoundEffects;
|
|
|
|
void RepeatingSoundEffect::StopAllSounds(){
|
|
for(auto&id:playingSoundEffects){
|
|
Audio::Engine().Stop(id);
|
|
Audio::Engine().UnloadSound(id);
|
|
}
|
|
playingSoundEffects.clear();
|
|
}
|
|
|
|
SoundEffect::SoundEffect(const std::string_view filename,const float&vol,const float&minPitch,const float&maxPitch,const bool combatSound,const bool treatAsBGM)
|
|
:filename(filename),vol(vol),minPitch(minPitch),maxPitch(maxPitch),combatSound(combatSound),treatAsBGM(treatAsBGM){
|
|
if(vol<0.f||vol>1.f)ERR(std::format("WARNING! Volume must be between 0.0f ~ 1.0f! Provided value {}",vol));
|
|
}
|
|
|
|
void SoundEffect::Initialize(){
|
|
for(auto&[key,size]:DATA["Events"]["SFX"]){
|
|
int counter=0;
|
|
bool combatSound=false;
|
|
bool treatAsBGM{false};
|
|
if(DATA["Events"]["SFX"][key].HasProperty("CombatSound")){
|
|
combatSound=DATA["Events"]["SFX"][key]["CombatSound"].GetBool();
|
|
}
|
|
if(DATA["Events"]["SFX"][key].HasProperty("Treat as BGM")){
|
|
treatAsBGM=DATA["Events"]["SFX"][key]["Treat as BGM"].GetBool();
|
|
}
|
|
while(DATA["Events"]["SFX"][key].HasProperty(std::format("File[{}]",counter))){
|
|
utils::datafile&data=DATA["Events"]["SFX"][key][std::format("File[{}]",counter)];
|
|
float minPitch=0.9f;
|
|
float maxPitch=1.1f;
|
|
if(data.GetValueCount()>=3){minPitch=data.GetInt(2)/100.f;}
|
|
if(data.GetValueCount()>=4){maxPitch=data.GetInt(3)/100.f;}
|
|
SOUND_EFFECTS.insert({key,SoundEffect{data.GetString(0),data.GetInt(1)/100.f,minPitch,maxPitch,combatSound,treatAsBGM}});
|
|
counter++;
|
|
}
|
|
auto itr=SOUND_EFFECTS.equal_range(key);
|
|
for(auto it=itr.first;it!=itr.second;++it){
|
|
it->second.combatSound=combatSound;
|
|
}
|
|
}
|
|
}
|
|
|
|
void SoundEffect::PlaySFX(const std::string&eventName,const vf2d&pos){
|
|
if(game->TestingModeEnabled()||eventName.length()==0)return;
|
|
const SoundEffect&sfx=GetRandomSFXFromFile(eventName);
|
|
|
|
if(GameState::STATE==GameState::states[States::MAIN_MENU]&&sfx.combatSound)return; //Do not play combat sounds on the main menu.
|
|
|
|
float pitchDiff=sfx.maxPitch-sfx.minPitch;
|
|
float pitch=util::random(pitchDiff)+sfx.minPitch;
|
|
|
|
if(pos==CENTERED){
|
|
float vol{Audio::GetCalculatedSFXVolume(sfx)};
|
|
Audio::Engine().Play(operator""_SFX(sfx.filename.c_str(),sfx.filename.length()),vol,0.0f,pitch);
|
|
}else{
|
|
const float soundActivationRange="Audio.Environmental Audio Activation Range"_F;
|
|
|
|
float distanceFromPlayer=geom2d::line<float>(game->GetPlayer()->GetPos(),pos).length();
|
|
if(distanceFromPlayer<soundActivationRange){
|
|
float distRatio=1-distanceFromPlayer/soundActivationRange; //0-1 where 1 is full volume.
|
|
float xDistRatio=(pos.x-game->GetPlayer()->GetX())/soundActivationRange; //0-1 where 1 is full volume.
|
|
|
|
float vol=distRatio*Audio::GetCalculatedSFXVolume(sfx);
|
|
float pan=xDistRatio;
|
|
Audio::Engine().Play(operator""_SFX(sfx.filename.c_str(),sfx.filename.length()),vol,pan,pitch);
|
|
}
|
|
}
|
|
}
|
|
|
|
size_t SoundEffect::PlayLoopingSFX(const std::string&eventName,const vf2d&pos){
|
|
if(game->TestingModeEnabled()||eventName.length()==0)return 0U;
|
|
const SoundEffect&sfx=GetRandomSFXFromFile(eventName);
|
|
const size_t id=Audio::Engine().LoadSound(operator""_SFX(sfx.filename.c_str(),sfx.filename.length()),MiniAudio::SFX);
|
|
RepeatingSoundEffect::playingSoundEffects.insert(id);
|
|
Audio::Engine().Play(id,true);
|
|
return id;
|
|
}
|
|
|
|
void SoundEffect::StopLoopingSFX(const int id){
|
|
if(!RepeatingSoundEffect::playingSoundEffects.count(id))return; //Prevent crashes from stopping a sound more than once. Called by accident probably.
|
|
Audio::Engine().Stop(id);
|
|
Audio::Engine().UnloadSound(id);
|
|
RepeatingSoundEffect::playingSoundEffects.erase(id);
|
|
}
|
|
|
|
SoundEffect&SoundEffect::GetRandomSFXFromFile(const std::string&eventName){
|
|
auto itr=SOUND_EFFECTS.equal_range(eventName);
|
|
size_t soundCount=std::distance(itr.first,itr.second);
|
|
if(soundCount==0)ERR("WARNING! Sound Effect "<<std::quoted(eventName)<<" does not have any sound effects loaded/doesn't exist!")
|
|
size_t soundEffectChoice=util::random()%soundCount;
|
|
|
|
int counter=0;
|
|
auto it=itr.first;
|
|
while(counter!=soundEffectChoice){
|
|
++counter;
|
|
++it;
|
|
}
|
|
|
|
return (*it).second;
|
|
}
|
|
|
|
const float&SoundEffect::GetVolume()const{
|
|
return vol;
|
|
}
|
|
const bool&SoundEffect::TreatAsBPM()const{
|
|
return treatAsBGM;
|
|
} |