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, 15 years ago

some enhancements, but not working…

  • Property svn:eol-style set to native
File size: 8.3 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, "bSharedMunition", setSharedMunition, getSharedMunition, xmlelement, mode);
71        XMLPortParam(Weapon, "bullets", setBulletAmount, getBulletAmount, xmlelement, mode);
72        XMLPortParam(Weapon, "magazines", setMagazineAmount, getMagazineAmount, xmlelement, mode);
73        XMLPortParam(Weapon, "unlimitedMunition", setUnlimitedMunition, getUnlimitedMunition, xmlelement, mode);
74    }
75
76    void Weapon::setWeapon()
77    {
78        this->munition_->fillBullets();
79        this->munition_->fillMagazines();
80    }
81
82    void Weapon::setMunition()
83    {
84        this->munition_->setMaxBullets(this->bulletAmount_);
85        this->munition_->setMaxMagazines(this->magazineAmount_);
86    }
87
88    void Weapon::fire()
89    {
90COUT(0) << "Weapon::fire" << std::endl;
91        if ( this->bulletReadyToShoot_ && this->magazineReadyToShoot_ && !this->bReloading_)
92        {
93COUT(0) << "Weapon::fire 2" << std::endl;
94            this->bulletReadyToShoot_ = false;
95            if ( this->unlimitedMunition_== true )
96            {
97COUT(0) << "Weapon::fire 3" << std::endl;
98                //shoot
99                this->reloadBullet();
100                this->createProjectile();
101            }
102            else
103            {
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                }
124            }
125        }
126        else
127        {
128COUT(0) << "Weapon::fire not reloaded" << std::endl;
129            //weapon not reloaded
130        }
131
132    }
133
134    /*
135    * weapon reloading
136    */
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        this->magazineReadyToShoot_ = true;
159        this->bulletReadyToShoot_ = true;
160    }
161
162
163    void Weapon::attachNeededMunition(std::string munitionName)
164    {
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        */
168        if (this->parentWeaponSystem_)
169        {
170            if (this->bSharedMunition_ == false)
171            {
172                this->munitionIdentifier_ = ClassByString(munitionName);
173                this->munition_ = this->munitionIdentifier_.fabricate(this);
174                this->parentWeaponSystem_->setNewMunition(munitionName, this->munition_);
175            }
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            }
190            this->setMunition();
191        }
192    }
193
194
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
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
231    void Weapon::setSharedMunition(bool bSharedMunition)
232    {   this->bSharedMunition_ = bSharedMunition; }
233
234    const bool Weapon::getSharedMunition()
235    {   return this->bSharedMunition_;  }
236
237    void Weapon::setBulletAmount(unsigned int amount)
238    {   this->bulletAmount_ = amount; }
239
240    const unsigned int Weapon::getBulletAmount()
241    {   return this->bulletAmount_;  }
242
243    void Weapon::setMagazineAmount(unsigned int amount)
244    {   this->magazineAmount_ = amount; }
245
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
255}
Note: See TracBrowser for help on using the repository browser.