Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/weaponsystem/WeaponPack.cc @ 3053

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

merged weapons branch back to trunk

  • Property svn:eol-style set to native
File size: 4.3 KB
RevLine 
[2710]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:
[2915]25 *      ...
26 *
[2710]27 */
28
29#include "OrxonoxStableHeaders.h"
[2912]30#include "WeaponPack.h"
[2710]31
32#include "core/CoreIncludes.h"
33#include "core/XMLPort.h"
34#include "objects/worldentities/pawns/Pawn.h"
35
[2912]36#include "Weapon.h"
37#include "WeaponSlot.h"
[2914]38#include "WeaponSystem.h"
[2915]39#include "DefaultWeaponmodeLink.h"
[2912]40
[2710]41namespace orxonox
42{
43    CreateFactory(WeaponPack);
44
45    WeaponPack::WeaponPack(BaseObject* creator) : BaseObject(creator)
46    {
47        RegisterObject(WeaponPack);
48
[2912]49        this->weaponSystem_ = 0;
[2710]50    }
51
52    WeaponPack::~WeaponPack()
53    {
[2914]54        if (this->isInitialized() && this->weaponSystem_)
[2915]55        {
[2914]56            this->weaponSystem_->removeWeaponPack(this);
[2915]57
58            while (!this->weapons_.empty())
59                delete (*this->weapons_.begin());
60
61            for (std::set<DefaultWeaponmodeLink*>::iterator it = this->links_.begin(); it != this->links_.end(); )
62                delete (*(it++));
63        }
[2710]64    }
65
[2912]66    void WeaponPack::XMLPort(Element& xmlelement, XMLPort::Mode mode)
[2710]67    {
[2912]68        SUPER(WeaponPack, XMLPort, xmlelement, mode);
69
[2918]70        XMLPortObjectExtended(WeaponPack, Weapon, "", addWeapon, getWeapon, xmlelement, mode, false, false);
[2915]71        XMLPortObject(WeaponPack, DefaultWeaponmodeLink, "links", addDefaultWeaponmodeLink, getDefaultWeaponmodeLink, xmlelement, mode);
[2912]72    }
73
[2914]74    void WeaponPack::fire(unsigned int weaponmode)
[2912]75    {
[2914]76        for (std::set<Weapon *>::iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
[2918]77            (*it)->fire(weaponmode);
[2710]78    }
79
[2918]80    void WeaponPack::reload()
81    {
82        for (std::set<Weapon *>::iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
83            (*it)->reload();
84    }
85
[2914]86    void WeaponPack::addWeapon(Weapon * weapon)
[2710]87    {
[2914]88        if (!weapon)
89            return;
[2710]90
[2914]91        this->weapons_.insert(weapon);
92        weapon->setWeaponPack(this);
[2710]93    }
94
[2915]95    void WeaponPack::removeWeapon(Weapon * weapon)
96    {
97        if (!weapon)
98            return;
99
100        this->weapons_.erase(weapon);
101        weapon->setWeaponPack(0);
102    }
103
[2914]104    Weapon * WeaponPack::getWeapon(unsigned int index) const
[2710]105    {
[2914]106        unsigned int i = 0;
[2710]107
[2914]108        for (std::set<Weapon *>::const_iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
109        {
110            if (i == index)
111                return (*it);
112            ++i;
113        }
[2710]114
[2914]115        return 0;
[2710]116    }
117
[2915]118    void WeaponPack::addDefaultWeaponmodeLink(DefaultWeaponmodeLink* link)
119    {
120        this->links_.insert(link);
121    }
122
123    DefaultWeaponmodeLink* WeaponPack::getDefaultWeaponmodeLink(unsigned int index) const
124    {
125        unsigned int i = 0;
126        for (std::set<DefaultWeaponmodeLink*>::const_iterator it = this->links_.begin(); it != this->links_.end(); ++it)
127        {
128            if (i == index)
129                return (*it);
130
131            ++i;
132        }
133        return 0;
134    }
135
136    unsigned int WeaponPack::getDesiredWeaponmode(unsigned int firemode) const
137    {
138        for (std::set<DefaultWeaponmodeLink*>::const_iterator it = this->links_.begin(); it != this->links_.end(); ++it)
139            if ((*it)->getFiremode() == firemode)
140                return (*it)->getWeaponmode();
141
142        return WeaponSystem::WEAPON_MODE_UNASSIGNED;
143    }
[2918]144
145    void WeaponPack::notifyWeapons()
146    {
147        for (std::set<Weapon *>::const_iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
148            (*it)->setWeaponPack(this);
149    }
[2710]150}
Note: See TracBrowser for help on using the repository browser.