- Timestamp:
- Apr 18, 2009, 6:14:52 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/weapons/src/orxonox/objects/weaponSystem/Weapon.cc
r2915 r2918 22 22 * Author: 23 23 * Martin Polak 24 * Fabian 'x3n' Landau 24 25 * Co-authors: 25 26 * ... … … 33 34 #include "core/XMLPort.h" 34 35 35 #include " Munition.h"36 #include "WeaponMode.h" 36 37 #include "WeaponPack.h" 37 38 #include "WeaponSystem.h" … … 45 46 RegisterObject(Weapon); 46 47 47 this->bulletReadyToShoot_ = true;48 this->magazineReadyToShoot_ = true;49 this->weaponSystem_ = 0;50 48 this->weaponPack_ = 0; 51 49 this->weaponSlot_ = 0; 52 this->bulletLoadingTime_ = 0;53 this->magazineLoadingTime_ = 0;54 50 this->bReloading_ = false; 55 this->bulletAmount_= 0; 56 this->magazineAmount_ = 0; 57 this->munition_ = 0; 58 this->unlimitedMunition_ = false; 59 this->setObjectMode(0x0); 51 this->reloadingWeaponmode_ = WeaponSystem::WEAPON_MODE_UNASSIGNED; 52 53 this->reloadTimer_.setTimer(0.0f, false, this, createExecutor(createFunctor(&Weapon::reloaded))); 54 this->reloadTimer_.stopTimer(); 60 55 61 56 COUT(0) << "+Weapon" << std::endl; … … 65 60 { 66 61 COUT(0) << "~Weapon" << std::endl; 67 if (this->isInitialized() && this->weaponPack_) 68 this->weaponPack_->removeWeapon(this); 62 63 if (this->isInitialized()) 64 { 65 if (this->weaponPack_) 66 this->weaponPack_->removeWeapon(this); 67 68 for (std::multimap<unsigned int, WeaponMode*>::iterator it = this->weaponmodes_.begin(); it != this->weaponmodes_.end(); ++it) 69 delete it->second; 70 } 69 71 } 70 72 … … 73 75 SUPER(Weapon, XMLPort, xmlelement, mode); 74 76 75 XMLPortParam(Weapon, "munitionType", setMunitionType, getMunitionType, xmlelement, mode); 76 XMLPortParam(Weapon, "bulletLoadingTime", setBulletLoadingTime, getBulletLoadingTime, xmlelement, mode); 77 XMLPortParam(Weapon, "magazineLoadingTime", setMagazineLoadingTime, getMagazineLoadingTime, xmlelement, mode); 78 XMLPortParam(Weapon, "bullets", setBulletAmount, getBulletAmount, xmlelement, mode); 79 XMLPortParam(Weapon, "magazines", setMagazineAmount, getMagazineAmount, xmlelement, mode); 80 XMLPortParam(Weapon, "unlimitedMunition", setUnlimitedMunition, getUnlimitedMunition, xmlelement, mode); 77 XMLPortObject(Weapon, WeaponMode, "", addWeaponmode, getWeaponmode, xmlelement, mode); 81 78 } 82 79 83 void Weapon:: setWeapon()80 void Weapon::addWeaponmode(WeaponMode* weaponmode) 84 81 { 85 this->munition_->fillBullets(); 86 this->munition_->fillMagazines(); 82 if (!weaponmode) 83 return; 84 85 this->weaponmodes_.insert(std::pair<unsigned int, WeaponMode*>(weaponmode->getMode(), weaponmode)); 86 weaponmode->setWeapon(this); 87 87 } 88 88 89 void Weapon::setMunition()89 WeaponMode* Weapon::getWeaponmode(unsigned int index) const 90 90 { 91 this->munition_->setMaxBullets(this->bulletAmount_); 92 this->munition_->setMaxMagazines(this->magazineAmount_); 91 unsigned int i = 0; 92 for (std::multimap<unsigned int, WeaponMode*>::const_iterator it = this->weaponmodes_.begin(); it != this->weaponmodes_.end(); ++it) 93 { 94 if (i == index) 95 return it->second; 96 97 ++i; 98 } 99 return 0; 93 100 } 94 101 95 void Weapon::fire( )102 void Weapon::fire(unsigned int mode) 96 103 { 97 if ( this->bulletReadyToShoot_ && this->magazineReadyToShoot_ && !this->bReloading_) 104 // To avoid firing with more than one mode at the same time, we lock the weapon (reloading) for 105 // all modes except the one which is currently reloading. 106 // 107 // Example: 108 // WeaponMode A -> mode 0 109 // WeaponMode B -> mode 0 110 // WeaponMode C -> mode 1 111 // 112 // -> A and B can fire at the same time, but C has to wait until both (A and B) have reloaded 113 // -> If C fires, A and B have to wait until C has reloaded 114 // 115 // Note: The reloading of each WeaponMode is internally handled by each A, B and C. 116 // The reloading of the weapon is only performed to avoid firing with different modes at the same time. 117 if (this->bReloading_ && this->reloadingWeaponmode_ != mode) 118 return; 119 120 std::multimap<unsigned int, WeaponMode*>::iterator start = this->weaponmodes_.lower_bound(mode); 121 std::multimap<unsigned int, WeaponMode*>::iterator end = this->weaponmodes_.upper_bound(mode); 122 123 for (std::multimap<unsigned int, WeaponMode*>::iterator it = start; it != end; ++it) 98 124 { 99 this->bulletReadyToShoot_ = false;100 if ( this->unlimitedMunition_== true)125 float reloading_time = 0; 126 if (it->second->fire(&reloading_time)) 101 127 { 102 //shoot 103 this->reloadBullet(); 104 this->createProjectile(); 105 } 106 else 107 { 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 } 126 } 127 } 128 else 129 { 130 //weapon not reloaded 131 } 128 this->bReloading_ = true; 129 this->reloadingWeaponmode_ = mode; 132 130 133 } 134 135 136 //weapon reloading 137 void Weapon::bulletTimer(float bulletLoadingTime) 138 { 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 } 147 148 void Weapon::bulletReloaded() 149 { 150 this->bReloading_ = false; 151 this->bulletReadyToShoot_ = true; 152 } 153 154 void Weapon::magazineReloaded() 155 { 156 this->bReloading_ = false; 157 this->munition_->fillBullets(); 158 } 159 160 161 162 void Weapon::attachNeededMunition(const std::string& munitionName) 163 { 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 */ 166 if (this->weaponSystem_) 167 { 168 //getMunitionType returns 0 if there is no such munitionType 169 Munition* munition = this->weaponSystem_->getMunitionType(munitionName); 170 if ( munition ) 171 { 172 this->munition_ = munition; 173 this->setMunition(); 174 } 175 else 176 { 177 //create new munition with identifier because there is no such munitionType 178 this->munitionIdentifier_ = ClassByString(munitionName); 179 this->munition_ = this->munitionIdentifier_.fabricate(this); 180 this->weaponSystem_->setNewMunition(munitionName, this->munition_); 181 this->setMunition(); 131 this->reloadTimer_.setInterval(reloading_time); 132 this->reloadTimer_.startTimer(); 182 133 } 183 134 } 184 135 } 185 136 186 187 Munition * Weapon::getAttachedMunition(const std::string& munitionType) 137 void Weapon::reload() 188 138 { 189 this->munition_ = this->weaponSystem_->getMunitionType(munitionType);190 return this->munition_;139 for (std::multimap<unsigned int, WeaponMode*>::iterator it = this->weaponmodes_.begin(); it != this->weaponmodes_.end(); ++it) 140 it->second->reload(); 191 141 } 192 142 143 void Weapon::reloaded() 144 { 145 this->bReloading_ = false; 146 this->reloadingWeaponmode_ = WeaponSystem::WEAPON_MODE_UNASSIGNED; 147 } 193 148 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 203 void Weapon::setMunitionType(const std::string& munitionType) 204 { this->munitionType_ = munitionType; } 205 206 const std::string& Weapon::getMunitionType() const 207 { return this->munitionType_; } 208 209 void Weapon::setBulletLoadingTime(float loadingTime) 210 { this->bulletLoadingTime_ = loadingTime; } 211 212 const float Weapon::getBulletLoadingTime() const 213 { return this->bulletLoadingTime_; } 214 215 void Weapon::setMagazineLoadingTime(float loadingTime) 216 { this->magazineLoadingTime_ = loadingTime; } 217 218 const float Weapon::getMagazineLoadingTime() const 219 { return this->magazineLoadingTime_; } 220 221 void Weapon::setBulletAmount(unsigned int amount) 222 { this->bulletAmount_ = amount; } 223 224 const unsigned int Weapon::getBulletAmount() const 225 { return this->bulletAmount_; } 226 227 void Weapon::setMagazineAmount(unsigned int amount) 228 { this->magazineAmount_ = amount; } 229 230 const unsigned int Weapon::getMagazineAmount() const 231 { return this->magazineAmount_; } 232 233 void Weapon::setUnlimitedMunition(bool unlimitedMunition) 234 { this->unlimitedMunition_ = unlimitedMunition; } 235 236 const bool Weapon::getUnlimitedMunition() const 237 { return this->unlimitedMunition_; } 238 149 void Weapon::notifyWeaponModes() 150 { 151 for (std::multimap<unsigned int, WeaponMode*>::iterator it = this->weaponmodes_.begin(); it != this->weaponmodes_.end(); ++it) 152 it->second->setWeapon(this); 153 } 239 154 }
Note: See TracChangeset
for help on using the changeset viewer.