[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) |
---|
[6307] | 44 | : BaseObject(creator), Synchronisable(creator) |
---|
[3060] | 45 | { |
---|
[5896] | 46 | RegisterObject(AmbientSound); |
---|
[6117] | 47 | |
---|
| 48 | // Ambient sounds always fade in |
---|
| 49 | this->setVolume(0); |
---|
[6307] | 50 | this->registerVariables(); |
---|
[3060] | 51 | } |
---|
| 52 | |
---|
[5896] | 53 | AmbientSound::~AmbientSound() |
---|
[3060] | 54 | { |
---|
| 55 | } |
---|
[6349] | 56 | |
---|
| 57 | void AmbientSound::preDestroy() |
---|
| 58 | { |
---|
| 59 | if (GameMode::playsSound()) |
---|
| 60 | { |
---|
| 61 | // Smoothly fade out by keeping a SmartPtr |
---|
| 62 | SoundManager::getInstance().unregisterAmbientSound(this); |
---|
| 63 | } |
---|
| 64 | } |
---|
[6307] | 65 | |
---|
| 66 | void AmbientSound::registerVariables() |
---|
| 67 | { |
---|
| 68 | registerVariable(ambientSource_, ObjectDirection::ToClient, new NetworkCallback<AmbientSound>(this, &AmbientSound::ambientSourceChanged)); |
---|
| 69 | registerVariable(bLooping_, ObjectDirection::ToClient, new NetworkCallback<BaseSound>(static_cast<BaseSound*>(this), &BaseSound::loopingChanged)); |
---|
| 70 | registerVariable(pitch_, ObjectDirection::ToClient, new NetworkCallback<BaseSound>(static_cast<BaseSound*>(this), &BaseSound::pitchChanged)); |
---|
[6320] | 71 | registerVariable((int&)(BaseSound::state_), ObjectDirection::ToClient, new NetworkCallback<BaseSound>(static_cast<BaseSound*>(this), &BaseSound::stateChanged)); |
---|
[6307] | 72 | } |
---|
[3060] | 73 | |
---|
[5896] | 74 | void AmbientSound::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[3060] | 75 | { |
---|
[5896] | 76 | SUPER(AmbientSound, XMLPort, xmlelement, mode); |
---|
[6117] | 77 | BaseSound::XMLPortExtern(xmlelement, mode); |
---|
| 78 | XMLPortParam(AmbientSound, "ambientsource", setAmbientSource, getAmbientSource, xmlelement, mode); |
---|
[3060] | 79 | } |
---|
| 80 | |
---|
[5896] | 81 | void AmbientSound::XMLEventPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[3060] | 82 | { |
---|
[5896] | 83 | SUPER(AmbientSound, XMLEventPort, xmlelement, mode); |
---|
| 84 | XMLPortEventState(AmbientSound, BaseObject, "play", play, xmlelement, mode); |
---|
[3060] | 85 | } |
---|
[6117] | 86 | |
---|
| 87 | void AmbientSound::play() |
---|
| 88 | { |
---|
| 89 | if (GameMode::playsSound()) |
---|
| 90 | { |
---|
| 91 | SoundManager::getInstance().registerAmbientSound(this); |
---|
| 92 | } |
---|
[6320] | 93 | else |
---|
| 94 | BaseSound::play(); |
---|
[6117] | 95 | } |
---|
| 96 | |
---|
| 97 | void AmbientSound::doPlay() |
---|
| 98 | { |
---|
| 99 | BaseSound::play(); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | void AmbientSound::stop() |
---|
| 103 | { |
---|
| 104 | if (GameMode::playsSound()) |
---|
| 105 | { |
---|
| 106 | SoundManager::getInstance().unregisterAmbientSound(this); |
---|
| 107 | } |
---|
[6320] | 108 | else |
---|
| 109 | BaseSound::stop(); |
---|
[6117] | 110 | } |
---|
| 111 | |
---|
| 112 | void AmbientSound::doStop() |
---|
| 113 | { |
---|
| 114 | BaseSound::stop(); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | void AmbientSound::pause() |
---|
| 118 | { |
---|
| 119 | if (GameMode::playsSound()) |
---|
| 120 | { |
---|
| 121 | SoundManager::getInstance().pauseAmbientSound(this); |
---|
| 122 | } |
---|
[6320] | 123 | else |
---|
| 124 | BaseSound::pause(); |
---|
[6117] | 125 | } |
---|
[6186] | 126 | |
---|
| 127 | float AmbientSound::getVolumeGain() |
---|
| 128 | { |
---|
[6322] | 129 | assert(GameMode::playsSound()); |
---|
[6186] | 130 | return SoundManager::getInstance().getVolume(SoundType::ambient); |
---|
| 131 | } |
---|
[6117] | 132 | |
---|
| 133 | void AmbientSound::doPause() |
---|
| 134 | { |
---|
| 135 | BaseSound::pause(); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | void AmbientSound::setAmbientSource(const std::string& source) |
---|
| 139 | { |
---|
| 140 | this->ambientSource_ = source; |
---|
| 141 | if (GameMode::playsSound()) |
---|
| 142 | { |
---|
| 143 | std::string path = "ambient/" + MoodManager::getInstance().getMood() + "/" + source; |
---|
| 144 | shared_ptr<ResourceInfo> fileInfo = Resource::getInfo(path); |
---|
| 145 | if (fileInfo != NULL) |
---|
| 146 | this->setSource(path); |
---|
| 147 | else |
---|
| 148 | COUT(3) << "Sound: " << source << ": Not a valid name! Ambient sound will not change." << std::endl; |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
[6320] | 152 | void AmbientSound::changedActivity() |
---|
[6117] | 153 | { |
---|
| 154 | SUPER(AmbientSound, changedActivity); |
---|
| 155 | if (this->isActive()) |
---|
| 156 | this->play(); |
---|
| 157 | else |
---|
| 158 | this->stop(); |
---|
| 159 | } |
---|
[5896] | 160 | } |
---|