unit.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. #include "AbstractFactory.h"
  2. #include "units/unit.h"
  3. #include <iostream>
  4. #include <algorithm>
  5. #include <cassert>
  6. #include <string>
  7. #include <QFile>
  8. #include <QString>
  9. #include <QTextStream>
  10. Unit::Unit(QString parameters) {
  11. QStringList params = parameters.split("|");
  12. assert(params.size() >= 2);
  13. race_id_ = params[0];
  14. unit_id_ = params[1];
  15. QString unit_folder = ":/assets/units/" + race_id_ + "/" + unit_id_ + "/";
  16. loadUnitName(unit_folder);
  17. loadUnitDescr(unit_folder);
  18. loadUnitBaseClass(unit_folder);
  19. loadUnitTraits(unit_folder);
  20. loadUnitIcon(unit_folder);
  21. loadUnitPrevSpecs(unit_folder);
  22. loadUnitUpgradeSpecs(unit_folder);
  23. }
  24. void Unit::loadUnitName(QString unit_folder) {
  25. QFile file(unit_folder + "unitname.txt");
  26. file.open(QIODevice::ReadOnly);
  27. QTextStream in(&file);
  28. unit_name_ = in.readLine();
  29. }
  30. void Unit::loadUnitDescr(QString unit_folder) {
  31. QFile file(unit_folder + "descr.txt");
  32. file.open(QIODevice::ReadOnly);
  33. QTextStream in(&file);
  34. unit_descr_ = in.readAll();
  35. }
  36. void Unit::loadUnitBaseClass(QString unit_folder) {
  37. QFile file(unit_folder + "baseclass.txt");
  38. file.open(QIODevice::ReadOnly);
  39. QTextStream in(&file);
  40. base_class_id_ = in.readLine();
  41. }
  42. void Unit::loadUnitTraits(QString unit_folder) {
  43. // TO BE DONE!!!
  44. health_points_ = rand();
  45. activity_points_ = rand();
  46. attack_cost_ = rand();
  47. attack_range_ = rand();
  48. }
  49. void Unit::loadUnitIcon(QString unit_folder) {
  50. unit_icon_.load(unit_folder + "icon.png");
  51. }
  52. void Unit::loadUnitPrevSpecs(QString unit_folder) {
  53. QFile file(unit_folder + "prevgrades.txt");
  54. file.open(QIODevice::ReadOnly);
  55. QTextStream in(&file);
  56. QString line = in.readLine();
  57. while (!in.atEnd()) {
  58. parent_specs_.push_back(line);
  59. line = in.readLine();
  60. }
  61. parent_specs_.push_back(line);
  62. line = in.readLine();
  63. }
  64. void Unit::loadUnitUpgradeSpecs(QString unit_folder) {
  65. QFile file(unit_folder + "nextgrades.txt");
  66. file.open(QIODevice::ReadOnly);
  67. QTextStream in(&file);
  68. QString line = in.readLine();
  69. while (!in.atEnd()) {
  70. upgrade_specs_.push_back(line);
  71. line = in.readLine();
  72. }
  73. upgrade_specs_.push_back(line);
  74. line = in.readLine();
  75. }
  76. int Unit::getCost(){
  77. return cost_;
  78. }
  79. std::vector<QString> Unit::getParentSpecs(){
  80. return parent_specs_;
  81. }
  82. std::vector<QString> Unit::getUpgradeSpecs(){
  83. return upgrade_specs_;
  84. }
  85. double Unit::getExperience() {
  86. return experience_;
  87. }
  88. double Unit::getLevel() {
  89. return level_;
  90. }
  91. double Unit::getHealthPoints() {
  92. return health_points_;
  93. }
  94. double Unit::getAttackRange() {
  95. return attack_range_;
  96. }
  97. int Unit::getActivityPoints(){
  98. return activity_points_;
  99. }
  100. int Unit::getStartingActivityPoints() {
  101. return starting_activity_points_;
  102. }
  103. Cell* Unit::getLocation() {
  104. return location_;
  105. }
  106. void Unit::setLocation(Cell* to) {
  107. location_ = to;
  108. }
  109. int Unit::getMovementSpeed() {
  110. return movement_speed_;
  111. }
  112. int Unit::getAttackCost(){
  113. return attack_cost_;
  114. }
  115. double Unit::getInitiative() {
  116. return initiative_;
  117. }
  118. double Unit::getDamagePerHit() {
  119. return damage_per_hit_;
  120. }
  121. double Unit::getIntelligence() {
  122. return intelligence_;
  123. }
  124. double Unit::getStrength() {
  125. return strength_;
  126. }
  127. double Unit::getAgility() {
  128. return agility_;
  129. }
  130. int Unit::getAttackPoints(){
  131. return attack_cost_;
  132. }
  133. double Unit::getMagicDefence() {
  134. return magic_defence_;
  135. }
  136. double Unit::getPhysicDefence() {
  137. return physic_defence_;
  138. }
  139. double Unit::getRealX() {
  140. return real_x_;
  141. }
  142. void Unit::setRealX(double x) {
  143. real_x_ = x;
  144. }
  145. double Unit::getRealY() {
  146. return real_y_;
  147. }
  148. void Unit::setRealY(double y) {
  149. real_y_ = y;
  150. }
  151. void Unit::calculateDamagePerHit() {
  152. damage_per_hit_ = 0.5 * std::max(getAgility(), std::max(getStrength(), getIntelligence()));
  153. }
  154. double Unit::reduceIncomingDamage(std::string damageType, int damage) { //returns damage after reducing by defence
  155. assert("Incorrect damage type in call reduceIncomingDamage(), expected" &&
  156. damageType[0] == 'p' || damageType[0] == 'P' || damageType[0] == 'm' || damageType[0] == 'M');
  157. assert("Magic defence of unit is incorrectly high (>40), but must be" && magic_defence_ <= 40);
  158. assert("Physic defence of unit is incorrectly high (>40), but must be" && physic_defence_ <= 40);
  159. if (damageType[0] == 'p' || damageType[0] == 'P') {
  160. return (1 - 2.5 * physic_defence_ / 100) * damage;
  161. }
  162. else if (damageType[0] == 'm' || damageType[0] == 'M') {
  163. return (1 - 2.5 * magic_defence_ / 100) * damage;
  164. }
  165. }
  166. int Unit::lenOfActualPath(Cell* destination) {
  167. return getLocation()->actualPath(destination).size();
  168. }
  169. bool Unit::canMoveForDistance(int distance) {
  170. return (activity_points_ * movement_speed_ >= distance);
  171. }
  172. bool Unit::canMoveToCell(Cell* destination) {
  173. return (destination->isEmpty() && lenOfActualPath(destination) > 0 && canMoveForDistance(lenOfActualPath(destination)));
  174. }
  175. void Unit::moveToCell(Cell* destination) {
  176. if (!canMoveToCell(destination))
  177. return; //here could be a gui-message about failed move (x-mark, for example)
  178. else {
  179. movement_speed_ -= lenOfActualPath(destination);
  180. setLocation(destination);
  181. }
  182. }
  183. QString Unit::getUnitId() const {
  184. return unit_id_;
  185. }
  186. QString Unit::getUnitName() const {
  187. return unit_name_;
  188. }
  189. QString Unit::getUnitDescr() const {
  190. return unit_descr_;
  191. }
  192. QString Unit::getUnitBaseClassId() const {
  193. return base_class_id_;
  194. }
  195. std::vector<QString> Unit::getUnitTraits() const {
  196. return {
  197. QString::number(health_points_),
  198. QString::number(attack_range_),
  199. QString::number(activity_points_),
  200. QString::number(initiative_)
  201. };
  202. }
  203. QImage Unit::getUnitIcon() const {
  204. return unit_icon_;
  205. }