race.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. QString unit_name = in.readLine();
  24. while (!in.atEnd()) {
  25. available_units_list_.push_back(unit_name);
  26. all_units_list_.push_back(createUnit(unit_name));
  27. unit_name = in.readLine();
  28. }
  29. if (unit_name.length() > 2) {
  30. available_units_list_.push_back(unit_name);
  31. all_units_list_.push_back(createUnit(unit_name));
  32. }
  33. QFile file0(":/assets/units/" + race_name + "/racename.txt");
  34. file0.open(QIODevice::ReadOnly);
  35. QTextStream in0(&file0);
  36. race_name_ = in0.readLine();
  37. QFile file1(":/assets/units/" + race_name + "/descr.txt");
  38. file1.open(QIODevice::ReadOnly);
  39. QTextStream in1(&file1);
  40. race_descr_ = in1.readAll();
  41. race_icon_.load(":/assets/units/" + race_name + "/icon.png");
  42. }
  43. QString Race::getRaceId() {
  44. return race_id_;
  45. }
  46. QString Race::getRaceName() {
  47. return race_name_;
  48. }
  49. QImage Race::getRaceIcon() {
  50. return race_icon_;
  51. }
  52. QString Race::getRaceDescr() {
  53. return race_descr_;
  54. }
  55. Unit* Race::createUnit(QString unit_name) {
  56. qDebug() << unit_name;
  57. QFile file(":/assets/units/" + race_id_ + "/" + unit_name + "/baseclass.txt");
  58. file.open(QIODevice::ReadOnly);
  59. QTextStream in(&file);
  60. QString class_id = in.readLine();
  61. qDebug() << "Creating unit of base class " << class_id << " and major class" << race_id_ + "|" + unit_name;
  62. return units_factory_.createObject<QString>(class_id, race_id_ + "|" + unit_name);
  63. }
  64. const std::vector<QString>& Race::getAvailableUnitsList() {
  65. return available_units_list_;
  66. }
  67. const std::vector<Unit*> &Race::getAllUnitsList() {
  68. return all_units_list_;
  69. }