unitsqueue.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include <hotseatgame/unitsqueue.h>
  2. #include <vector>
  3. #include <string>
  4. #include <algorithm>
  5. #include <units/unit.h>
  6. void UnitsQueue::add(Unit* unit){
  7. queue_.push_back(unit);
  8. recalcQueue();
  9. }
  10. void UnitsQueue::remove(viterator iter){
  11. if (beginIterator() <= iter && iter < endIterator()){
  12. queue_.erase(iter);
  13. return;
  14. }
  15. throw std::string("Iterator is not in UnitsQueue");
  16. }
  17. void UnitsQueue::remove(Unit* unit){
  18. for (viterator it = beginIterator(); it != endIterator();++it){
  19. if( (*it) == unit){
  20. remove(it);
  21. return;
  22. }
  23. }
  24. throw std::string("Unit* is not in UnitsQueue");
  25. }
  26. UnitsQueue::viterator UnitsQueue::beginIterator() {
  27. return queue_.begin();
  28. }
  29. UnitsQueue::viterator UnitsQueue::endIterator(){
  30. return queue_.end();
  31. }
  32. Unit* UnitsQueue::front(){
  33. return *beginIterator();
  34. }
  35. void UnitsQueue::pop(){
  36. return remove(beginIterator());
  37. }
  38. void UnitsQueue::recalcQueue(){
  39. std::stable_sort(beginIterator(), endIterator(),[](Unit* a, Unit* b)->bool{
  40. auto a_init = a->getInitiative();
  41. auto b_init = b->getInitiative();
  42. return a_init > b_init;
  43. });
  44. }