Sign in to follow this  
Carl_Ruben

Programming multiple enemies?

Recommended Posts

Hey! Me and a friend is going to make a game in C++ for our schoolproject.. We have faced a small problem with the programming tho. We want to have multiple enemies moving around the screen, spawning on random places, attacking the player and disappearing when attacked. How am i going to code this, without coding each enemy induvidually?

Share this post


Link to post
Share on other sites
Perhaps create an enemy class, and then populate a linked list (or array, or vector) with them.

Then when you need to draw/update/etc, you just loop through the collection of enemies and update them as required.

Share this post


Link to post
Share on other sites
With a for loop over enemy objects and an update() method on each object?

You simply spawn the enemy objects as needed, then periodically (or on each game loop) update them to check for players near-by and determine your action from there.

There's plenty of designs for AI and different aspects, but it all comes down to how your game works.

Each NPC is in simplest form represented with a state machine. As things change in the world (players move, creatures respawn and die, objects expire, some global events occur), they adjust their state. That would probably be the simplest way.

Share this post


Link to post
Share on other sites
The game is a clone of the game Phobia, the player is running around
on a playfield while enemies are spawning from the playfield borders
and starts to chase the player. There are some obstacles to hide
behind and ofc weapons to kill them.

So if we make an enemy class and a linked list (array, vector and so on),
how can we detect which enemy gets hit so that only that one dies and
despawns?


Thanks for the previous answers! :}

Share this post


Link to post
Share on other sites
When you are updating the 'bullet' object, simply check all of the other actors to see if any of them are overlapping with the bullet, and if so handle the collision.

Share this post


Link to post
Share on other sites
Here's a non tested simple example of how it could be done.
It's the tick() function where you separate the different AI types. (And update each enemies ai).

It adds 10 enemies at random positions in the beginning, and then it updates their logic and draws them.
If player collides with one of them, it dies (Calls the die() function).

class Enemy : public Entity
{
Enemy(int x, int y);
~Enemy();

void die();
virtual void tick();
virtual void draw();
}

void gameLoop()
{
// Init enemies (10)
std::list<Enemy*> enemies;
for(int i = 0; i < 10; i++)
{
Enemy* t_em = new Enemy(rand()%20, rand()%20);
enemies.push_back(t_em);
}

while(true != false)
{
// Loop through all enemies
for(std::list<Enemy*>::iterator it = enemies.begin(); it != enemies.end(); it++)
{
// Logic update for enemy
(*it)->tick();

// Draw enemy
(*it)->draw();
}

/* Do some fancy player stuff*/

// Could be moved (Loop through all enemies)
for(std::list<Enemy*>::iterator it = enemies.begin(); it != enemies.end(); it++)
{
if(isCollosion(player, (*it))
{
(*it)->die();
}
}
}
}

Remember it's not tested nor coded with care.

Share this post


Link to post
Share on other sites
I personally wouldn't use ::iterators for something that simple. Iterators are slow compared to using simple array lookup and there's not really any need for them.

Understood it wasn't coded with care but just thought I'd put my 0.02 in :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Ehrys
I personally wouldn't use ::iterators for something that simple. Iterators are slow compared to using simple array lookup and there's not really any need for them.

Understood it wasn't coded with care but just thought I'd put my 0.02 in :)
Where did you hear this? You might want to run your own benchmark for fun. You'd be surprised which one is faster. From what I've heard the speed between debugging and release mode for the iterators is different, but they are not slower.

The reason I posted though was to encourage you to look into polymorphism while your at it. Then you could spawn boss units that derive from the base class. You'd have a simple virtual update command that could be used. :)

//edit just realized that blueapple's code used inheritance from a base entity class which is the ideal way to handle this.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sirisian
Quote:
Original post by Ehrys
I personally wouldn't use ::iterators for something that simple. Iterators are slow compared to using simple array lookup and there's not really any need for them.

Understood it wasn't coded with care but just thought I'd put my 0.02 in :)

Where did you hear this? You might want to run your own benchmark for fun. You'd be surprised which one is faster. From what I've heard the speed between debugging and release mode for the iterators is different, but they are not slower.


Actually I already did run my own benchmarks with DevPartners Profiler. The reason I found out was that I was creating a game for my final dissertation, however, it only ran at 15 fps. Of course this is terrible ;) Using the profiler to find where all the cycles were going, it all led back to the fact I was using iterators to perform simple lookup loops. Switching to the [] operator wherever I used iterators improved performance by a factor of 10.

I'll get my old program out and take a screen shot of the test results. But I am 100% certain that for this particular job, they are slower.

Share this post


Link to post
Share on other sites
Vectors, I'm just doing some screenshots of the Profiler Benchmarks now. I'll edit this post to include them when I'm finished.
[edit]

Ok now I feel like an idiot. Taking into account what Sirisian said about iterators being slower in debug mode, I ran the profiler in debug mode to see what I got...The exact same results.

Seems the person I was working with had changed the Release Mode config to include _DEBUG. Why he would do this I have no idea.

I apologise and take back everything I said.

Iterators are on par with array lookup. (0.0029 compared to 0.0026)

One word....GAH! >.<
(Oh and sorry :))

[Edited by - Ehrys on May 24, 2007 5:10:25 AM]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this