Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 18, 2009, 6:14:52 PM (16 years ago)
Author:
landauf
Message:

Added many new features in the Munition class:

  • there are now 3 modes: a) every weapon has it's own magazine b) all weapons use the same magazin c) no magazines, just a big munition pool
  • the Munition class handles the reloading of the magazine

Split the Weapon class into Weapon and WeaponMode. WeaponMode creates the fire of the Weapon. A weapon can own several WeaponModes (for example primary and secondary fire). But it's also possible to have a weapon with several muzzles which all fire at the same time (there's a WeaponMode for each muzzle).

Renamed LaserGun to LaserFire and Fusion to FusionFire. They inherit now from WeaponMode.

Changed the code in the Weapon class to use the new Munition functionality.

Added ReplenishingMunition, a subclass of Munition that replenishes itself (used for LaserGunMunition).

Added a reload command to reload magazines.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/weapons/src/orxonox/objects/weaponSystem/Weapon.cc

    r2915 r2918  
    2222 *   Author:
    2323 *      Martin Polak
     24 *      Fabian 'x3n' Landau
    2425 *   Co-authors:
    2526 *      ...
     
    3334#include "core/XMLPort.h"
    3435
    35 #include "Munition.h"
     36#include "WeaponMode.h"
    3637#include "WeaponPack.h"
    3738#include "WeaponSystem.h"
     
    4546        RegisterObject(Weapon);
    4647
    47         this->bulletReadyToShoot_ = true;
    48         this->magazineReadyToShoot_ = true;
    49         this->weaponSystem_ = 0;
    5048        this->weaponPack_ = 0;
    5149        this->weaponSlot_ = 0;
    52         this->bulletLoadingTime_ = 0;
    53         this->magazineLoadingTime_ = 0;
    5450        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();
    6055
    6156COUT(0) << "+Weapon" << std::endl;
     
    6560    {
    6661COUT(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        }
    6971    }
    7072
     
    7375        SUPER(Weapon, XMLPort, xmlelement, mode);
    7476
    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);
    8178    }
    8279
    83     void Weapon::setWeapon()
     80    void Weapon::addWeaponmode(WeaponMode* weaponmode)
    8481    {
    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);
    8787    }
    8888
    89     void Weapon::setMunition()
     89    WeaponMode* Weapon::getWeaponmode(unsigned int index) const
    9090    {
    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;
    93100    }
    94101
    95     void Weapon::fire()
     102    void Weapon::fire(unsigned int mode)
    96103    {
    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)
    98124        {
    99             this->bulletReadyToShoot_ = false;
    100             if ( this->unlimitedMunition_== true )
     125            float reloading_time = 0;
     126            if (it->second->fire(&reloading_time))
    101127            {
    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;
    132130
    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();
    182133            }
    183134        }
    184135    }
    185136
    186 
    187     Munition * Weapon::getAttachedMunition(const std::string& munitionType)
     137    void Weapon::reload()
    188138    {
    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();
    191141    }
    192142
     143    void Weapon::reloaded()
     144    {
     145        this->bReloading_ = false;
     146        this->reloadingWeaponmode_ = WeaponSystem::WEAPON_MODE_UNASSIGNED;
     147    }
    193148
    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    }
    239154}
Note: See TracChangeset for help on using the changeset viewer.