player.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #ifndef INCLUDEPLAYER_H
  2. #define INCLUDEPLAYER_H
  3. #include "units/unit.h"
  4. #include "racemanager.h"
  5. #include <QObject>
  6. #include <QDebug>
  7. #include <set>
  8. class Race;
  9. template<class Obj, class Identifier>
  10. class ObjectFactory;
  11. class Player {
  12. struct unit_comparator {
  13. bool operator() (const std::shared_ptr<Unit> lhs, const std::shared_ptr<Unit> rhs) const {
  14. if (rhs == nullptr)
  15. return true;
  16. if (lhs->getUnitBaseClassId() != rhs->getUnitBaseClassId())
  17. return lhs->getUnitBaseClassId() < rhs->getUnitBaseClassId();
  18. return lhs->getUnitId() < rhs->getUnitId();
  19. }
  20. };
  21. public:
  22. Player() {
  23. init(1);
  24. }
  25. void init(int id) {
  26. player_name_ = "Игрок " + QString::number(id + 1);
  27. race_ = RaceManager::getInstance().getRace(
  28. RaceManager::getInstance().getAvailableRacesList()[0]
  29. );
  30. units_.clear();
  31. }
  32. QString getPlayerName() {
  33. return player_name_;
  34. }
  35. const std::multiset<std::shared_ptr<Unit>, unit_comparator>& getUnits() {
  36. return units_;
  37. }
  38. std::vector<std::shared_ptr<Unit>> getUnitsAsVector() {
  39. std::vector<std::shared_ptr<Unit>> result;
  40. for (auto unit: units_)
  41. result.push_back(unit);
  42. return result;
  43. }
  44. void addUnit(std::shared_ptr<Unit> a) {
  45. if (units_.size() >= 10)
  46. return;
  47. units_.insert(a);
  48. }
  49. void deleteUnit(std::shared_ptr<Unit> a) {
  50. qDebug() << "deleting unit " << a->getUnitName();
  51. auto unit = units_.find(a);
  52. if (unit != units_.end())
  53. units_.erase(unit);
  54. }
  55. void clearUnits() {
  56. units_.clear();
  57. }
  58. std::shared_ptr<Race> getRace() {
  59. return race_;
  60. }
  61. void setRace(std::shared_ptr<Race> race) {
  62. race_ = race;
  63. }
  64. signals:
  65. public slots:
  66. private:
  67. QString player_name_;
  68. std::multiset<std::shared_ptr<Unit>, unit_comparator> units_;
  69. std::shared_ptr<Race> race_;
  70. };
  71. #endif // INCLUDEPLAYER_H