From d269b360ce5b32dc16f0c0fd4d007a3f5ac53ad3 Mon Sep 17 00:00:00 2001 From: Modnark <66146584+Modnark@users.noreply.github.com> Date: Tue, 11 Oct 2022 21:16:14 -0400 Subject: [PATCH] SoundService & SoundInstance Basic implementation. More work needs to be done. --- Blocks3D.vcproj | 16 ++++ src/include/DataModelV2/DataModelInstance.h | 20 ++++- src/include/DataModelV2/Instance.h | 1 - src/include/DataModelV2/SoundInstance.h | 30 +++++++ src/include/DataModelV2/SoundService.h | 16 ++++ src/source/DataModelV2/DataModelInstance.cpp | 9 ++- src/source/DataModelV2/PartInstance.cpp | 6 +- src/source/DataModelV2/SoundInstance.cpp | 52 ++++++++++++ src/source/DataModelV2/SoundService.cpp | 83 ++++++++++++++++++++ 9 files changed, 225 insertions(+), 8 deletions(-) create mode 100644 src/include/DataModelV2/SoundInstance.h create mode 100644 src/include/DataModelV2/SoundService.h create mode 100644 src/source/DataModelV2/SoundInstance.cpp create mode 100644 src/source/DataModelV2/SoundService.cpp diff --git a/Blocks3D.vcproj b/Blocks3D.vcproj index e67dd6c..c123448 100644 --- a/Blocks3D.vcproj +++ b/Blocks3D.vcproj @@ -505,6 +505,14 @@ RelativePath=".\src\source\DataModel\SelectionService.cpp" > + + + + @@ -878,6 +886,14 @@ RelativePath=".\src\include\DataModelV2\SelectionService.h" > + + + + diff --git a/src/include/DataModelV2/DataModelInstance.h b/src/include/DataModelV2/DataModelInstance.h index 9591ffa..bae7bd4 100644 --- a/src/include/DataModelV2/DataModelInstance.h +++ b/src/include/DataModelV2/DataModelInstance.h @@ -1,12 +1,17 @@ #pragma once + +// Instances #include "WorkspaceInstance.h" #include "LevelInstance.h" #include "PartInstance.h" -#include "ThumbnailGeneratorInstance.h" #include "SelectionService.h" -#include "rapidxml/rapidxml.hpp" #include "GuiRootInstance.h" +#include "ThumbnailGeneratorInstance.h" #include "XplicitNgine/XplicitNgine.h" +#include "SoundService.h" + +// Libraries +#include "rapidxml/rapidxml.hpp" class GuiRootInstance; @@ -25,10 +30,12 @@ public: bool readXMLFileStream(std::ifstream* file); void drawMessage(RenderDevice*); + // Instance getters WorkspaceInstance* getWorkspace(); LevelInstance* getLevel(); XplicitNgine* getEngine(); ThumbnailGeneratorInstance* getThumbnailGenerator(); + SoundService* getSoundService(); std::string message; std::string _loadedFileName; @@ -53,11 +60,16 @@ private: std::string _errMsg; bool _legacyLoad; float _modY; + + // Instances WorkspaceInstance* workspace; LevelInstance* level; GuiRootInstance* guiRoot; SelectionService* selectionService; - ThumbnailGeneratorInstance * thumbnailGenerator; + ThumbnailGeneratorInstance* thumbnailGenerator; + XplicitNgine* xplicitNgine; + SoundService* soundService; + bool running; - XplicitNgine * xplicitNgine; + }; diff --git a/src/include/DataModelV2/Instance.h b/src/include/DataModelV2/Instance.h index b8009bd..7e25dce 100644 --- a/src/include/DataModelV2/Instance.h +++ b/src/include/DataModelV2/Instance.h @@ -2,7 +2,6 @@ #include #include "propertyGrid.h" #include "map" -//#include "Properties/BoolProperty.h" class Instance { diff --git a/src/include/DataModelV2/SoundInstance.h b/src/include/DataModelV2/SoundInstance.h new file mode 100644 index 0000000..c70d168 --- /dev/null +++ b/src/include/DataModelV2/SoundInstance.h @@ -0,0 +1,30 @@ +#pragma once +#include "Instance.h" + +class SoundInstance : + public Instance +{ +public: + SoundInstance(void); + ~SoundInstance(void); + + // Getters + float getSoundVolume(); + std::string getSoundId(); + bool isPlayedOnRemove(); + bool isLooped(); + + // Setters + void setSoundVolume(float newVolume); + void setSoundId(std::string newSoundId); + void setIsPlayedOnRemove(bool isPlayed); + void setIsLooped(bool isLooped); + + // Functions + void play(); +private: + float soundVolume; + std::string soundId; + bool playOnRemove; + bool looped; +}; diff --git a/src/include/DataModelV2/SoundService.h b/src/include/DataModelV2/SoundService.h new file mode 100644 index 0000000..62b1ae5 --- /dev/null +++ b/src/include/DataModelV2/SoundService.h @@ -0,0 +1,16 @@ +#pragma once +#include "Instance.h" +#include "SoundInstance.h" + +class SoundService : + public Instance +{ +public: + SoundService(void); + ~SoundService(void); + + float getMusicVolume(); + void playSound(SoundInstance* sound); +private: + float musicVolume; +}; diff --git a/src/source/DataModelV2/DataModelInstance.cpp b/src/source/DataModelV2/DataModelInstance.cpp index 4da558f..f4fc9f0 100644 --- a/src/source/DataModelV2/DataModelInstance.cpp +++ b/src/source/DataModelV2/DataModelInstance.cpp @@ -14,14 +14,15 @@ using namespace std; using namespace rapidxml; - DataModelInstance::DataModelInstance(void) { + // Instances Instance::Instance(); workspace = new WorkspaceInstance(); guiRoot = new GuiRootInstance(); level = new LevelInstance(); thumbnailGenerator = new ThumbnailGeneratorInstance(); + soundService = new SoundService(); selectionService = new SelectionService(); selectionService->setPropertyWindow(g_usableApp->_propWindow); @@ -31,6 +32,7 @@ DataModelInstance::DataModelInstance(void) _modY=0; workspace->setParent(this); level->setParent(this); + _loadedFileName="..//skooter.rbxm"; listicon = 5; running = false; @@ -655,3 +657,8 @@ ThumbnailGeneratorInstance* DataModelInstance::getThumbnailGenerator() { return thumbnailGenerator; } + +SoundService* DataModelInstance::getSoundService() +{ + return soundService; +} \ No newline at end of file diff --git a/src/source/DataModelV2/PartInstance.cpp b/src/source/DataModelV2/PartInstance.cpp index defcd56..a38bbec 100644 --- a/src/source/DataModelV2/PartInstance.cpp +++ b/src/source/DataModelV2/PartInstance.cpp @@ -518,15 +518,17 @@ void PartInstance::onTouch() break; } + SoundService* sndService = g_dataModel->getSoundService(); + switch(OnTouchSound) { case Enum::Sound::NoSound: break; case Enum::Sound::Victory: - AudioPlayer::playSound(GetFileInPath("/content/sounds/victory.wav")); + sndService->playSound(dynamic_cast(sndService->findFirstChild("Victory"))); break; case Enum::Sound::Boing: - AudioPlayer::playSound(GetFileInPath("/content/sounds/boing.wav")); + sndService->playSound(dynamic_cast(sndService->findFirstChild("Boing"))); break; } } diff --git a/src/source/DataModelV2/SoundInstance.cpp b/src/source/DataModelV2/SoundInstance.cpp new file mode 100644 index 0000000..7f3072e --- /dev/null +++ b/src/source/DataModelV2/SoundInstance.cpp @@ -0,0 +1,52 @@ +#include "DataModelV2/SoundInstance.h" +#include "Globals.h" + +SoundInstance::SoundInstance() +{ + name = "Sound"; + className = "Sound"; + + soundVolume = 0.5; + soundId = ""; + playOnRemove = false; + looped = false; +} + +SoundInstance::~SoundInstance(void) +{ + if(isPlayedOnRemove()) + play(); +} + +// Functions +void SoundInstance::play() +{ + g_dataModel->getSoundService()->playSound(this); +} + +// Getters +float SoundInstance::getSoundVolume() +{ + return soundVolume; +} + +bool SoundInstance::isPlayedOnRemove() +{ + return playOnRemove; +} + +std::string SoundInstance::getSoundId() +{ + return soundId; +} + +bool SoundInstance::isLooped() +{ + return looped; +} + +// Setters +void SoundInstance::setSoundId(std::string newSoundId) +{ + soundId = newSoundId; +} \ No newline at end of file diff --git a/src/source/DataModelV2/SoundService.cpp b/src/source/DataModelV2/SoundService.cpp new file mode 100644 index 0000000..17c9ffd --- /dev/null +++ b/src/source/DataModelV2/SoundService.cpp @@ -0,0 +1,83 @@ +#include "DataModelV2/SoundService.h" +#include "StringFunctions.h" +#include "AudioPlayer.h" +#include "Globals.h" + +SoundService::SoundService() +{ + name = "SoundService"; + className = "SoundService"; + musicVolume = 0.3f; + + // Create stock sounds + SoundInstance* stockSound = new SoundInstance(); + + // Victory + stockSound = new SoundInstance(); + stockSound->setName("Victory"); + stockSound->setSoundId("/content/sounds/victory.wav"); + stockSound->setParent(this); + + // Boing + stockSound = new SoundInstance(); + stockSound->setName("Boing"); + stockSound->setSoundId("/content/sounds/bass.wav"); + stockSound->setParent(this); + + // Bomb + stockSound = new SoundInstance(); + stockSound->setName("Bomb"); + stockSound->setSoundId("/content/sounds/collide.wav"); + stockSound->setParent(this); + + // Ping + stockSound = new SoundInstance(); + stockSound->setName("Ping"); + stockSound->setSoundId("/content/sounds/electronicpingshort.wav"); + stockSound->setParent(this); + + // Break + stockSound = new SoundInstance(); + stockSound->setName("Break"); + stockSound->setSoundId("/content/sounds/glassbreak.wav"); + stockSound->setParent(this); + + // Splat + stockSound = new SoundInstance(); + stockSound->setName("Splat"); + stockSound->setSoundId("/content/sounds/splat.wav"); + stockSound->setParent(this); + + // Swoosh + stockSound = new SoundInstance(); + stockSound->setName("Swoosh"); + stockSound->setSoundId("/content/sounds/swoosh.wav"); + stockSound->setParent(this); + + // Snap + stockSound = new SoundInstance(); + stockSound->setName("Snap"); + stockSound->setSoundId("/content/sounds/snap.wav"); + stockSound->setParent(this); + + // Page + stockSound = new SoundInstance(); + stockSound->setName("Page"); + stockSound->setSoundId("/content/sounds/pageturn.wav"); + stockSound->setParent(this); +} + +SoundService::~SoundService(void) +{ +} + +void SoundService::playSound(SoundInstance* sound) +{ + std::string soundId = sound->getSoundId(); + AudioPlayer::playSound(GetFileInPath(soundId)); +} + +float SoundService::getMusicVolume() +{ + return musicVolume; +} \ No newline at end of file