Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Apr 9, 2009, 3:18:11 AM (15 years ago)
Author:
landauf
Message:

Several small adjustments in the weaponsystem (like additional const keyword, includes moved from .h to .cc where possible, …)

Firemode is now an unsigned int instead of an Enum. Instead of "fire" and "altFire" use "fire 0" and "fire 1"

File:
1 edited

Legend:

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

    r2896 r2912  
    2828
    2929#include "OrxonoxStableHeaders.h"
    30 
    31 #include <vector>
     30#include "WeaponSystem.h"
    3231
    3332#include "core/CoreIncludes.h"
    34 #include "core/XMLPort.h"
    35 #include "util/Debug.h"
     33#include "objects/worldentities/pawns/Pawn.h"
    3634
    37 #include "WeaponSystem.h"
    38 
     35#include "WeaponSlot.h"
     36#include "WeaponPack.h"
     37#include "WeaponSet.h"
    3938
    4039/* WeaponSystem
     
    5150        RegisterObject(WeaponSystem);
    5251
    53         this->parentPawn_ = 0;
     52        this->pawn_ = 0;
     53COUT(0) << "+WeaponSystem" << std::endl;
    5454    }
    5555
    5656    WeaponSystem::~WeaponSystem()
    5757    {
     58COUT(0) << "~WeaponSystem" << std::endl;
     59        if (this->isInitialized())
     60        {
     61            if (this->pawn_)
     62                this->pawn_->setWeaponSystem(0);
     63        }
    5864    }
    5965
    60     void WeaponSystem::attachWeaponPack(WeaponPack *wPack, unsigned int firemode)
     66    void WeaponSystem::attachWeaponSet(WeaponSet *wSet)
    6167    {
    62         if (firemode < this->weaponSets_.size())
    63             this->weaponSets_[firemode]->attachWeaponPack(wPack);
    64         this->weaponPacks_.push_back(wPack);
     68        if (!wSet)
     69            return;
     70
     71        this->weaponSets_[wSet->getFireMode()] = wSet;
     72        wSet->setWeaponSystem(this);
     73    }
     74
     75    WeaponSet * WeaponSystem::getWeaponSet(unsigned int index) const
     76    {
     77        unsigned int i = 0;
     78        for (std::map<unsigned int, WeaponSet*>::const_iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it)
     79        {
     80            ++i;
     81            if (i > index)
     82                return it->second;
     83        }
     84        return 0;
    6585    }
    6686
    6787    void WeaponSystem::attachWeaponSlot(WeaponSlot *wSlot)
    6888    {
    69         wSlot->setParentWeaponSystem(this);
    70         this->weaponSlots_.push_back(wSlot);
     89        if (!wSlot)
     90            return;
     91
     92        this->weaponSlots_.insert(wSlot);
     93        wSlot->setWeaponSystem(this);
    7194    }
    7295
    73     void WeaponSystem::attachWeaponSet(WeaponSet *wSet)
     96    WeaponSlot * WeaponSystem::getWeaponSlot(unsigned int index) const
    7497    {
    75         wSet->setParentWeaponSystem(this);
    76         this->weaponSets_.push_back(wSet);
     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;
    77106    }
    78107
    79     void WeaponSystem::setNewMunition(std::string munitionType, Munition * munitionToAdd)
     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   }
     133
     134    void WeaponSystem::setNewMunition(const std::string& munitionType, Munition * munitionToAdd)
    80135    {
    81136        this->munitionSet_[munitionType] = munitionToAdd;
     
    84139
    85140    //returns the Pointer to the munitionType, if this munitionType doesn't exist returns 0, see Weapon::attachNeededMunition
    86     Munition * WeaponSystem::getMunitionType(std::string munitionType)
     141    Munition * WeaponSystem::getMunitionType(const std::string& munitionType) const
    87142    {
    88143        std::map<std::string, Munition *>::const_iterator it = this->munitionSet_.find(munitionType);
     
    97152    //SpaceShip.cc only needs to have the keybinding to a specific Set-number n (=firemode)
    98153    //in future this could be well defined and not only for 3 different WeaponModes
    99     void WeaponSystem::fire(WeaponMode::Enum n)
     154    void WeaponSystem::fire(unsigned int firemode)
    100155    {
    101         int set = 0;
    102         switch (n)
    103         {
    104             case WeaponMode::fire:
    105                 set = 0;
    106                 break;
    107             case WeaponMode::altFire:
    108                 set = 1;
    109                 break;
    110             case WeaponMode::altFire2:
    111                 set = 2;
    112                 break;
    113         }
    114         if (set < (int)this->weaponSets_.size())
    115             this->weaponSets_[set]->fire();
     156        std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.find(firemode);
     157        if (it != this->weaponSets_.end() && it->second)
     158            it->second->fire();
    116159    }
    117 
    118 
    119     WeaponSet * WeaponSystem::getWeaponSetPointer(unsigned int n)
    120     {
    121         if (n < this->weaponSets_.size())
    122             return this->weaponSets_[n];
    123         else
    124             return 0;
    125     }
    126 
    127     WeaponSlot * WeaponSystem::getWeaponSlotPointer(unsigned int n)
    128     {
    129         if (n < this->weaponSlots_.size())
    130             return this->weaponSlots_[n];
    131         else
    132             return 0;
    133     }
    134 
    135     WeaponPack * WeaponSystem::getWeaponPackPointer(unsigned int n)
    136     {
    137         if (n < this->weaponPacks_.size())
    138             return this->weaponPacks_[n];
    139         else
    140             return 0;
    141     }
    142 
    143     void WeaponSystem::XMLPort(Element& xmlelement, XMLPort::Mode mode)
    144     {
    145         SUPER(WeaponSystem, XMLPort, xmlelement, mode);
    146     }
    147 
    148160}
Note: See TracChangeset for help on using the changeset viewer.