# Enemy class architecture

I'm developing a game for Android in Java. I would like to know how a good Enemy class architecture could be implemented.

I've created an abstract class called Enemy. It stores common properties like Health and Animation data. It has methods like LoadContent, Update and Draw.

I want to inherit this class and implement different enemy classes. I want to be able to store all my active enemies in some sort of collection. Then in my Update function of my game loop, I'll do something like:

for each Enemy in Enemies
{
Enemy.Update()
}


And the Update method of the correct child class would be called.

How can this be achieved?

• This question is exceedingly vague. You don't say what kind of game you're making, so we have no way of knowing how to structure any particular class for it. There are games where it is reasonable for the structure dealing with enemies to be very different from the player. And there are games where it is not reasonable, that there should just be Entity, which may be controlled by an AI or the player. Without knowing what you're making, there is no way to know how to properly answer your question. – Nicol Bolas Sep 25 '11 at 18:23
• You've answered this in your question. Just use an abstract base class and inherit from it. Whats the problem? – deceleratedcaviar Sep 25 '11 at 23:06

## 2 Answers

I would consider making an enemy factory. Every time you make a new enemy of any sort, add it to a list inside your factory instance. This means that you can update all of your enemies from your enemy factory, and you know that every enemy is in the list.

• +1. The other advantage to the factory/manager class is that in garbage collected environments (Java, C#, Actionscript), keeping the number of references down is always a good thing. – Jordaan Mylonas Sep 26 '11 at 3:13

If Update is abstract in your base class then all you have to do is override it in your child classes and the relevant update function will be called on each one.