Best Way for Creating Multiple Enemies?

Started by
5 comments, last by Alberth 3 years, 1 month ago

Hi. I m working on a game project and still trying to learn. I need some informations about creating enemies or npc's.

For instance I created an enemy class.

There will be different types enemies or npc's and they will have their own hit points, stats etc.

They will do different actions in a time for example one of them will stay and another one will move (randomly) …

When one enemy or npc dies i have to delete its sprite or sometimes will add new enemies… actions like this.

İmportant note: I need to detect collision between enemies from same class. if a zombie touchs another zombie i need to detect this action and i tried with arrays and vectors but i cant do that.

My question is:

What is the best way to do that? What kind a container i need to use like array or vector or another" type?

And if you know tutorial links about this matter please share, i would appreciate it. Thanks for every help and advice.

Advertisement

Something like this

class NPC {
   int hit_points;
   int speed;
   Sprite sprite;
};

std::vector<NPC> staying;
std::vector<NPC> moving;

void move(std::vector<NPC> &npcs)
{ 
         for (auto &npc : npcs) {
                // do move logic
         } 
}

move(&moving);

To kill an NPC, remove it from your vector with vector.erase()

This tutorial covers many different patterns used in game programming. https://gameprogrammingpatterns.com/contents.html

aganm said:

Something like this

class NPC {
   int hit_points;
   int speed;
   Sprite sprite;
};

std::vector<NPC> staying;
std::vector<NPC> moving;

void move(std::vector<NPC> &npcs)
{ 
         for (auto &npc : npcs) {
                // do move logic
         } 
}

move(&moving);

To kill an NPC, remove it from your vector with vector.erase()

This tutorial covers many different patterns used in game programming. https://gameprogrammingpatterns.com/contents.html

Thanks for examples. If i use vectors is that possible to detect collisions between objects of same class?

yusufabi said:
Thanks for examples. If i use vectors is that possible to detect collisions between objects of same class?

The type of container only affects how you can store and retrieve its elements, it doesn't affect doing operations on the elements themselves.

yusufabi said:
i tried with arrays and vectors but i cant do that.

I would be helpful as how you ended up with this conclusion, so we could explain where you made the wrong move, but as an example, I would guess something like this:

void detect_collisions(std::vector<NPC> &npcs) {
    // Loop over all pairs of npcs, avoiding checking pair (j, i) if (i,j) is also checked.
    for (int i = 0; i < npcs.size() - 1; i++) { // Loop over all npcs, except the last one.
        for (int j = i + 1; j < npcs.size(); j++) { // Loop over all npcs after npcs[i].
            if (has_collision(npcs[i], npcs[j]) { ... }
        }
    }
}

Now this attempts collision between every pair of elements in the vector. If you want only collision detection between “the same class of NCP", then either, make a vector for each class (std::vector<NPC> stayers, std::vector<NPC> movers, etc), or add a way to distinguish NPC on class, eg

struct NPC {
    NpcClass npc_class;
    ... // Other properties.
};
;
enum class NpcClass {
   Mover,
   Stayer,
   Jumper,
   ...
};

and then you can check is the class is the same with a line like

if (npcs[i].npc_class == npcs[j].npc_class) { /* same class, do something. */ }

in the above loop you likely want the opposite check and skip over the pair instead.

Note that pairwise checking is a simple approach, but it's not very efficient. It's fine for a limited number of enemies though.

Alberth said:

yusufabi said:
Thanks for examples. If i use vectors is that possible to detect collisions between objects of same class?

The type of container only affects how you can store and retrieve its elements, it doesn't affect doing operations on the elements themselves.

yusufabi said:
i tried with arrays and vectors but i cant do that.

I would be helpful as how you ended up with this conclusion, so we could explain where you made the wrong move, but as an example, I would guess something like this:

void detect_collisions(std::vector<NPC> &npcs) {
    // Loop over all pairs of npcs, avoiding checking pair (j, i) if (i,j) is also checked.
    for (int i = 0; i < npcs.size() - 1; i++) { // Loop over all npcs, except the last one.
        for (int j = i + 1; j < npcs.size(); j++) { // Loop over all npcs after npcs[i].
            if (has_collision(npcs[i], npcs[j]) { ... }
        }
    }
}

Now this attempts collision between every pair of elements in the vector. If you want only collision detection between “the same class of NCP", then either, make a vector for each class (std::vector<NPC> stayers, std::vector<NPC> movers, etc), or add a way to distinguish NPC on class, eg

struct NPC {
    NpcClass npc_class;
    ... // Other properties.
};
;
enum class NpcClass {
   Mover,
   Stayer,
   Jumper,
   ...
};

and then you can check is the class is the same with a line like

if (npcs[i].npc_class == npcs[j].npc_class) { /* same class, do something. */ }

in the above loop you likely want the opposite check and skip over the pair instead.

Note that pairwise checking is a simple approach, but it's not very efficient. It's fine for a limited number of enemies though.

Thanks, it works on my project ? I read your another answer at my another post. Thanks for your advices. I suppose my problem is that i started game developing before i learn good programming. I will try to keep learning i hope. Thank you again for spend your time and aswered my questions. Best regards.

Learning to program takes around a decade or so, so it's understandable why you make games as well :D

This topic is closed to new replies.

Advertisement