Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/weapon2/src/orxonox/objects/weaponSystem/Weapon.cc @ 2518

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

altfire now available

  • Property svn:eol-style set to native
File size: 6.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) : PositionableEntity(creator)
42    {
43        RegisterObject(Weapon);
44        this->bulletReadyToShoot_ = true;
45        this->magazineReadyToShoot_ = true;
46        this->parentWeaponSystem_ = 0;
47        this->attachedToWeaponSlot_ = 0;
48        this->munition_ = 0;
49        this->bulletLoadingTime_ = 0;
50        this->magazineLoadingTime_ = 0;
51        this->bReloading_ = false;
52    }
53
54    Weapon::~Weapon()
55    {
56    }
57
58
59    void Weapon::XMLPort(Element& xmlelement, XMLPort::Mode mode)
60    {
61        SUPER(Weapon, XMLPort, xmlelement, mode);
62        XMLPortParam(Weapon, "munitionType", setMunitionType, getMunitionType, xmlelement, mode);
63        XMLPortParam(Weapon, "bulletLoadingTime", setBulletLoadingTime, getBulletLoadingTime, xmlelement, mode);
64        XMLPortParam(Weapon, "magazineLoadingTime", setMagazineLoadingTime, getMagazineLoadingTime, xmlelement, mode);
65    }
66
67    void Weapon::setWeapon()
68    {
69        this->munition_->fillBullets();
70        this->munition_->fillMagazines();
71    }
72
73
74    void Weapon::fire()
75    {
76COUT(0) << "LaserGun::fire, this=" << this << std::endl;
77        if ( this->bulletReadyToShoot_ && this->magazineReadyToShoot_ && !this->bReloading_)
78        {
79COUT(0) << "LaserGun::fire - ready to shoot" << std::endl;
80COUT(0) << "LaserGun::fire - bullets" << this->munition_->bullets() << std::endl;
81            this->bulletReadyToShoot_ = false;
82            if ( this->munition_->bullets() > 0)
83            {
84                //shoot
85                this->takeBullets();
86                this->createProjectile();
87            }
88            //if there are no bullets, but magazines
89            else if ( this->munition_->magazines() > 0 && this->munition_->bullets() == 0 )
90            {
91COUT(0) << "LaserGun::fire - no bullets" << std::endl;
92                this->takeMagazines();
93            }
94            else
95            {
96COUT(0) << "LaserGun::fire - no magazines" << std::endl;
97                //actions
98            }
99        }
100        else
101        {
102COUT(0) << "LaserGun::fire - weapon not reloaded - bullets remaining:" << this->munition_->bullets() << std::endl;
103            //actions
104        }
105
106    }
107
108
109    void Weapon::bulletTimer(float bulletLoadingTime)
110    {
111COUT(0) << "Weapon::bulletTimer started" << std::endl;
112        this->bReloading_ = true;
113        this->bulletReloadTimer_.setTimer( bulletLoadingTime , false , this , createExecutor(createFunctor(&Weapon::bulletReloaded)));
114    }
115    void Weapon::magazineTimer(float magazineLoadingTime)
116    {
117COUT(0) << "Weapon::magazineTimer started" << std::endl;
118        this->bReloading_ = true;
119        this->magazineReloadTimer_.setTimer( magazineLoadingTime , false , this , createExecutor(createFunctor(&Weapon::magazineReloaded)));
120    }
121
122    void Weapon::bulletReloaded()
123    {
124        this->bReloading_ = false;
125        this->bulletReadyToShoot_ = true;
126    }
127
128    void Weapon::magazineReloaded()
129    {
130        this->bReloading_ = false;
131        this->munition_->fillBullets();
132        this->magazineReadyToShoot_ = true;
133        this->bulletReadyToShoot_ = true;
134    }
135
136
137    void Weapon::attachNeededMunition(std::string munitionName)
138    {
139COUT(0) << "Weapon::attachNeededMunition, parentWeaponSystem=" << this->parentWeaponSystem_ << std::endl;
140        //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
141        if (this->parentWeaponSystem_)
142        {
143COUT(0) << "Weapon::attachNeededMunition " << munitionName << std::endl;
144            Munition* munition = this->parentWeaponSystem_->getMunitionType(munitionName);
145            if ( munition )
146                this->munition_ = munition;
147            else
148            {
149                //create new munition with identifier
150COUT(0) << "Weapon::attachNeededMunition, create new Munition of Type " << munitionName << std::endl;
151                this->munitionIdentifier_ = ClassByString(munitionName);
152                this->munition_ = this->munitionIdentifier_.fabricate(this);
153                this->parentWeaponSystem_->setNewMunition(munitionName, this->munition_);
154            }
155        }
156    }
157
158
159     /*get and set functions
160     *
161     */
162
163    void Weapon::setMunitionType(std::string munitionType)
164    {   this->munitionType_ = munitionType; }
165
166    const std::string Weapon::getMunitionType()
167    {   return this->munitionType_;  }
168
169    void Weapon::setBulletLoadingTime(float loadingTime)
170    {   this->bulletLoadingTime_ = loadingTime; }
171
172    const float Weapon::getBulletLoadingTime()
173    {   return this->bulletLoadingTime_;  }
174
175    void Weapon::setMagazineLoadingTime(float loadingTime)
176    {   this->magazineLoadingTime_ = loadingTime; }
177
178    const float Weapon::getMagazineLoadingTime()
179    {   return this->magazineLoadingTime_;  }
180
181
182    Munition * Weapon::getAttachedMunition(std::string munitionType)
183    {   
184COUT(0) << "Weapon::getAttachedMunition, parentWeaponSystem_="<< this->parentWeaponSystem_ << std::endl;
185        this->munition_ = this->parentWeaponSystem_->getMunitionType(munitionType);
186COUT(0) << "Weapon::getAttachedMunition, munition_="<< this->munition_ << std::endl;
187return this->munition_; }
188
189    void Weapon::takeBullets() { };
190    void Weapon::createProjectile() { };
191    void Weapon::takeMagazines() { };
192
193}
Note: See TracBrowser for help on using the repository browser.