unit.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. level_ = traits.value("lvl", -1).toInt();
  52. cost_ = traits.value("cost", -1).toInt();
  53. starting_activity_points_ = traits.value("starting_ap", -1).toInt();
  54. strength_ = traits.value("strength", -1).toInt();
  55. agility_ = traits.value("agility", -1).toInt();
  56. initiative_ = traits.value("initiative", -1).toInt();
  57. magic_defence_ = traits.value("magic_def", -1).toInt();
  58. physic_defence_ = traits.value("physic_def", -1).toInt();
  59. }
  60. void Unit::loadUnitIcon(QString unit_folder) {
  61. unit_icon_.load(unit_folder + "icon.png");
  62. }
  63. void Unit::loadUnitPrevSpecs(QString unit_folder) {
  64. QFile file(unit_folder + "prevgrades.txt");
  65. file.open(QIODevice::ReadOnly);
  66. QTextStream in(&file);
  67. in.setCodec("UTF-8");
  68. QString line = in.readLine();
  69. while (!in.atEnd()) {
  70. parent_specs_.push_back(line);
  71. line = in.readLine();
  72. }
  73. parent_specs_.push_back(line);
  74. line = in.readLine();
  75. }
  76. void Unit::loadUnitUpgradeSpecs(QString unit_folder) {
  77. QFile file(unit_folder + "nextgrades.txt");
  78. file.open(QIODevice::ReadOnly);
  79. QTextStream in(&file);
  80. in.setCodec("UTF-8");
  81. QString line = in.readLine();
  82. while (!in.atEnd()) {
  83. upgrade_specs_.push_back(line);
  84. line = in.readLine();
  85. }
  86. upgrade_specs_.push_back(line);
  87. line = in.readLine();
  88. }
  89. int Unit::getCost(){
  90. return cost_;
  91. }
  92. std::vector<QString> Unit::getParentSpecs(){
  93. return parent_specs_;
  94. }
  95. std::vector<QString> Unit::getUpgradeSpecs(){
  96. return upgrade_specs_;
  97. }
  98. double Unit::getExperience() {
  99. return experience_;
  100. }
  101. int Unit::getLevel() {
  102. return level_;
  103. }
  104. int Unit::getHealthPoints() {
  105. return health_points_;
  106. }
  107. int Unit::getActivityPoints(){
  108. return activity_points_;
  109. }
  110. int Unit::getStartingActivityPoints() {
  111. return starting_activity_points_;
  112. }
  113. Cell* Unit::getLocation() {
  114. return location_;
  115. }
  116. void Unit::setLocation(Cell* to) {
  117. location_ = to;
  118. }
  119. int Unit::getMaxHealthPoints(){
  120. return max_health_points_;
  121. }
  122. void Unit::setHealthPoints(int value){
  123. health_points_ = value;
  124. }
  125. int Unit::getInitiative() {
  126. return initiative_;
  127. }
  128. int Unit::getIntelligence() {
  129. return intelligence_;
  130. }
  131. int Unit::getStrength() {
  132. return strength_;
  133. }
  134. int Unit::getAgility() {
  135. return agility_;
  136. }
  137. int Unit::getMagicDefence() {
  138. return magic_defence_;
  139. }
  140. int Unit::getPhysicDefence() {
  141. return physic_defence_;
  142. }
  143. double Unit::getRealX() {
  144. return real_x_;
  145. }
  146. void Unit::setRealX(double x) {
  147. real_x_ = x;
  148. }
  149. double Unit::getRealY() {
  150. return real_y_;
  151. }
  152. void Unit::setRealY(double y) {
  153. real_y_ = y;
  154. }
  155. bool Unit::isCharacter(){
  156. return true;
  157. }
  158. QString Unit::getUnitId() const {
  159. return unit_id_;
  160. }
  161. QString Unit::getUnitName() const {
  162. return unit_name_;
  163. }
  164. QString Unit::getUnitDescr() const {
  165. return unit_descr_;
  166. }
  167. QString Unit::getUnitBaseClassId() const {
  168. return base_class_id_;
  169. }
  170. QImage Unit::getUnitIcon() const {
  171. return unit_icon_;
  172. }
  173. void Unit::operateEffectList(){
  174. for(std::vector<Effect*>::iterator it = beginIteratorEffectsList();
  175. it != endIteratorEffectsList(); ++it){
  176. (*it)->OperateOnUnit(this);
  177. }
  178. }
  179. void Unit::add(Effect* effect){
  180. if(effect == nullptr)
  181. throw new std::string("Try to add undefined effect to unit");
  182. effects_.push_back(effect);
  183. }
  184. void Unit::remove(std::vector<Effect*>::iterator it){
  185. if(beginIteratorEffectsList() <= it && it < endIteratorEffectsList()){
  186. effects_.erase(it);
  187. }
  188. }
  189. void Unit::remove(Effect* effect){
  190. for(std::vector<Effect*>::iterator it = beginIteratorEffectsList();
  191. it != endIteratorEffectsList(); ++it){
  192. if((*it) == effect){
  193. remove(it);
  194. return;
  195. }
  196. }
  197. }
  198. std::vector<Effect*>::iterator Unit::beginIteratorEffectsList(){
  199. return effects_.begin();
  200. }
  201. std::vector<Effect*>::iterator Unit::endIteratorEffectsList(){
  202. return effects_.end();
  203. }