Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/ingamemenu/src/orxonox/weaponsystem/WeaponMode.cc @ 6020

Last change on this file since 6020 was 5929, checked in by rgrieder, 15 years ago

Merged core5 branch back to the trunk.
Key features include clean level unloading and an extended XML event system.

Two important notes:
Delete your keybindings.ini files! * or you will still get parser errors when loading the key bindings.
Delete build_dir/lib/modules/libgamestates.module! * or orxonox won't start.
Best thing to do is to delete the build folder ;)

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