Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/presentation2/src/orxonox/weaponsystem/WeaponMode.cc @ 6271

Last change on this file since 6271 was 6265, checked in by scheusso, 15 years ago

fixed weapon sound source problem

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