Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

*How To Program Multiple Instances Of Enemies?

This topic is 5219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How would you store multiple isntance of an enemy, and have them all work properly?(They all attack the character, move, and die when hit) Would you store the enemies in an array? A linked list? Also, I know how to program a homing attack(like the main character firing a missile toward an enemy), but I''d have no clue how to make that work with multiple enemies. Can anyone help?

Share this post


Link to post
Share on other sites
Advertisement
in what language are you coding? normally you do some form of linked list of enemies so that you can have variable amounts of them on screen/in game. if you're working with C++ you can use the stl vector or list classes as your container. basically though you need something like a struct or a class to hold the information of your enemies:

in c++

struct Enemy
{
public:
//the x/y position of your enemy

//it's probably sensible for the position to be it's own struct

int posX, posY;

int hitPoints;

//whatever else you have

}

//then in your program something like:

std::vector<Enemy*> enemyList;

for (int i = 0; i < NUMBER_OF_ENEMIES; ++i)
enemyList.push_back( new Enemy() );

mainGameLoop();

for (unsigned int i = 0; i < enemyList.size(); ++i)
{
if (enemyList[i])
delete enemyList[i];
}
enemyList.clear();


the above assumes that you know how to use pointers and classes, which you may not. if you don't know classes or structs you absolutely need to learn them. try googling for "C++ tutorial" and read the relevant things. if you don't know pointers you don't necessarily need to know them. assuming you have a very small applicaion you can just do something like:

Enemy enemyList[NUMBER_OF_ENEMIES];

As for your homing weapon thing, you do whatever you normally do for the homing weapon, you just have to give that weapon a target enemy when it is fired. store either a pointer to the enemy (can be dangerous if the target dies before your weapon hits it) or an ID for that enemy that you can look up in your enemyList. then just do your update calculations using the target enemy's position. something like this might work for your missile object:


struct Missile
{
int posX, posY;

int targetID;
}


-me

[edited by - Palidine on February 5, 2004 11:37:27 PM]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!