G
G
Grigory Kalyashov2015-04-17 17:18:36
Programming
Grigory Kalyashov, 2015-04-17 17:18:36

How to solve the problem with OOP?

There is a first base class

class FireParent
{
public:
  FireParent(float _x, float _y, int _speed, float _angle, float _targetX, float _targetY);
  
  ...
  
private:
  
  
};

There are classes that inherit from it:
class Bullet : public FireParent
{
public:
  Bullet(float _x, float _y, int _speed, float _angle, float _targetX, float _targetY);
private:
};

class Rocket : public FireParent
{
public:
  Rocket(float _x, float _y, int _speed, float _angle, float _targetX, float _targetY);
private:
};

There is a second base class, with a field of type FireParent
class TowerParent
{
public:
  TowerParent(int _col, int _row, float _posX, float _posY, float _attackRange, float _angle);
  
  ...

  FireParent* towerFire;
  
private:
  
};

There are classes that inherit from TowerParent:
class SimpleTower : public TowerParent
{
public:
  SimpleTower(int _col, int _row, float _posX, float _posY, float _attackRange, float _angle);
private:
  
};


class RocketTower : public TowerParent
{
public:
  RocketTower(int _col, int _row, float _posX, float _posY, float _attackRange, float _angle);
private:
  
};

How can I make the towerFire attribute of the SimpleTower class be Bullet and the RocketTower class be Rocket ?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Evgeny Shatunov, 2015-04-17
@kalyashov

Yes, my friend, the king of content!
It's a joke. Although not funny for homemade game developers. :)
So, but jokes aside. You don't have to do that at all. The type of damage/projectile/unit/weapon dealt should never be described by its own class. All units must be of the same class, all bullets must be the same, all buildings must be the same.
You should only have a set of generic classes for entities. Specific parameters (what flies from where, how it damages and how it all visually looks) should be in the resources.
I recommend reading: habrahabr.ru/post/255561 The above is much more important for the area of ​​\u200b\u200bfavorite scooters in C ++.
Though... If so it is necessary for you to implement on classes, but not on resources, then you should simply organize actual communication between entities. Let's offer one terrible variant... It's all too much to debug yourself.

class TowerParent
{
public:
  // Это тип для той штуки, которая будет создавать нам нужную боеголовку.
  typedef FireParent* (*FireConstructor)( float, float, int, float, float, float );
  
  TowerParent(int _col, int _row, float _posX, float _posY, float _attackRange, float _angle);

  //...
  
  FireParent* towerFire;
protected:
  FireConstructor	fire_constructor; // инстанция конструктора боеголовок.
  
  // Вот так мы будем определять тип конструируемой боеголовки.
  template< class fire_t >
  inline void DefineFireType(){
    fire_constructor = TowerParent::template MakeFire<fire_t>;
  };
  
private:
  // А вот так мы будем конструировать боеголовку.
  template< class fire_t >
  static FireParent* MakeFire( float _x, float _y, int _speed, float _angle, float _targetX, float _targetY ){
    return new fire_t( _x, _y, _speed, _angle, _targetX, _targetY );
  };
};


SimpleTower( int _col, int _row, float _posX, float _posY, float _attackRange, float _angle ){
  DefineFireType<Bullet>();
};

RocketTower(int _col, int _row, float _posX, float _posY, float _attackRange, float _angle){
  DefineFireType<Rocket>();
};

Construction is done through:
One more amendment. Not Rocket, but Missile. Because ... :)
Another option, already less scary, is base class templating:
template< class fire_t >
class TowerParent
{
public:
  TowerParent(int _col, int _row, float _posX, float _posY, float _attackRange, float _angle);

  //...

  fire_t* towerFire;

protected:
  inline fire_t* MkeFire( float _x, float _y, int _speed, float _angle, float _targetX, float _targetY ){
    return new fire_t( _x, _y, _speed, _angle, _targetX, _targetY );
  };
};

class Bullet : public FireParent<Bullet>
{
public:
  Bullet(float _x, float _y, int _speed, float _angle, float _targetX, float _targetY);
private:
};

class Rocket : public FireParent<Rocket>
{
public:
  Rocket(float _x, float _y, int _speed, float _angle, float _targetX, float _targetY);
private:
};

V
Vladimir Martyanov, 2015-04-17
@vilgeforce

Templates?

D
DISaccount, 2015-04-17
@DISaccount

In the SimpleTower constructor:

SimpleTower(int _col, int _row, float _posX, float _posY, float _attackRange, float _angle)
{
...
towerFire = new Bullet(...);
...
}

In the RocketTower constructor:
RocketTower(int _col, int _row, float _posX, float _posY, float _attackRange, float _angle)
{
...
towerFire = new Rocket(...);
...
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question