unit.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "abstractfactory.h"
  2. #include "units/unit.h"
  3. #include "cell.h"
  4. #include "effects/effect.h"
  5. #include <iostream>
  6. #include <algorithm>
  7. #include <cassert>
  8. #include <string>
  9. #include <QFile>
  10. #include <QString>
  11. #include <QTextStream>
  12. #include <QSettings>
  13. #include <QDebug>
  14. Unit::Unit(QString parameters) {
  15. QStringList params = parameters.split("|");
  16. assert(params.size() >= 2);
  17. race_id_ = params[0];
  18. unit_id_ = params[1];
  19. QString unit_folder = ":/assets/units/" + race_id_ + "/" + unit_id_ + "/";
  20. loadUnitName(unit_folder);
  21. loadUnitDescr(unit_folder);
  22. loadUnitBaseClass(unit_folder);
  23. loadUnitTraits(unit_folder);
  24. loadUnitIcon(unit_folder);
  25. loadUnitPrevSpecs(unit_folder);
  26. loadUnitUpgradeSpecs(unit_folder);
  27. }
  28. void Unit::loadUnitName(QString unit_folder) {
  29. QFile file(unit_folder + "unitname.txt");
  30. file.open(QIODevice::ReadOnly);
  31. QTextStream in(&file);
  32. in.setCodec("UTF-8");
  33. unit_name_ = in.readLine();
  34. }
  35. void Unit::loadUnitDescr(QString unit_folder) {
  36. QFile file(unit_folder + "descr.txt");
  37. file.open(QIODevice::ReadOnly);
  38. QTextStream in(&file);
  39. in.setCodec("UTF-8");
  40. unit_descr_ = in.readAll();
  41. }
  42. void Unit::loadUnitBaseClass(QString unit_folder) {
  43. QFile file(unit_folder + "baseclass.txt");
  44. file.open(QIODevice::ReadOnly);
  45. QTextStream in(&file);
  46. in.setCodec("UTF-8");
  47. base_class_id_ = in.readLine();
  48. }
  49. void Unit::loadUnitTraits(QString unit_folder) {
  50. QSettings traits(unit_folder + "traits.ini", QSettings::IniFormat);
  51. //traits.beginGroup("CharactersTraits");
  52. level_ = traits.value("lvl", -1).toInt();
  53. cost_ = traits.value("cost", -1).toInt();
  54. starting_activity_points_ = traits.value("starting_ap", -1).toInt();
  55. strength_ = traits.value("strength", -1).toInt();
  56. agility_ = traits.value("agility", -1).toInt();
  57. initiative_ = traits.value("initiative", -1).toInt();
  58. magic_defence_ = traits.value("magic_def", -1).toInt();
  59. physic_defence_ = traits.value("physic_def", -1).toInt();
  60. //traits.endGroup();
  61. qDebug() << unit_folder + "traits.ini" << " " << level_;
  62. }
  63. void Unit::loadUnitIcon(QString unit_folder) {
  64. unit_icon_.load(unit_folder + "icon.png");
  65. }
  66. void Unit::loadUnitPrevSpecs(QString unit_folder) {
  67. QFile file(unit_folder + "prevgrades.txt");
  68. file.open(QIODevice::ReadOnly);
  69. QTextStream in(&file);
  70. in.setCodec("UTF-8");
  71. QString line = in.readLine();
  72. while (!in.atEnd()) {
  73. parent_specs_.push_back(line);
  74. line = in.readLine();
  75. }
  76. parent_specs_.push_back(line);
  77. line = in.readLine();
  78. }
  79. void Unit::loadUnitUpgradeSpecs(QString unit_folder) {
  80. QFile file(unit_folder + "nextgrades.txt");
  81. file.open(QIODevice::ReadOnly);
  82. QTextStream in(&file);
  83. in.setCodec("UTF-8");
  84. QString line = in.readLine();
  85. while (!in.atEnd()) {
  86. upgrade_specs_.push_back(line);
  87. line = in.readLine();
  88. }
  89. upgrade_specs_.push_back(line);
  90. line = in.readLine();
  91. }
  92. int Unit::getCost(){
  93. return cost_;
  94. }
  95. std::vector<QString> Unit::getParentSpecs(){
  96. return parent_specs_;
  97. }
  98. std::vector<QString> Unit::getUpgradeSpecs(){
  99. return upgrade_specs_;
  100. }
  101. double Unit::getExperience() {
  102. return experience_;
  103. }
  104. int Unit::getLevel() {
  105. return level_;
  106. }
  107. int Unit::getHealthPoints() {
  108. return health_points_;
  109. }
  110. int Unit::getActivityPoints(){
  111. return activity_points_;
  112. }
  113. int Unit::getStartingActivityPoints() {
  114. return starting_activity_points_;
  115. }
  116. Cell* Unit::getLocation() {
  117. return location_;
  118. }
  119. void Unit::setLocation(Cell* to) {
  120. location_ = to;
  121. }
  122. int Unit::getInitiative() {
  123. return initiative_;
  124. }
  125. int Unit::getIntelligence() {
  126. return intelligence_;
  127. }
  128. int Unit::getStrength() {
  129. return strength_;
  130. }
  131. int Unit::getAgility() {
  132. return agility_;
  133. }
  134. int Unit::getMagicDefence() {
  135. return magic_defence_;
  136. }
  137. int Unit::getPhysicDefence() {
  138. return physic_defence_;
  139. }
  140. double Unit::getRealX() {
  141. return real_x_;
  142. }
  143. void Unit::setRealX(double x) {
  144. real_x_ = x;
  145. }
  146. double Unit::getRealY() {
  147. return real_y_;
  148. }
  149. void Unit::setRealY(double y) {
  150. real_y_ = y;
  151. }
  152. bool Unit::isCharacter(){
  153. return true;
  154. }
  155. QString Unit::getUnitId() const {
  156. return unit_id_;
  157. }
  158. QString Unit::getUnitName() const {
  159. return unit_name_;
  160. }
  161. QString Unit::getUnitDescr() const {
  162. return unit_descr_;
  163. }
  164. QString Unit::getUnitBaseClassId() const {
  165. return base_class_id_;
  166. }
  167. QImage Unit::getUnitIcon() const {
  168. return unit_icon_;
  169. }
  170. void Unit::operateEffectList(){
  171. for(std::vector<Effect*>::iterator it = beginIteratorEffectsList();
  172. it != endIteratorEffectsList(); ++it){
  173. (*it)->OperateOnUnit(this);
  174. }
  175. }
  176. void Unit::add(Effect* effect){
  177. if(effect == nullptr)
  178. throw new std::string("Try to add undefined effect to unit");
  179. effects_.push_back(effect);
  180. }
  181. void Unit::remove(std::vector<Effect*>::iterator it){
  182. if(beginIteratorEffectsList() <= it && it < endIteratorEffectsList()){
  183. effects_.erase(it);
  184. }
  185. }
  186. void Unit::remove(Effect* effect){
  187. for(std::vector<Effect*>::iterator it = beginIteratorEffectsList();
  188. it != endIteratorEffectsList(); ++it){
  189. if((*it) == effect){
  190. remove(it);
  191. return;
  192. }
  193. }
  194. }
  195. std::vector<Effect*>::iterator Unit::beginIteratorEffectsList(){
  196. return effects_.begin();
  197. }
  198. std::vector<Effect*>::iterator Unit::endIteratorEffectsList(){
  199. return effects_.end();
  200. }