Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/weapons/src/orxonox/objects/weaponSystem/Weapon.cc @ 2915

Last change on this file since 2915 was 2915, checked in by landauf, 15 years ago
  • switched back to std::vector for the WeaponSlots to keep them in the same order as in the XML file
  • added DefaultWeaponmodeLink, a class which links weaponmodes (a property of a Weapon or a WeaponPack) with firemodes (one firemode corresponds to one WeaponSet). This can be changed later (for example in a nice GUI), but DefaultWeaponmodeLink defines the default value.
  • 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 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "Weapon.h"
31
32#include "core/CoreIncludes.h"
33#include "core/XMLPort.h"
34
35#include "Munition.h"
36#include "WeaponPack.h"
37#include "WeaponSystem.h"
38
39namespace orxonox
40{
41    CreateFactory(Weapon);
42
43    Weapon::Weapon(BaseObject* creator) : StaticEntity(creator)
44    {
45        RegisterObject(Weapon);
46
47        this->bulletReadyToShoot_ = true;
48        this->magazineReadyToShoot_ = true;
49        this->weaponSystem_ = 0;
50        this->weaponPack_ = 0;
51        this->weaponSlot_ = 0;
52        this->bulletLoadingTime_ = 0;
53        this->magazineLoadingTime_ = 0;
54        this->bReloading_ = false;
55        this->bulletAmount_= 0;
56        this->magazineAmount_ = 0;
57        this->munition_ = 0;
58        this->unlimitedMunition_ = false;
59        this->setObjectMode(0x0);
60
61COUT(0) << "+Weapon" << std::endl;
62    }
63
64    Weapon::~Weapon()
65    {
66COUT(0) << "~Weapon" << std::endl;
67        if (this->isInitialized() && this->weaponPack_)
68            this->weaponPack_->removeWeapon(this);
69    }
70
71    void Weapon::XMLPort(Element& xmlelement, XMLPort::Mode mode)
72    {
73        SUPER(Weapon, XMLPort, xmlelement, mode);
74
75        XMLPortParam(Weapon, "munitionType", setMunitionType, getMunitionType, xmlelement, mode);
76        XMLPortParam(Weapon, "bulletLoadingTime", setBulletLoadingTime, getBulletLoadingTime, xmlelement, mode);
77        XMLPortParam(Weapon, "magazineLoadingTime", setMagazineLoadingTime, getMagazineLoadingTime, xmlelement, mode);
78        XMLPortParam(Weapon, "bullets", setBulletAmount, getBulletAmount, xmlelement, mode);
79        XMLPortParam(Weapon, "magazines", setMagazineAmount, getMagazineAmount, xmlelement, mode);
80        XMLPortParam(Weapon, "unlimitedMunition", setUnlimitedMunition, getUnlimitedMunition, xmlelement, mode);
81    }
82
83    void Weapon::setWeapon()
84    {
85        this->munition_->fillBullets();
86        this->munition_->fillMagazines();
87    }
88
89    void Weapon::setMunition()
90    {
91        this->munition_->setMaxBullets(this->bulletAmount_);
92        this->munition_->setMaxMagazines(this->magazineAmount_);
93    }
94
95    void Weapon::fire()
96    {
97        if ( this->bulletReadyToShoot_ && this->magazineReadyToShoot_ && !this->bReloading_)
98        {
99            this->bulletReadyToShoot_ = false;
100            if ( this->unlimitedMunition_== true )
101            {
102                //shoot
103                this->reloadBullet();
104                this->createProjectile();
105            }
106            else
107            {
108                if ( this->munition_->bullets() > 0)
109                {
110                    //shoot and reload
111                    this->takeBullets();
112                    this->reloadBullet();
113                    this->createProjectile();
114                }
115                //if there are no bullets, but magazines
116                else if ( this->munition_->magazines() > 0 && this->munition_->bullets() == 0 )
117                {
118                    //reload magazine
119                    this->takeMagazines();
120                    this->reloadMagazine();
121                }
122                else
123                {
124                    //no magazines
125                }
126            }
127        }
128        else
129        {
130            //weapon not reloaded
131        }
132
133    }
134
135
136    //weapon reloading
137    void Weapon::bulletTimer(float bulletLoadingTime)
138    {
139        this->bReloading_ = true;
140        this->bulletReloadTimer_.setTimer( bulletLoadingTime , false , this , createExecutor(createFunctor(&Weapon::bulletReloaded)));
141    }
142    void Weapon::magazineTimer(float magazineLoadingTime)
143    {
144        this->bReloading_ = true;
145        this->magazineReloadTimer_.setTimer( magazineLoadingTime , false , this , createExecutor(createFunctor(&Weapon::magazineReloaded)));
146    }
147
148    void Weapon::bulletReloaded()
149    {
150        this->bReloading_ = false;
151        this->bulletReadyToShoot_ = true;
152    }
153
154    void Weapon::magazineReloaded()
155    {
156        this->bReloading_ = false;
157        this->munition_->fillBullets();
158    }
159
160
161
162    void Weapon::attachNeededMunition(const std::string& munitionName)
163    {
164        /*  if munition type already exists attach it, else create a new one of this type and attach it to the weapon and to the WeaponSystem
165        */
166        if (this->weaponSystem_)
167        {
168            //getMunitionType returns 0 if there is no such munitionType
169            Munition* munition = this->weaponSystem_->getMunitionType(munitionName);
170            if ( munition )
171            {
172                this->munition_ = munition;
173                this->setMunition();
174            }
175            else
176            {
177                //create new munition with identifier because there is no such munitionType
178                this->munitionIdentifier_ = ClassByString(munitionName);
179                this->munition_ = this->munitionIdentifier_.fabricate(this);
180                this->weaponSystem_->setNewMunition(munitionName, this->munition_);
181                this->setMunition();
182            }
183        }
184    }
185
186
187    Munition * Weapon::getAttachedMunition(const std::string& munitionType)
188    {
189        this->munition_ = this->weaponSystem_->getMunitionType(munitionType);
190        return this->munition_;
191    }
192
193
194    //these function are defined in the weapon classes
195    void Weapon::takeBullets() { };
196    void Weapon::createProjectile() { };
197    void Weapon::takeMagazines() { };
198    void Weapon::reloadBullet() { };
199    void Weapon::reloadMagazine() { };
200
201
202    //get and set functions for XMLPort
203    void Weapon::setMunitionType(const std::string& munitionType)
204    {   this->munitionType_ = munitionType; }
205
206    const std::string& Weapon::getMunitionType() const
207    {   return this->munitionType_;  }
208
209    void Weapon::setBulletLoadingTime(float loadingTime)
210    {   this->bulletLoadingTime_ = loadingTime; }
211
212    const float Weapon::getBulletLoadingTime() const
213    {   return this->bulletLoadingTime_;  }
214
215    void Weapon::setMagazineLoadingTime(float loadingTime)
216    {   this->magazineLoadingTime_ = loadingTime; }
217
218    const float Weapon::getMagazineLoadingTime() const
219    {   return this->magazineLoadingTime_;  }
220
221    void Weapon::setBulletAmount(unsigned int amount)
222    {   this->bulletAmount_ = amount; }
223
224    const unsigned int Weapon::getBulletAmount() const
225    {   return this->bulletAmount_;  }
226
227    void Weapon::setMagazineAmount(unsigned int amount)
228    {   this->magazineAmount_ = amount; }
229
230    const unsigned int Weapon::getMagazineAmount() const
231    {   return this->magazineAmount_;   }
232
233    void Weapon::setUnlimitedMunition(bool unlimitedMunition)
234    {   this->unlimitedMunition_ = unlimitedMunition;   }
235
236    const bool Weapon::getUnlimitedMunition() const
237    {   return this->unlimitedMunition_;    }
238
239}
Note: See TracBrowser for help on using the repository browser.