Changeset 11108 for code/trunk/src/orxonox
- Timestamp:
- Feb 4, 2016, 11:54:04 PM (9 years ago)
- Location:
- code/trunk/src/orxonox/weaponsystem
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
code/trunk/src/orxonox/weaponsystem/WeaponMode.cc
r11071 r11108 77 77 hudImageString_ = "Orxonox/WSHUD_WM_Unknown"; 78 78 79 if( GameMode::isMaster() ) 80 { 81 this->defSndWpnFire_ = new WorldSound(this->getContext()); 82 this->defSndWpnFire_->setLooping(false); 83 this->defSndWpnFire_->setVolume(0.8f); 84 this->bSoundAttached_ = false; 85 } 86 else 87 this->defSndWpnFire_ = 0; 79 this->fireSoundPath_ = BLANKSTRING; 80 this->fireSoundVolume_ = 1.0; 81 this->fireSounds_.clear(); 82 83 this->reloadSoundPath_ = BLANKSTRING; 84 this->reloadSoundVolume_ = 1.0; 85 this->reloadSound_ = nullptr; 88 86 } 89 87 … … 92 90 if (this->isInitialized()) 93 91 { 94 if (this->defSndWpnFire_) 95 this->defSndWpnFire_->destroy(); 92 for (auto sound : fireSounds_) 93 { 94 sound->destroy(); 95 } 96 96 } 97 97 } … … 121 121 { 122 122 (*reloadTime) = this->reloadTime_; 123 if( !this->bSoundAttached_ && GameMode::isMaster() )124 {125 assert(this->getWeapon());126 this->getWeapon()->attach(this->defSndWpnFire_);127 this->bSoundAttached_ = true;128 }129 123 130 124 // Fireing is only possible if this weapon mode is not reloading and there is enough munition … … 147 141 // The time needed to reload is the sum of the reload time of the weapon mode and the magazine. 148 142 tempReloadtime = this->reloadTime_ + this->munition_->getReloadTime(); 149 } 143 } 144 playReloadSound(); 150 145 } 146 } 147 148 // For stacked munition, a reload sound is played after every fired projectile 149 if (this->munition_->getMunitionDeployment() == MunitionDeployment::Stack) 150 { 151 playReloadSound(); 151 152 } 152 153 … … 156 157 this->reloadTimer_.startTimer(); 157 158 158 if( this->defSndWpnFire_ && !(this->defSndWpnFire_->isPlaying())) 159 { 160 this->defSndWpnFire_->play(); 161 } 162 159 // Play the fire sound and fire the weapon mode 160 this->playFireSound(); 163 161 this->fire(); 164 162 … … 250 248 void WeaponMode::reloaded() 251 249 { 252 if( this->defSndWpnFire_ && this->defSndWpnFire_->isPlaying())253 {254 this->defSndWpnFire_->stop();255 }256 250 this->bReloading_ = false; 257 251 } … … 282 276 } 283 277 284 void WeaponMode::setDefaultSound(const std::string& soundPath) 285 { 286 if( this->defSndWpnFire_ ) 287 this->defSndWpnFire_->setSource(soundPath); 288 } 289 290 const std::string& WeaponMode::getDefaultSound() 291 { 292 if( this->defSndWpnFire_ ) 293 return this->defSndWpnFire_->getSource(); 294 else 295 return BLANKSTRING; 296 } 297 298 void WeaponMode::setDefaultSoundWithVolume(const std::string& soundPath, const float soundVolume) 299 { 300 if (this->defSndWpnFire_) 301 { 302 this->defSndWpnFire_->setSource(soundPath); 303 this->defSndWpnFire_->setVolume(soundVolume); 304 } 305 } 306 278 void WeaponMode::setFireSound(const std::string& soundPath, const float soundVolume) 279 { 280 fireSoundPath_ = soundPath; 281 fireSoundVolume_ = soundVolume; 282 } 283 284 const std::string& WeaponMode::getFireSound() 285 { 286 return fireSoundPath_; 287 } 288 289 void WeaponMode::setReloadSound(const std::string& soundPath, const float soundVolume) 290 { 291 reloadSoundPath_ = soundPath; 292 reloadSoundVolume_ = soundVolume; 293 } 294 295 const std::string& WeaponMode::getReloadSound() 296 { 297 return reloadSoundPath_; 298 } 299 300 void WeaponMode::playFireSound() 301 { 302 WorldSound* unusedSound = nullptr; 303 304 if (!GameMode::isMaster()) 305 { 306 return; 307 } 308 309 // If no sound path or no weapon was specified, then no sound is played. 310 if (fireSoundPath_ == BLANKSTRING || !this->getWeapon()) 311 { 312 return; 313 } 314 315 // Search in the sound list for a WorldSound that may be used. It must be an idle WorldSound instance (i.e. it is not playing a sound now) 316 for (auto sound : fireSounds_) 317 { 318 if( sound && !(sound->isPlaying())) 319 { 320 // Unused sound found 321 unusedSound = sound; 322 break; 323 } 324 } 325 326 // If no unused sound was found, create a new one and add it to the list 327 if (!unusedSound) 328 { 329 unusedSound = new WorldSound(this->getContext()); 330 fireSounds_.push_back(unusedSound); 331 unusedSound->setLooping(false); 332 unusedSound->setSource(fireSoundPath_); 333 unusedSound->setVolume(fireSoundVolume_); 334 this->getWeapon()->attach(unusedSound); 335 } 336 337 // Play the fire sound 338 unusedSound->play(); 339 } 340 341 void WeaponMode::playReloadSound() 342 { 343 if (!GameMode::isMaster()) 344 { 345 return; 346 } 347 348 // If no sound path or no weapon was specified, then no sound is played. 349 if (reloadSoundPath_ == BLANKSTRING || !this->getWeapon()) 350 { 351 return; 352 } 353 354 // Create a reload WorldSound if not done yet 355 if (!reloadSound_) 356 { 357 reloadSound_ = new WorldSound(this->getContext()); 358 reloadSound_->setSource(reloadSoundPath_); 359 reloadSound_->setVolume(reloadSoundVolume_); 360 this->getWeapon()->attach(reloadSound_); 361 } 362 363 // Play the reload sound 364 reloadSound_->play(); 365 } 307 366 } -
code/trunk/src/orxonox/weaponsystem/WeaponMode.h
r11071 r11108 34 34 35 35 #include <string> 36 #include <vector> 36 37 #include "util/Math.h" 37 38 #include "core/BaseObject.h" … … 54 55 virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode) override; 55 56 56 bool fire(float* reloadTime);57 virtual bool fire(float* reloadTime); 57 58 bool reload(); 58 59 // Interacting with the default Firing sound60 void setDefaultSound(const std::string& soundPath);61 const std::string& getDefaultSound();62 void setDefaultSoundWithVolume(const std::string& soundPath, const float soundVolume);63 59 64 60 // Munition … … 155 151 void updateMunition(); 156 152 protected: 153 // Interacting with the firing sound 154 void setFireSound(const std::string& soundPath, const float soundVolume = 1.0); 155 const std::string& getFireSound(); 156 void playFireSound(); 157 158 // Interacting with the reloading sound 159 void setReloadSound(const std::string& soundPath, const float soundVolume = 1.0); 160 const std::string& getReloadSound(); 161 void playReloadSound(); 162 157 163 virtual void fire() = 0; 158 164 … … 188 194 Quaternion muzzleOrientation_; 189 195 190 WorldSound* defSndWpnFire_; 191 bool bSoundAttached_; 196 std::string fireSoundPath_; // The path of the sound played when fireing 197 float fireSoundVolume_; // The volume of the sound played when fireing 198 std::vector<WorldSound*> fireSounds_; // List of sounds used by the weapon mode. Because multiple sounds may overlap, we need mor than one WorldSound instance. 199 std::string reloadSoundPath_; // The path of the sound played when reloading 200 float reloadSoundVolume_; // The volume of the sound played when reloading 201 WorldSound* reloadSound_; 192 202 }; 193 203 }
Note: See TracChangeset
for help on using the changeset viewer.