How to make enemies interact with each other.
This may seem like it should be easy, but the way I wrote the code makes it nearly impossible for enemy objects to directly interact with each other.
Why? Well first, I need to explain how the enemy objects work.
First, there is an abstract enemy base class. This holds all the properties common to every enemy. All enemies are derived from that class.
Than all enemies that are currently spawned into the map are handled by the "Enemy Manager", which updates and draws them. The enemy manager boils down to one thing in the end, this:
Any type of enemy class can be added to this vector, but once they are, they essentialy lose their identity. Ofcourse, I suppose that I could find out the type of an enemy by:
(I'm not sure about this, I didn't even know about typeid until a couple of days ago)
This still doesn't fix my problem. The problem really lies in the fact that, by default, an enemy class doesn't "know" that there will be more enemies. If it doesn't know about other enemies, how can it directly interact with them?
The C++ Cookbook supplies a possible solution. One of the "recipes" in the book regards creating classes that automatically store themselves in a container:
// Member variables
// Automatically insert self into container upon creation
// Will this work?
Usually when I think of class recursion like this, my brain starts to hurt, and I have to lie down.
Ofcourse, I won't be using this method, because I don't believe it will work.
So with the problem still not solved, I kept thinking. The only solution I can think of is to have enemies send signals. Since enemy objects are not able to interact directly, I figure that they can act indirectly through messages sent out from the enemies themselves.
To give an example, I recieved a suggestion awhile ago that BombBots should be able to damage other enemies when they explode. I didn't think I could do this at the time, but now I think I should change my enemy code so this is possible. For instance, when a BombBot dies, it will send out a signal saying that there is an explosion at it's current location. Then, if another enemy walks into the explosion area, they will get that signal and the object will know that it should take damage.
This would also allow the level 2 battleships to parachute in new enemies without having to allocate memory. Instead of having to create new enemies to parachute, using a signal would allow dead enemies to be reused.
Much like the homing launcher, this is still theory and not yet tried, so I don't even know if it will work.