race.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include "race.h"
  2. #include "units/unit.h"
  3. #include <QFile>
  4. #include <QTextStream>
  5. #include <QString>
  6. #include <QDebug>
  7. #include <cassert>
  8. #include <vector>
  9. #include <algorithm>
  10. Race::Race(QString race_name, QObject *parent) : QObject(parent)
  11. {
  12. race_id_ = race_name;
  13. units_factory_.addClass<Unit, QString>("Warrior");
  14. units_factory_.addClass<Unit, QString>("Mage");
  15. units_factory_.addClass<Unit, QString>("Rider");
  16. units_factory_.addClass<Unit, QString>("Rogue");
  17. units_factory_.addClass<Unit, QString>("Archer");
  18. QFile file(":/assets/units/" + race_name + "/units.txt");
  19. file.open(QIODevice::ReadOnly);
  20. QTextStream in(&file);
  21. in.setCodec("UTF-8");
  22. QString unit_name = in.readLine();
  23. while (!in.atEnd()) {
  24. available_units_list_.push_back(unit_name);
  25. all_units_list_.push_back(createUnit(unit_name));
  26. unit_name = in.readLine();
  27. }
  28. if (unit_name.length() > 2) {
  29. available_units_list_.push_back(unit_name);
  30. all_units_list_.push_back(createUnit(unit_name));
  31. }
  32. QFile file0(":/assets/units/" + race_name + "/racename.txt");
  33. file0.open(QIODevice::ReadOnly);
  34. QTextStream in0(&file0);
  35. in0.setCodec("UTF-8");
  36. race_name_ = in0.readLine();
  37. QFile file1(":/assets/units/" + race_name + "/descr.txt");
  38. file1.open(QIODevice::ReadOnly);
  39. QTextStream in1(&file1);
  40. in1.setCodec("UTF-8");
  41. race_descr_ = in1.readAll();
  42. race_icon_.load(":/assets/units/" + race_name + "/icon.png");
  43. }
  44. QString Race::getRaceId() {
  45. return race_id_;
  46. }
  47. QString Race::getRaceName() {
  48. return race_name_;
  49. }
  50. QImage Race::getRaceIcon() {
  51. return race_icon_;
  52. }
  53. QString Race::getRaceDescr() {
  54. return race_descr_;
  55. }
  56. std::shared_ptr<Unit> Race::createUnit(QString unit_name) {
  57. qDebug() << unit_name;
  58. QFile file(":/assets/units/" + race_id_ + "/" + unit_name + "/baseclass.txt");
  59. file.open(QIODevice::ReadOnly);
  60. QTextStream in(&file);
  61. in.setCodec("UTF-8");
  62. QString class_id = in.readLine();
  63. qDebug() << "Creating unit of base class " << class_id << " and major class" << race_id_ + "|" + unit_name;
  64. return units_factory_.createObject<QString>(class_id, race_id_ + "|" + unit_name);
  65. }
  66. const std::vector<QString>& Race::getAvailableUnitsList() {
  67. return available_units_list_;
  68. }
  69. const std::vector<std::shared_ptr<Unit>> &Race::getAllUnitsList() {
  70. return all_units_list_;
  71. }