Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 10, 2009, 12:05:01 AM (16 years ago)
Author:
landauf
Message:

More changes in the WeaponSystem:

  • WeaponSets can now contain several WeaponPacks
  • WeaponPacks can now belong to several WeaponSets

(until now this seemingly was a 1:1 relationship… now it's n:m)

  • Started support for multiple weaponmodes
  • Added some code to the destructor of some classes… according to some legends, this helps destroying objects. (WeaponSets and WeaponPacks weren't deleted before…)

Not yet finished, but I have to synchronize desktop computer and notebook.

File:
1 edited

Legend:

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

    r2912 r2914  
    3636#include "WeaponPack.h"
    3737#include "WeaponSet.h"
     38#include "Weapon.h"
    3839
    3940/* WeaponSystem
     
    6162            if (this->pawn_)
    6263                this->pawn_->setWeaponSystem(0);
    63         }
    64     }
    65 
    66     void WeaponSystem::attachWeaponSet(WeaponSet *wSet)
    67     {
    68         if (!wSet)
    69             return;
    70 
    71         this->weaponSets_[wSet->getFireMode()] = wSet;
    72         wSet->setWeaponSystem(this);
     64
     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++));
     73        }
     74    }
     75
     76    void WeaponSystem::addWeaponSlot(WeaponSlot * wSlot)
     77    {
     78        if (!wSlot)
     79            return;
     80
     81        this->weaponSlots_.insert(wSlot);
     82        wSlot->setWeaponSystem(this);
     83    }
     84
     85    void WeaponSystem::removeWeaponSlot(WeaponSlot * wSlot)
     86    {
     87        if (!wSlot)
     88            return;
     89
     90        if (wSlot->getWeapon())
     91            this->removeWeaponPack(wSlot->getWeapon()->getWeaponPack());
     92
     93        this->weaponSlots_.erase(wSlot);
     94    }
     95
     96    WeaponSlot * WeaponSystem::getWeaponSlot(unsigned int index) const
     97    {
     98        unsigned int i = 0;
     99        for (std::set<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
     100        {
     101            ++i;
     102            if (i > index)
     103                return (*it);
     104        }
     105        return 0;
     106    }
     107
     108    bool WeaponSystem::addWeaponSet(WeaponSet * wSet)
     109    {
     110        if (wSet)
     111            return this->addWeaponSet(wSet, wSet->getDesiredFiremode());
     112        else
     113            return false;
     114    }
     115
     116    bool WeaponSystem::addWeaponSet(WeaponSet * wSet, unsigned int firemode)
     117    {
     118        if (!wSet || firemode >= WeaponSystem::MAX_FIRE_MODES)
     119            return false;
     120
     121        std::map<unsigned int, WeaponSet*>::const_iterator it = this->weaponSets_.find(firemode);
     122        if (it == this->weaponSets_.end())
     123        {
     124            this->weaponSets_[firemode] = wSet;
     125            wSet->setWeaponSystem(this);
     126            return true;
     127        }
     128
     129        return false;
     130    }
     131
     132    void WeaponSystem::removeWeaponSet(WeaponSet * wSet)
     133    {
     134        for (std::map<unsigned int, WeaponSet*>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); )
     135        {
     136            if (it->second == wSet)
     137                this->weaponSets_.erase(it++);
     138            else
     139                ++it;
     140        }
    73141    }
    74142
     
    85153    }
    86154
    87     void WeaponSystem::attachWeaponSlot(WeaponSlot *wSlot)
    88     {
    89         if (!wSlot)
    90             return;
    91 
    92         this->weaponSlots_.insert(wSlot);
    93         wSlot->setWeaponSystem(this);
    94     }
    95 
    96     WeaponSlot * WeaponSystem::getWeaponSlot(unsigned int index) const
    97     {
    98         unsigned int i = 0;
     155    bool WeaponSystem::canAddWeaponPack(WeaponPack * wPack)
     156    {
     157        if (!wPack)
     158            return false;
     159
     160        unsigned int freeSlots = 0;
    99161        for (std::set<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
     162        {
     163            if (!(*it)->isOccupied())
     164                ++freeSlots;
     165        }
     166
     167        return (freeSlots >= wPack->getNumWeapons());
     168    }
     169
     170    bool WeaponSystem::addWeaponPack(WeaponPack * wPack)
     171    {
     172        if (!this->canAddWeaponPack(wPack))
     173            return false;
     174
     175        // Attach all weapons to the first free slots (and to the Pawn)
     176        unsigned int i = 0;
     177        for (std::set<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
     178        {
     179            if (!(*it)->isOccupied() && i < wPack->getNumWeapons())
     180            {
     181                Weapon* weapon = wPack->getWeapon(i);
     182                (*it)->attachWeapon(weapon);
     183                this->getPawn()->attach(weapon);
     184                ++i;
     185            }
     186        }
     187
     188        // Assign the desired weaponmode to the firemodes
     189        for (std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it)
     190        {
     191            unsigned int weaponmode = wPack->getDesiredWeaponmode(it->first);
     192            if (weaponmode != WeaponSystem::WEAPON_MODE_UNASSIGNED)
     193                it->second->setWeaponmodeLink(wPack, weaponmode);
     194        }
     195
     196        this->weaponPacks_.insert(wPack);
     197        wPack->setWeaponSystem(this);
     198        wPack->attachNeededMunitionToAllWeapons(); // TODO - what is this?
     199
     200        return true;
     201    }
     202
     203    void WeaponSystem::removeWeaponPack(WeaponPack * wPack)
     204    {
     205        // Remove all weapons from their WeaponSlot
     206        unsigned int i = 0;
     207        Weapon* weapon = 0;
     208        while (weapon = wPack->getWeapon(i++))
     209            weapon->getWeaponSlot()->removeWeapon();
     210
     211        // Remove all added links from the WeaponSets
     212        for (std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it)
     213            it->second->removeWeaponmodeLink(wPack);
     214
     215        // Remove the WeaponPack from the WeaponSystem
     216        this->weaponPacks_.erase(wPack);
     217    }
     218
     219    WeaponPack * WeaponSystem::getWeaponPack(unsigned int index) const
     220    {
     221        unsigned int i = 0;
     222        for (std::set<WeaponPack*>::iterator it = this->weaponPacks_.begin(); it != this->weaponPacks_.end(); ++it)
    100223        {
    101224            ++i;
     
    106229    }
    107230
    108     void WeaponSystem::attachWeaponPack(WeaponPack *wPack, unsigned int wSetNumber)
    109     {
    110         if (!wPack)
    111             return;
    112 
    113         std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.find(wSetNumber);
    114         if (it != this->weaponSets_.end() && it->second)
    115             it->second->attachWeaponPack(wPack);
    116 
    117         this->weaponPacks_.insert(wPack);
    118         wPack->setWeaponSystem(this);
    119         wPack->attachNeededMunitionToAllWeapons(); // TODO - what is this?
    120     }
    121 
    122     WeaponPack * WeaponSystem::getWeaponPack(unsigned int index) const
    123     {
    124         unsigned int i = 0;
    125         for (std::set<WeaponPack*>::iterator it = this->weaponPacks_.begin(); it != this->weaponPacks_.end(); ++it)
    126         {
    127             ++i;
    128             if (i > index)
    129                 return (*it);
    130         }
    131         return 0;
    132    }
     231    bool WeaponSystem::swapWeaponSlots(WeaponSlot * wSlot1, WeaponSlot * wSlot2)
     232    {
     233        // TODO
     234    }
     235
     236    void WeaponSystem::changeWeaponmode(WeaponPack * wPack, WeaponSet * wSet, unsigned int weaponmode)
     237    {
     238        if (!wPack || !wSet)
     239            return;
     240
     241        // Check if the WeaponPack belongs to this WeaponSystem
     242        std::set<WeaponPack *>::iterator it1 = this->weaponPacks_.find(wPack);
     243        if (it1 == this->weaponPacks_.end())
     244            return;
     245
     246        // Check if the WeaponSet belongs to this WeaponSystem
     247        bool foundWeaponSet = false;
     248        for (std::map<unsigned int, WeaponSet *>::iterator it2 = this->weaponSets_.begin(); it2 != this->weaponSets_.end(); ++it2)
     249        {
     250            if (it2->second == wSet)
     251            {
     252                foundWeaponSet = true;
     253                break;
     254            }
     255        }
     256        if (!foundWeaponSet)
     257            return;
     258
     259        // Finally set the link between firemode and weaponmode
     260        wSet->setWeaponmodeLink(wPack, weaponmode);
     261    }
    133262
    134263    void WeaponSystem::setNewMunition(const std::string& munitionType, Munition * munitionToAdd)
     
    148277    }
    149278
    150 
    151     //n is the n'th weaponSet, starting with zero
    152     //SpaceShip.cc only needs to have the keybinding to a specific Set-number n (=firemode)
    153     //in future this could be well defined and not only for 3 different WeaponModes
    154279    void WeaponSystem::fire(unsigned int firemode)
    155280    {
Note: See TracChangeset for help on using the changeset viewer.