Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 6156 was 6153, checked in by scheusso, 15 years ago

some fixes in weapon system / weapons
fabian: projectiles also spawn on wrong position in standalone mode

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