Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/weapons/src/orxonox/objects/weaponSystem/WeaponPack.cc @ 2915

Last change on this file since 2915 was 2915, checked in by landauf, 15 years ago
  • switched back to std::vector for the WeaponSlots to keep them in the same order as in the XML file
  • added DefaultWeaponmodeLink, a class which links weaponmodes (a property of a Weapon or a WeaponPack) with firemodes (one firemode corresponds to one WeaponSet). This can be changed later (for example in a nice GUI), but DefaultWeaponmodeLink defines the default value.
  • Property svn:eol-style set to native
File size: 4.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 "WeaponPack.h"
31
32#include "core/CoreIncludes.h"
33#include "core/XMLPort.h"
34#include "objects/worldentities/pawns/Pawn.h"
35
36#include "Weapon.h"
37#include "WeaponSlot.h"
38#include "WeaponSystem.h"
39#include "DefaultWeaponmodeLink.h"
40
41namespace orxonox
42{
43    CreateFactory(WeaponPack);
44
45    WeaponPack::WeaponPack(BaseObject* creator) : BaseObject(creator)
46    {
47        RegisterObject(WeaponPack);
48
49        this->weaponSystem_ = 0;
50
51COUT(0) << "+WeaponPack" << std::endl;
52    }
53
54    WeaponPack::~WeaponPack()
55    {
56COUT(0) << "~WeaponPack" << std::endl;
57
58        if (this->isInitialized() && this->weaponSystem_)
59        {
60            this->weaponSystem_->removeWeaponPack(this);
61
62            while (!this->weapons_.empty())
63                delete (*this->weapons_.begin());
64
65            for (std::set<DefaultWeaponmodeLink*>::iterator it = this->links_.begin(); it != this->links_.end(); )
66                delete (*(it++));
67        }
68    }
69
70    void WeaponPack::XMLPort(Element& xmlelement, XMLPort::Mode mode)
71    {
72        SUPER(WeaponPack, XMLPort, xmlelement, mode);
73
74        XMLPortObject(WeaponPack, Weapon, "", addWeapon, getWeapon, xmlelement, mode);
75        XMLPortObject(WeaponPack, DefaultWeaponmodeLink, "links", addDefaultWeaponmodeLink, getDefaultWeaponmodeLink, xmlelement, mode);
76    }
77
78    void WeaponPack::fire(unsigned int weaponmode)
79    {
80        for (std::set<Weapon *>::iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
81            (*it)->fire();
82    }
83
84    void WeaponPack::addWeapon(Weapon * weapon)
85    {
86        if (!weapon)
87            return;
88
89        this->weapons_.insert(weapon);
90        weapon->setWeaponPack(this);
91    }
92
93    void WeaponPack::removeWeapon(Weapon * weapon)
94    {
95        if (!weapon)
96            return;
97
98        this->weapons_.erase(weapon);
99        weapon->setWeaponPack(0);
100    }
101
102    Weapon * WeaponPack::getWeapon(unsigned int index) const
103    {
104        unsigned int i = 0;
105
106        for (std::set<Weapon *>::const_iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
107        {
108            if (i == index)
109                return (*it);
110            ++i;
111        }
112
113        return 0;
114    }
115
116    void WeaponPack::setWeaponSystemToAllWeapons()
117    {
118        for (std::set<Weapon *>::const_iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
119            (*it)->setWeaponSystem(this->weaponSystem_);
120    }
121
122    void WeaponPack::attachNeededMunitionToAllWeapons()
123    {
124        for (std::set<Weapon *>::const_iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
125        {
126            (*it)->attachNeededMunition((*it)->getMunitionType());
127            (*it)->setWeapon();
128        }
129    }
130
131    void WeaponPack::addDefaultWeaponmodeLink(DefaultWeaponmodeLink* link)
132    {
133        this->links_.insert(link);
134    }
135
136    DefaultWeaponmodeLink* WeaponPack::getDefaultWeaponmodeLink(unsigned int index) const
137    {
138        unsigned int i = 0;
139        for (std::set<DefaultWeaponmodeLink*>::const_iterator it = this->links_.begin(); it != this->links_.end(); ++it)
140        {
141            if (i == index)
142                return (*it);
143
144            ++i;
145        }
146        return 0;
147    }
148
149    unsigned int WeaponPack::getDesiredWeaponmode(unsigned int firemode) const
150    {
151        for (std::set<DefaultWeaponmodeLink*>::const_iterator it = this->links_.begin(); it != this->links_.end(); ++it)
152            if ((*it)->getFiremode() == firemode)
153                return (*it)->getWeaponmode();
154
155        return WeaponSystem::WEAPON_MODE_UNASSIGNED;
156    }
157}
Note: See TracBrowser for help on using the repository browser.