Answer the question
In order to leave comments, you need to log in
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:
};
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:
};
class TowerParent
{
public:
TowerParent(int _col, int _row, float _posX, float _posY, float _attackRange, float _angle);
...
FireParent* towerFire;
private:
};
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:
};
Answer the question
In order to leave comments, you need to log in
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>();
};
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:
};
In the SimpleTower constructor:
SimpleTower(int _col, int _row, float _posX, float _posY, float _attackRange, float _angle)
{
...
towerFire = new Bullet(...);
...
}
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 questionAsk a Question
731 491 924 answers to any question