Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/sagerjFS16/src/orxonox/weaponsystem/WeaponMode.cc @ 11170

Last change on this file since 11170 was 11170, checked in by sagerj, 9 years ago

running backup

  • Property svn:eol-style set to native
File size: 12.7 KB
RevLine 
[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]45namespace orxonox
46{
[10624]47    RegisterAbstractClass(WeaponMode).inheritsFrom<BaseObject>();
[9667]48
49    WeaponMode::WeaponMode(Context* context) : BaseObject(context)
[2918]50    {
51        RegisterObject(WeaponMode);
52
[11071]53        this->weapon_ = nullptr;
[2918]54        this->mode_ = WeaponSystem::WEAPON_MODE_UNASSIGNED;
55
[11071]56        this->munition_ = nullptr;
[2918]57        this->initialMunition_ = 0;
58        this->initialMagazines_ = 0;
59        this->munitionPerShot_ = 1;
60
61        this->reloadTime_ = 0.25;
62        this->bReloading_ = false;
63        this->bAutoReload_ = true;
64        this->bParallelReload_ = true;
65
[5929]66        this->reloadTimer_.setTimer(0.0f, false, createExecutor(createFunctor(&WeaponMode::reloaded, this)));
[2918]67        this->reloadTimer_.stopTimer();
68
69        this->damage_ = 0;
[8706]70        this->healthdamage_ = 0;
71        this->shielddamage_ = 0;
[6417]72
[2918]73        this->muzzleOffset_ = Vector3::ZERO;
[6417]74        this->muzzlePosition_ = Vector3::ZERO;
75        this->muzzleOrientation_ = Quaternion::IDENTITY;
76
[11052]77        hudImageString_ = "Orxonox/WSHUD_WM_Unknown";
78
[11108]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;
[2918]86    }
87
88    WeaponMode::~WeaponMode()
89    {
[6417]90        if (this->isInitialized())
91        {
[11108]92            for (auto sound : fireSounds_)
93            {
94                sound->destroy();
95            }
[6417]96        }
[2918]97    }
98
99    void WeaponMode::XMLPort(Element& xmlelement, XMLPort::Mode mode)
100    {
101        SUPER(WeaponMode, XMLPort, xmlelement, mode);
102
103        XMLPortParam(WeaponMode, "mode",             setMode,             getMode,             xmlelement, mode);
104
105        XMLPortParam(WeaponMode, "munitiontype",     setMunitionName,     getMunitionName,     xmlelement, mode);
106        XMLPortParam(WeaponMode, "initialmunition",  setInitialMunition,  getInitialMunition,  xmlelement, mode);
107        XMLPortParam(WeaponMode, "initialmagazines", setInitialMagazines, getInitialMagazines, xmlelement, mode);
108        XMLPortParam(WeaponMode, "munitionpershot",  setMunitionPerShot,  getMunitionPerShot,  xmlelement, mode);
109
110        XMLPortParam(WeaponMode, "reloadtime",       setReloadTime,       getReloadTime,       xmlelement, mode);
111        XMLPortParam(WeaponMode, "autoreload",       setAutoReload,       getAutoReload,       xmlelement, mode).description("If true, the weapon reloads the magazine automatically");
112        XMLPortParam(WeaponMode, "parallelreload",   setParallelReload,   getParallelReload,   xmlelement, mode).description("If true, the weapon reloads in parallel to the magazine reloading");
113
114        XMLPortParam(WeaponMode, "damage",           setDamage,           getDamage,           xmlelement, mode);
[8706]115        XMLPortParam(WeaponMode, "healthdamage",     setHealthDamage,     getHealthDamage,     xmlelement, mode);
116        XMLPortParam(WeaponMode, "shielddamage",     setShieldDamage,     getShieldDamage,     xmlelement, mode);
[2918]117        XMLPortParam(WeaponMode, "muzzleoffset",     setMuzzleOffset,     getMuzzleOffset,     xmlelement, mode);
118    }
119
120    bool WeaponMode::fire(float* reloadTime)
121    {
122        (*reloadTime) = this->reloadTime_;
[11052]123        // Fireing is only possible if this weapon mode is not reloading and there is enough munition
[2918]124        if (!this->bReloading_ && this->munition_ && this->munition_->takeMunition(this->munitionPerShot_, this))
125        {
[10650]126            float tempReloadtime = this->reloadTime_;
[2918]127
128            if (this->bAutoReload_ && this->munition_->needReload(this))
129            {
130                if (this->munition_->reload(this))
131                {
[11052]132                    // If true, the weapon reloads in parallel to the magazine reloading
[7847]133                    if (this->bParallelReload_)
[11052]134                    {
135                        // The time needed to reload is the maximum of the reload time of the weapon mode and the magazine.
[10650]136                        tempReloadtime = std::max(this->reloadTime_, this->munition_->getReloadTime());
[11052]137                    }                       
[7847]138                    else
[11052]139                    {
140                        // The time needed to reload is the sum of the reload time of the weapon mode and the magazine.
[10650]141                        tempReloadtime = this->reloadTime_ + this->munition_->getReloadTime();
[11108]142                    }
143                    playReloadSound();
[2918]144                }
145            }
146
[11108]147            // For stacked munition, a reload sound is played after every fired projectile
148            if (this->munition_->getMunitionDeployment() == MunitionDeployment::Stack)
149            {
150                playReloadSound();
151            }
152
[11052]153            // Mark this weapon mode as reloading and start the reload timer
[2918]154            this->bReloading_ = true;
[10650]155            this->reloadTimer_.setInterval(tempReloadtime);
[2918]156            this->reloadTimer_.startTimer();
157
[11108]158            // Play the fire sound and fire the weapon mode
159            this->playFireSound();
[2918]160            this->fire();
161
162            return true;
163        }
164        else
165        {
166            return false;
167        }
168    }
169
[11166]170    bool WeaponMode::release(float* reloadTime)
171    {
[11170]172        this->release();
[11166]173        return false;
174    }
[11153]175
[2918]176    bool WeaponMode::reload()
177    {
178        if (this->munition_ && this->munition_->reload(this))
179        {
180            if (!this->bParallelReload_)
181            {
182                this->bReloading_ = true;
183                this->reloadTimer_.setInterval(this->reloadTime_ + this->munition_->getReloadTime());
184                this->reloadTimer_.startTimer();
185            }
186
187            return true;
188        }
189
190        return false;
191    }
192
193    void WeaponMode::setMunitionType(Identifier* identifier)
194    {
195        this->munitionname_ = identifier->getName();
196        this->munitiontype_ = identifier;
197        this->updateMunition();
198    }
199
200    void WeaponMode::setMunitionName(const std::string& munitionname)
201    {
202        this->munitionname_ = munitionname;
[6417]203        Identifier* identifier = ClassByString(this->munitionname_);
204        if (identifier)
205            this->munitiontype_ = identifier;
206        else
[8858]207            orxout(internal_warning) << "No munition class defined in WeaponMode " << this->getName() << endl;
[2918]208        this->updateMunition();
209    }
210
211    void WeaponMode::updateMunition()
212    {
213        if (this->munitiontype_ && this->weapon_ && this->weapon_->getWeaponPack() && this->weapon_->getWeaponPack()->getWeaponSystem())
214        {
215            this->munition_ = this->weapon_->getWeaponPack()->getWeaponSystem()->getMunition(&this->munitiontype_);
216
217            if (this->munition_)
218            {
219                // Add the initial magazines
220                this->munition_->addMagazines(this->initialMagazines_);
221
222                // Maybe we have to reload (if this munition is used the first time or if there weren't any magazines available before)
223                if (this->munition_->needReload(this))
224                    this->munition_->reload(this, false);
225
226                // Add the initial munition
227                if (this->initialMunition_ > 0 && this->munition_->getNumMunitionInCurrentMagazine(this) == this->munition_->getMaxMunitionPerMagazine())
228                {
229                    // The current magazine is still full, so let's just add another magazine to
230                    // the stack and reduce the current magazine to the given amount of munition
231
232                    unsigned int initialmunition = this->initialMunition_;
233                    if (initialmunition > this->munition_->getMaxMunitionPerMagazine())
234                        initialmunition = this->munition_->getMaxMunitionPerMagazine();
235
236                    this->munition_->takeMunition(this->munition_->getMaxMunitionPerMagazine() - initialmunition, this);
237                    this->munition_->addMagazines(1);
238                }
239                else
240                {
241                    // The current magazine isn't full, add the munition directly
242
243                    this->munition_->addMunition(this->initialMunition_);
244                }
245            }
246        }
247        else
[11052]248        {
[11071]249            this->munition_ = nullptr;
[11052]250        }
[2918]251    }
252
253    void WeaponMode::reloaded()
254    {
255        this->bReloading_ = false;
256    }
257
[6417]258    void WeaponMode::computeMuzzleParameters(const Vector3& target)
[2918]259    {
260        if (this->weapon_)
[6417]261        {
262            this->muzzlePosition_ = this->weapon_->getWorldPosition() + this->weapon_->getWorldOrientation() * this->muzzleOffset_;
263
264            Vector3 muzzleDirection;
265            muzzleDirection = target - this->muzzlePosition_;
266            this->muzzleOrientation_ = (this->weapon_->getWorldOrientation() * WorldEntity::FRONT).getRotationTo(muzzleDirection) * this->weapon_->getWorldOrientation();
267        }
[2918]268        else
[6417]269        {
270            this->muzzlePosition_ = this->muzzleOffset_;
271            this->muzzleOrientation_ = Quaternion::IDENTITY;
272        }
[2918]273    }
274
[6417]275    Vector3 WeaponMode::getMuzzleDirection() const
[2918]276    {
277        if (this->weapon_)
[6417]278            return (this->getMuzzleOrientation() * WorldEntity::FRONT);
[2918]279        else
[6417]280            return WorldEntity::FRONT;
[2918]281    }
282
[11108]283    void WeaponMode::setFireSound(const std::string& soundPath, const float soundVolume)
[2918]284    {
[11108]285        fireSoundPath_ = soundPath;
286        fireSoundVolume_ = soundVolume;
[6417]287    }
288
[11108]289    const std::string& WeaponMode::getFireSound()
[6417]290    {
[11108]291        return fireSoundPath_;
[2918]292    }
[7163]293
[11108]294    void WeaponMode::setReloadSound(const std::string& soundPath, const float soundVolume)
[11052]295    {
[11108]296        reloadSoundPath_ = soundPath;
297        reloadSoundVolume_ = soundVolume;
298    }
299
300    const std::string& WeaponMode::getReloadSound()
301    {
302        return reloadSoundPath_;
303    }
304
305    void WeaponMode::playFireSound()
306    {
307        WorldSound* unusedSound = nullptr;
308
309        if (!GameMode::isMaster())
[11052]310        {
[11108]311            return;
[7163]312        }
[11108]313
314        // If no sound path or no weapon was specified, then no sound is played.
315        if (fireSoundPath_ == BLANKSTRING || !this->getWeapon())
316        {
317            return;
318        }
319
320        // 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)
321        for (auto sound : fireSounds_)
322        {
323            if( sound && !(sound->isPlaying()))
324            {
325                // Unused sound found
326                unusedSound = sound;
327                break;
328            }
329        }
330
331        // If no unused sound was found, create a new one and add it to the list
332        if (!unusedSound)
333        {
334            unusedSound = new WorldSound(this->getContext());
335            fireSounds_.push_back(unusedSound);
336            unusedSound->setLooping(false);
337            unusedSound->setSource(fireSoundPath_);
338            unusedSound->setVolume(fireSoundVolume_);
339            this->getWeapon()->attach(unusedSound);
340        }
341
342        // Play the fire sound
343        unusedSound->play();
[7163]344    }
345
[11108]346    void WeaponMode::playReloadSound()
347    {
348        if (!GameMode::isMaster())
349        {
350            return;
351        }
352
353        // If no sound path or no weapon was specified, then no sound is played.
354        if (reloadSoundPath_ == BLANKSTRING || !this->getWeapon())
355        {
356            return;
357        }
358
359        // Create a reload WorldSound if not done yet
360        if (!reloadSound_)
361        {
362            reloadSound_ = new WorldSound(this->getContext());
363            reloadSound_->setSource(reloadSoundPath_);
364            reloadSound_->setVolume(reloadSoundVolume_);
365            this->getWeapon()->attach(reloadSound_);
366        }
367
368        // Play the reload sound
369        reloadSound_->play();
370    }
[2918]371}
Note: See TracBrowser for help on using the repository browser.