| 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 |     RegisterClass(WeaponPack); | 
|---|
| 41 |  | 
|---|
| 42 |     WeaponPack::WeaponPack(Context* context) : BaseObject(context) | 
|---|
| 43 |     { | 
|---|
| 44 |         RegisterObject(WeaponPack); | 
|---|
| 45 |  | 
|---|
| 46 |         this->weaponSystem_ = 0; | 
|---|
| 47 |     } | 
|---|
| 48 |  | 
|---|
| 49 |     WeaponPack::~WeaponPack() | 
|---|
| 50 |     { | 
|---|
| 51 |         if (this->isInitialized()) | 
|---|
| 52 |         { | 
|---|
| 53 |             if( this->weaponSystem_ ) | 
|---|
| 54 |                 this->weaponSystem_->removeWeaponPack(this); | 
|---|
| 55 |  | 
|---|
| 56 |             while (!this->weapons_.empty()) | 
|---|
| 57 |                 (*this->weapons_.begin())->destroy(); | 
|---|
| 58 |  | 
|---|
| 59 |             for (std::set<DefaultWeaponmodeLink*>::iterator it = this->links_.begin(); it != this->links_.end(); ) | 
|---|
| 60 |                 (*(it++))->destroy(); | 
|---|
| 61 |         } | 
|---|
| 62 |     } | 
|---|
| 63 |  | 
|---|
| 64 |     void WeaponPack::XMLPort(Element& xmlelement, XMLPort::Mode mode) | 
|---|
| 65 |     { | 
|---|
| 66 |         SUPER(WeaponPack, XMLPort, xmlelement, mode); | 
|---|
| 67 |  | 
|---|
| 68 |         XMLPortObjectExtended(WeaponPack, Weapon, "", addWeapon, getWeapon, xmlelement, mode, false, false); | 
|---|
| 69 |         XMLPortObject(WeaponPack, DefaultWeaponmodeLink, "links", addDefaultWeaponmodeLink, getDefaultWeaponmodeLink, xmlelement, mode); | 
|---|
| 70 |     } | 
|---|
| 71 |  | 
|---|
| 72 |     /** | 
|---|
| 73 |     @brief | 
|---|
| 74 |         Fire all weapons in this WeaponSet with the defined weaponmode. | 
|---|
| 75 |     */ | 
|---|
| 76 |     void WeaponPack::fire(unsigned int weaponmode) | 
|---|
| 77 |     { | 
|---|
| 78 |         for (std::vector<Weapon *>::iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it) | 
|---|
| 79 |             (*it)->fire(weaponmode); | 
|---|
| 80 |     } | 
|---|
| 81 |  | 
|---|
| 82 |     /** | 
|---|
| 83 |     @brief | 
|---|
| 84 |         Reload all weapons in this WeaponSet. | 
|---|
| 85 |     */ | 
|---|
| 86 |     void WeaponPack::reload() | 
|---|
| 87 |     { | 
|---|
| 88 |         for (std::vector<Weapon *>::iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it) | 
|---|
| 89 |             (*it)->reload(); | 
|---|
| 90 |     } | 
|---|
| 91 |  | 
|---|
| 92 |     void WeaponPack::addWeapon(Weapon * weapon) | 
|---|
| 93 |     { | 
|---|
| 94 |         if (!weapon) | 
|---|
| 95 |             return; | 
|---|
| 96 |  | 
|---|
| 97 |         this->weapons_.push_back(weapon); | 
|---|
| 98 |         weapon->setWeaponPack(this); | 
|---|
| 99 |     } | 
|---|
| 100 |  | 
|---|
| 101 |     void WeaponPack::removeWeapon(Weapon * weapon) | 
|---|
| 102 |     { | 
|---|
| 103 |         if (!weapon) | 
|---|
| 104 |             return; | 
|---|
| 105 |  | 
|---|
| 106 |         std::vector<Weapon*>::iterator it = std::find(this->weapons_.begin(), this->weapons_.end(), weapon); | 
|---|
| 107 |         assert(it != this->weapons_.end()); | 
|---|
| 108 |         this->weapons_.erase(it); | 
|---|
| 109 |         weapon->setWeaponPack(0); | 
|---|
| 110 |     } | 
|---|
| 111 |  | 
|---|
| 112 |     Weapon * WeaponPack::getWeapon(unsigned int index) const | 
|---|
| 113 |     { | 
|---|
| 114 |         unsigned int i = 0; | 
|---|
| 115 |  | 
|---|
| 116 |         for (std::vector<Weapon *>::const_iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it) | 
|---|
| 117 |         { | 
|---|
| 118 |             if (i == index) | 
|---|
| 119 |                 return (*it); | 
|---|
| 120 |             ++i; | 
|---|
| 121 |         } | 
|---|
| 122 |  | 
|---|
| 123 |         return 0; | 
|---|
| 124 |     } | 
|---|
| 125 |  | 
|---|
| 126 |     void WeaponPack::addDefaultWeaponmodeLink(DefaultWeaponmodeLink* link) | 
|---|
| 127 |     { | 
|---|
| 128 |         this->links_.insert(link); | 
|---|
| 129 |     } | 
|---|
| 130 |  | 
|---|
| 131 |     DefaultWeaponmodeLink* WeaponPack::getDefaultWeaponmodeLink(unsigned int index) const | 
|---|
| 132 |     { | 
|---|
| 133 |         unsigned int i = 0; | 
|---|
| 134 |         for (std::set<DefaultWeaponmodeLink*>::const_iterator it = this->links_.begin(); it != this->links_.end(); ++it) | 
|---|
| 135 |         { | 
|---|
| 136 |             if (i == index) | 
|---|
| 137 |                 return (*it); | 
|---|
| 138 |  | 
|---|
| 139 |             ++i; | 
|---|
| 140 |         } | 
|---|
| 141 |         return 0; | 
|---|
| 142 |     } | 
|---|
| 143 |  | 
|---|
| 144 |     unsigned int WeaponPack::getDesiredWeaponmode(unsigned int firemode) const | 
|---|
| 145 |     { | 
|---|
| 146 |         for (std::set<DefaultWeaponmodeLink*>::const_iterator it = this->links_.begin(); it != this->links_.end(); ++it) | 
|---|
| 147 |             if ((*it)->getFiremode() == firemode) | 
|---|
| 148 |                 return (*it)->getWeaponmode(); | 
|---|
| 149 |  | 
|---|
| 150 |         return WeaponSystem::WEAPON_MODE_UNASSIGNED; | 
|---|
| 151 |     } | 
|---|
| 152 |  | 
|---|
| 153 |     void WeaponPack::notifyWeapons() | 
|---|
| 154 |     { | 
|---|
| 155 |         for (std::vector<Weapon *>::const_iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it) | 
|---|
| 156 |             (*it)->setWeaponPack(this); | 
|---|
| 157 |     } | 
|---|
| 158 | } | 
|---|