| 1 | /* |
|---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
|---|
| 3 | * > www.orxonox.net < |
|---|
| 4 | * |
|---|
| 5 | * |
|---|
| 6 | * License notice: |
|---|
| 7 | * |
|---|
| 8 | * This program is free software; you can redistribute it and/or |
|---|
| 9 | * modify it under the terms of the GNU General Public License |
|---|
| 10 | * as published by the Free Software Foundation; either version 2 |
|---|
| 11 | * of the License, or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, |
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 16 | * GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * You should have received a copy of the GNU General Public License |
|---|
| 19 | * along with this program; if not, write to the Free Software |
|---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 21 | * |
|---|
| 22 | * Author: |
|---|
| 23 | * Reto Grieder |
|---|
| 24 | * Co-authors: |
|---|
| 25 | * ... |
|---|
| 26 | * |
|---|
| 27 | */ |
|---|
| 28 | |
|---|
| 29 | #include "AmbientSound.h" |
|---|
| 30 | |
|---|
| 31 | #include "core/CoreIncludes.h" |
|---|
| 32 | #include "core/EventIncludes.h" |
|---|
| 33 | #include "core/GameMode.h" |
|---|
| 34 | #include "core/Resource.h" |
|---|
| 35 | #include "core/XMLPort.h" |
|---|
| 36 | #include "SoundManager.h" |
|---|
| 37 | |
|---|
| 38 | namespace orxonox |
|---|
| 39 | { |
|---|
| 40 | CreateFactory(AmbientSound); |
|---|
| 41 | |
|---|
| 42 | AmbientSound::AmbientSound(BaseObject* creator) |
|---|
| 43 | : BaseObject(creator) |
|---|
| 44 | , Synchronisable(creator) |
|---|
| 45 | , bPlayOnLoad_(false) |
|---|
| 46 | { |
|---|
| 47 | RegisterObject(AmbientSound); |
|---|
| 48 | |
|---|
| 49 | // Ambient sounds always fade in |
|---|
| 50 | this->setVolume(0); |
|---|
| 51 | this->registerVariables(); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | void AmbientSound::preDestroy() |
|---|
| 55 | { |
|---|
| 56 | if (GameMode::playsSound()) |
|---|
| 57 | { |
|---|
| 58 | // Smoothly fade out by keeping a SmartPtr |
|---|
| 59 | SoundManager::getInstance().unregisterAmbientSound(this); |
|---|
| 60 | } |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | void AmbientSound::registerVariables() |
|---|
| 64 | { |
|---|
| 65 | registerVariable(ambientSource_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::ambientSourceChanged)); |
|---|
| 66 | registerVariable(bLooping_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::loopingChanged)); |
|---|
| 67 | registerVariable(pitch_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::pitchChanged)); |
|---|
| 68 | registerVariable(bPlayOnLoad_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::playOnLoadChanged)); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | void AmbientSound::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| 72 | { |
|---|
| 73 | SUPER(AmbientSound, XMLPort, xmlelement, mode); |
|---|
| 74 | BaseSound::XMLPortExtern(xmlelement, mode); |
|---|
| 75 | XMLPortParam(AmbientSound, "ambientSource", setAmbientSource, getAmbientSource, xmlelement, mode); |
|---|
| 76 | XMLPortParam(AmbientSound, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | void AmbientSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
|---|
| 80 | { |
|---|
| 81 | SUPER(AmbientSound, XMLEventPort, xmlelement, mode); |
|---|
| 82 | XMLPortEventState(AmbientSound, BaseObject, "play", play, xmlelement, mode); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | void AmbientSound::play() |
|---|
| 86 | { |
|---|
| 87 | if (GameMode::playsSound()) |
|---|
| 88 | SoundManager::getInstance().registerAmbientSound(this); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | void AmbientSound::stop() |
|---|
| 92 | { |
|---|
| 93 | if (GameMode::playsSound()) |
|---|
| 94 | SoundManager::getInstance().unregisterAmbientSound(this); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | void AmbientSound::pause() |
|---|
| 98 | { |
|---|
| 99 | if (GameMode::playsSound()) |
|---|
| 100 | SoundManager::getInstance().pauseAmbientSound(this); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | float AmbientSound::getRealVolume() |
|---|
| 104 | { |
|---|
| 105 | assert(GameMode::playsSound()); |
|---|
| 106 | return SoundManager::getInstance().getRealVolume(SoundType::Music); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | void AmbientSound::setAmbientSource(const std::string& source) |
|---|
| 110 | { |
|---|
| 111 | this->ambientSource_ = source; |
|---|
| 112 | this->moodChanged(this->getMood()); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | void AmbientSound::moodChanged(const std::string& mood) |
|---|
| 116 | { |
|---|
| 117 | if (GameMode::playsSound()) |
|---|
| 118 | { |
|---|
| 119 | const std::string& path = "ambient/" + MoodManager::getInstance().getMood() + '/' + this->ambientSource_; |
|---|
| 120 | shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path); |
|---|
| 121 | if (fileInfo != NULL) |
|---|
| 122 | this->setSource(path); |
|---|
| 123 | else |
|---|
| 124 | COUT(3) << "Sound: " << this->ambientSource_ << ": Not a valid name! Ambient sound will not change." << std::endl; |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | void AmbientSound::setPlayOnLoad(bool val) |
|---|
| 129 | { |
|---|
| 130 | this->bPlayOnLoad_ = val; |
|---|
| 131 | if (val) |
|---|
| 132 | this->play(); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | void AmbientSound::changedActivity() |
|---|
| 136 | { |
|---|
| 137 | SUPER(AmbientSound, changedActivity); |
|---|
| 138 | if (this->isActive()) |
|---|
| 139 | this->play(); |
|---|
| 140 | else |
|---|
| 141 | this->stop(); |
|---|
| 142 | } |
|---|
| 143 | } |
|---|