1
\$\begingroup\$

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?

\$\endgroup\$
2
  • 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\$ Commented Sep 25, 2011 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\$ Commented Sep 25, 2011 at 23:06

2 Answers 2

2
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\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\$ Commented Sep 26, 2011 at 3:13
4
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .