Search code examples
c++oopdesign-patternsgame-engine

Game development - Update method and State pattern


For my game, I use a State Pattern to handle my screens. The screen need to update and render each frame. And the game loop is in the Game class.

But I also need an update and render method in the Entity class. Should I use an interface in the Screen and Entity class (e.g. FrameProcess) ? Or can I leave it like this (see image)?

What is the best pratice in OOP ? Are there any pattern for that ?

Basic UML of this problem:

enter image description here


Solution

  • I would create pure virtual functions for update() and render() methods in the Entity class and let the classes that inherit from it define their own behaviour which will let the system behave in a polymorphic manner. Then in Game class or some other type of handler class call update() and draw() methods based on the current state of the system which decides it using the State pattern.

    Btw, it tells more about State pattern and its example usage(s) in a very clear manner at http://gameprogrammingpatterns.com/state.html