[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 |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
| 29 | #include "OrxonoxStableHeaders.h" |
---|
[2912] | 30 | #include "Weapon.h" |
---|
[2049] | 31 | |
---|
| 32 | #include "core/CoreIncludes.h" |
---|
| 33 | #include "core/XMLPort.h" |
---|
| 34 | |
---|
[2912] | 35 | #include "Munition.h" |
---|
[2915] | 36 | #include "WeaponPack.h" |
---|
[2912] | 37 | #include "WeaponSystem.h" |
---|
[2096] | 38 | |
---|
[2049] | 39 | namespace orxonox |
---|
| 40 | { |
---|
[2662] | 41 | CreateFactory(Weapon); |
---|
| 42 | |
---|
| 43 | Weapon::Weapon(BaseObject* creator) : StaticEntity(creator) |
---|
[2049] | 44 | { |
---|
| 45 | RegisterObject(Weapon); |
---|
[2893] | 46 | |
---|
[2662] | 47 | this->bulletReadyToShoot_ = true; |
---|
| 48 | this->magazineReadyToShoot_ = true; |
---|
[2912] | 49 | this->weaponSystem_ = 0; |
---|
[2914] | 50 | this->weaponPack_ = 0; |
---|
| 51 | this->weaponSlot_ = 0; |
---|
[2662] | 52 | this->bulletLoadingTime_ = 0; |
---|
| 53 | this->magazineLoadingTime_ = 0; |
---|
| 54 | this->bReloading_ = false; |
---|
[2893] | 55 | this->bulletAmount_= 0; |
---|
| 56 | this->magazineAmount_ = 0; |
---|
| 57 | this->munition_ = 0; |
---|
| 58 | this->unlimitedMunition_ = false; |
---|
[2662] | 59 | this->setObjectMode(0x0); |
---|
[2912] | 60 | |
---|
| 61 | COUT(0) << "+Weapon" << std::endl; |
---|
[2049] | 62 | } |
---|
| 63 | |
---|
| 64 | Weapon::~Weapon() |
---|
| 65 | { |
---|
[2912] | 66 | COUT(0) << "~Weapon" << std::endl; |
---|
[2915] | 67 | if (this->isInitialized() && this->weaponPack_) |
---|
| 68 | this->weaponPack_->removeWeapon(this); |
---|
[2049] | 69 | } |
---|
[2096] | 70 | |
---|
[2662] | 71 | void Weapon::XMLPort(Element& xmlelement, XMLPort::Mode mode) |
---|
[2096] | 72 | { |
---|
[2662] | 73 | SUPER(Weapon, XMLPort, xmlelement, mode); |
---|
[2915] | 74 | |
---|
[2662] | 75 | XMLPortParam(Weapon, "munitionType", setMunitionType, getMunitionType, xmlelement, mode); |
---|
| 76 | XMLPortParam(Weapon, "bulletLoadingTime", setBulletLoadingTime, getBulletLoadingTime, xmlelement, mode); |
---|
| 77 | XMLPortParam(Weapon, "magazineLoadingTime", setMagazineLoadingTime, getMagazineLoadingTime, xmlelement, mode); |
---|
[2893] | 78 | XMLPortParam(Weapon, "bullets", setBulletAmount, getBulletAmount, xmlelement, mode); |
---|
| 79 | XMLPortParam(Weapon, "magazines", setMagazineAmount, getMagazineAmount, xmlelement, mode); |
---|
| 80 | XMLPortParam(Weapon, "unlimitedMunition", setUnlimitedMunition, getUnlimitedMunition, xmlelement, mode); |
---|
[2662] | 81 | } |
---|
[2096] | 82 | |
---|
[2662] | 83 | void Weapon::setWeapon() |
---|
| 84 | { |
---|
| 85 | this->munition_->fillBullets(); |
---|
| 86 | this->munition_->fillMagazines(); |
---|
[2049] | 87 | } |
---|
[2096] | 88 | |
---|
[2893] | 89 | void Weapon::setMunition() |
---|
| 90 | { |
---|
| 91 | this->munition_->setMaxBullets(this->bulletAmount_); |
---|
| 92 | this->munition_->setMaxMagazines(this->magazineAmount_); |
---|
| 93 | } |
---|
[2662] | 94 | |
---|
| 95 | void Weapon::fire() |
---|
[2096] | 96 | { |
---|
[2662] | 97 | if ( this->bulletReadyToShoot_ && this->magazineReadyToShoot_ && !this->bReloading_) |
---|
| 98 | { |
---|
| 99 | this->bulletReadyToShoot_ = false; |
---|
[2893] | 100 | if ( this->unlimitedMunition_== true ) |
---|
[2662] | 101 | { |
---|
| 102 | //shoot |
---|
[2893] | 103 | this->reloadBullet(); |
---|
[2662] | 104 | this->createProjectile(); |
---|
| 105 | } |
---|
| 106 | else |
---|
| 107 | { |
---|
[2893] | 108 | if ( this->munition_->bullets() > 0) |
---|
| 109 | { |
---|
| 110 | //shoot and reload |
---|
| 111 | this->takeBullets(); |
---|
| 112 | this->reloadBullet(); |
---|
| 113 | this->createProjectile(); |
---|
| 114 | } |
---|
| 115 | //if there are no bullets, but magazines |
---|
| 116 | else if ( this->munition_->magazines() > 0 && this->munition_->bullets() == 0 ) |
---|
| 117 | { |
---|
| 118 | //reload magazine |
---|
| 119 | this->takeMagazines(); |
---|
| 120 | this->reloadMagazine(); |
---|
| 121 | } |
---|
| 122 | else |
---|
| 123 | { |
---|
| 124 | //no magazines |
---|
| 125 | } |
---|
[2662] | 126 | } |
---|
| 127 | } |
---|
| 128 | else |
---|
| 129 | { |
---|
[2893] | 130 | //weapon not reloaded |
---|
[2662] | 131 | } |
---|
[2096] | 132 | |
---|
| 133 | } |
---|
[2097] | 134 | |
---|
[2662] | 135 | |
---|
[2893] | 136 | //weapon reloading |
---|
[2662] | 137 | void Weapon::bulletTimer(float bulletLoadingTime) |
---|
[2097] | 138 | { |
---|
[2662] | 139 | this->bReloading_ = true; |
---|
| 140 | this->bulletReloadTimer_.setTimer( bulletLoadingTime , false , this , createExecutor(createFunctor(&Weapon::bulletReloaded))); |
---|
| 141 | } |
---|
| 142 | void Weapon::magazineTimer(float magazineLoadingTime) |
---|
| 143 | { |
---|
| 144 | this->bReloading_ = true; |
---|
| 145 | this->magazineReloadTimer_.setTimer( magazineLoadingTime , false , this , createExecutor(createFunctor(&Weapon::magazineReloaded))); |
---|
| 146 | } |
---|
[2097] | 147 | |
---|
[2662] | 148 | void Weapon::bulletReloaded() |
---|
| 149 | { |
---|
| 150 | this->bReloading_ = false; |
---|
| 151 | this->bulletReadyToShoot_ = true; |
---|
[2097] | 152 | } |
---|
[2662] | 153 | |
---|
| 154 | void Weapon::magazineReloaded() |
---|
| 155 | { |
---|
| 156 | this->bReloading_ = false; |
---|
| 157 | this->munition_->fillBullets(); |
---|
| 158 | } |
---|
| 159 | |
---|
| 160 | |
---|
[2893] | 161 | |
---|
[2912] | 162 | void Weapon::attachNeededMunition(const std::string& munitionName) |
---|
[2662] | 163 | { |
---|
[2893] | 164 | /* if munition type already exists attach it, else create a new one of this type and attach it to the weapon and to the WeaponSystem |
---|
| 165 | */ |
---|
[2912] | 166 | if (this->weaponSystem_) |
---|
[2662] | 167 | { |
---|
[2893] | 168 | //getMunitionType returns 0 if there is no such munitionType |
---|
[2912] | 169 | Munition* munition = this->weaponSystem_->getMunitionType(munitionName); |
---|
[2662] | 170 | if ( munition ) |
---|
[2893] | 171 | { |
---|
[2662] | 172 | this->munition_ = munition; |
---|
[2893] | 173 | this->setMunition(); |
---|
| 174 | } |
---|
[2662] | 175 | else |
---|
| 176 | { |
---|
[2893] | 177 | //create new munition with identifier because there is no such munitionType |
---|
[2662] | 178 | this->munitionIdentifier_ = ClassByString(munitionName); |
---|
| 179 | this->munition_ = this->munitionIdentifier_.fabricate(this); |
---|
[2912] | 180 | this->weaponSystem_->setNewMunition(munitionName, this->munition_); |
---|
[2893] | 181 | this->setMunition(); |
---|
[2662] | 182 | } |
---|
| 183 | } |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | |
---|
[2912] | 187 | Munition * Weapon::getAttachedMunition(const std::string& munitionType) |
---|
[2893] | 188 | { |
---|
[2912] | 189 | this->munition_ = this->weaponSystem_->getMunitionType(munitionType); |
---|
[2893] | 190 | return this->munition_; |
---|
| 191 | } |
---|
[2662] | 192 | |
---|
[2893] | 193 | |
---|
| 194 | //these function are defined in the weapon classes |
---|
| 195 | void Weapon::takeBullets() { }; |
---|
| 196 | void Weapon::createProjectile() { }; |
---|
| 197 | void Weapon::takeMagazines() { }; |
---|
| 198 | void Weapon::reloadBullet() { }; |
---|
| 199 | void Weapon::reloadMagazine() { }; |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | //get and set functions for XMLPort |
---|
[2912] | 203 | void Weapon::setMunitionType(const std::string& munitionType) |
---|
[2662] | 204 | { this->munitionType_ = munitionType; } |
---|
| 205 | |
---|
[2912] | 206 | const std::string& Weapon::getMunitionType() const |
---|
[2662] | 207 | { return this->munitionType_; } |
---|
| 208 | |
---|
| 209 | void Weapon::setBulletLoadingTime(float loadingTime) |
---|
| 210 | { this->bulletLoadingTime_ = loadingTime; } |
---|
| 211 | |
---|
[2912] | 212 | const float Weapon::getBulletLoadingTime() const |
---|
[2662] | 213 | { return this->bulletLoadingTime_; } |
---|
| 214 | |
---|
| 215 | void Weapon::setMagazineLoadingTime(float loadingTime) |
---|
| 216 | { this->magazineLoadingTime_ = loadingTime; } |
---|
| 217 | |
---|
[2912] | 218 | const float Weapon::getMagazineLoadingTime() const |
---|
[2662] | 219 | { return this->magazineLoadingTime_; } |
---|
| 220 | |
---|
[2893] | 221 | void Weapon::setBulletAmount(unsigned int amount) |
---|
| 222 | { this->bulletAmount_ = amount; } |
---|
[2662] | 223 | |
---|
[2912] | 224 | const unsigned int Weapon::getBulletAmount() const |
---|
[2893] | 225 | { return this->bulletAmount_; } |
---|
[2662] | 226 | |
---|
[2893] | 227 | void Weapon::setMagazineAmount(unsigned int amount) |
---|
| 228 | { this->magazineAmount_ = amount; } |
---|
[2662] | 229 | |
---|
[2912] | 230 | const unsigned int Weapon::getMagazineAmount() const |
---|
[2893] | 231 | { return this->magazineAmount_; } |
---|
| 232 | |
---|
| 233 | void Weapon::setUnlimitedMunition(bool unlimitedMunition) |
---|
| 234 | { this->unlimitedMunition_ = unlimitedMunition; } |
---|
| 235 | |
---|
[2912] | 236 | const bool Weapon::getUnlimitedMunition() const |
---|
[2893] | 237 | { return this->unlimitedMunition_; } |
---|
| 238 | |
---|
[2096] | 239 | } |
---|