[SDL] Game Dev. Question.

I'm quite familiar with the C++ syntax and the SDL2 library, but not so much with structure. I'm going to be making a simple platformer game and want to structure it better than I have in the past.

In the past I've had an enemy, player, and NPC class. All of which more or less had identical functions. Load, Draw, Update, etc, etc. For this reason I've decided to try a different approach, an Entity class that all 3 of those can inherit from.

My question is where do you draw the line, I feel like the player, enemy, and NPC are sharing so much of the same data, functions, etc, etc that once they inherit from the Entity class, their own classes are almost empty. Once I've loaded, drawn, and updated them from the Entity class, their own classes are doing almost nothing.

Should I reserve things like HP, mana, inventory for the player class? I feel like even those could be put into the parent class of "Entity". So can I get some input on this. I feel like my Entity class has taken over the entire game, rather than just helping the 3 derived classes.
Well, there's nothing inherently wrong in anything you've said. A design is only bad if it hinders you in some way.
From what you've said, I expect that the only difference between the three classes is that an enemy has a hostile AI, an NPC has a neutral or no AI, and a player has no AI, but also has its behavior determined by external events. If you feel that the three classes are unnecessary, you could do away with them and instead have something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class BehaviorSource{
public:
    virtual Action get_action() = 0;
};

class AI : public BehaviorSource{
public:
    Action get_action() override;
};

//different subtypes of AI

class User : public BehaviorSource{
public:
    Action get_action() override;
}

//has no subclasses:
class Entity{
    std::unique_ptr<BehaviorSource> behavior_source;
    void perform_action(const Action &);
public:
    void update(){
        auto action = this->behavior_source->get_action();
        this->perform_action(action);
    }
};

Still, I don't think there's anything wrong with your design, from what you've said.
Last edited on
Topic archived. No new replies allowed.