race.cpp 2.4 KB

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