Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

overwritten many fire functions now only need to implement timer/charges

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