Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 21, 2007, 12:44:02 AM (18 years ago)
Author:
rgrieder
Message:
  • modified the AmmunitionDump to hold different types of ammo
  • converted the RunManager into a Singleton
  • added some methods to address ammo by string
  • created a BaseWeapon class
  • derived BarrelGun from BaseWeapon
Location:
code/branches/main_reto_vs05/src/weapon
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • code/branches/main_reto_vs05/src/weapon/ammunition_dump.cc

    r198 r232  
    2626 */
    2727
     28#include "run_manager.h"
     29
    2830#include "ammunition_dump.h"
    2931
     
    3234namespace weapon {
    3335
    34   AmmunitionDump::AmmunitionDump(int capacity)
    35         : stock_(0), capacity_(capacity)
     36  AmmunitionDump::AmmunitionDump()
     37    : numberOfAmmos_(RunManager::getSingletonPtr()->getNumberOfAmmos()),
     38      stock_(new int[numberOfAmmos_]),
     39      capacity_(new int[numberOfAmmos_])
    3640  {
     41    for (int i = 0; i < numberOfAmmos_; i++)
     42    {
     43      stock_[i] = 0;
     44      capacity_[i] = 0;
     45    }
    3746  }
    3847
     
    4049  AmmunitionDump::~AmmunitionDump()
    4150  {
     51    if (stock_)
     52      delete stock_;
     53    if (capacity_)
     54      delete capacity_;
     55  }
     56
     57  void AmmunitionDump::setDumpSize(const Ogre::String &name, int size)
     58  {
     59    if (size < 0)
     60      return;
     61    int id = RunManager::getSingletonPtr()->getAmmunitionID(name);
     62    if (id == -1)
     63      return;
     64    capacity_[id] = size;
    4265  }
    4366
    4467 
    45   void AmmunitionDump::store(int quantity)
     68  int AmmunitionDump::store(const Ogre::String &name, int quantity)
    4669  {
    47     stock_ += quantity;
    48     if (stock_ > capacity_)
    49       stock_ = capacity_;
     70    int id = RunManager::getSingletonPtr()->getAmmunitionID(name);
     71    if (id == -1)
     72      return quantity;
     73    stock_[id] += quantity;
     74    if (stock_[id] > capacity_[id])
     75    {
     76      quantity = capacity_[id] - stock_[id];
     77      stock_[id] = capacity_[id];
     78      return quantity;
     79    }
     80    else
     81      return 0;
    5082  }
    5183
    5284
    53   int AmmunitionDump::getAmmunition(int quantity)
     85  int AmmunitionDump::getAmmunition(const Ogre::String &name, int quantity)
    5486  {
    55     if (stock_ >= quantity)
    56     {
    57       stock_ -= quantity;
    58       return quantity;
    59     }
     87    int id = RunManager::getSingletonPtr()->getAmmunitionID(name);
     88    if (id == -1)
     89      return 0;
     90    if (stock_[id] >= quantity)
     91      stock_[id] -= quantity;
    6092    else
    6193    {
    62       quantity = stock_;
    63       stock_ = 0;
    64       return quantity;
     94      quantity = stock_[id];
     95      stock_[id] = 0;
    6596    }
     97    return quantity;
    6698  }
    6799
    68100
    69   int AmmunitionDump::getStockSize()
     101  int AmmunitionDump::getStockSize(const Ogre::String &name)
    70102  {
    71     return stock_;
     103    int id = RunManager::getSingletonPtr()->getAmmunitionID(name);
     104    if (id = -1)
     105      return -1;
     106    return stock_[id];
    72107  }
    73108}
  • code/branches/main_reto_vs05/src/weapon/ammunition_dump.h

    r198 r232  
    4141  {
    4242  public:
    43           AmmunitionDump(int capacity);
     43    AmmunitionDump();
    4444          ~AmmunitionDump();
    4545
    46     void store(int quantiy);
     46    void setDumpSize(const Ogre::String &name, int size);
    4747
    48     int getAmmunition(int quantity);
     48    int store(const Ogre::String &name, int quantiy);
    4949
    50     int getStockSize();
     50    int getAmmunition(const Ogre::String &name, int quantity);
     51
     52    int getStockSize(const Ogre::String &name);
    5153
    5254  protected:
    53     int stock_;
    54     int capacity_;
     55    int numberOfAmmos_;
     56    int *stock_;
     57    int *capacity_;
    5558
    5659  protected:
  • code/branches/main_reto_vs05/src/weapon/base_weapon.cpp

    r198 r232  
    3737#include "inertial_node.h"
    3838#include "ammunition_dump.h"
     39#include "run_manager.h"
    3940
    4041#include "base_weapon.h"
     
    4546  using namespace Ogre;
    4647
    47   BaseWeapon::BaseWeapon(SceneManager *sceneMgr, InertialNode *node,
    48         BulletManager *bulletManager, AmmunitionDump *ammoDump)
    49         : sceneMgr_(sceneMgr), node_(node),
    50         bulletCounter_(0), primaryFireRequest_(false), currentState_(IDLE),
    51         secondaryFireRequest_(false),
    52         bulletManager_(bulletManager), secondaryFired_(false),
    53         timeSinceNextActionAdded_(0), actionAdded_(false), nextAction_(NOTHING),
    54         name_("Base Weapon"), primaryFirePower_(100), secondaryFirePower_(500),
    55         primaryFiringRate_(10), secondaryFiringRate_(2), primaryBulletSpeed_(1000),
    56         secondaryBulletSpeed_(500), magazineSize_(25), ammoDump_(ammoDump)
     48  BaseWeapon::BaseWeapon(InertialNode *node, AmmunitionDump *ammoDump)
     49    : sceneMgr_(RunManager::getSingletonPtr()->getSceneManagerPtr()), node_(node),
     50      bulletCounter_(0), primaryFireRequest_(false), currentState_(IDLE),
     51      secondaryFireRequest_(false),
     52      bulletManager_(RunManager::getSingletonPtr()->getBulletManagerPtr()),
     53      secondaryFired_(false),
     54      timeSinceNextActionAdded_(0), actionAdded_(false), nextAction_(NOTHING),
     55      ammoDump_(ammoDump)
    5756  {
    58     leftAmmo_ = ammoDump_->getAmmunition(magazineSize_);
     57    leftAmmo_ = 0;
    5958  }
    6059
     
    8483
    8584
    86   void BaseWeapon::primaryFire()
    87   {
    88     if (leftAmmo_ < 1)
    89     {
    90       currentState_ = IDLE;
    91       return;
    92     }
    93 
    94     SceneNode *temp = sceneMgr_->getRootSceneNode()->createChildSceneNode(
    95           node_->getSceneNode()->getWorldPosition(),
    96           node_->getSceneNode()->getWorldOrientation());
    97 
    98     Entity* bulletEntity = sceneMgr_->createEntity("BulletEntity"
    99           + StringConverter::toString(bulletCounter_++), "Barrel.mesh");
    100 
    101     Vector3 speed = (temp->getOrientation() * Vector3(0, 0, -1))
    102           .normalisedCopy() * primaryBulletSpeed_;
    103     speed += node_->getWorldSpeed();
    104 
    105           temp->setScale(Vector3(1, 1, 1) * 4);
    106           temp->yaw(Degree(-90));
    107 
    108           bulletManager_->addBullet(new Bullet(temp, bulletEntity, speed));
    109 
    110     --leftAmmo_;
    111   }
    112 
    113 
    114   void BaseWeapon::primaryFiring(unsigned int time)
    115   {
    116     if (time > 100)
    117     {
    118       currentState_ = IDLE;
    119     }
    120   }
    121 
    122 
    12385  void BaseWeapon::secondaryFireRequest()
    12486  {
    12587    secondaryFireRequest_ = true;
    126   }
    127 
    128 
    129   void BaseWeapon::secondaryFire()
    130   {
    131     if (leftAmmo_ < 5)
    132     {
    133       currentState_ = IDLE;
    134       return;
    135     }
    136 
    137     SceneNode *temp = sceneMgr_->getRootSceneNode()->createChildSceneNode(
    138           node_->getSceneNode()->getWorldPosition(),
    139           node_->getSceneNode()->getWorldOrientation());
    140 
    141     Entity* bulletEntity = sceneMgr_->createEntity("BulletEntity"
    142           + StringConverter::toString(bulletCounter_++), "Barrel.mesh");
    143 
    144     Vector3 speed = (temp->getOrientation() * Vector3(0, 0, -1))
    145           .normalisedCopy() * secondaryBulletSpeed_*0.5;
    146     speed += node_->getWorldSpeed();
    147 
    148           temp->setScale(Vector3(1, 1, 1) * 10);
    149           temp->yaw(Degree(-90));
    150 
    151           bulletManager_->addBullet(new Bullet(temp, bulletEntity, speed));
    152 
    153     leftAmmo_ -= 5;
    154   }
    155 
    156 
    157   void BaseWeapon::secondaryFiring(unsigned int time)
    158   {
    159     if (time > 250)
    160       currentState_ = IDLE;
    16188  }
    16289
     
    181108        {
    182109        case RELOAD:
    183           leftAmmo_ += ammoDump_->getAmmunition(magazineSize_ - leftAmmo_);
     110          leftAmmo_ += ammoDump_->getAmmunition("Barrel", magazineSize_ - leftAmmo_);
    184111          break;
    185112
  • code/branches/main_reto_vs05/src/weapon/base_weapon.h

    r198 r232  
    5858
    5959  public:
    60     BaseWeapon(Ogre::SceneManager*, InertialNode*, BulletManager*,
    61           AmmunitionDump*);
     60    BaseWeapon(InertialNode*, AmmunitionDump*);
    6261          virtual ~BaseWeapon();
    6362
     
    7372
    7473  protected:
    75     void primaryFire();
     74    virtual void primaryFire() = 0;
    7675
    77     void primaryFiring(unsigned int);
     76    virtual void primaryFiring(unsigned int) = 0;
    7877
    79     void secondaryFire();
     78    virtual void secondaryFire() = 0;
    8079
    81     void secondaryFiring(unsigned int);
     80    virtual void secondaryFiring(unsigned int) = 0;
    8281
    8382  public:
Note: See TracChangeset for help on using the changeset viewer.