[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 | |
---|
| 30 | #include "OrxonoxStableHeaders.h" |
---|
[2912] | 31 | #include "Weapon.h" |
---|
[2049] | 32 | |
---|
| 33 | #include "core/CoreIncludes.h" |
---|
| 34 | #include "core/XMLPort.h" |
---|
| 35 | |
---|
[2918] | 36 | #include "WeaponMode.h" |
---|
[2915] | 37 | #include "WeaponPack.h" |
---|
[2912] | 38 | #include "WeaponSystem.h" |
---|
[2096] | 39 | |
---|
[2049] | 40 | namespace orxonox |
---|
| 41 | { |
---|
[2662] | 42 | CreateFactory(Weapon); |
---|
| 43 | |
---|
| 44 | Weapon::Weapon(BaseObject* creator) : StaticEntity(creator) |
---|
[2049] | 45 | { |
---|
| 46 | RegisterObject(Weapon); |
---|
[2893] | 47 | |
---|
[2914] | 48 | this->weaponPack_ = 0; |
---|
| 49 | this->weaponSlot_ = 0; |
---|
[2662] | 50 | this->bReloading_ = false; |
---|
[2918] | 51 | this->reloadingWeaponmode_ = WeaponSystem::WEAPON_MODE_UNASSIGNED; |
---|
[2912] | 52 | |
---|
[2918] | 53 | this->reloadTimer_.setTimer(0.0f, false, this, createExecutor(createFunctor(&Weapon::reloaded))); |
---|
| 54 | this->reloadTimer_.stopTimer(); |
---|
[2049] | 55 | } |
---|
| 56 | |
---|
| 57 | Weapon::~Weapon() |
---|
| 58 | { |
---|
[2918] | 59 | if (this->isInitialized()) |
---|
| 60 | { |
---|
| 61 | if (this->weaponPack_) |
---|
| 62 | this->weaponPack_->removeWeapon(this); |
---|
| 63 | |
---|
| 64 | for (std::multimap<unsigned int, WeaponMode*>::iterator it = this->weaponmodes_.begin(); it != this->weaponmodes_.end(); ++it) |
---|
| 65 | delete it->second; |
---|
| 66 | } |
---|
[2049] | 67 | } |
---|
[2096] | 68 | |
---|
[2662] | 69 | void Weapon::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[2096] | 70 | { |
---|
[2662] | 71 | SUPER(Weapon, XMLPort, xmlelement, mode); |
---|
[2915] | 72 | |
---|
[2918] | 73 | XMLPortObject(Weapon, WeaponMode, "", addWeaponmode, getWeaponmode, xmlelement, mode); |
---|
[2662] | 74 | } |
---|
[2096] | 75 | |
---|
[2918] | 76 | void Weapon::addWeaponmode(WeaponMode* weaponmode) |
---|
[2662] | 77 | { |
---|
[2918] | 78 | if (!weaponmode) |
---|
| 79 | return; |
---|
| 80 | |
---|
| 81 | this->weaponmodes_.insert(std::pair<unsigned int, WeaponMode*>(weaponmode->getMode(), weaponmode)); |
---|
| 82 | weaponmode->setWeapon(this); |
---|
[2049] | 83 | } |
---|
[2096] | 84 | |
---|
[2918] | 85 | WeaponMode* Weapon::getWeaponmode(unsigned int index) const |
---|
[2893] | 86 | { |
---|
[2918] | 87 | unsigned int i = 0; |
---|
| 88 | for (std::multimap<unsigned int, WeaponMode*>::const_iterator it = this->weaponmodes_.begin(); it != this->weaponmodes_.end(); ++it) |
---|
| 89 | { |
---|
| 90 | if (i == index) |
---|
| 91 | return it->second; |
---|
| 92 | |
---|
| 93 | ++i; |
---|
| 94 | } |
---|
| 95 | return 0; |
---|
[2893] | 96 | } |
---|
[2662] | 97 | |
---|
[2918] | 98 | void Weapon::fire(unsigned int mode) |
---|
[2096] | 99 | { |
---|
[2918] | 100 | // To avoid firing with more than one mode at the same time, we lock the weapon (reloading) for |
---|
| 101 | // all modes except the one which is currently reloading. |
---|
| 102 | // |
---|
| 103 | // Example: |
---|
| 104 | // WeaponMode A -> mode 0 |
---|
| 105 | // WeaponMode B -> mode 0 |
---|
| 106 | // WeaponMode C -> mode 1 |
---|
| 107 | // |
---|
| 108 | // -> A and B can fire at the same time, but C has to wait until both (A and B) have reloaded |
---|
| 109 | // -> If C fires, A and B have to wait until C has reloaded |
---|
| 110 | // |
---|
| 111 | // Note: The reloading of each WeaponMode is internally handled by each A, B and C. |
---|
| 112 | // The reloading of the weapon is only performed to avoid firing with different modes at the same time. |
---|
| 113 | if (this->bReloading_ && this->reloadingWeaponmode_ != mode) |
---|
| 114 | return; |
---|
| 115 | |
---|
| 116 | std::multimap<unsigned int, WeaponMode*>::iterator start = this->weaponmodes_.lower_bound(mode); |
---|
| 117 | std::multimap<unsigned int, WeaponMode*>::iterator end = this->weaponmodes_.upper_bound(mode); |
---|
| 118 | |
---|
| 119 | for (std::multimap<unsigned int, WeaponMode*>::iterator it = start; it != end; ++it) |
---|
[2662] | 120 | { |
---|
[2918] | 121 | float reloading_time = 0; |
---|
| 122 | if (it->second->fire(&reloading_time)) |
---|
[2662] | 123 | { |
---|
[2918] | 124 | this->bReloading_ = true; |
---|
| 125 | this->reloadingWeaponmode_ = mode; |
---|
| 126 | |
---|
| 127 | this->reloadTimer_.setInterval(reloading_time); |
---|
| 128 | this->reloadTimer_.startTimer(); |
---|
[2662] | 129 | } |
---|
| 130 | } |
---|
[2096] | 131 | } |
---|
[2097] | 132 | |
---|
[2918] | 133 | void Weapon::reload() |
---|
[2097] | 134 | { |
---|
[2918] | 135 | for (std::multimap<unsigned int, WeaponMode*>::iterator it = this->weaponmodes_.begin(); it != this->weaponmodes_.end(); ++it) |
---|
| 136 | it->second->reload(); |
---|
[2662] | 137 | } |
---|
[2097] | 138 | |
---|
[2918] | 139 | void Weapon::reloaded() |
---|
[2662] | 140 | { |
---|
| 141 | this->bReloading_ = false; |
---|
[2918] | 142 | this->reloadingWeaponmode_ = WeaponSystem::WEAPON_MODE_UNASSIGNED; |
---|
[2097] | 143 | } |
---|
[2662] | 144 | |
---|
[2918] | 145 | void Weapon::notifyWeaponModes() |
---|
[2662] | 146 | { |
---|
[2918] | 147 | for (std::multimap<unsigned int, WeaponMode*>::iterator it = this->weaponmodes_.begin(); it != this->weaponmodes_.end(); ++it) |
---|
| 148 | it->second->setWeapon(this); |
---|
[2662] | 149 | } |
---|
[2096] | 150 | } |
---|