[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 | |
---|
| 29 | #include "OrxonoxStableHeaders.h" |
---|
[2912] | 30 | #include "WeaponPack.h" |
---|
[2710] | 31 | |
---|
| 32 | #include "core/CoreIncludes.h" |
---|
| 33 | #include "core/XMLPort.h" |
---|
| 34 | #include "objects/worldentities/pawns/Pawn.h" |
---|
| 35 | |
---|
[2912] | 36 | #include "Weapon.h" |
---|
| 37 | #include "WeaponSlot.h" |
---|
[2914] | 38 | #include "WeaponSystem.h" |
---|
[2915] | 39 | #include "DefaultWeaponmodeLink.h" |
---|
[2912] | 40 | |
---|
[2710] | 41 | namespace orxonox |
---|
| 42 | { |
---|
| 43 | CreateFactory(WeaponPack); |
---|
| 44 | |
---|
| 45 | WeaponPack::WeaponPack(BaseObject* creator) : BaseObject(creator) |
---|
| 46 | { |
---|
| 47 | RegisterObject(WeaponPack); |
---|
| 48 | |
---|
[2912] | 49 | this->weaponSystem_ = 0; |
---|
[2710] | 50 | |
---|
[2912] | 51 | COUT(0) << "+WeaponPack" << std::endl; |
---|
[2710] | 52 | } |
---|
| 53 | |
---|
| 54 | WeaponPack::~WeaponPack() |
---|
| 55 | { |
---|
[2912] | 56 | COUT(0) << "~WeaponPack" << std::endl; |
---|
[2914] | 57 | |
---|
| 58 | if (this->isInitialized() && this->weaponSystem_) |
---|
[2915] | 59 | { |
---|
[2914] | 60 | this->weaponSystem_->removeWeaponPack(this); |
---|
[2915] | 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 | } |
---|
[2710] | 68 | } |
---|
| 69 | |
---|
[2912] | 70 | void WeaponPack::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[2710] | 71 | { |
---|
[2912] | 72 | SUPER(WeaponPack, XMLPort, xmlelement, mode); |
---|
| 73 | |
---|
| 74 | XMLPortObject(WeaponPack, Weapon, "", addWeapon, getWeapon, xmlelement, mode); |
---|
[2915] | 75 | XMLPortObject(WeaponPack, DefaultWeaponmodeLink, "links", addDefaultWeaponmodeLink, getDefaultWeaponmodeLink, xmlelement, mode); |
---|
[2912] | 76 | } |
---|
| 77 | |
---|
[2914] | 78 | void WeaponPack::fire(unsigned int weaponmode) |
---|
[2912] | 79 | { |
---|
[2914] | 80 | for (std::set<Weapon *>::iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it) |
---|
| 81 | (*it)->fire(); |
---|
[2710] | 82 | } |
---|
| 83 | |
---|
[2914] | 84 | void WeaponPack::addWeapon(Weapon * weapon) |
---|
[2710] | 85 | { |
---|
[2914] | 86 | if (!weapon) |
---|
| 87 | return; |
---|
[2710] | 88 | |
---|
[2914] | 89 | this->weapons_.insert(weapon); |
---|
| 90 | weapon->setWeaponPack(this); |
---|
[2710] | 91 | } |
---|
| 92 | |
---|
[2915] | 93 | void WeaponPack::removeWeapon(Weapon * weapon) |
---|
| 94 | { |
---|
| 95 | if (!weapon) |
---|
| 96 | return; |
---|
| 97 | |
---|
| 98 | this->weapons_.erase(weapon); |
---|
| 99 | weapon->setWeaponPack(0); |
---|
| 100 | } |
---|
| 101 | |
---|
[2914] | 102 | Weapon * WeaponPack::getWeapon(unsigned int index) const |
---|
[2710] | 103 | { |
---|
[2914] | 104 | unsigned int i = 0; |
---|
[2710] | 105 | |
---|
[2914] | 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 | } |
---|
[2710] | 112 | |
---|
[2914] | 113 | return 0; |
---|
[2710] | 114 | } |
---|
| 115 | |
---|
[2915] | 116 | void WeaponPack::setWeaponSystemToAllWeapons() |
---|
[2710] | 117 | { |
---|
[2914] | 118 | for (std::set<Weapon *>::const_iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it) |
---|
[2915] | 119 | (*it)->setWeaponSystem(this->weaponSystem_); |
---|
[2710] | 120 | } |
---|
| 121 | |
---|
| 122 | void WeaponPack::attachNeededMunitionToAllWeapons() |
---|
| 123 | { |
---|
[2914] | 124 | for (std::set<Weapon *>::const_iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it) |
---|
[2710] | 125 | { |
---|
[2914] | 126 | (*it)->attachNeededMunition((*it)->getMunitionType()); |
---|
| 127 | (*it)->setWeapon(); |
---|
[2710] | 128 | } |
---|
| 129 | } |
---|
[2915] | 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 | } |
---|
[2710] | 157 | } |
---|