diff --git a/src/Battling/Flow/ChoiceQueue.cpp b/src/Battling/Flow/ChoiceQueue.cpp index 1f0d465..8f7b333 100644 --- a/src/Battling/Flow/ChoiceQueue.cpp +++ b/src/Battling/Flow/ChoiceQueue.cpp @@ -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; +} diff --git a/src/Battling/Flow/ChoiceQueue.hpp b/src/Battling/Flow/ChoiceQueue.hpp index 78523a9..8ccf44c 100644 --- a/src/Battling/Flow/ChoiceQueue.hpp +++ b/src/Battling/Flow/ChoiceQueue.hpp @@ -21,9 +21,13 @@ namespace CreatureLib::Battling { return b; } + BaseTurnChoice* Peek() { return _queue[_current]; } + [[nodiscard]] bool HasNext() const { return _current < _queue.size(); } std::vector& GetInnerQueue() { return _queue; } + + bool MoveCreatureChoiceNext(Creature* creature); }; } diff --git a/tests/BattleTests/ChoiceQueueTests.cpp b/tests/BattleTests/ChoiceQueueTests.cpp new file mode 100644 index 0000000..0968d6d --- /dev/null +++ b/tests/BattleTests/ChoiceQueueTests.cpp @@ -0,0 +1,48 @@ +#ifdef TESTS_BUILD + +#include "../../extern/catch.hpp" +#include "../../src/Battling/Flow/ChoiceQueue.hpp" +#include "../../src/Battling/Models/BattleSide.hpp" +#include "../../src/Battling/Models/CreateCreature.hpp" +#include "../../src/Battling/TurnChoices/PassTurnChoice.hpp" +#include "../TestLibrary/TestLibrary.hpp" + +using namespace CreatureLib::Battling; + +TEST_CASE("Move creature choice up next.", "[Battling]") { + auto lib = TestLibrary::Get(); + auto c1 = CreateCreature(lib, "testSpecies1", 1).Create(); + auto c2 = CreateCreature(lib, "testSpecies1", 1).Create(); + auto c3 = CreateCreature(lib, "testSpecies1", 1).Create(); + auto c4 = CreateCreature(lib, "testSpecies1", 1).Create(); + + std::vector choices = { + new PassTurnChoice(c1), + new PassTurnChoice(c2), + new PassTurnChoice(c3), + new PassTurnChoice(c4), + }; + + auto choiceQueue = ChoiceQueue(choices); + CHECK(choiceQueue.MoveCreatureChoiceNext(c4)); + auto currentChoices = choiceQueue.GetInnerQueue(); + CHECK(currentChoices[0]->GetUser() == c4); + CHECK(currentChoices[1]->GetUser() == c1); + CHECK(currentChoices[2]->GetUser() == c2); + CHECK(currentChoices[3]->GetUser() == c3); + + CHECK_FALSE(choiceQueue.MoveCreatureChoiceNext(c4)); + delete choiceQueue.Dequeue(); + delete choiceQueue.Dequeue(); + CHECK_FALSE(choiceQueue.MoveCreatureChoiceNext(c1)); + + delete choiceQueue.Dequeue(); + delete choiceQueue.Dequeue(); + + delete c1; + delete c2; + delete c3; + delete c4; +} + +#endif \ No newline at end of file