race.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "race.h"
  2. #include "units/unit.h"
  3. #include "units/warrior.h"
  4. #include "units/mage.h"
  5. #include <QFile>
  6. #include <QTextStream>
  7. #include <QString>
  8. #include <cassert>
  9. #include <vector>
  10. #include <algorithm>
  11. Race::Race(QString race_name, QObject *parent) : QObject(parent)
  12. {
  13. race_id_ = race_name;
  14. units_factory_.addClass<Warrior, QString>("Warrior");
  15. units_factory_.addClass<Warrior, QString>("Mage");
  16. QFile file(":/assets/units/" + race_name + "/units.txt");
  17. QTextStream in(&file);
  18. QString unit_name = in.readLine();
  19. while (!in.atEnd()) {
  20. all_units_list_.push_back(createUnit(unit_name));
  21. available_units_list_.push_back(unit_name);
  22. unit_name = in.readLine();
  23. }
  24. QFile file0(":/assets/units/" + race_name + "/racename.txt");
  25. QTextStream in0(&file);
  26. race_name_ = in0.readLine();
  27. QFile file1(":/assets/units/" + race_name + "/descr.txt");
  28. QTextStream in1(&file);
  29. race_descr_ = in1.readAll();
  30. race_icon_.load(":/assets/units/" + race_name + "/descr.txt");
  31. }
  32. QString Race::getRaceId() {
  33. return race_id_;
  34. }
  35. QString Race::getRaceName() {
  36. return race_name_;
  37. }
  38. QImage Race::getRaceIcon() {
  39. return race_icon_;
  40. }
  41. QString Race::getRaceDescr() {
  42. return race_descr_;
  43. }
  44. Unit* Race::createUnit(QString unit_name) {
  45. assert(std::count(available_units_list_.begin(), available_units_list_.end(), unit_name) != 0);
  46. QFile file(":/asserts/units/" + race_id_ + "/" + unit_name + "/baseclass.txt");
  47. QTextStream in(&file);
  48. QString class_id = in.readLine();
  49. return units_factory_.createObject<QString>(class_id, race_id_ + "|" + unit_name);
  50. }
  51. const std::vector<QString>& Race::getAvailableUnitsList() {
  52. return available_units_list_;
  53. }
  54. const std::vector<Unit*> &Race::getAllUnitsList() {
  55. return all_units_list_;
  56. }