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