Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 2915


Ignore:
Timestamp:
Apr 12, 2009, 12:34:55 AM (16 years ago)
Author:
landauf
Message:
  • switched back to std::vector for the WeaponSlots to keep them in the same order as in the XML file
  • added DefaultWeaponmodeLink, a class which links weaponmodes (a property of a Weapon or a WeaponPack) with firemodes (one firemode corresponds to one WeaponSet). This can be changed later (for example in a nice GUI), but DefaultWeaponmodeLink defines the default value.
Location:
code/branches/weapons/src/orxonox
Files:
2 added
8 edited

Legend:

Unmodified
Added
Removed
  • code/branches/weapons/src/orxonox/OrxonoxPrereqs.h

    r2912 r2915  
    157157    class WeaponPack;
    158158    class Weapon;
     159    class DefaultWeaponmodeLink;
    159160    class Munition;
    160161    class LaserGun;
  • code/branches/weapons/src/orxonox/objects/weaponSystem/CMakeLists.txt

    r2710 r2915  
    66  WeaponSlot.cc
    77  WeaponSystem.cc
     8  DefaultWeaponmodeLink.cc
    89)
    910
  • code/branches/weapons/src/orxonox/objects/weaponSystem/Weapon.cc

    r2914 r2915  
    3434
    3535#include "Munition.h"
     36#include "WeaponPack.h"
    3637#include "WeaponSystem.h"
    3738
     
    6465    {
    6566COUT(0) << "~Weapon" << std::endl;
     67        if (this->isInitialized() && this->weaponPack_)
     68            this->weaponPack_->removeWeapon(this);
    6669    }
    6770
     
    6972    {
    7073        SUPER(Weapon, XMLPort, xmlelement, mode);
     74
    7175        XMLPortParam(Weapon, "munitionType", setMunitionType, getMunitionType, xmlelement, mode);
    7276        XMLPortParam(Weapon, "bulletLoadingTime", setBulletLoadingTime, getBulletLoadingTime, xmlelement, mode);
  • code/branches/weapons/src/orxonox/objects/weaponSystem/WeaponPack.cc

    r2914 r2915  
    2323 *      Martin Polak
    2424 *   Co-authors:
    25  *      ... *
     25 *      ...
     26 *
    2627 */
    2728
     
    3637#include "WeaponSlot.h"
    3738#include "WeaponSystem.h"
     39#include "DefaultWeaponmodeLink.h"
    3840
    3941namespace orxonox
     
    5557
    5658        if (this->isInitialized() && this->weaponSystem_)
     59        {
    5760            this->weaponSystem_->removeWeaponPack(this);
     61
     62            while (!this->weapons_.empty())
     63                delete (*this->weapons_.begin());
     64
     65            for (std::set<DefaultWeaponmodeLink*>::iterator it = this->links_.begin(); it != this->links_.end(); )
     66                delete (*(it++));
     67        }
    5868    }
    5969
     
    6373
    6474        XMLPortObject(WeaponPack, Weapon, "", addWeapon, getWeapon, xmlelement, mode);
     75        XMLPortObject(WeaponPack, DefaultWeaponmodeLink, "links", addDefaultWeaponmodeLink, getDefaultWeaponmodeLink, xmlelement, mode);
    6576    }
    6677
     
    8091    }
    8192
     93    void WeaponPack::removeWeapon(Weapon * weapon)
     94    {
     95        if (!weapon)
     96            return;
     97
     98        this->weapons_.erase(weapon);
     99        weapon->setWeaponPack(0);
     100    }
     101
    82102    Weapon * WeaponPack::getWeapon(unsigned int index) const
    83103    {
     
    94114    }
    95115
    96     void WeaponPack::setWeaponSystemToAllWeapons(WeaponSystem * weaponSystem)
     116    void WeaponPack::setWeaponSystemToAllWeapons()
    97117    {
    98118        for (std::set<Weapon *>::const_iterator it = this->weapons_.begin(); it != this->weapons_.end(); ++it)
    99             (*it)->setWeaponSystem(weaponSystem);
     119            (*it)->setWeaponSystem(this->weaponSystem_);
    100120    }
    101121
     
    108128        }
    109129    }
     130
     131    void WeaponPack::addDefaultWeaponmodeLink(DefaultWeaponmodeLink* link)
     132    {
     133        this->links_.insert(link);
     134    }
     135
     136    DefaultWeaponmodeLink* WeaponPack::getDefaultWeaponmodeLink(unsigned int index) const
     137    {
     138        unsigned int i = 0;
     139        for (std::set<DefaultWeaponmodeLink*>::const_iterator it = this->links_.begin(); it != this->links_.end(); ++it)
     140        {
     141            if (i == index)
     142                return (*it);
     143
     144            ++i;
     145        }
     146        return 0;
     147    }
     148
     149    unsigned int WeaponPack::getDesiredWeaponmode(unsigned int firemode) const
     150    {
     151        for (std::set<DefaultWeaponmodeLink*>::const_iterator it = this->links_.begin(); it != this->links_.end(); ++it)
     152            if ((*it)->getFiremode() == firemode)
     153                return (*it)->getWeaponmode();
     154
     155        return WeaponSystem::WEAPON_MODE_UNASSIGNED;
     156    }
    110157}
  • code/branches/weapons/src/orxonox/objects/weaponSystem/WeaponPack.h

    r2914 r2915  
    5050
    5151            void addWeapon(Weapon * weapon);
     52            void removeWeapon(Weapon * weapon);
    5253            Weapon * getWeapon(unsigned int index) const;
    5354
     
    5556                { return this->weapons_.size(); }
    5657
    57             unsigned int getDesiredWeaponmode(unsigned int firemode) { return 0; } // TODO
     58            void addDefaultWeaponmodeLink(DefaultWeaponmodeLink* link);
     59            DefaultWeaponmodeLink* getDefaultWeaponmodeLink(unsigned int index) const;
    5860
    59             void attachNeededMunitionToAllWeapons();
     61            unsigned int getDesiredWeaponmode(unsigned int firemode) const;
    6062
    6163            inline void setWeaponSystem(WeaponSystem *weaponSystem)
    62                 { this->weaponSystem_ = weaponSystem; this->setWeaponSystemToAllWeapons(weaponSystem); }
     64            {
     65                this->weaponSystem_ = weaponSystem;
     66                this->setWeaponSystemToAllWeapons();
     67                this->attachNeededMunitionToAllWeapons();
     68            }
    6369            inline WeaponSystem * getWeaponSystem() const
    6470                { return this->weaponSystem_; }
    6571
    6672        private:
    67             void setWeaponSystemToAllWeapons(WeaponSystem * weaponSystem);
     73            void setWeaponSystemToAllWeapons();
     74            void attachNeededMunitionToAllWeapons();
    6875
    6976            std::set<Weapon *> weapons_;
     77            std::set<DefaultWeaponmodeLink *> links_;
    7078            WeaponSystem * weaponSystem_;
    7179    };
  • code/branches/weapons/src/orxonox/objects/weaponSystem/WeaponSet.cc

    r2914 r2915  
    2323 *      Martin Polak
    2424 *   Co-authors:
    25  *      ... *
     25 *      ...
     26 *
    2627 */
    2728
  • code/branches/weapons/src/orxonox/objects/weaponSystem/WeaponSystem.cc

    r2914 r2915  
    6363                this->pawn_->setWeaponSystem(0);
    6464
    65             for (std::map<unsigned int, WeaponSet*>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); )
    66                 delete (it++)->second;
    67 
    68             for (std::set<WeaponPack*>::iterator it = this->weaponPacks_.begin(); it != this->weaponPacks_.end(); )
    69                 delete (*(it++));
    70 
    71             for (std::set<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); )
    72                 delete (*(it++));
     65//            for (std::map<unsigned int, WeaponSet*>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); )
     66//                delete (it++)->second;
     67            while (!this->weaponSets_.empty())
     68                delete (this->weaponSets_.begin()->second);
     69
     70//            for (std::set<WeaponPack*>::iterator it = this->weaponPacks_.begin(); it != this->weaponPacks_.end(); )
     71//                delete (*(it++));
     72            while (!this->weaponPacks_.empty())
     73                delete (*this->weaponPacks_.begin());
     74
     75//            for (std::vector<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); )
     76//                delete (*(it++));
     77            while (!this->weaponSlots_.empty())
     78                delete (*this->weaponSlots_.begin());
    7379        }
    7480    }
     
    7985            return;
    8086
    81         this->weaponSlots_.insert(wSlot);
     87        this->weaponSlots_.push_back(wSlot);
    8288        wSlot->setWeaponSystem(this);
    8389    }
     
    9197            this->removeWeaponPack(wSlot->getWeapon()->getWeaponPack());
    9298
    93         this->weaponSlots_.erase(wSlot);
     99        for (std::vector<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
     100        {
     101            if ((*it) == wSlot)
     102            {
     103                this->weaponSlots_.erase(it);
     104                break;
     105            }
     106        }
    94107    }
    95108
     
    97110    {
    98111        unsigned int i = 0;
    99         for (std::set<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
     112        for (std::vector<WeaponSlot*>::const_iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
    100113        {
    101114            ++i;
     
    159172
    160173        unsigned int freeSlots = 0;
    161         for (std::set<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
     174        for (std::vector<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
    162175        {
    163176            if (!(*it)->isOccupied())
     
    175188        // Attach all weapons to the first free slots (and to the Pawn)
    176189        unsigned int i = 0;
    177         for (std::set<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
     190        for (std::vector<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
    178191        {
    179192            if (!(*it)->isOccupied() && i < wPack->getNumWeapons())
     
    196209        this->weaponPacks_.insert(wPack);
    197210        wPack->setWeaponSystem(this);
    198         wPack->attachNeededMunitionToAllWeapons(); // TODO - what is this?
    199211
    200212        return true;
     
    231243    bool WeaponSystem::swapWeaponSlots(WeaponSlot * wSlot1, WeaponSlot * wSlot2)
    232244    {
    233         // TODO
     245        if (!wSlot1 || !wSlot2)
     246            return false;
     247
     248        Weapon* weapon1 = wSlot1->getWeapon();
     249        Weapon* weapon2 = wSlot2->getWeapon();
     250
     251        wSlot1->attachWeapon(weapon2);
     252        wSlot2->attachWeapon(weapon1);
     253
     254        return true;
     255        // In the future, certain weapons might not fit to some slots. Swapping would then be
     256        // impossible and the returnvalue would be false.
    234257    }
    235258
  • code/branches/weapons/src/orxonox/objects/weaponSystem/WeaponSystem.h

    r2914 r2915  
    3535#include <set>
    3636#include <map>
     37#include <vector>
    3738
    3839#include "core/BaseObject.h"
     
    9394        private:
    9495            std::map<unsigned int, WeaponSet *> weaponSets_;
    95             std::set<WeaponSlot *> weaponSlots_;
     96            std::vector<WeaponSlot *> weaponSlots_;
    9697            std::set<WeaponPack *> weaponPacks_;
    9798            std::map<std::string, Munition *> munitionSet_;
Note: See TracChangeset for help on using the changeset viewer.