Liskov Replacement Principle and Cool Design for the Game

In my game, I defined a class Screenthat contains links to several visual objects Entitythat can be drawn on the display:

class Screen {
public:

private:
  std::vector<Entity*> entities_;
};

All Entity's have a function Draw():

class Entity {
  public:
    void Draw();
  private:
    int xpos;
    int ypos;
};

Screenresponsible for calling the function Draw()for each of its Entitys.

The problem is that some (but not all) Entitymust also be updated, that is, over time, they will change their appearance / position. ScreenYou also need to call the function Update(), but only for those objects that are updatable.

My question is:

Does it make sense to infer a class from Entitythat has an update function:

class ChangingEntity : public Entity {
  public: 
     void Update(int time);
};

and Screenlook like this:

class Screen {
public:

private:
  std::vector<Entity*> entities_;
  std::vector<ChangingEntity*> changing_entities_;
};

, Draw() Entity s Draw() Update() ChangingEntity s.

- Update() Entity, Entity , Update() ?

+4
1

, :

, Draw() Drawable ( ). , Entity, , , . .

-, , Entity, Ogre3D, , Entity an instance of a discrete, movable object based on a Mesh, , , . Updatable, StaticEntity . , .

-, Screen Renderer , . Drawable.

+3

Source: https://habr.com/ru/post/1528922/


All Articles