Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11185 was 11185, checked in by sagerj, 8 years ago

fix the charging during reloadtime, the hudchargebar working so far…

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