[10688] | 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: |
---|
[10724] | 23 | * Fabien Vultier |
---|
[10688] | 24 | * Co-authors: |
---|
[10724] | 25 | * ... |
---|
[10688] | 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "HUDWeaponSystem.h" |
---|
| 30 | |
---|
| 31 | #include "core/CoreIncludes.h" |
---|
| 32 | #include "core/XMLPort.h" |
---|
| 33 | #include "weaponsystem/WeaponSystem.h" |
---|
| 34 | #include "weaponsystem/WeaponPack.h" |
---|
| 35 | #include "weaponsystem/Weapon.h" |
---|
| 36 | #include "util/Convert.h" |
---|
| 37 | #include "core/class/Super.h" |
---|
| 38 | |
---|
| 39 | namespace orxonox |
---|
| 40 | { |
---|
| 41 | RegisterClass(HUDWeaponSystem); |
---|
| 42 | |
---|
| 43 | HUDWeaponSystem::HUDWeaponSystem(Context* context) : OrxonoxOverlay(context) |
---|
| 44 | { |
---|
| 45 | RegisterObject(HUDWeaponSystem); |
---|
| 46 | |
---|
[10721] | 47 | weaponModeHUDSize_ = Vector2(0.0f,0.0f); |
---|
| 48 | weaponModeHUDActualSize_ = Vector2(0.0f,0.0f); |
---|
| 49 | |
---|
[10688] | 50 | weapons_.clear(); |
---|
| 51 | hudWeapons_.clear(); |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | HUDWeaponSystem::~HUDWeaponSystem() |
---|
| 55 | { |
---|
| 56 | if (this->isInitialized()) |
---|
| 57 | { |
---|
| 58 | destroyHUDChilds(); |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | void HUDWeaponSystem::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
| 63 | { |
---|
| 64 | SUPER(HUDWeaponSystem, XMLPort, xmlelement, mode); |
---|
| 65 | |
---|
[10721] | 66 | XMLPortParam(HUDWeaponSystem, "weaponModeHUDSize", setWeaponModeHUDSize, getWeaponModeHUDSize, xmlelement, mode); |
---|
[10688] | 67 | } |
---|
| 68 | |
---|
| 69 | void HUDWeaponSystem::positionChanged() |
---|
| 70 | { |
---|
| 71 | OrxonoxOverlay::positionChanged(); |
---|
| 72 | |
---|
| 73 | positionHUDChilds(); |
---|
[10721] | 74 | } |
---|
[10688] | 75 | |
---|
[10721] | 76 | void HUDWeaponSystem::sizeChanged() |
---|
| 77 | { |
---|
| 78 | OrxonoxOverlay::sizeChanged(); |
---|
| 79 | |
---|
| 80 | weaponModeHUDActualSize_ = this->getActualSize(); |
---|
| 81 | |
---|
| 82 | positionHUDChilds(); |
---|
| 83 | } |
---|
| 84 | |
---|
[10688] | 85 | void HUDWeaponSystem::changedOwner() |
---|
| 86 | { |
---|
| 87 | SUPER(HUDWeaponSystem, changedOwner); |
---|
| 88 | |
---|
| 89 | this->owner_ = orxonox_cast<Pawn*>(this->getOwner()); |
---|
| 90 | |
---|
| 91 | updateWeaponList(); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void HUDWeaponSystem::changedOverlayGroup() |
---|
| 95 | { |
---|
| 96 | SUPER(HUDWeaponSystem, changedOverlayGroup); |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | void HUDWeaponSystem::changedVisibility() |
---|
| 100 | { |
---|
| 101 | SUPER(HUDWeaponSystem, changedVisibility); |
---|
[10794] | 102 | |
---|
| 103 | bool visible = this->isVisible(); |
---|
| 104 | |
---|
| 105 | for (std::vector<WeakPtr<HUDWeapon> >::iterator it = hudWeapons_.begin(); it != hudWeapons_.end(); ++it) |
---|
| 106 | { |
---|
| 107 | (*it)->setVisible(visible); |
---|
| 108 | } |
---|
[10688] | 109 | } |
---|
| 110 | |
---|
| 111 | void HUDWeaponSystem::changedName() |
---|
| 112 | { |
---|
| 113 | SUPER(HUDWeaponSystem, changedName); |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | void HUDWeaponSystem::updateWeaponList() |
---|
| 117 | { |
---|
| 118 | if (owner_ == NULL) |
---|
| 119 | { |
---|
| 120 | return; |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | weapons_.clear(); |
---|
| 124 | |
---|
| 125 | destroyHUDChilds(); |
---|
| 126 | |
---|
| 127 | std::vector<WeaponPack*>* weaponPacks = owner_->getAllWeaponPacks(); |
---|
| 128 | |
---|
| 129 | for (std::vector<WeaponPack*>::const_iterator itPacks = weaponPacks->begin(); itPacks != weaponPacks->end(); ++itPacks) |
---|
| 130 | { |
---|
| 131 | std::vector<Weapon*>* weapons = (*itPacks)->getAllWeapons(); |
---|
| 132 | |
---|
| 133 | for (std::vector<Weapon*>::const_iterator itWeapons = weapons->begin(); itWeapons != weapons->end(); ++itWeapons) |
---|
| 134 | { |
---|
| 135 | this->weapons_.push_back(*itWeapons); |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | createHUDChilds(); |
---|
[10721] | 140 | positionHUDChilds(); |
---|
[10688] | 141 | } |
---|
| 142 | |
---|
| 143 | void HUDWeaponSystem::createHUDChilds() |
---|
| 144 | { |
---|
[10721] | 145 | int positionIndex = 0; |
---|
| 146 | |
---|
[10688] | 147 | for (std::vector<WeakPtr<Weapon> >::iterator it = weapons_.begin(); it != weapons_.end(); ++it) |
---|
| 148 | { |
---|
[10721] | 149 | HUDWeapon* hudWeapon = new HUDWeapon(this->getContext()); |
---|
[10688] | 150 | hudWeapon->setOwner(owner_); |
---|
[10721] | 151 | hudWeapon->setOverlayGroup(this->getOverlayGroup()); |
---|
[10688] | 152 | hudWeapon->setWeapon(*it); |
---|
[10721] | 153 | hudWeapon->setAspectCorrection(false); |
---|
| 154 | hudWeapon->setPickPoint(Vector2(0.0f,0.0f)); |
---|
| 155 | |
---|
[10688] | 156 | hudWeapons_.push_back(hudWeapon); |
---|
[10721] | 157 | |
---|
| 158 | ++ positionIndex; |
---|
| 159 | } |
---|
[10688] | 160 | } |
---|
| 161 | |
---|
| 162 | void HUDWeaponSystem::positionHUDChilds() |
---|
| 163 | { |
---|
| 164 | int positionIndex = 0; |
---|
[10739] | 165 | Vector2 offset = this->getPosition(); |
---|
[10688] | 166 | |
---|
| 167 | for (std::vector<WeakPtr<HUDWeapon> >::iterator it = hudWeapons_.begin(); it != hudWeapons_.end(); ++it) |
---|
| 168 | { |
---|
[10739] | 169 | (*it)->setPositionOffset(offset); |
---|
[10721] | 170 | (*it)->setWeaponIndex(positionIndex); |
---|
| 171 | (*it)->setWeaponModeHUDActualSize(this->weaponModeHUDActualSize_); |
---|
[10688] | 172 | |
---|
| 173 | ++ positionIndex; |
---|
[10721] | 174 | } |
---|
[10688] | 175 | } |
---|
| 176 | |
---|
| 177 | void HUDWeaponSystem::destroyHUDChilds() |
---|
| 178 | { |
---|
| 179 | for (std::vector<WeakPtr<HUDWeapon> >::iterator it = hudWeapons_.begin(); it != hudWeapons_.end(); ++it) |
---|
| 180 | { |
---|
| 181 | (*it)->destroy(); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | hudWeapons_.clear(); |
---|
[10721] | 185 | } |
---|
[10688] | 186 | } |
---|