Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/weaponsystem/src/orxonox/objects/weaponSystem/Weapon.cc @ 2852

Last change on this file since 2852 was 2852, checked in by polakma, 15 years ago

added unlimited munition, reverted shared munition and fixed some bugs

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