Added Helper function to ChoiceQueue to move a specific creature's choice up next.

This commit is contained in:
2020-02-22 14:16:38 +01:00
parent d4fd7282f5
commit 442d6cd5b1
3 changed files with 79 additions and 0 deletions

View File

@@ -1 +1,28 @@
#include "ChoiceQueue.hpp"
bool CreatureLib::Battling::ChoiceQueue::MoveCreatureChoiceNext(CreatureLib::Battling::Creature* creature) {
// Find which index the creature choice is at.
size_t choiceIndex = -1;
for (size_t index = _current; index < _queue.size(); index++) {
if (_queue.at(index)->GetUser() == creature) {
// If the creature choice is already next up, return.
if (index == _current)
return false;
choiceIndex = index;
}
}
// If the creature has no choice anymore, return.
if (choiceIndex == -1)
return false;
// Save the pointer to the creature choice.
auto creatureChoice = _queue[choiceIndex];
// Starting at the position before the current creature choice, and iterating backwards up till the current choice.
for (size_t i = choiceIndex - 1; i >= _current && i != SIZE_MAX; i--) {
// Place everything in one spot later.
_queue[i + 1] = _queue[i];
}
// Set the creature choice up next.
_queue[_current] = creatureChoice;
return true;
}

View File

@@ -21,9 +21,13 @@ namespace CreatureLib::Battling {
return b;
}
BaseTurnChoice* Peek() { return _queue[_current]; }
[[nodiscard]] bool HasNext() const { return _current < _queue.size(); }
std::vector<BaseTurnChoice*>& GetInnerQueue() { return _queue; }
bool MoveCreatureChoiceNext(Creature* creature);
};
}