Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some enhancements, but not working…

  • Property svn:eol-style set to native
File size: 8.3 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);
[2778]70        XMLPortParam(Weapon, "bSharedMunition", setSharedMunition, getSharedMunition, xmlelement, mode);
[2804]71        XMLPortParam(Weapon, "bullets", setBulletAmount, getBulletAmount, xmlelement, mode);
72        XMLPortParam(Weapon, "magazines", setMagazineAmount, getMagazineAmount, xmlelement, mode);
73        XMLPortParam(Weapon, "unlimitedMunition", setUnlimitedMunition, getUnlimitedMunition, xmlelement, mode);
[2662]74    }
[2096]75
[2662]76    void Weapon::setWeapon()
77    {
78        this->munition_->fillBullets();
79        this->munition_->fillMagazines();
[2049]80    }
[2096]81
[2804]82    void Weapon::setMunition()
83    {
84        this->munition_->setMaxBullets(this->bulletAmount_);
85        this->munition_->setMaxMagazines(this->magazineAmount_);
86    }
[2662]87
88    void Weapon::fire()
[2096]89    {
[2804]90COUT(0) << "Weapon::fire" << std::endl;
[2662]91        if ( this->bulletReadyToShoot_ && this->magazineReadyToShoot_ && !this->bReloading_)
92        {
[2804]93COUT(0) << "Weapon::fire 2" << std::endl;
[2662]94            this->bulletReadyToShoot_ = false;
[2804]95            if ( this->unlimitedMunition_== true )
[2662]96            {
[2804]97COUT(0) << "Weapon::fire 3" << std::endl;
[2662]98                //shoot
[2804]99                this->reloadBullet();
[2662]100                this->createProjectile();
101            }
102            else
103            {
[2804]104COUT(0) << "Weapon::fire 4" << std::endl;
105                if ( this->munition_->bullets() > 0)
106                {
107COUT(0) << "Weapon::fire 5" << std::endl;
108                    //shoot
109                    this->takeBullets();
110                    this->reloadBullet();
111                    this->createProjectile();
112                }
113                //if there are no bullets, but magazines
114                else if ( this->munition_->magazines() > 0 && this->munition_->bullets() == 0 )
115                {
116COUT(0) << "Weapon::fire 6" << std::endl;
117                    this->takeMagazines();
118                    this->reloadMagazine();
119                }
120                else
121                {
122                    //no magazines
123                }
[2662]124            }
125        }
126        else
127        {
[2804]128COUT(0) << "Weapon::fire not reloaded" << std::endl;
[2778]129            //weapon not reloaded
[2662]130        }
[2096]131
132    }
[2097]133
[2804]134    /*
135    * weapon reloading
136    */
[2662]137    void Weapon::bulletTimer(float bulletLoadingTime)
[2097]138    {
[2662]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    }
[2097]147
[2662]148    void Weapon::bulletReloaded()
149    {
150        this->bReloading_ = false;
151        this->bulletReadyToShoot_ = true;
[2097]152    }
[2662]153
154    void Weapon::magazineReloaded()
155    {
156        this->bReloading_ = false;
157        this->munition_->fillBullets();
158        this->magazineReadyToShoot_ = true;
159        this->bulletReadyToShoot_ = true;
160    }
161
162
163    void Weapon::attachNeededMunition(std::string munitionName)
164    {
[2804]165        /*  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
166        *   if the weapon shares one munitionType put it into sharedMunitionSet else to munitionSet
167        */
[2662]168        if (this->parentWeaponSystem_)
169        {
[2778]170            if (this->bSharedMunition_ == false)
[2662]171            {
172                this->munitionIdentifier_ = ClassByString(munitionName);
173                this->munition_ = this->munitionIdentifier_.fabricate(this);
174                this->parentWeaponSystem_->setNewMunition(munitionName, this->munition_);
175            }
[2778]176            else
177            {
178                //getMunitionType returns 0 if there is no such munitionType
179                Munition* munition = this->parentWeaponSystem_->getMunitionType(munitionName);
180                if ( munition )
181                    this->munition_ = munition;
182                else
183                {
184                    //create new munition with identifier because there is no such munitionType
185                    this->munitionIdentifier_ = ClassByString(munitionName);
186                    this->munition_ = this->munitionIdentifier_.fabricate(this);
187                    this->parentWeaponSystem_->setNewSharedMunition(munitionName, this->munition_);
188                }
189            }
[2804]190            this->setMunition();
[2662]191        }
192    }
193
194
[2804]195    Munition * Weapon::getAttachedMunition(std::string munitionType)
196    {
197        this->munition_ = this->parentWeaponSystem_->getMunitionType(munitionType);
198        return this->munition_;
199    }
200
201
202    //these function are defined in the weapon classes
203    void Weapon::takeBullets() { };
204    void Weapon::createProjectile() { };
205    void Weapon::takeMagazines() { };
206    void Weapon::reloadBullet() { };
207    void Weapon::reloadMagazine() { };
208
209
[2662]210     /*get and set functions
211     *
212     */
213    void Weapon::setMunitionType(std::string munitionType)
214    {   this->munitionType_ = munitionType; }
215
216    const std::string Weapon::getMunitionType()
217    {   return this->munitionType_;  }
218
219    void Weapon::setBulletLoadingTime(float loadingTime)
220    {   this->bulletLoadingTime_ = loadingTime; }
221
222    const float Weapon::getBulletLoadingTime()
223    {   return this->bulletLoadingTime_;  }
224
225    void Weapon::setMagazineLoadingTime(float loadingTime)
226    {   this->magazineLoadingTime_ = loadingTime; }
227
228    const float Weapon::getMagazineLoadingTime()
229    {   return this->magazineLoadingTime_;  }
230
[2778]231    void Weapon::setSharedMunition(bool bSharedMunition)
232    {   this->bSharedMunition_ = bSharedMunition; }
[2662]233
[2778]234    const bool Weapon::getSharedMunition()
235    {   return this->bSharedMunition_;  }
236
[2804]237    void Weapon::setBulletAmount(unsigned int amount)
238    {   this->bulletAmount_ = amount; }
[2778]239
[2804]240    const unsigned int Weapon::getBulletAmount()
241    {   return this->bulletAmount_;  }
[2662]242
[2804]243    void Weapon::setMagazineAmount(unsigned int amount)
244    {   this->magazineAmount_ = amount; }
[2662]245
[2804]246    const unsigned int Weapon::getMagazineAmount()
247    {   return this->magazineAmount_;  }
248
249    void Weapon::setUnlimitedMunition(bool unlimitedMunition)
250    {   this->unlimitedMunition_ = unlimitedMunition;   }
251
252    const bool Weapon::getUnlimitedMunition()
253    {   return this->unlimitedMunition_;    }
254
[2096]255}
Note: See TracBrowser for help on using the repository browser.