Managing lists of different types of entities - is there a better way?

I am developing a 2D space game for mobile devices, but it gets realy complex and my solution is realy confusing and produces lots of repeated code segments.

I hava a world class in which i have multible lists of different object like:

List<Enemy> enemys;
List<Projectile> projectiles;
List<Collectable> collectables;
List<Asteroid> asteroids;
List<Effect> effects;
..


Each list gets updated by the world class. but thats not all.. Each enemy has a list of engines, and list of weaponlunchers which gets updated by the enemy. Now each engine adds some fire effects to the world list 'effects', and each weaponluncher adds projectiles to the world list 'projectiles'. All these classes have different parameter, so i need an extra update AND extra render function for each class.

At least they are all childs of 'GameObject' which provides them basic things like position, velocity and acceleration vectors, bounding Polygons and functions like applyForce and a finite state machine

Is there a better or more common way to do this? like one catch-all class which contains all possible parameters and methods for all different objects. (i think this would produce even more confusing code)

Is there a better or more common way to do this? like one catch-all class which contains all possible parameters and methods for all different objects.

Yes, it's called a architecture. Have a look here for details.

This would let just one list of entities in your world class:

List<Entity>


It's one example of Composition over inheritance.

Within the bounds of your original question one option would be to break out the lists into Actions instead of Types like what you have. For example, you'd have the List<HasAI> and one for List<Collidable> and when you create a new object it adds itself to whatever action list it needs, when the object is destroyed then it removes itself from those lists. An object can be on multiple lists.

Step two of that option is to refactor your classes from a hierarchy into one that uses Interfaces, so your HasAI type is a class that implements the IBrains interface as an example. Matching that the List<HasAI> only requires objects to have the IBrains interface.

That should help clean up some of the complexity.

I think what you need to do is abstract the rendering in to its own classes, I think one for player and one for enemy (or maybe just one for player, and then create an instance of it for player and enemy - not 100% sure of your game design.)

You could use a visitor pattern to let the renderer loop around the various objects and decide what to draw, that might be less complex.

Using a base class and relying on polymorphism is a very good idea. But there are some alternate approaches. There are two articles by Noel Llopis that are worth reading. I've been looking at Data Oriented Design lately and think it has some merit.

The articles are here and here. It may simplify your code a little bit more then polymorphism. But like anything YMMV, take a look and see if it fits in with what you are trying to achieve. Polymorphism uses inheritance and DOD uses aggregation both have pros and cons so choose your poison.

You can by utilizing OOP and especially polymorphism.

Basically, you have to create a base class, which all game entities inherit from. This base class, is as abstract as possible, it will contain things such as a Creation, Update and Perhaps a Destroy function.

Then you derive all your other game objects from this class. You will probably also need to add some sort of reflection into your code if your language does not support it, to do some more advanced things, but it's avoidable if you know how to structure your inheritance correctly.

• He already did this -- all his classes are children of GameObject. He just needs to turn his series of lists into a single list of GameObjects. May 20 '12 at 20:45