C++ Sort enemies by life

I have a problem with my little game which I have programmed, with the help of a c++ book.

So first I have a class called PlayerObject, and a array with all game objects stored in. When the game starts, I loop through all the objects inside the array.

while (true) {
  for (auto i = 0; i < 100; i++) {
    auto object = FuncObjectId(i);
if (PlayerObject.IsEnemy(object) &amp;&amp;
    PlayerObject.Player.Position.Distance(object.Position) &lt; 1000) {
  // enemies found in range
  FuncMoveTo(object)
}

}
}

But what if there are several enemies in the given range? How can i order them ?

sorting by life or distance, for example. So that my player move to the closest target, or to the lowest health target.

Besides, the whole thing has to reset somehow. The player should not stop after killing the target, he should automatically go to the next enemy with the lowest life/closest position.

#c++ #game-engine

4 Likes2.40 GEEK