C ++ Weapons Inventory [closed]

1

I am studying videogame development using C ++. For my project, I plan to create a mini-game FPS with weapons. I post here in order to help me a little to think about what the classes in charge of managing the inventory would be like.

I would like my game to have several types of weapons, each type associated with a ".mesh" which are 3D models, plus each weapon has a power, cadence and number of different magazine bullets.

I would like you to have different weapons assigned to your inventory as you advance in level.

I have some course code:

using namespace std;
using namespace std::placeholders;


template<class T>
T* deleter(T* x) {
  delete x;
  return 0;
}

class Weapon {
  int ammo;

protected:
  virtual int power(void) const = 0;
  virtual int max_ammo(void) const = 0;

public:
  Weapon(int ammo=0) : ammo(ammo) { }

  void shoot(void) {
    if (ammo > 0) ammo--;
  }

  bool is_empty(void) {
    return ammo == 0;
  }

  int get_ammo(void) {
    return ammo;
  }

  void add_ammo(int amount) {
    ammo = min(ammo + amount, max_ammo());
  }

  bool same_weapon_as(Weapon* other) {
    return typeid(*this) == typeid(*other);
  }
};


class Pistol : public Weapon {
  virtual int power(void) const    { return 1; };
  virtual int max_ammo(void) const { return 50; };

    public:
      Pistol(int ammo=0) : Weapon(ammo) {}
    };

class Inventory : public vector<Weapon*> {
public:
  typedef typename Inventory::const_iterator WeaponIter;
  typedef vector<Weapon*> WeaponVector;
  class WeaponNotFound {};
  ~Inventory();

  void add(Weapon* weapon) {
    auto it = find_if(begin(), end(),
              bind(&Weapon::same_weapon_as, _1, weapon));

    if (it != end()) {
      (*it)->add_ammo(weapon);
      delete weapon;
      return;
    }

    push_back(weapon);
  }

  WeaponVector weapons_with_ammo(void) {
    WeaponVector retval;

    remove_copy_if(begin(), end(), back_inserter(retval),
           bind(&Weapon::is_empty, _1));

    if (retval.begin() == retval.end())
      throw Inventory::WeaponNotFound();

    return retval;
  }

  Weapon* more_powerful_weapon(void) {
    WeaponVector weapons = weapons_with_ammo();

    sort(weapons.begin(), weapons.end(),
         bind(&Weapon::less_powerful_than, _1, _2));

    return *(weapons.end()-1);
  }
};

Inventory::~Inventory() {
  transform(begin(), end(), begin(), deleter<Weapon>);
  clear();
}

What do they think?

    
asked by Strelok 13.04.2017 в 14:46
source

0 answers