Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/weaponsystem/WeaponMode.cc @ 12112

Last change on this file since 12112 was 11108, checked in by fvultier, 9 years ago

A few modifications in the weapon system: WeaponModes may play a reload sound now. Fireing Sounds of WeaponModes may overlap now. New weapon: FlameGun, a flame thrower for close combat (e.g. for the FPS player)

  • Property svn:eol-style set to native
File size: 12.6 KB
Line 
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"
34#include "controllers/Controller.h"
35#include "worldentities/pawns/Pawn.h"
36
37#include "Munition.h"
38#include "Weapon.h"
39#include "WeaponPack.h"
40#include "WeaponSystem.h"
41#include "WeaponSlot.h"
42
43#include "sound/WorldSound.h"
44
45namespace orxonox
46{
47    RegisterAbstractClass(WeaponMode).inheritsFrom<BaseObject>();
48
49    WeaponMode::WeaponMode(Context* context) : BaseObject(context)
50    {
51        RegisterObject(WeaponMode);
52
53        this->weapon_ = nullptr;
54        this->mode_ = WeaponSystem::WEAPON_MODE_UNASSIGNED;
55
56        this->munition_ = nullptr;
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
66        this->reloadTimer_.setTimer(0.0f, false, createExecutor(createFunctor(&WeaponMode::reloaded, this)));
67        this->reloadTimer_.stopTimer();
68
69        this->damage_ = 0;
70        this->healthdamage_ = 0;
71        this->shielddamage_ = 0;
72
73        this->muzzleOffset_ = Vector3::ZERO;
74        this->muzzlePosition_ = Vector3::ZERO;
75        this->muzzleOrientation_ = Quaternion::IDENTITY;
76
77        hudImageString_ = "Orxonox/WSHUD_WM_Unknown";
78
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;
86    }
87
88    WeaponMode::~WeaponMode()
89    {
90        if (this->isInitialized())
91        {
92            for (auto sound : fireSounds_)
93            {
94                sound->destroy();
95            }
96        }
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);
115        XMLPortParam(WeaponMode, "healthdamage",     setHealthDamage,     getHealthDamage,     xmlelement, mode);
116        XMLPortParam(WeaponMode, "shielddamage",     setShieldDamage,     getShieldDamage,     xmlelement, mode);
117        XMLPortParam(WeaponMode, "muzzleoffset",     setMuzzleOffset,     getMuzzleOffset,     xmlelement, mode);
118    }
119
120    bool WeaponMode::fire(float* reloadTime)
121    {
122        (*reloadTime) = this->reloadTime_;
123       
124        // Fireing is only possible if this weapon mode is not reloading and there is enough munition
125        if (!this->bReloading_ && this->munition_ && this->munition_->takeMunition(this->munitionPerShot_, this))
126        {
127            float tempReloadtime = this->reloadTime_;
128
129            if (this->bAutoReload_ && this->munition_->needReload(this))
130            {
131                if (this->munition_->reload(this))
132                {
133                    // If true, the weapon reloads in parallel to the magazine reloading
134                    if (this->bParallelReload_)
135                    {
136                        // The time needed to reload is the maximum of the reload time of the weapon mode and the magazine.
137                        tempReloadtime = std::max(this->reloadTime_, this->munition_->getReloadTime());
138                    }                       
139                    else
140                    {
141                        // The time needed to reload is the sum of the reload time of the weapon mode and the magazine.
142                        tempReloadtime = this->reloadTime_ + this->munition_->getReloadTime();
143                    }
144                    playReloadSound();
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();
152            }
153
154            // Mark this weapon mode as reloading and start the reload timer
155            this->bReloading_ = true;
156            this->reloadTimer_.setInterval(tempReloadtime);
157            this->reloadTimer_.startTimer();
158
159            // Play the fire sound and fire the weapon mode
160            this->playFireSound();
161            this->fire();
162
163            return true;
164        }
165        else
166        {
167            return false;
168        }
169    }
170
171    bool WeaponMode::reload()
172    {
173        if (this->munition_ && this->munition_->reload(this))
174        {
175            if (!this->bParallelReload_)
176            {
177                this->bReloading_ = true;
178                this->reloadTimer_.setInterval(this->reloadTime_ + this->munition_->getReloadTime());
179                this->reloadTimer_.startTimer();
180            }
181
182            return true;
183        }
184
185        return false;
186    }
187
188    void WeaponMode::setMunitionType(Identifier* identifier)
189    {
190        this->munitionname_ = identifier->getName();
191        this->munitiontype_ = identifier;
192        this->updateMunition();
193    }
194
195    void WeaponMode::setMunitionName(const std::string& munitionname)
196    {
197        this->munitionname_ = munitionname;
198        Identifier* identifier = ClassByString(this->munitionname_);
199        if (identifier)
200            this->munitiontype_ = identifier;
201        else
202            orxout(internal_warning) << "No munition class defined in WeaponMode " << this->getName() << endl;
203        this->updateMunition();
204    }
205
206    void WeaponMode::updateMunition()
207    {
208        if (this->munitiontype_ && this->weapon_ && this->weapon_->getWeaponPack() && this->weapon_->getWeaponPack()->getWeaponSystem())
209        {
210            this->munition_ = this->weapon_->getWeaponPack()->getWeaponSystem()->getMunition(&this->munitiontype_);
211
212            if (this->munition_)
213            {
214                // Add the initial magazines
215                this->munition_->addMagazines(this->initialMagazines_);
216
217                // Maybe we have to reload (if this munition is used the first time or if there weren't any magazines available before)
218                if (this->munition_->needReload(this))
219                    this->munition_->reload(this, false);
220
221                // Add the initial munition
222                if (this->initialMunition_ > 0 && this->munition_->getNumMunitionInCurrentMagazine(this) == this->munition_->getMaxMunitionPerMagazine())
223                {
224                    // The current magazine is still full, so let's just add another magazine to
225                    // the stack and reduce the current magazine to the given amount of munition
226
227                    unsigned int initialmunition = this->initialMunition_;
228                    if (initialmunition > this->munition_->getMaxMunitionPerMagazine())
229                        initialmunition = this->munition_->getMaxMunitionPerMagazine();
230
231                    this->munition_->takeMunition(this->munition_->getMaxMunitionPerMagazine() - initialmunition, this);
232                    this->munition_->addMagazines(1);
233                }
234                else
235                {
236                    // The current magazine isn't full, add the munition directly
237
238                    this->munition_->addMunition(this->initialMunition_);
239                }
240            }
241        }
242        else
243        {
244            this->munition_ = nullptr;
245        }
246    }
247
248    void WeaponMode::reloaded()
249    {
250        this->bReloading_ = false;
251    }
252
253    void WeaponMode::computeMuzzleParameters(const Vector3& target)
254    {
255        if (this->weapon_)
256        {
257            this->muzzlePosition_ = this->weapon_->getWorldPosition() + this->weapon_->getWorldOrientation() * this->muzzleOffset_;
258
259            Vector3 muzzleDirection;
260            muzzleDirection = target - this->muzzlePosition_;
261            this->muzzleOrientation_ = (this->weapon_->getWorldOrientation() * WorldEntity::FRONT).getRotationTo(muzzleDirection) * this->weapon_->getWorldOrientation();
262        }
263        else
264        {
265            this->muzzlePosition_ = this->muzzleOffset_;
266            this->muzzleOrientation_ = Quaternion::IDENTITY;
267        }
268    }
269
270    Vector3 WeaponMode::getMuzzleDirection() const
271    {
272        if (this->weapon_)
273            return (this->getMuzzleOrientation() * WorldEntity::FRONT);
274        else
275            return WorldEntity::FRONT;
276    }
277
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    }
366}
Note: See TracBrowser for help on using the repository browser.