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