unit.cpp 5.1 KB

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