Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Jul 24, 2005, 11:15:00 PM (19 years ago)
Author:
bensch
Message:

orxonox/trunk: the new WeaponManager is online :)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/trunk/src/world_entities/weapons/weapon_manager.cc

    r4949 r4951  
    1313   main-programmer: Patrick Boenzli
    1414   co-programmer: Benjamin Grauer
     15
     16   2005-07-24: Benjamin Grauer: restructurate, so it can handle the new Weapons.
    1517*/
    1618
     
    3436 * @param number of weapon slots of the model/ship <= 8 (limitied)
    3537 */
    36 WeaponManager::WeaponManager(int slotCount)
     38WeaponManager::WeaponManager(unsigned int slotCount)
    3739{
    3840  this->init();
    39   this->slotCount = slotCount;
     41  this->setSlotCount(slotCount);
    4042}
    4143
     
    5153WeaponManager::~WeaponManager()
    5254{
    53   /*
    54   i dont have to delete the weapons itself, because they are
    55   worldentities and therefore in the entities list of the world.
    56   world will clean them up for me
    57   */
    58   for(int i = 0; i < WM_MAX_CONFIGS; ++i)
    59   {
    60     this->configs[i].bUsed = false;
    61     for(int j = 0; j < WM_MAX_SLOTS; ++j)
    62       this->configs[i].slots[j] = NULL;
    63   }
    64 
    6555  // crosshair being a PNode it must not be deleted (this is because PNodes delete themselves.)
    6656  //delete this->crosshair;
     
    7464  this->setClassID(CL_WEAPON_MANAGER, "WeaponManager");
    7565
    76   for(int i = 0; i < WM_MAX_CONFIGS; ++i)
    77   {
    78     this->configs[i].bUsed = false;
    79     for(int j = 0; j < WM_MAX_SLOTS; ++j)
    80       this->configs[i].slots[j] = NULL;
    81   }
    82   this->currConfID = WM_CONFIG0;
    83 
    84 
    85 
    86 
     66  this->parent = NULL;
     67
     68  for (int i = 0; i < WM_MAX_CONFIGS; i++)
     69    for (int j = 0; j < WM_MAX_SLOTS; j++)
     70      this->configs[i][j] = NULL;
     71
     72  for (int i = 0; i < WM_MAX_SLOTS; i++)
     73  {
     74    this->currentSlotConfig[i].capability = WM_SLOTC_ALL;
     75    this->currentSlotConfig[i].currentWeapon = NULL;
     76    this->currentSlotConfig[i].nextWeapon = NULL;
     77  }
     78
     79  for (int i = 0; i < WM_MAX_LOADED_WEAPONS; i++)
     80    this->availiableWeapons[i] = NULL;
     81
     82
     83  this->currentConfigID = 0;
     84  this->slotCount = 2;
     85  this->weaponChange;
     86
     87  // CROSSHAIR INITIALISATION
    8788  this->crosshair = new Crosshair();
    8889
     
    101102{
    102103  static_cast<BaseObject*>(this)->loadParams(root);
    103 
     104/*
    104105  LoadParam<WeaponManager>(root, "slot-count", this, &WeaponManager::setSlotCount)
    105106      .describe("how many slots(cannons) the WeaponManager can handle");
     
    111112      // LoadParam<WeaponManager>(root, "Weapon", this, &WeaponManager::addWeapon);
    112113
    113   LOAD_PARAM_END_CYCLE;
     114  LOAD_PARAM_END_CYCLE;*/
    114115}
    115116
     
    134135 * @param slotCount the number of slots
    135136 */
    136 void WeaponManager::setSlotCount(int slotCount)
    137 {
    138   this->slotCount = slotCount;
     137void WeaponManager::setSlotCount(unsigned int slotCount)
     138{
     139  if (slotCount <= WM_MAX_SLOTS)
     140    this->slotCount = slotCount;
     141  else
     142    this->slotCount = WM_MAX_SLOTS;
    139143}
    140144
     
    152156void WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID)
    153157{
    154   if( slotID == WM_FREE_SLOT)
    155   {
    156     int freeSlot = this->getNextFreeSlot( configID);
    157     if( freeSlot < 0 || freeSlot >= this->slotCount)
     158  if (unlikely(configID >= WM_MAX_CONFIGS || slotID >= (int)this->slotCount))
     159  {
     160    PRINTF(2)("Slot %d of config %d is not availiabe\n", slotID, configID);
     161    return;
     162  }
     163
     164  if (this->configs[configID][slotID] != NULL && configID > 0 && slotID > 0)
     165    PRINTF(3)("Weapon-slot %d/%d of %s already poulated, overwriting\n", configID, slotID, this->getName());
     166
     167  if (slotID == -1) // WM_FREE_SLOT
     168  {
     169    slotID = this->getNextFreeSlot(configID);
     170    if( slotID < 0 || slotID >= this->slotCount)
    158171    {
    159172      PRINTF(0)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n");
    160173      return;
    161174    }
    162     PRINTF(3)("Added new Weapon to Config:%i/Slot:%i\n", configID, freeSlot);
    163     this->configs[configID].bUsed = true;
    164     this->configs[configID].slots[freeSlot] = weapon;
    165     return;
    166   }
    167   this->configs[configID].bUsed = true;
    168   this->configs[configID].slots[slotID] = weapon;
     175  }
     176  this->configs[configID][slotID] = weapon;
    169177  PRINTF(3)("Added a new Weapon to the WeaponManager: config %i/ slot %i\n", configID, slotID);
    170178}
     
    187195void WeaponManager::nextWeaponConf()
    188196{
    189   PRINTF(4)("Changing weapon configuration: from %i to next\n", this->currConfID);
    190 
    191   int i, lastConfID;
    192   lastConfID = this->currConfID;
    193   for(i = this->currConfID + 1; i < WM_MAX_CONFIGS && !this->configs[i].bUsed; ++i);
    194   if( i == WM_MAX_CONFIGS) this->currConfID = WM_CONFIG0;
    195   else this->currConfID = i;
    196 
    197 
    198   Weapon *w1, *w2;
    199   for(int j = 0; j < WM_MAX_SLOTS; ++j)
    200   {
    201     w1 = this->configs[lastConfID].slots[j];
    202     w2 = this->configs[this->currConfID].slots[j];
    203 
    204     if( w1 == w2)
     197  PRINTF(4)("Changing weapon configuration: from %i to next\n", this->currentConfigID);
     198
     199  ++this->currentConfigID;
     200  if (this->currentConfigID >= WM_MAX_CONFIGS)
     201    this->currentConfigID = 0;
     202
     203  for (int i = 0; i < WM_MAX_SLOTS; i++)
     204  {
     205    this->currentSlotConfig[i].nextWeapon = this->configs[currentConfigID][i];
     206    if (this->currentSlotConfig[i].currentWeapon != this->currentSlotConfig[i].nextWeapon && this->currentSlotConfig[i].currentWeapon != NULL)
     207      (this->currentSlotConfig[i].currentWeapon->requestAction(WA_DEACTIVATE));
     208  }
     209
     210  this->debug();
     211}
     212
     213
     214/**
     215 * triggers fire of all weapons in the current weaponconfig
     216 */
     217void WeaponManager::fire()
     218{
     219  Weapon* firingWeapon;
     220  for(int i = 0; i < this->slotCount; i++)
     221  {
     222    firingWeapon = this->currentSlotConfig[i].currentWeapon;
     223    if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT);
     224  }
     225  this->crosshair->setRotationSpeed(500);
     226  this->crossHairSizeAnim->replay();
     227}
     228
     229
     230/**
     231 * triggers tick of all weapons in the current weaponconfig
     232 * @param second passed since last tick
     233 */
     234void WeaponManager::tick(float dt)
     235{
     236  Weapon* tickWeapon;
     237
     238  // all weapons
     239  for(int i = 0; i < this->slotCount; i++)
     240  {
     241
     242    tickWeapon = this->currentSlotConfig[i].currentWeapon;
     243    if (tickWeapon != this->currentSlotConfig[i].nextWeapon) // if no change occures
    205244    {
    206       printf("no need for change\n");
    207     }
    208     else
    209     {
    210       if( w1 != NULL )
     245      if (tickWeapon != NULL && tickWeapon->isActive())
    211246      {
    212         w1->requestAction(WA_DEACTIVATE);
    213         printf("deactivating %i,%i\n", j,lastConfID);
     247        tickWeapon->requestAction(WA_DEACTIVATE);
    214248      }
    215       if( w2 != NULL)
     249      else
    216250      {
    217         w2->requestAction(WA_ACTIVATE);
    218         printf("activating %i,%i\n", j, this->currConfID);
     251        tickWeapon = this->currentSlotConfig[i].currentWeapon = this->currentSlotConfig[i].nextWeapon;
     252        if (tickWeapon != NULL)
     253          tickWeapon->requestAction(WA_ACTIVATE);
    219254      }
    220255    }
    221   }
    222 }
    223 
    224 
    225 /**
    226  * triggers fire of all weapons in the current weaponconfig
    227  */
    228 void WeaponManager::fire()
    229 {
    230   Weapon* firingWeapon;
    231   for(int i = 0; i < WM_MAX_SLOTS; ++i)
    232   {
    233     firingWeapon = this->configs[this->currConfID].slots[i];
    234     if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT);
    235   }
    236   this->crosshair->setRotationSpeed(500);
    237   this->crossHairSizeAnim->replay();
    238 }
    239 
    240 
    241 /**
    242  * triggers tick of all weapons in the current weaponconfig
    243  * @param second passed since last tick
    244  */
    245 void WeaponManager::tick(float dt)
    246 {
    247   Weapon* w;
    248   for(int i = 0; i < WM_MAX_SLOTS; ++i)
    249   {
    250     w = this->configs[this->currConfID].slots[i];
    251     if( w != NULL && w->isActive())
    252       w->tickW(dt);
     256
     257    if( tickWeapon != NULL && tickWeapon->isActive())
     258      tickWeapon->tickW(dt);
    253259  }
    254260
     
    260266 * triggers draw of all weapons in the current weaponconfig
    261267 */
    262 void WeaponManager::draw()
    263 {
    264   Weapon* w;
    265   for (int j = 0; j < 4; ++j )
    266   for(int i = 0; i < WM_MAX_SLOTS; ++i)
    267   {
    268     w = this->configs[j].slots[i];
    269     if( w != NULL && w->isVisible())
    270       w->draw();
     268void WeaponManager::draw() const
     269{
     270  Weapon* drawWeapon;
     271  for (int i = 0; i < this->slotCount; i++)
     272  {
     273    drawWeapon = this->currentSlotConfig[i].currentWeapon;
     274    if( drawWeapon != NULL && drawWeapon->isVisible())
     275      drawWeapon->draw();
    271276  }
    272277}
     
    279284int WeaponManager::getNextFreeSlot(int configID)
    280285{
    281   for( int i = 0; i < WM_MAX_SLOTS; ++i)
    282   {
    283     if( this->configs[configID].slots[i] == NULL)
     286  for( int i = 0; i < this->slotCount; ++i)
     287  {
     288    if( this->configs[configID][i] == NULL)
    284289      return i;
    285290  }
     
    287292}
    288293
     294
     295
     296void WeaponManager::debug() const
     297{
     298  PRINT(3)("WeaponManager Debug Information\n");
     299  PRINT(3)("-------------------------------\n");
     300  PRINT(3)("current Config is %d\n", this->currentConfigID);
     301  for (int i = 0; i < WM_MAX_CONFIGS; i++)
     302  {
     303    PRINT(3)("Listing Weapons in Configuration %d\n", i);
     304    for (int j = 0; j < WM_MAX_SLOTS; j++)
     305    {
     306      if (this->configs[i][j] != NULL)
     307        PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassName());
     308    }
     309  }
     310}
Note: See TracChangeset for help on using the changeset viewer.