Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 9729 was 9667, checked in by landauf, 11 years ago

merged core6 back to trunk

  • Property svn:eol-style set to native
File size: 10.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(Class(BaseObject));
48
49    WeaponMode::WeaponMode(Context* context) : BaseObject(context)
50    {
51        RegisterObject(WeaponMode);
52
53        this->weapon_ = 0;
54        this->mode_ = WeaponSystem::WEAPON_MODE_UNASSIGNED;
55
56        this->munition_ = 0;
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        if( GameMode::isMaster() )
78        {
79            this->defSndWpnFire_ = new WorldSound(this->getContext());
80            this->defSndWpnFire_->setLooping(false);
81            this->bSoundAttached_ = false;
82        }
83        else
84            this->defSndWpnFire_ = 0;
85    }
86
87    WeaponMode::~WeaponMode()
88    {
89        if (this->isInitialized())
90        {
91            if (this->defSndWpnFire_)
92                this->defSndWpnFire_->destroy();
93        }
94    }
95
96    void WeaponMode::XMLPort(Element& xmlelement, XMLPort::Mode mode)
97    {
98        SUPER(WeaponMode, XMLPort, xmlelement, mode);
99
100        XMLPortParam(WeaponMode, "mode",             setMode,             getMode,             xmlelement, mode);
101
102        XMLPortParam(WeaponMode, "munitiontype",     setMunitionName,     getMunitionName,     xmlelement, mode);
103        XMLPortParam(WeaponMode, "initialmunition",  setInitialMunition,  getInitialMunition,  xmlelement, mode);
104        XMLPortParam(WeaponMode, "initialmagazines", setInitialMagazines, getInitialMagazines, xmlelement, mode);
105        XMLPortParam(WeaponMode, "munitionpershot",  setMunitionPerShot,  getMunitionPerShot,  xmlelement, mode);
106
107        XMLPortParam(WeaponMode, "reloadtime",       setReloadTime,       getReloadTime,       xmlelement, mode);
108        XMLPortParam(WeaponMode, "autoreload",       setAutoReload,       getAutoReload,       xmlelement, mode).description("If true, the weapon reloads the magazine automatically");
109        XMLPortParam(WeaponMode, "parallelreload",   setParallelReload,   getParallelReload,   xmlelement, mode).description("If true, the weapon reloads in parallel to the magazine reloading");
110
111        XMLPortParam(WeaponMode, "damage",           setDamage,           getDamage,           xmlelement, mode);
112        XMLPortParam(WeaponMode, "healthdamage",     setHealthDamage,     getHealthDamage,     xmlelement, mode);
113        XMLPortParam(WeaponMode, "shielddamage",     setShieldDamage,     getShieldDamage,     xmlelement, mode);
114        XMLPortParam(WeaponMode, "muzzleoffset",     setMuzzleOffset,     getMuzzleOffset,     xmlelement, mode);
115    }
116
117    bool WeaponMode::fire(float* reloadTime)
118    {
119        (*reloadTime) = this->reloadTime_;
120        if( !this->bSoundAttached_ && GameMode::isMaster() )
121        {
122            assert(this->getWeapon());
123            this->getWeapon()->attach(this->defSndWpnFire_);
124            this->bSoundAttached_ = true;
125        }
126
127        if (!this->bReloading_ && this->munition_ && this->munition_->takeMunition(this->munitionPerShot_, this))
128        {
129            float reloadtime = this->reloadTime_;
130
131            if (this->bAutoReload_ && this->munition_->needReload(this))
132            {
133                if (this->munition_->reload(this))
134                {
135                    if (this->bParallelReload_)
136                        reloadtime = std::max(this->reloadTime_, this->munition_->getReloadTime());
137                    else
138                        reloadtime = this->reloadTime_ + this->munition_->getReloadTime();
139                }
140            }
141
142            this->bReloading_ = true;
143            this->reloadTimer_.setInterval(reloadtime);
144            this->reloadTimer_.startTimer();
145
146            if( this->defSndWpnFire_ && !(this->defSndWpnFire_->isPlaying()))
147            {
148                this->defSndWpnFire_->play();
149            }
150
151            this->fire();
152
153            return true;
154        }
155        else
156        {
157            return false;
158        }
159    }
160
161    bool WeaponMode::reload()
162    {
163        if (this->munition_ && this->munition_->reload(this))
164        {
165            if (!this->bParallelReload_)
166            {
167                this->bReloading_ = true;
168                this->reloadTimer_.setInterval(this->reloadTime_ + this->munition_->getReloadTime());
169                this->reloadTimer_.startTimer();
170            }
171
172            return true;
173        }
174
175        return false;
176    }
177
178    void WeaponMode::setMunitionType(Identifier* identifier)
179    {
180        this->munitionname_ = identifier->getName();
181        this->munitiontype_ = identifier;
182        this->updateMunition();
183    }
184
185    void WeaponMode::setMunitionName(const std::string& munitionname)
186    {
187        this->munitionname_ = munitionname;
188        Identifier* identifier = ClassByString(this->munitionname_);
189        if (identifier)
190            this->munitiontype_ = identifier;
191        else
192            orxout(internal_warning) << "No munition class defined in WeaponMode " << this->getName() << endl;
193        this->updateMunition();
194    }
195
196    void WeaponMode::updateMunition()
197    {
198        if (this->munitiontype_ && this->weapon_ && this->weapon_->getWeaponPack() && this->weapon_->getWeaponPack()->getWeaponSystem())
199        {
200            this->munition_ = this->weapon_->getWeaponPack()->getWeaponSystem()->getMunition(&this->munitiontype_);
201
202            if (this->munition_)
203            {
204                // Add the initial magazines
205                this->munition_->addMagazines(this->initialMagazines_);
206
207                // Maybe we have to reload (if this munition is used the first time or if there weren't any magazines available before)
208                if (this->munition_->needReload(this))
209                    this->munition_->reload(this, false);
210
211                // Add the initial munition
212                if (this->initialMunition_ > 0 && this->munition_->getNumMunitionInCurrentMagazine(this) == this->munition_->getMaxMunitionPerMagazine())
213                {
214                    // The current magazine is still full, so let's just add another magazine to
215                    // the stack and reduce the current magazine to the given amount of munition
216
217                    unsigned int initialmunition = this->initialMunition_;
218                    if (initialmunition > this->munition_->getMaxMunitionPerMagazine())
219                        initialmunition = this->munition_->getMaxMunitionPerMagazine();
220
221                    this->munition_->takeMunition(this->munition_->getMaxMunitionPerMagazine() - initialmunition, this);
222                    this->munition_->addMagazines(1);
223                }
224                else
225                {
226                    // The current magazine isn't full, add the munition directly
227
228                    this->munition_->addMunition(this->initialMunition_);
229                }
230            }
231        }
232        else
233            this->munition_ = 0;
234    }
235
236    void WeaponMode::reloaded()
237    {
238        if( this->defSndWpnFire_ && this->defSndWpnFire_->isPlaying())
239        {
240            this->defSndWpnFire_->stop();
241        }
242        this->bReloading_ = false;
243    }
244
245    void WeaponMode::computeMuzzleParameters(const Vector3& target)
246    {
247        if (this->weapon_)
248        {
249            this->muzzlePosition_ = this->weapon_->getWorldPosition() + this->weapon_->getWorldOrientation() * this->muzzleOffset_;
250
251            Vector3 muzzleDirection;
252            muzzleDirection = target - this->muzzlePosition_;
253            this->muzzleOrientation_ = (this->weapon_->getWorldOrientation() * WorldEntity::FRONT).getRotationTo(muzzleDirection) * this->weapon_->getWorldOrientation();
254        }
255        else
256        {
257            this->muzzlePosition_ = this->muzzleOffset_;
258            this->muzzleOrientation_ = Quaternion::IDENTITY;
259        }
260    }
261
262    Vector3 WeaponMode::getMuzzleDirection() const
263    {
264        if (this->weapon_)
265            return (this->getMuzzleOrientation() * WorldEntity::FRONT);
266        else
267            return WorldEntity::FRONT;
268    }
269
270    void WeaponMode::setDefaultSound(const std::string& soundPath)
271    {
272        if( this->defSndWpnFire_ )
273            this->defSndWpnFire_->setSource(soundPath);
274    }
275
276    const std::string& WeaponMode::getDefaultSound()
277    {
278        if( this->defSndWpnFire_ )
279            return this->defSndWpnFire_->getSource();
280        else
281            return BLANKSTRING;
282    }
283
284    void WeaponMode::setDefaultSoundWithVolume(const std::string& soundPath, const float soundVolume){
285        if (this->defSndWpnFire_) {
286            this->defSndWpnFire_->setSource(soundPath);
287            this->defSndWpnFire_->setVolume(soundVolume);
288        }
289    }
290
291}
Note: See TracBrowser for help on using the repository browser.