// // SoundsRepo.cpp // WattsenglishToyApp-mobile // // Created by Katarzyna Kalinowska-Górska on 26/11/2019. // #include #include "SoundsRepo.h" #include "MathUtils.h" #include "json/document.h" static float SOUND_EFFECTS_VOLUME = 0.75; std::string SoundsRepo::musicBgFilePath = "common/sounds/menu_bg_music.mp3"; std::string SoundsRepo::soundsFolder = "common/sounds/"; std::map SoundsRepo::soundDurations = std::map(); int SoundsRepo::currentMusicSoundId = -1; const char * SoundsRepo::SHOOT_GAME_SOUND_INTRO = "common/sounds/games/game_shoot/start.mp3"; const char * SoundsRepo::SHOOT_GAME_SOUND_ALL_IN_TROLLEY = "common/sounds/games/game_shoot/all_in_trolley.mp3"; const char * SoundsRepo::SHOOT_GAME_SOUND_SHOP_CLOSED = "common/sounds/games/game_shoot/shop_closed.mp3"; const char * SoundsRepo::SHOOT_GAME_SOUND_WRONG_THREE_TIMES = "common/sounds/games/game_shoot/wrong_3_times.mp3"; const char* SoundsRepo::SHOOT_GAME_SOUND_EFFECT_CATAPULT = "common/sounds/games/game_shoot/effect_catapult.mp3"; const char* SoundsRepo::SHOOT_GAME_SOUND_EFFECT_IN_TROLLEY = "common/sounds/games/game_shoot/effect_in_trolley.mp3"; const char* SoundsRepo::SHOOT_GAME_SOUND_EFFECT_SPLAT = "common/sounds/games/game_shoot/effect_hit.mp3"; const char* SoundsRepo::SHOOT_GAME_SOUND_IN_TROLLEY = "common/sounds/games/game_shoot/in_trolley.mp3"; const char* SoundsRepo::SHOOT_GAME_SOUND_NOT_IN_TROLLEY = "common/sounds/games/game_shoot/not_in_trolley.mp3"; const char* SoundsRepo::SHOOT_GAME_SOUND_HIT_STEVE = "common/sounds/games/game_shoot/hit_steve.mp3"; const char* SoundsRepo::SHOOT_GAME_SOUND_HIT_MAGGIE = "common/sounds/games/game_shoot/hit_maggie.mp3"; const char * SoundsRepo::MAP_GAME_SOUND_INTRO = "common/sounds/games/game_map/intro.mp3"; const char * SoundsRepo::MAP_GAME_SOUND_ALL_DONE = "common/sounds/games/game_map/full_tummy.mp3"; void SoundsRepo::preloadAllShootGameSounds(){ preloadSoundEffect(SHOOT_GAME_SOUND_ALL_IN_TROLLEY); preloadSoundEffect(SHOOT_GAME_SOUND_SHOP_CLOSED); preloadSoundEffect(SHOOT_GAME_SOUND_WRONG_THREE_TIMES); preloadSoundEffect(SHOOT_GAME_SOUND_EFFECT_CATAPULT); preloadSoundEffect(SHOOT_GAME_SOUND_EFFECT_IN_TROLLEY); preloadSoundEffect(SHOOT_GAME_SOUND_EFFECT_SPLAT); preloadSoundEffect(SHOOT_GAME_SOUND_IN_TROLLEY); preloadSoundEffect(SHOOT_GAME_SOUND_NOT_IN_TROLLEY); preloadSoundEffect(SHOOT_GAME_SOUND_HIT_MAGGIE); preloadSoundEffect(SHOOT_GAME_SOUND_HIT_STEVE); } SoundsRepo::SoundInfo SoundsRepo::gameTNCIntroSound(){ auto fp = soundsFolder + "games/g_tnc_intro.mp3"; return SoundInfo(fp, soundDuration(fp)); } SoundsRepo::SoundInfo SoundsRepo::gameLetsStartIntroSound(){ auto fp = soundsFolder + "games/g_letsstart.mp3"; return SoundInfo(fp, soundDuration(fp)); } SoundsRepo::SoundInfo SoundsRepo::hooraySound(){ std::vector hooraySounds = {/*"games/g_yes.mp3",*/ "../g_whoo_hoo.mp3", "../g_yeah.mp3", "../g_well_done.mp3", "../maggie_thats_right.mp3", "../maggie_yeah.mp3", "../maggie_super.mp3"}; auto soundFilename = MathUtils::getRandomElement(hooraySounds); auto fp = soundsFolder + soundFilename; return SoundInfo(fp, soundDuration(fp)); } SoundsRepo::SoundInfo SoundsRepo::oopsSound(){ std::vector oopsSounds = {"games/g_oops.mp3", "games/g_no.mp3", "games/g_uh_oh.mp3"}; auto soundFilename = MathUtils::getRandomElement(oopsSounds); auto fp = soundsFolder + soundFilename; return SoundInfo(fp, soundDuration(fp)); } SoundsRepo::SoundInfo SoundsRepo::pureOopsSound(){ std::vector oopsSounds = {"games/g_oops.mp3", "games/g_uh_oh.mp3"}; auto soundFilename = MathUtils::getRandomElement(oopsSounds); auto fp = soundsFolder + soundFilename; return SoundInfo(fp, soundDuration(fp)); } SoundsRepo::SoundInfo SoundsRepo::noSound(){ auto fp = soundsFolder + "games/g_no.mp3"; return SoundInfo(fp, soundDuration(fp)); } SoundsRepo::SoundInfo SoundsRepo::pickLevelSound(){ auto fp = soundsFolder + "../../level_picking/g_pick_level.mp3"; return SoundInfo(fp, soundDuration(fp)); } SoundsRepo::SoundInfo SoundsRepo::changeLevelSound(){ auto fp = soundsFolder + "games/g_next_level.mp3"; return SoundInfo(fp, soundDuration(fp)); } //SoundsRepo::SoundInfo SoundsRepo::gameFinishedSound(){ // auto fp = soundsFolder + "games/g_finished.mp3"; // return SoundInfo(fp, soundDuration(fp)); //} // //SoundsRepo::SoundInfo SoundsRepo::scoreSound(){ // auto fp = soundsFolder + "games/g_your_score.mp3"; // return SoundInfo(fp, soundDuration(fp)); //} SoundsRepo::SoundInfo SoundsRepo::hurryUpSound(){ auto fp = soundsFolder + "games/g_hurry_up.mp3"; return SoundInfo(fp, soundDuration(fp)); } //SoundsRepo::SoundInfo SoundsRepo::tooSlowSound(){ // std::vector hooraySounds = {"games/g_slow.mp3", "games/g_slow2.mp3"}; // auto soundFilename = MathUtils::getRandomElement(hooraySounds); // auto fp = soundsFolder + soundFilename; // return SoundInfo(fp, soundDuration(fp)); //} //SoundsRepo::SoundInfo SoundsRepo::wrongThreeTimesSound(){ // auto fp = soundsFolder + "games/g_wrong_3_times.mp3"; // return SoundInfo(fp, soundDuration(fp)); //} SoundsRepo::SoundInfo SoundsRepo::typeConfirmSound(int game, int itemType){ std::string soundPath = soundsFolder; switch(itemType){ case 1: soundPath += "games/game" + std::to_string(game) + "/g_conf_1.mp3"; break; case 2: soundPath += "games/game" + std::to_string(game) + "/g_conf_2.mp3"; break; case 3: soundPath += "games/game" + std::to_string(game) + "/g_conf_3.mp3"; break; case 4: soundPath += "games/game" + std::to_string(game) + "/g_conf_4.mp3"; break; default: soundPath = ""; break; } return SoundInfo(soundPath, soundDurations[soundPath]); } SoundsRepo::SoundInfo SoundsRepo::typeRequestSound(int game, int itemType){ std::string soundPath = soundsFolder; switch(itemType){ case 1: soundPath += "games/game" + std::to_string(game) + "/g_request_1.mp3"; break; case 2: soundPath += "games/game" + std::to_string(game) + "/g_request_2.mp3"; break; case 3: soundPath += "games/game" + std::to_string(game) + "/g_request_3.mp3"; break; case 4: soundPath += "games/game" + std::to_string(game) + "/g_request_4.mp3"; break; default: soundPath = ""; break; } return SoundInfo(soundPath, soundDurations[soundPath]); } void SoundsRepo::stopAllSounds(){ cocos2d::AudioEngine::stopAll(); } void SoundsRepo::stopSoundById(int audioId){ cocos2d::AudioEngine::stop(audioId); } int SoundsRepo::playSound(const std::string soundFilepath, bool stopOtherEffects){ if(stopOtherEffects){ cocos2d::AudioEngine::stopAll(); } auto soundId = cocos2d::AudioEngine::play2d(soundFilepath); // cocos2d::AudioEngine::setVolume(soundId, SOUND_EFFECTS_VOLUME); return soundId; //return 0; } int SoundsRepo::playMusic(const std::string musicFilepath){ currentMusicSoundId = cocos2d::AudioEngine::play2d(musicFilepath, true); return currentMusicSoundId; } void SoundsRepo::stopMusic(){ cocos2d::AudioEngine::stop(currentMusicSoundId); } float SoundsRepo::soundDuration(const std::string soundFilepath){ if(soundDurations.find(soundFilepath) != soundDurations.end()){ return soundDurations[soundFilepath]; } else { return 0; } } void SoundsRepo::preloadSoundEffect(std::string soundPath){ cocos2d::AudioEngine::preload(soundPath); } void SoundsRepo::preloadAllLoadedSoundEffects(){ for(auto soundIt = soundDurations.begin(); soundIt != soundDurations.end(); soundIt++){ std::string fullSoundPath = /*commonConfig.soundConfig.soundsFolder + */soundIt->first; // soundEngine->preloadEffect(fullSoundPath.c_str()); cocos2d::AudioEngine::preload(fullSoundPath); } } void SoundsRepo::pauseAllSounds(){ cocos2d::AudioEngine::pauseAll(); } void SoundsRepo::resumeAllSounds(){ cocos2d::AudioEngine::resumeAll(); } void SoundsRepo::loadSoundsList(){ parseSoundInfoFile(SoundsRepo::soundsFolder, SoundsRepo::soundsFolder + "sounds_info.si", SoundsRepo::soundDurations); } void SoundsRepo::parseSoundInfoFile(std::string soundFolderPath, std::string filePath, std::map& mapToFill){ if(cocos2d::FileUtils::getInstance()->isFileExist(filePath)){ auto soundInfoJson = new rapidjson::Document(); auto jsonString = cocos2d::FileUtils::getInstance()->getStringFromFile(filePath); soundInfoJson->Parse(jsonString.c_str()); auto soundsList = (*soundInfoJson)["sounds"].GetArray(); for(const auto& sound : soundsList){ auto soundFilename = sound["filename"].GetString(); auto soundDuration = sound["duration_seconds"].GetFloat(); mapToFill.insert(std::make_pair(soundFolderPath + soundFilename, soundDuration)); } } }