Multiple AI opponents

Jul 8, 2012 at 12:29am
I've been putting together a 2D side-scolling game using the Allegro 5 library. However, I've hit a wall with this, as I've suddenly realised I'm not really too sure how I would implement AI opponents that have the same behaviour, but behave differently under different conditions.

For example, say I wanted 5 AI opponents shooting at my character in one part of the level. How would I go about creating one section of code and using it to control the behaviour of each of these AI characters?

Thanks in advance!
Jul 8, 2012 at 12:36am
Create a class for handling an opponent. You can then create an instance of the class for each opponent you want. Store the objects in a container so that you can easily loop through the opponents.
Jul 8, 2012 at 5:36pm
How would I create an instance dynamically for each individual enemy? I understand what you're saying, I just don't know how to go about doing it.
Jul 8, 2012 at 7:30pm
Something as simple as this usually works:

1
2
3
4
5
6
7
std::vector<ENEMY_CLASS*> EnemyArray;

for(int i = 0; i < NUMBER_OF_ENEMYS; ++i)
{
      ENEMY_CLASS* Enemy = new ENEMY_CLASS();
      EnemyArray.push_back(Enemy);
}


Obviously if your constructor took any arguments you would pass them in on Line 5. Also note that this is a vector or pointers to objects, so you will need to de-reference them when you use them.
Jul 9, 2012 at 3:01pm
I've never used a vector before, so this is probably going to sound really stupid, but how would I access and use the instances of the enemy class once they have been created?
Jul 9, 2012 at 3:09pm
Same way you would with an array. Or use the at() method. Or use an iterator.
Topic archived. No new replies allowed.