Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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?

share|improve this question
3  
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
1  
You've answered this in your question. Just use an abstract base class and inherit from it. Whats the problem? –  Daniel Sep 25 '11 at 23:06
add comment

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
+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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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