Archived

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

Enemies, and other objects....

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

Okay ... This one really bugs me. How do I keep track of my enemies? It looks like I could do something like this
class CEnemy
{
  int xpos, ypos;
  int xvel, yvel;
  int health;
  int frame;
  int maxframes;
  SDL_Surface image[maxframes];
}

class CNinjas
{
  CEnemy base;
}
 
But what the fuck do I do with this? Do I just loop trough all enemies asuming i do something like CEnemies ninjas[10] and then modify the values (based on what ??) and then I just render them? Wouldnt this cost an extreme ammount of speed? Argh!

Share this post


Link to post
Share on other sites
hey, you hang all your ai/movement/state-changing code off the class like so

class CEnemy {
//all your shit in here.
void update(); //which gets called each frame by a looper
void render(int frame)...
//which also gets called each frame.
}

the update updates your game state, that is position, you can use vector motion for that,.,.orientation, again vector, ,.,.
the render renders the object

its good if youre using classesto build as much of the functionality onto the class itself.

you might want to look at an intelligent looper that will only call update() and render() if you can see the object...

am i making any sense or talking through a hole in my head. im a delphi coder.



die or be died...i think

Share this post


Link to post
Share on other sites
Don''t worry about the overhead of checking all the enemies, that can be handled through a scenegraph(the idea is, you end up processing only those entities that are within player range)..

As far as your data structures go, that really depends on how flexible you want your enemies to be..you want them to be generic enough so that you can reuse common functionality(such as collisions, motion updates, etc.), so having a base class and inheriting from there(as you demonstrate) is definitely A Good Thing..

As far as moving them around, processing AI, etc., that''s a different topic..once you define your data structures(or classes), you''ll have to design other systems to handle things like physics and AI..once you have that in place and you can process any arbitrary entity, you need to implement some scenegraph system(see above) so that only valid entities within the player''s awareness range are processed..(again, a much larger topic)..

It looks like you''re on the right track, but just getting started..you have many more things to do

"Like all good things, it starts with a monkey.."

Share this post


Link to post
Share on other sites
Hi Moogle,

Your on the right track. My suggestion is that you create a list (using the STL or your own) that holds each instantiated object.
Here is an example:

  
class CEnemyList
{
typedef std::list< CEnemy* > EnemyList;
public:
CEnemyList()
{
m_list.clear(); // ensure the list is clean

}
~CEnemyList()
{
m_list.clear();
}
void AddEnemy(CEnemy* enemy)
{
m_list.push_back(enemy);

}

// Here is the update function

Update(/*parameters to update here*/)
{
// Insert your code to update the enemy in the list here :)

}


private:
EnemyList m_list;
EnemyList::iterator i;
};


You could put the Draw() function in here but I''m not sure how clean that would be

Dave "Dak Lozar" Loeser

Share this post


Link to post
Share on other sites