Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

added unlimited munition, reverted shared munition and fixed some bugs

  • Property svn:eol-style set to native
File size: 7.2 KB
RevLine 
[2049]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
[2096]35#include "Weapon.h"
36
[2049]37namespace orxonox
38{
[2662]39    CreateFactory(Weapon);
40
41    Weapon::Weapon(BaseObject* creator) : StaticEntity(creator)
[2049]42    {
43        RegisterObject(Weapon);
[2804]44
[2662]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;
[2804]52        this->bulletAmount_= 0;
53        this->magazineAmount_ = 0;
54        this->munition_ = 0;
55        this->unlimitedMunition_ = false;
[2662]56        this->setObjectMode(0x0);
[2049]57    }
58
59    Weapon::~Weapon()
60    {
61    }
[2096]62
[2662]63
64    void Weapon::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[2096]65    {
[2662]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);
[2804]70        XMLPortParam(Weapon, "bullets", setBulletAmount, getBulletAmount, xmlelement, mode);
71        XMLPortParam(Weapon, "magazines", setMagazineAmount, getMagazineAmount, xmlelement, mode);
72        XMLPortParam(Weapon, "unlimitedMunition", setUnlimitedMunition, getUnlimitedMunition, xmlelement, mode);
[2662]73    }
[2096]74
[2662]75    void Weapon::setWeapon()
76    {
77        this->munition_->fillBullets();
78        this->munition_->fillMagazines();
[2049]79    }
[2096]80
[2804]81    void Weapon::setMunition()
82    {
83        this->munition_->setMaxBullets(this->bulletAmount_);
84        this->munition_->setMaxMagazines(this->magazineAmount_);
85    }
[2662]86
87    void Weapon::fire()
[2096]88    {
[2662]89        if ( this->bulletReadyToShoot_ && this->magazineReadyToShoot_ && !this->bReloading_)
90        {
91            this->bulletReadyToShoot_ = false;
[2804]92            if ( this->unlimitedMunition_== true )
[2662]93            {
94                //shoot
[2804]95                this->reloadBullet();
[2662]96                this->createProjectile();
97            }
98            else
99            {
[2804]100                if ( this->munition_->bullets() > 0)
101                {
[2852]102                    //shoot and reload
[2804]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                {
[2852]110                    //reload magazine
[2804]111                    this->takeMagazines();
112                    this->reloadMagazine();
113                }
114                else
115                {
116                    //no magazines
117                }
[2662]118            }
119        }
120        else
121        {
[2778]122            //weapon not reloaded
[2662]123        }
[2096]124
125    }
[2097]126
[2852]127
128    //weapon reloading
[2662]129    void Weapon::bulletTimer(float bulletLoadingTime)
[2097]130    {
[2662]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    }
[2097]139
[2662]140    void Weapon::bulletReloaded()
141    {
142        this->bReloading_ = false;
143        this->bulletReadyToShoot_ = true;
[2097]144    }
[2662]145
146    void Weapon::magazineReloaded()
147    {
148        this->bReloading_ = false;
149        this->munition_->fillBullets();
150    }
151
152
[2852]153
[2662]154    void Weapon::attachNeededMunition(std::string munitionName)
155    {
[2804]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        */
[2662]158        if (this->parentWeaponSystem_)
159        {
[2852]160            //getMunitionType returns 0 if there is no such munitionType
161            Munition* munition = this->parentWeaponSystem_->getMunitionType(munitionName);
162            if ( munition )
[2662]163            {
[2852]164                this->munition_ = munition;
165                this->setMunition();
166            }
167            else
168            {
169                //create new munition with identifier because there is no such munitionType
[2662]170                this->munitionIdentifier_ = ClassByString(munitionName);
171                this->munition_ = this->munitionIdentifier_.fabricate(this);
172                this->parentWeaponSystem_->setNewMunition(munitionName, this->munition_);
[2852]173                this->setMunition();
[2662]174            }
175        }
176    }
177
178
[2804]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
[2852]194    //get and set functions for XMLPort
[2662]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
[2804]213    void Weapon::setBulletAmount(unsigned int amount)
214    {   this->bulletAmount_ = amount; }
[2778]215
[2804]216    const unsigned int Weapon::getBulletAmount()
217    {   return this->bulletAmount_;  }
[2662]218
[2804]219    void Weapon::setMagazineAmount(unsigned int amount)
220    {   this->magazineAmount_ = amount; }
[2662]221
[2804]222    const unsigned int Weapon::getMagazineAmount()
[2852]223    {   return this->magazineAmount_;   }
[2804]224
225    void Weapon::setUnlimitedMunition(bool unlimitedMunition)
226    {   this->unlimitedMunition_ = unlimitedMunition;   }
227
228    const bool Weapon::getUnlimitedMunition()
229    {   return this->unlimitedMunition_;    }
230
[2096]231}
Note: See TracBrowser for help on using the repository browser.