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

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

How can this be achieved?

  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – Nicol Bolas Sep 25 '11 at 18:23
  • 1
    \$\begingroup\$ You've answered this in your question. Just use an abstract base class and inherit from it. Whats the problem? \$\endgroup\$ – deceleratedcaviar Sep 25 '11 at 23:06

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.

| improve this answer | |
  • \$\begingroup\$ +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. \$\endgroup\$ – 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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.