105 lines
4.2 KiB
C++
105 lines
4.2 KiB
C++
#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 <20> 2023 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};
|
||
|
||
SoundEffect::SoundEffect(const std::string_view filename,const float&vol,const float&minPitch,const float&maxPitch)
|
||
:filename(filename),vol(vol),minPitch(minPitch),maxPitch(maxPitch){
|
||
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;
|
||
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}});
|
||
counter++;
|
||
}
|
||
}
|
||
}
|
||
|
||
void SoundEffect::PlaySFX(const std::string_view eventName,const vf2d&pos){
|
||
if(eventName.length()==0)return;
|
||
auto itr=SOUND_EFFECTS.equal_range(std::string(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;
|
||
}
|
||
const SoundEffect&sfx=(*it).second;
|
||
|
||
float pitchDiff=sfx.maxPitch-sfx.minPitch;
|
||
float pitch=util::random(pitchDiff)+sfx.minPitch;
|
||
|
||
if(pos==CENTERED){
|
||
Audio::Engine().Play(operator""_SFX(sfx.filename.c_str(),sfx.filename.length()),sfx.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*sfx.vol;
|
||
float pan=xDistRatio;
|
||
Audio::Engine().Play(operator""_SFX(sfx.filename.c_str(),sfx.filename.length()),vol,pan,pitch);
|
||
}
|
||
}
|
||
} |