Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/questsystem5/src/orxonox/objects/weaponSystem/Weapon.cc @ 3012

Last change on this file since 3012 was 2908, checked in by dafrick, 16 years ago

Reverted to revision 2906 (because I'm too stupid to merge correctly, 2nd try will follow shortly. ;))

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