[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 | |
---|
[2912] | 29 | #include "WeaponPack.h" |
---|
[2710] | 30 | |
---|
| 31 | #include "core/CoreIncludes.h" |
---|
| 32 | #include "core/XMLPort.h" |
---|
| 33 | |
---|
[2912] | 34 | #include "Weapon.h" |
---|
[2914] | 35 | #include "WeaponSystem.h" |
---|
[2915] | 36 | #include "DefaultWeaponmodeLink.h" |
---|
[2912] | 37 | |
---|
[2710] | 38 | namespace orxonox |
---|
| 39 | { |
---|
| 40 | CreateFactory(WeaponPack); |
---|
| 41 | |
---|
| 42 | WeaponPack::WeaponPack(BaseObject* creator) : BaseObject(creator) |
---|
| 43 | { |
---|
| 44 | RegisterObject(WeaponPack); |
---|
| 45 | |
---|
[2912] | 46 | this->weaponSystem_ = 0; |
---|
[2710] | 47 | } |
---|
| 48 | |
---|
| 49 | WeaponPack::~WeaponPack() |
---|
| 50 | { |
---|
[2914] | 51 | if (this->isInitialized() && this->weaponSystem_) |
---|
[2915] | 52 | { |
---|
[2914] | 53 | this->weaponSystem_->removeWeaponPack(this); |
---|
[2915] | 54 | |
---|
| 55 | while (!this->weapons_.empty()) |
---|
[5929] | 56 | (*this->weapons_.begin())->destroy(); |
---|
[2915] | 57 | |
---|
| 58 | for (std::set<DefaultWeaponmodeLink*>::iterator it = this->links_.begin(); it != this->links_.end(); ) |
---|
[5929] | 59 | (*(it++))->destroy(); |
---|
[2915] | 60 | } |
---|
[2710] | 61 | } |
---|
| 62 | |
---|
[2912] | 63 | void WeaponPack::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[2710] | 64 | { |
---|
[2912] | 65 | SUPER(WeaponPack, XMLPort, xmlelement, mode); |
---|
| 66 | |
---|
[2918] | 67 | XMLPortObjectExtended(WeaponPack, Weapon, "", addWeapon, getWeapon, xmlelement, mode, false, false); |
---|
[2915] | 68 | XMLPortObject(WeaponPack, DefaultWeaponmodeLink, "links", addDefaultWeaponmodeLink, getDefaultWeaponmodeLink, xmlelement, mode); |
---|
[2912] | 69 | } |
---|
| 70 | |
---|
[2914] | 71 | void WeaponPack::fire(unsigned int weaponmode) |
---|
[2912] | 72 | { |
---|
[2914] | 73 | for (std::set<Weapon *>::iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it) |
---|
[2918] | 74 | (*it)->fire(weaponmode); |
---|
[2710] | 75 | } |
---|
| 76 | |
---|
[2918] | 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 | |
---|
[2914] | 83 | void WeaponPack::addWeapon(Weapon * weapon) |
---|
[2710] | 84 | { |
---|
[2914] | 85 | if (!weapon) |
---|
| 86 | return; |
---|
[2710] | 87 | |
---|
[2914] | 88 | this->weapons_.insert(weapon); |
---|
| 89 | weapon->setWeaponPack(this); |
---|
[2710] | 90 | } |
---|
| 91 | |
---|
[2915] | 92 | void WeaponPack::removeWeapon(Weapon * weapon) |
---|
| 93 | { |
---|
| 94 | if (!weapon) |
---|
| 95 | return; |
---|
| 96 | |
---|
| 97 | this->weapons_.erase(weapon); |
---|
| 98 | weapon->setWeaponPack(0); |
---|
| 99 | } |
---|
| 100 | |
---|
[2914] | 101 | Weapon * WeaponPack::getWeapon(unsigned int index) const |
---|
[2710] | 102 | { |
---|
[2914] | 103 | unsigned int i = 0; |
---|
[2710] | 104 | |
---|
[2914] | 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 | } |
---|
[2710] | 111 | |
---|
[2914] | 112 | return 0; |
---|
[2710] | 113 | } |
---|
| 114 | |
---|
[2915] | 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 | } |
---|
[2918] | 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 | } |
---|
[2710] | 147 | } |
---|