[2918] | 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 | * Martin Polak |
---|
| 24 | * Fabian 'x3n' Landau |
---|
| 25 | * Co-authors: |
---|
| 26 | * ... |
---|
| 27 | * |
---|
| 28 | */ |
---|
| 29 | |
---|
| 30 | #include "WeaponMode.h" |
---|
| 31 | |
---|
| 32 | #include "core/CoreIncludes.h" |
---|
| 33 | #include "core/XMLPort.h" |
---|
[6417] | 34 | #include "controllers/Controller.h" |
---|
| 35 | #include "worldentities/pawns/Pawn.h" |
---|
[2918] | 36 | |
---|
| 37 | #include "Munition.h" |
---|
| 38 | #include "Weapon.h" |
---|
| 39 | #include "WeaponPack.h" |
---|
| 40 | #include "WeaponSystem.h" |
---|
[6417] | 41 | #include "WeaponSlot.h" |
---|
[2918] | 42 | |
---|
[6417] | 43 | #include "sound/WorldSound.h" |
---|
| 44 | |
---|
[2918] | 45 | namespace orxonox |
---|
| 46 | { |
---|
| 47 | WeaponMode::WeaponMode(BaseObject* creator) : BaseObject(creator) |
---|
| 48 | { |
---|
| 49 | RegisterObject(WeaponMode); |
---|
| 50 | |
---|
| 51 | this->weapon_ = 0; |
---|
| 52 | this->mode_ = WeaponSystem::WEAPON_MODE_UNASSIGNED; |
---|
| 53 | |
---|
| 54 | this->munition_ = 0; |
---|
| 55 | this->initialMunition_ = 0; |
---|
| 56 | this->initialMagazines_ = 0; |
---|
| 57 | this->munitionPerShot_ = 1; |
---|
| 58 | |
---|
| 59 | this->reloadTime_ = 0.25; |
---|
| 60 | this->bReloading_ = false; |
---|
| 61 | this->bAutoReload_ = true; |
---|
| 62 | this->bParallelReload_ = true; |
---|
| 63 | |
---|
[5929] | 64 | this->reloadTimer_.setTimer(0.0f, false, createExecutor(createFunctor(&WeaponMode::reloaded, this))); |
---|
[2918] | 65 | this->reloadTimer_.stopTimer(); |
---|
| 66 | |
---|
| 67 | this->damage_ = 0; |
---|
[6417] | 68 | |
---|
[2918] | 69 | this->muzzleOffset_ = Vector3::ZERO; |
---|
[6417] | 70 | this->muzzlePosition_ = Vector3::ZERO; |
---|
| 71 | this->muzzleOrientation_ = Quaternion::IDENTITY; |
---|
| 72 | |
---|
| 73 | if( GameMode::isMaster() ) |
---|
| 74 | { |
---|
| 75 | this->defSndWpnFire_ = new WorldSound(this); |
---|
| 76 | this->defSndWpnFire_->setLooping(false); |
---|
| 77 | this->bSoundAttached_ = false; |
---|
| 78 | } |
---|
| 79 | else |
---|
| 80 | this->defSndWpnFire_ = 0; |
---|
[2918] | 81 | } |
---|
| 82 | |
---|
| 83 | WeaponMode::~WeaponMode() |
---|
| 84 | { |
---|
[6417] | 85 | if (this->isInitialized()) |
---|
| 86 | { |
---|
| 87 | if (this->defSndWpnFire_) |
---|
| 88 | this->defSndWpnFire_->destroy(); |
---|
| 89 | } |
---|
[2918] | 90 | } |
---|
| 91 | |
---|
| 92 | void WeaponMode::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 93 | { |
---|
| 94 | SUPER(WeaponMode, XMLPort, xmlelement, mode); |
---|
| 95 | |
---|
| 96 | XMLPortParam(WeaponMode, "mode", setMode, getMode, xmlelement, mode); |
---|
| 97 | |
---|
| 98 | XMLPortParam(WeaponMode, "munitiontype", setMunitionName, getMunitionName, xmlelement, mode); |
---|
| 99 | XMLPortParam(WeaponMode, "initialmunition", setInitialMunition, getInitialMunition, xmlelement, mode); |
---|
| 100 | XMLPortParam(WeaponMode, "initialmagazines", setInitialMagazines, getInitialMagazines, xmlelement, mode); |
---|
| 101 | XMLPortParam(WeaponMode, "munitionpershot", setMunitionPerShot, getMunitionPerShot, xmlelement, mode); |
---|
| 102 | |
---|
| 103 | XMLPortParam(WeaponMode, "reloadtime", setReloadTime, getReloadTime, xmlelement, mode); |
---|
| 104 | XMLPortParam(WeaponMode, "autoreload", setAutoReload, getAutoReload, xmlelement, mode).description("If true, the weapon reloads the magazine automatically"); |
---|
| 105 | XMLPortParam(WeaponMode, "parallelreload", setParallelReload, getParallelReload, xmlelement, mode).description("If true, the weapon reloads in parallel to the magazine reloading"); |
---|
| 106 | |
---|
| 107 | XMLPortParam(WeaponMode, "damage", setDamage, getDamage, xmlelement, mode); |
---|
| 108 | XMLPortParam(WeaponMode, "muzzleoffset", setMuzzleOffset, getMuzzleOffset, xmlelement, mode); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | bool WeaponMode::fire(float* reloadTime) |
---|
| 112 | { |
---|
| 113 | (*reloadTime) = this->reloadTime_; |
---|
[6417] | 114 | if( !this->bSoundAttached_ && GameMode::isMaster() ) |
---|
| 115 | { |
---|
| 116 | assert(this->getWeapon()); |
---|
| 117 | this->getWeapon()->attach(this->defSndWpnFire_); |
---|
| 118 | this->bSoundAttached_ = true; |
---|
| 119 | } |
---|
[2918] | 120 | |
---|
| 121 | if (!this->bReloading_ && this->munition_ && this->munition_->takeMunition(this->munitionPerShot_, this)) |
---|
| 122 | { |
---|
| 123 | float reloadtime = this->reloadTime_; |
---|
| 124 | |
---|
| 125 | if (this->bAutoReload_ && this->munition_->needReload(this)) |
---|
| 126 | { |
---|
| 127 | if (this->munition_->reload(this)) |
---|
| 128 | { |
---|
| 129 | if (!this->bParallelReload_) |
---|
| 130 | reloadtime += this->munition_->getReloadTime(); |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | this->bReloading_ = true; |
---|
| 135 | this->reloadTimer_.setInterval(reloadtime); |
---|
| 136 | this->reloadTimer_.startTimer(); |
---|
| 137 | |
---|
[6417] | 138 | if( this->defSndWpnFire_ && !(this->defSndWpnFire_->isPlaying())) |
---|
| 139 | { |
---|
| 140 | this->defSndWpnFire_->play(); |
---|
| 141 | } |
---|
| 142 | |
---|
[2918] | 143 | this->fire(); |
---|
| 144 | |
---|
| 145 | return true; |
---|
| 146 | } |
---|
| 147 | else |
---|
| 148 | { |
---|
| 149 | return false; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | bool WeaponMode::reload() |
---|
| 154 | { |
---|
| 155 | if (this->munition_ && this->munition_->reload(this)) |
---|
| 156 | { |
---|
| 157 | if (!this->bParallelReload_) |
---|
| 158 | { |
---|
| 159 | this->bReloading_ = true; |
---|
| 160 | this->reloadTimer_.setInterval(this->reloadTime_ + this->munition_->getReloadTime()); |
---|
| 161 | this->reloadTimer_.startTimer(); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | return true; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | return false; |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | void WeaponMode::setMunitionType(Identifier* identifier) |
---|
| 171 | { |
---|
| 172 | this->munitionname_ = identifier->getName(); |
---|
| 173 | this->munitiontype_ = identifier; |
---|
| 174 | this->updateMunition(); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | void WeaponMode::setMunitionName(const std::string& munitionname) |
---|
| 178 | { |
---|
| 179 | this->munitionname_ = munitionname; |
---|
[6417] | 180 | Identifier* identifier = ClassByString(this->munitionname_); |
---|
| 181 | if (identifier) |
---|
| 182 | this->munitiontype_ = identifier; |
---|
| 183 | else |
---|
| 184 | COUT(2) << "Warning: No munition class defined in WeaponMode " << this->getName() << std::endl; |
---|
[2918] | 185 | this->updateMunition(); |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | void WeaponMode::updateMunition() |
---|
| 189 | { |
---|
| 190 | if (this->munitiontype_ && this->weapon_ && this->weapon_->getWeaponPack() && this->weapon_->getWeaponPack()->getWeaponSystem()) |
---|
| 191 | { |
---|
| 192 | this->munition_ = this->weapon_->getWeaponPack()->getWeaponSystem()->getMunition(&this->munitiontype_); |
---|
| 193 | |
---|
| 194 | if (this->munition_) |
---|
| 195 | { |
---|
| 196 | // Add the initial magazines |
---|
| 197 | this->munition_->addMagazines(this->initialMagazines_); |
---|
| 198 | |
---|
| 199 | // Maybe we have to reload (if this munition is used the first time or if there weren't any magazines available before) |
---|
| 200 | if (this->munition_->needReload(this)) |
---|
| 201 | this->munition_->reload(this, false); |
---|
| 202 | |
---|
| 203 | // Add the initial munition |
---|
| 204 | if (this->initialMunition_ > 0 && this->munition_->getNumMunitionInCurrentMagazine(this) == this->munition_->getMaxMunitionPerMagazine()) |
---|
| 205 | { |
---|
| 206 | // The current magazine is still full, so let's just add another magazine to |
---|
| 207 | // the stack and reduce the current magazine to the given amount of munition |
---|
| 208 | |
---|
| 209 | unsigned int initialmunition = this->initialMunition_; |
---|
| 210 | if (initialmunition > this->munition_->getMaxMunitionPerMagazine()) |
---|
| 211 | initialmunition = this->munition_->getMaxMunitionPerMagazine(); |
---|
| 212 | |
---|
| 213 | this->munition_->takeMunition(this->munition_->getMaxMunitionPerMagazine() - initialmunition, this); |
---|
| 214 | this->munition_->addMagazines(1); |
---|
| 215 | } |
---|
| 216 | else |
---|
| 217 | { |
---|
| 218 | // The current magazine isn't full, add the munition directly |
---|
| 219 | |
---|
| 220 | this->munition_->addMunition(this->initialMunition_); |
---|
| 221 | } |
---|
| 222 | } |
---|
| 223 | } |
---|
| 224 | else |
---|
| 225 | this->munition_ = 0; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | void WeaponMode::reloaded() |
---|
| 229 | { |
---|
[6417] | 230 | if( this->defSndWpnFire_ && this->defSndWpnFire_->isPlaying()) |
---|
| 231 | { |
---|
| 232 | this->defSndWpnFire_->stop(); |
---|
| 233 | } |
---|
[2918] | 234 | this->bReloading_ = false; |
---|
| 235 | } |
---|
| 236 | |
---|
[6417] | 237 | void WeaponMode::computeMuzzleParameters(const Vector3& target) |
---|
[2918] | 238 | { |
---|
| 239 | if (this->weapon_) |
---|
[6417] | 240 | { |
---|
| 241 | this->muzzlePosition_ = this->weapon_->getWorldPosition() + this->weapon_->getWorldOrientation() * this->muzzleOffset_; |
---|
| 242 | |
---|
| 243 | Vector3 muzzleDirection; |
---|
| 244 | muzzleDirection = target - this->muzzlePosition_; |
---|
| 245 | // COUT(0) << "muzzleDirection " << muzzleDirection << endl; |
---|
| 246 | this->muzzleOrientation_ = (this->weapon_->getWorldOrientation() * WorldEntity::FRONT).getRotationTo(muzzleDirection) * this->weapon_->getWorldOrientation(); |
---|
| 247 | } |
---|
[2918] | 248 | else |
---|
[6417] | 249 | { |
---|
| 250 | this->muzzlePosition_ = this->muzzleOffset_; |
---|
| 251 | this->muzzleOrientation_ = Quaternion::IDENTITY; |
---|
| 252 | } |
---|
[2918] | 253 | } |
---|
| 254 | |
---|
[6417] | 255 | Vector3 WeaponMode::getMuzzleDirection() const |
---|
[2918] | 256 | { |
---|
| 257 | if (this->weapon_) |
---|
[6417] | 258 | return (this->getMuzzleOrientation() * WorldEntity::FRONT); |
---|
[2918] | 259 | else |
---|
[6417] | 260 | return WorldEntity::FRONT; |
---|
[2918] | 261 | } |
---|
| 262 | |
---|
[6417] | 263 | void WeaponMode::setDefaultSound(const std::string& soundPath) |
---|
[2918] | 264 | { |
---|
[6417] | 265 | if( this->defSndWpnFire_ ) |
---|
| 266 | this->defSndWpnFire_->setSource(soundPath); |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | const std::string& WeaponMode::getDefaultSound() |
---|
| 270 | { |
---|
| 271 | if( this->defSndWpnFire_ ) |
---|
| 272 | return this->defSndWpnFire_->getSource(); |
---|
[2918] | 273 | else |
---|
[6417] | 274 | return BLANKSTRING; |
---|
[2918] | 275 | } |
---|
[7095] | 276 | |
---|
| 277 | void WeaponMode::setDefaultSoundWithVolume(const std::string& soundPath, const float soundVolume){ |
---|
| 278 | if (this->defSndWpnFire_) { |
---|
| 279 | this->defSndWpnFire_->setSource(soundPath); |
---|
| 280 | this->defSndWpnFire_->setVolume(soundVolume); |
---|
| 281 | } |
---|
| 282 | } |
---|
| 283 | |
---|
[2918] | 284 | } |
---|