Programming multiple enemies?
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?
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.
Then when you need to draw/update/etc, you just loop through the collection of enemies and update them as required.
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.
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.
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! :}
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! :}
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.
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).
Remember it's not tested nor coded with care.
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(); } } }}
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 :)
Understood it wasn't coded with care but just thought I'd put my 0.02 in :)
Quote:Original post by EhrysWhere 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.
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 :)
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.
Quote:Original post by SirisianQuote: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.
List traversal is slower then arrays but vectors should be on par with them. For your dissertation, were you using lists or vectors?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement