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
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"
[2912]30#include "Weapon.h"
[2049]31
32#include "core/CoreIncludes.h"
33#include "core/XMLPort.h"
34
[2912]35#include "Munition.h"
36#include "WeaponSystem.h"
[2096]37
[2049]38namespace orxonox
39{
[2662]40    CreateFactory(Weapon);
41
42    Weapon::Weapon(BaseObject* creator) : StaticEntity(creator)
[2049]43    {
44        RegisterObject(Weapon);
[2893]45
[2662]46        this->bulletReadyToShoot_ = true;
47        this->magazineReadyToShoot_ = true;
[2912]48        this->weaponSystem_ = 0;
[2914]49        this->weaponPack_ = 0;
50        this->weaponSlot_ = 0;
[2662]51        this->bulletLoadingTime_ = 0;
52        this->magazineLoadingTime_ = 0;
53        this->bReloading_ = false;
[2893]54        this->bulletAmount_= 0;
55        this->magazineAmount_ = 0;
56        this->munition_ = 0;
57        this->unlimitedMunition_ = false;
[2662]58        this->setObjectMode(0x0);
[2912]59
60COUT(0) << "+Weapon" << std::endl;
[2049]61    }
62
63    Weapon::~Weapon()
64    {
[2912]65COUT(0) << "~Weapon" << std::endl;
[2049]66    }
[2096]67
[2662]68    void Weapon::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[2096]69    {
[2662]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);
[2893]74        XMLPortParam(Weapon, "bullets", setBulletAmount, getBulletAmount, xmlelement, mode);
75        XMLPortParam(Weapon, "magazines", setMagazineAmount, getMagazineAmount, xmlelement, mode);
76        XMLPortParam(Weapon, "unlimitedMunition", setUnlimitedMunition, getUnlimitedMunition, xmlelement, mode);
[2662]77    }
[2096]78
[2662]79    void Weapon::setWeapon()
80    {
81        this->munition_->fillBullets();
82        this->munition_->fillMagazines();
[2049]83    }
[2096]84
[2893]85    void Weapon::setMunition()
86    {
87        this->munition_->setMaxBullets(this->bulletAmount_);
88        this->munition_->setMaxMagazines(this->magazineAmount_);
89    }
[2662]90
91    void Weapon::fire()
[2096]92    {
[2662]93        if ( this->bulletReadyToShoot_ && this->magazineReadyToShoot_ && !this->bReloading_)
94        {
95            this->bulletReadyToShoot_ = false;
[2893]96            if ( this->unlimitedMunition_== true )
[2662]97            {
98                //shoot
[2893]99                this->reloadBullet();
[2662]100                this->createProjectile();
101            }
102            else
103            {
[2893]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                }
[2662]122            }
123        }
124        else
125        {
[2893]126            //weapon not reloaded
[2662]127        }
[2096]128
129    }
[2097]130
[2662]131
[2893]132    //weapon reloading
[2662]133    void Weapon::bulletTimer(float bulletLoadingTime)
[2097]134    {
[2662]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    }
[2097]143
[2662]144    void Weapon::bulletReloaded()
145    {
146        this->bReloading_ = false;
147        this->bulletReadyToShoot_ = true;
[2097]148    }
[2662]149
150    void Weapon::magazineReloaded()
151    {
152        this->bReloading_ = false;
153        this->munition_->fillBullets();
154    }
155
156
[2893]157
[2912]158    void Weapon::attachNeededMunition(const std::string& munitionName)
[2662]159    {
[2893]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        */
[2912]162        if (this->weaponSystem_)
[2662]163        {
[2893]164            //getMunitionType returns 0 if there is no such munitionType
[2912]165            Munition* munition = this->weaponSystem_->getMunitionType(munitionName);
[2662]166            if ( munition )
[2893]167            {
[2662]168                this->munition_ = munition;
[2893]169                this->setMunition();
170            }
[2662]171            else
172            {
[2893]173                //create new munition with identifier because there is no such munitionType
[2662]174                this->munitionIdentifier_ = ClassByString(munitionName);
175                this->munition_ = this->munitionIdentifier_.fabricate(this);
[2912]176                this->weaponSystem_->setNewMunition(munitionName, this->munition_);
[2893]177                this->setMunition();
[2662]178            }
179        }
180    }
181
182
[2912]183    Munition * Weapon::getAttachedMunition(const std::string& munitionType)
[2893]184    {
[2912]185        this->munition_ = this->weaponSystem_->getMunitionType(munitionType);
[2893]186        return this->munition_;
187    }
[2662]188
[2893]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
[2912]199    void Weapon::setMunitionType(const std::string& munitionType)
[2662]200    {   this->munitionType_ = munitionType; }
201
[2912]202    const std::string& Weapon::getMunitionType() const
[2662]203    {   return this->munitionType_;  }
204
205    void Weapon::setBulletLoadingTime(float loadingTime)
206    {   this->bulletLoadingTime_ = loadingTime; }
207
[2912]208    const float Weapon::getBulletLoadingTime() const
[2662]209    {   return this->bulletLoadingTime_;  }
210
211    void Weapon::setMagazineLoadingTime(float loadingTime)
212    {   this->magazineLoadingTime_ = loadingTime; }
213
[2912]214    const float Weapon::getMagazineLoadingTime() const
[2662]215    {   return this->magazineLoadingTime_;  }
216
[2893]217    void Weapon::setBulletAmount(unsigned int amount)
218    {   this->bulletAmount_ = amount; }
[2662]219
[2912]220    const unsigned int Weapon::getBulletAmount() const
[2893]221    {   return this->bulletAmount_;  }
[2662]222
[2893]223    void Weapon::setMagazineAmount(unsigned int amount)
224    {   this->magazineAmount_ = amount; }
[2662]225
[2912]226    const unsigned int Weapon::getMagazineAmount() const
[2893]227    {   return this->magazineAmount_;   }
228
229    void Weapon::setUnlimitedMunition(bool unlimitedMunition)
230    {   this->unlimitedMunition_ = unlimitedMunition;   }
231
[2912]232    const bool Weapon::getUnlimitedMunition() const
[2893]233    {   return this->unlimitedMunition_;    }
234
[2096]235}
Note: See TracBrowser for help on using the repository browser.