Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2914 was 2914, checked in by landauf, 15 years ago

More changes in the WeaponSystem:

  • WeaponSets can now contain several WeaponPacks
  • WeaponPacks can now belong to several WeaponSets

(until now this seemingly was a 1:1 relationship… now it's n:m)

  • Started support for multiple weaponmodes
  • Added some code to the destructor of some classes… according to some legends, this helps destroying objects. (WeaponSets and WeaponPacks weren't deleted before…)

Not yet finished, but I have to synchronize desktop computer and notebook.

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