Jump to content
  • Advertisement
Sign in to follow this  
Kitasia

Enemy Array

This topic is 5088 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

Could someone explain how I would go about making an enemy array. For example if I have 5 enemies walking forward and one bumps into the wall, the rest of them keep going. I realize I could just check each enemy but that's a WHOLE LOT OF WORK! I'm sure there's a much simpler way. Could someone explain from top to bottom. Note: "Using SDL"

Share this post


Link to post
Share on other sites
Advertisement
I've reviewed that and it doesn't help much. I don't want to keep adding the enemies off screen. I want them to have there own coordinates like Super Mario for example.

Share this post


Link to post
Share on other sites
"that's a WHOLE LOT OF WORK" - Not really.

Reading 'masters of Doom', it surprised me to learn that John Carmack would often try the most obvious solution first, and then realize a better way to do it later.

Also, the neat thing about software, once it works like you want, you dont have to deal with it anymore. In other words, the 'whoole lotta work' only needs done once by you.

And finally, I use an array of objects in a game im working on, and it isnt an extreme amount of work to get each to play along nicely.

There are more elegant solutions, and ironically, using a computer to find them is the wrong way (IMHO) to go about it. Try a piece of paper and a pencil instead.

Hope this helps :)

-Jason

Share this post


Link to post
Share on other sites
im not 100% sure im reading from the same page as you, but i'll say something based on what i think your talking about.

couldnt you make an 'enemy' structure, with the coordinates etc within it, and make an array of the enemy structures? Checking each enemy wouldnt be a whole lot of work since all you would need is a simple for loop.

for(i=0; i<MAX_ENEMIES; i++;)
{
if(enemyarray.right <= wall.left)
{
enemyarray.walk = false;
}
}

or something similar using your code and variables. I may have got completely the wrong idea about what your asking, if so, just let me know.

Share this post


Link to post
Share on other sites
Quote:
Original post by AntiGuy
For example if I have 5 enemies walking forward and one bumps into the wall, the rest of them keep going. I realize I could just check each enemy but that's a WHOLE LOT OF WORK! I'm sure there's a much simpler way. Could someone explain from top to bottom.


that's pretty much how you do it:


void Engine::Update( float dt )
{
for (int i = 0; i < numEnemies; ++i)
{
myEnemies.Update(dt);
}
}



update each enemy every frame. update includes: locomotion updates, collision detection, AI, etc. some things, like AI can be timesliced so that it's not updated every single frame, but your physics and collision detection should be updated every frame. you'd be surprised at the amount of stuff a computer can do each frame...

-me

Share this post


Link to post
Share on other sites
Quote:
Original post by AntiGuy
For example if I have 5 enemies walking forward and one bumps into the wall, the rest of them keep going. I realize I could just check each enemy but that's a WHOLE LOT OF WORK! I'm sure there's a much simpler way. Could someone explain from top to bottom.


nope, that's pretty much how you do it:


void Engine::Update( float dt )
{
for (int i = 0; i < numEnemies; ++i)
{
myEnemies.Update(dt);
}
}




update each enemy every frame. update includes: locomotion updates, collision detection, AI, etc. some things, like AI can be timesliced so that it's not updated every single frame, but your physics and collision detection should be updated every frame. you'd be surprised at the amount of stuff a computer can do each frame...

-me

Share this post


Link to post
Share on other sites
Ok, one way would be to make a struct or class (personally I use classes) and assuming the class is called enemy do somthing like:


class cEnemy
{
int x;
int y;
int velocity;
};


cEnemy ObjectName[10];



And then you have 10 enemies that are completely seperate from each other and can have individual locations and speed.

If this isn't what you are talking about let me know, I'll try again ;)

Share this post


Link to post
Share on other sites
just make sure you try and encapsulate the code as good as possible. what i mean is, let all your object's only care about themselves, and not about outside world. at the very least make the data variables private. for example, have your Enemy class have functions like this:

Update()
Render()
Do_Collision()

etc. i would personally try to keep Update() the only interface the world has to the object, IE, call Render() and Do_Collision() from inside of Update(). then you just call Update() each frame like the way Palidine showed.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!