player.h 1.9 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 Unit* lhs, const 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<Unit*, unit_comparator>& getUnits() {
  36. return units_;
  37. }
  38. std::vector<Unit*> getUnitsAsVector() {
  39. std::vector<Unit*> result;
  40. for (auto unit: units_)
  41. result.push_back(unit);
  42. return result;
  43. }
  44. void addUnit(Unit *a) {
  45. if (units_.size() >= 10)
  46. return;
  47. units_.insert(a);
  48. }
  49. void deleteUnit(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. Race* getRace() {
  59. return race_;
  60. }
  61. void setRace(Race* race) {
  62. race_ = race;
  63. }
  64. signals:
  65. public slots:
  66. private:
  67. QString player_name_;
  68. std::multiset<Unit*, unit_comparator> units_;
  69. Race* race_;
  70. };
  71. #endif // INCLUDEPLAYER_H