🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Drawing Different Enemies With Same Class?

Started by
7 comments, last by scott8 2 years, 7 months ago

Hi. I m a little bit confused about inheritence. I created an enemy class and calling enemies with a vector like this:

class enemy {
public:
    sf::RectangleShape rect;
    float bottom, left, right, top;
    sf::Texture enemytexture;
    sf::Sprite enemysprite;

    enemy(sf::Vector2f position, sf::Vector2f size, sf::Color color)
    {
        rect.setPosition(position);
        rect.setSize(size);
        rect.setFillColor(color);
        enemytexture.loadFromFile("npc1.png");
        enemysprite.setTexture(enemytexture);
    }
    void update() {//////}
};

class goblin : public enemy
{
    public:
        goblin(sf::Vector2f position, sf::Vector2f size, sf::Color color);

    void update() {
        enemytexture.loadFromFile("npc2.png");
        enemysprite.setTexture(enemytexture);
    };
};
std::vector<std::unique_ptr<enemy> > enemies1;
enemies1.emplace_back(std::unique_ptr<enemy>(new enemy(sf::Vector2f(100, 100), sf::Vector2f(15, 10), sf::Color(255, 255, 255, 255))));
enemies1.emplace_back(std::unique_ptr<enemy>(new goblin::enemy(sf::Vector2f(100, 100), sf::Vector2f(15, 10), sf::Color(255, 255, 255, 255))));

But i would like to draw different enemies and sprites like goblin etc. With this sample i draw only npc1.png for enemy and goblin classes. Is it possible to do that or do i need create another class? I searched some tutorials about inheritence but i cannot solve my problem. Plz i will appreciate for every suggestion or help.

[Moderator edit: fixed formatting]

Advertisement

A simple approach might be to have your sub-classes (e.g. the “goblin” class) load their specific textures in their own constructors.

A perhaps-more-elegant approach might be to have your “enemy” class accept a texture-name as a parameter, and to then have each sub-class pass in their specific texture-names when they call their super-class constructors.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

You can overload the constructor of your class and specify the sprite you want to use on construction. This won't even allocate memory for as long as you're using a const string. You need inheritance only when you want to alter some behavior of your class and only if this isn't a pure data but functional change. for example you may want to have a default attack function which is a melee combat thing and in some classes override that function to be a ranged attack thingy.

Please use the code block next time you post source code for us to have a better read

The most sustainable approach is not having a number of classes like "goblin" that differ only in constants (e.g. "npc2.png") and random errors (e.g. calling setTexture() before loading the texture due to a cut and paste mishap).

Realistically, some kind of factory should populate the list of enemies in a level and “own” all enemy types and stats, without needing help from a fragile mess of enemy subclasses.

Omae Wa Mou Shindeiru

LorenzoGatti said:

The most sustainable approach is not having a number of classes like "goblin" that differ only in constants (e.g. "npc2.png") and random errors (e.g. calling setTexture() before loading the texture due to a cut and paste mishap).

Realistically, some kind of factory should populate the list of enemies in a level and “own” all enemy types and stats, without needing help from a fragile mess of enemy subclasses.

I would argue that this depends somewhat on the degree to which enemies will differ:

If they differ little--e.g. by no more than a set of constants, then indeed, there's likely little reason for separate classes.

Conversely, however, if different enemies are intended to include differing logic, then I would suggest that sub-classes might be preferable--and indeed, that a factory might quickly become a monolithic clutter.

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

The main problem here is that you haven't declared your update function as virtual - this means that it will never call goblin::update.

You will also want to move the loadFromFile call out of goblin::update and into the constructor - you don't want to load the file again in every update!

Thanks for all answers i will try to update my code. Best regards.

Just a thought:

You could have several different goblin graphics saved on disk. When the game loads, the goblin class loads one of the goblin graphics at random and stores it as a private item. Don't load graphics in Update(). This could be an easy way to introduce slight variations into the game.

Another variation would be you draw a base goblin, then add various accessories over the image. Maybe one goblin has a helmet, others don't. You could note which goblins have helmets and give them an extra armor rating.

I remember reading that Left4Dead used something like this to generate variation in the zombies. There were a few base zombies, but they would swap textures for shirts, add bloodstains, different hair styles, etc.

This topic is closed to new replies.

Advertisement