What's a good entity hierarchy for a 2D game?

Posted by futlib on Game Development See other posts from Game Development or by futlib
Published on 2012-10-19T11:22:18Z Indexed on 2012/10/19 17:20 UTC
Read the original article Hit count: 320

Filed under:
|
|
|
|

I'm in the process of building a new 2D game out of some code I wrote a while ago.

The object hierarchy for entities is like this:

  • Scene (e.g. MainMenu): Contains multiple entities and delegates update()/draw() to each
  • Entity: Base class for all things in a scene (e.g. MenuItem or Alien)
  • Sprite: Base class for all entities that just draw a texture, i.e. don't have their own drawing logic

Does it make sense to split up entities and sprites up like that? I think in a 2D game, the terms entity and sprite are somewhat synonymous, right?

But I do believe that I need some base class for entities that just draw a texture, as opposed to drawing themselves, to avoid duplication. Most entities are like that.

One weird case is my Text class: It derives from Sprite, which accepts either the path of an image or an already loaded texture in its constructor. Text loads a texture in its constructor and passes that to Sprite.

Can you outline a design that makes more sense? Or point me to a good object-oriented reference code base for a 2D game? I could only find 3D engine code bases of decent code quality, e.g. Doom 3 and HPL1Engine.

© Game Development or respective owner

Related posts about c++

Related posts about 2d