[2049] | 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 |
---|
[2918] | 24 | * Fabian 'x3n' Landau |
---|
[2049] | 25 | * Co-authors: |
---|
| 26 | * ... |
---|
| 27 | * |
---|
| 28 | */ |
---|
| 29 | |
---|
[2912] | 30 | #include "Weapon.h" |
---|
[2049] | 31 | |
---|
| 32 | #include "core/CoreIncludes.h" |
---|
| 33 | #include "core/XMLPort.h" |
---|
| 34 | |
---|
[2918] | 35 | #include "WeaponMode.h" |
---|
[2915] | 36 | #include "WeaponPack.h" |
---|
[2912] | 37 | #include "WeaponSystem.h" |
---|
[2096] | 38 | |
---|
[2049] | 39 | namespace orxonox |
---|
| 40 | { |
---|
[9667] | 41 | RegisterClass(Weapon); |
---|
[2662] | 42 | |
---|
[9667] | 43 | Weapon::Weapon(Context* context) : StaticEntity(context) |
---|
[2049] | 44 | { |
---|
| 45 | RegisterObject(Weapon); |
---|
[2893] | 46 | |
---|
[11071] | 47 | this->weaponPack_ = nullptr; |
---|
| 48 | this->weaponSlot_ = nullptr; |
---|
[2662] | 49 | this->bReloading_ = false; |
---|
[2918] | 50 | this->reloadingWeaponmode_ = WeaponSystem::WEAPON_MODE_UNASSIGNED; |
---|
[2912] | 51 | |
---|
[5929] | 52 | this->reloadTimer_.setTimer(0.0f, false, createExecutor(createFunctor(&Weapon::reloaded, this))); |
---|
[2918] | 53 | this->reloadTimer_.stopTimer(); |
---|
[2049] | 54 | } |
---|
| 55 | |
---|
| 56 | Weapon::~Weapon() |
---|
| 57 | { |
---|
[2918] | 58 | if (this->isInitialized()) |
---|
| 59 | { |
---|
| 60 | if (this->weaponPack_) |
---|
| 61 | this->weaponPack_->removeWeapon(this); |
---|
| 62 | |
---|
[11071] | 63 | for (const auto& mapEntry : this->weaponmodes_) |
---|
| 64 | mapEntry.second->destroy(); |
---|
[2918] | 65 | } |
---|
[2049] | 66 | } |
---|
[2096] | 67 | |
---|
[2662] | 68 | void Weapon::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[2096] | 69 | { |
---|
[2662] | 70 | SUPER(Weapon, XMLPort, xmlelement, mode); |
---|
[2915] | 71 | |
---|
[2918] | 72 | XMLPortObject(Weapon, WeaponMode, "", addWeaponmode, getWeaponmode, xmlelement, mode); |
---|
[2662] | 73 | } |
---|
[2096] | 74 | |
---|
[2918] | 75 | void Weapon::addWeaponmode(WeaponMode* weaponmode) |
---|
[2662] | 76 | { |
---|
[2918] | 77 | if (!weaponmode) |
---|
| 78 | return; |
---|
| 79 | |
---|
| 80 | this->weaponmodes_.insert(std::pair<unsigned int, WeaponMode*>(weaponmode->getMode(), weaponmode)); |
---|
| 81 | weaponmode->setWeapon(this); |
---|
[2049] | 82 | } |
---|
[2096] | 83 | |
---|
[2918] | 84 | WeaponMode* Weapon::getWeaponmode(unsigned int index) const |
---|
[2893] | 85 | { |
---|
[2918] | 86 | unsigned int i = 0; |
---|
[11071] | 87 | for (const auto& mapEntry : this->weaponmodes_) |
---|
[2918] | 88 | { |
---|
| 89 | if (i == index) |
---|
[11071] | 90 | return mapEntry.second; |
---|
[2918] | 91 | |
---|
| 92 | ++i; |
---|
| 93 | } |
---|
[11071] | 94 | return nullptr; |
---|
[2893] | 95 | } |
---|
[2662] | 96 | |
---|
[10650] | 97 | /** |
---|
| 98 | @brief |
---|
| 99 | Fire this Weapon with the the WeaponMode defined by @param mode |
---|
| 100 | */ |
---|
[11174] | 101 | void Weapon::push(unsigned int mode) |
---|
[2096] | 102 | { |
---|
[2918] | 103 | // To avoid firing with more than one mode at the same time, we lock the weapon (reloading) for |
---|
| 104 | // all modes except the one which is currently reloading. |
---|
| 105 | // |
---|
| 106 | // Example: |
---|
| 107 | // WeaponMode A -> mode 0 |
---|
| 108 | // WeaponMode B -> mode 0 |
---|
| 109 | // WeaponMode C -> mode 1 |
---|
| 110 | // |
---|
| 111 | // -> A and B can fire at the same time, but C has to wait until both (A and B) have reloaded |
---|
| 112 | // -> If C fires, A and B have to wait until C has reloaded |
---|
| 113 | // |
---|
| 114 | // Note: The reloading of each WeaponMode is internally handled by each A, B and C. |
---|
| 115 | // The reloading of the weapon is only performed to avoid firing with different modes at the same time. |
---|
[11170] | 116 | |
---|
| 117 | |
---|
[2918] | 118 | if (this->bReloading_ && this->reloadingWeaponmode_ != mode) |
---|
| 119 | return; |
---|
| 120 | |
---|
| 121 | std::multimap<unsigned int, WeaponMode*>::iterator start = this->weaponmodes_.lower_bound(mode); |
---|
| 122 | std::multimap<unsigned int, WeaponMode*>::iterator end = this->weaponmodes_.upper_bound(mode); |
---|
| 123 | |
---|
| 124 | for (std::multimap<unsigned int, WeaponMode*>::iterator it = start; it != end; ++it) |
---|
[2662] | 125 | { |
---|
[2918] | 126 | float reloading_time = 0; |
---|
[11174] | 127 | if (it->second->push(&reloading_time)) |
---|
[2662] | 128 | { |
---|
[2918] | 129 | this->bReloading_ = true; |
---|
| 130 | this->reloadingWeaponmode_ = mode; |
---|
| 131 | |
---|
| 132 | this->reloadTimer_.setInterval(reloading_time); |
---|
| 133 | this->reloadTimer_.startTimer(); |
---|
[2662] | 134 | } |
---|
| 135 | } |
---|
[2096] | 136 | } |
---|
[2097] | 137 | |
---|
[11164] | 138 | void Weapon::release(unsigned int mode) |
---|
[11166] | 139 | { |
---|
| 140 | if (this->bReloading_ && this->reloadingWeaponmode_ != mode) |
---|
| 141 | return; |
---|
[11164] | 142 | |
---|
[11166] | 143 | std::multimap<unsigned int, WeaponMode*>::iterator start = this->weaponmodes_.lower_bound(mode); |
---|
| 144 | std::multimap<unsigned int, WeaponMode*>::iterator end = this->weaponmodes_.upper_bound(mode); |
---|
| 145 | |
---|
| 146 | for (std::multimap<unsigned int, WeaponMode*>::iterator it = start; it != end; ++it) |
---|
| 147 | { |
---|
| 148 | float reloading_time = 0; |
---|
| 149 | if (it->second->release(&reloading_time)) |
---|
| 150 | { |
---|
| 151 | this->bReloading_ = true; |
---|
| 152 | this->reloadingWeaponmode_ = mode; |
---|
| 153 | |
---|
| 154 | this->reloadTimer_.setInterval(reloading_time); |
---|
| 155 | this->reloadTimer_.startTimer(); |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | } |
---|
| 159 | |
---|
[11052] | 160 | /** |
---|
| 161 | @brief |
---|
| 162 | Reload all @ref orxonox::WeaponMode weapon modes of this weapon. |
---|
| 163 | */ |
---|
[2918] | 164 | void Weapon::reload() |
---|
[2097] | 165 | { |
---|
[11071] | 166 | for (const auto& mapEntry : this->weaponmodes_) |
---|
| 167 | mapEntry.second->reload(); |
---|
[2662] | 168 | } |
---|
[2097] | 169 | |
---|
[2918] | 170 | void Weapon::reloaded() |
---|
[2662] | 171 | { |
---|
| 172 | this->bReloading_ = false; |
---|
[2918] | 173 | this->reloadingWeaponmode_ = WeaponSystem::WEAPON_MODE_UNASSIGNED; |
---|
[2097] | 174 | } |
---|
[2662] | 175 | |
---|
[2918] | 176 | void Weapon::notifyWeaponModes() |
---|
[2662] | 177 | { |
---|
[11071] | 178 | for (const auto& mapEntry : this->weaponmodes_) |
---|
| 179 | mapEntry.second->setWeapon(this); |
---|
[2662] | 180 | } |
---|
[11052] | 181 | |
---|
| 182 | void Weapon::updateMunition() |
---|
| 183 | { |
---|
[11071] | 184 | for (const auto& mapEnty : this->weaponmodes_) |
---|
| 185 | mapEnty.second->updateMunition(); |
---|
[11052] | 186 | } |
---|
[2096] | 187 | } |
---|