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