[3060] | 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: |
---|
[5896] | 23 | * Reto Grieder |
---|
[3060] | 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
[3196] | 28 | |
---|
[5896] | 29 | #include "AmbientSound.h" |
---|
[3196] | 30 | |
---|
[5914] | 31 | #include "core/CoreIncludes.h" |
---|
[5896] | 32 | #include "core/EventIncludes.h" |
---|
[6117] | 33 | #include "core/GameMode.h" |
---|
| 34 | #include "core/Resource.h" |
---|
[5896] | 35 | #include "core/XMLPort.h" |
---|
[6117] | 36 | #include "SoundManager.h" |
---|
| 37 | #include "MoodManager.h" |
---|
[3060] | 38 | |
---|
[5738] | 39 | namespace orxonox |
---|
[3060] | 40 | { |
---|
[5896] | 41 | CreateFactory(AmbientSound); |
---|
| 42 | |
---|
| 43 | AmbientSound::AmbientSound(BaseObject* creator) |
---|
[6382] | 44 | : BaseObject(creator) |
---|
| 45 | , Synchronisable(creator) |
---|
| 46 | , bPlayOnLoad_(false) |
---|
[3060] | 47 | { |
---|
[5896] | 48 | RegisterObject(AmbientSound); |
---|
[6117] | 49 | |
---|
| 50 | // Ambient sounds always fade in |
---|
| 51 | this->setVolume(0); |
---|
[6307] | 52 | this->registerVariables(); |
---|
[3060] | 53 | } |
---|
| 54 | |
---|
[6349] | 55 | void AmbientSound::preDestroy() |
---|
| 56 | { |
---|
| 57 | if (GameMode::playsSound()) |
---|
| 58 | { |
---|
| 59 | // Smoothly fade out by keeping a SmartPtr |
---|
| 60 | SoundManager::getInstance().unregisterAmbientSound(this); |
---|
| 61 | } |
---|
| 62 | } |
---|
[6307] | 63 | |
---|
| 64 | void AmbientSound::registerVariables() |
---|
| 65 | { |
---|
| 66 | registerVariable(ambientSource_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::ambientSourceChanged)); |
---|
[6382] | 67 | registerVariable(bLooping_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::loopingChanged)); |
---|
| 68 | registerVariable(pitch_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::pitchChanged)); |
---|
| 69 | registerVariable(bPlayOnLoad_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::playOnLoadChanged)); |
---|
[6307] | 70 | } |
---|
[3060] | 71 | |
---|
[5896] | 72 | void AmbientSound::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[3060] | 73 | { |
---|
[5896] | 74 | SUPER(AmbientSound, XMLPort, xmlelement, mode); |
---|
[6117] | 75 | BaseSound::XMLPortExtern(xmlelement, mode); |
---|
[6382] | 76 | XMLPortParam(AmbientSound, "ambientSource", setAmbientSource, getAmbientSource, xmlelement, mode); |
---|
| 77 | XMLPortParam(AmbientSound, "playOnLoad", setPlayOnLoad, getPlayOnLoad, xmlelement, mode); |
---|
[3060] | 78 | } |
---|
| 79 | |
---|
[5896] | 80 | void AmbientSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[3060] | 81 | { |
---|
[5896] | 82 | SUPER(AmbientSound, XMLEventPort, xmlelement, mode); |
---|
| 83 | XMLPortEventState(AmbientSound, BaseObject, "play", play, xmlelement, mode); |
---|
[3060] | 84 | } |
---|
[6117] | 85 | |
---|
| 86 | void AmbientSound::play() |
---|
| 87 | { |
---|
| 88 | if (GameMode::playsSound()) |
---|
| 89 | SoundManager::getInstance().registerAmbientSound(this); |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | void AmbientSound::stop() |
---|
| 93 | { |
---|
| 94 | if (GameMode::playsSound()) |
---|
| 95 | SoundManager::getInstance().unregisterAmbientSound(this); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | void AmbientSound::pause() |
---|
| 99 | { |
---|
| 100 | if (GameMode::playsSound()) |
---|
| 101 | SoundManager::getInstance().pauseAmbientSound(this); |
---|
| 102 | } |
---|
[6186] | 103 | |
---|
[6370] | 104 | float AmbientSound::getRealVolume() |
---|
[6186] | 105 | { |
---|
[6322] | 106 | assert(GameMode::playsSound()); |
---|
[6370] | 107 | return SoundManager::getInstance().getRealVolume(SoundType::Music); |
---|
[6186] | 108 | } |
---|
[6117] | 109 | |
---|
| 110 | void AmbientSound::setAmbientSource(const std::string& source) |
---|
| 111 | { |
---|
| 112 | this->ambientSource_ = source; |
---|
| 113 | if (GameMode::playsSound()) |
---|
| 114 | { |
---|
| 115 | std::string path = "ambient/" + MoodManager::getInstance().getMood() + "/" + source; |
---|
| 116 | shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path); |
---|
| 117 | if (fileInfo != NULL) |
---|
| 118 | this->setSource(path); |
---|
| 119 | else |
---|
| 120 | COUT(3) << "Sound: " << source << ": Not a valid name! Ambient sound will not change." << std::endl; |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
[6382] | 124 | void AmbientSound::setPlayOnLoad(bool val) |
---|
| 125 | { |
---|
| 126 | this->bPlayOnLoad_ = val; |
---|
| 127 | if (val) |
---|
| 128 | this->play(); |
---|
| 129 | } |
---|
| 130 | |
---|
[6320] | 131 | void AmbientSound::changedActivity() |
---|
[6117] | 132 | { |
---|
| 133 | SUPER(AmbientSound, changedActivity); |
---|
| 134 | if (this->isActive()) |
---|
| 135 | this->play(); |
---|
| 136 | else |
---|
| 137 | this->stop(); |
---|
| 138 | } |
---|
[5896] | 139 | } |
---|