unit.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cassert>
  4. #include <string>
  5. #include "unit.h"
  6. #include "AbstractFactory.h"
  7. int Unit::getCost(){
  8. return cost_;
  9. }
  10. void Unit::setCost(int value){
  11. cost_ = value;
  12. }
  13. std::string Unit::getParentSpec(){
  14. return parent_spec_;
  15. }
  16. void Unit::setParentSpec(std::string specId){
  17. parent_spec_ = specId;
  18. }
  19. std::vector<std::string> Unit::getUpgradeSpecs(){
  20. return upgrade_specs_;
  21. }
  22. void Unit::setUpgradeSpecs(std::vector<std::string> specs){
  23. upgrade_specs_ = specs;
  24. }
  25. double Unit::getExperience() {
  26. return experience_;
  27. }
  28. void Unit::setExperience(double value) {
  29. experience_ = value;
  30. }
  31. double Unit::getLevel() {
  32. return level_;
  33. };
  34. void Unit::setLevel(double value) {
  35. level_ = value;
  36. }
  37. double Unit::getHealthPoints() {
  38. return health_points_;
  39. };
  40. void Unit::setHealthPoints(double value) {
  41. health_points_ = value;
  42. }
  43. double Unit::getAttackRange() {
  44. return attack_range_;
  45. }
  46. void Unit::setAttackRange(double value) {
  47. attack_range_ = value;
  48. }
  49. int Unit::getActivityPoints(){
  50. return activity_points_;
  51. }
  52. void Unit::setActivityPoints(int value){
  53. activity_points_ = value;
  54. }
  55. Cell* Unit::getLocation() {
  56. return location_;
  57. }
  58. void Unit::setLocation(Cell* to) {
  59. location_ = to;
  60. }
  61. int Unit::getMovementSpeed() {
  62. return movement_speed_;
  63. }
  64. void Unit::setMovementSpeed(int value) {
  65. movement_speed_ = value;
  66. }
  67. int Unit::getAttackCost(){
  68. return attack_cost_;
  69. }
  70. void Unit::setAttackCost(int value){
  71. attack_cost_ = value;
  72. }
  73. double Unit::getInitiative() {
  74. return initiative_;
  75. }
  76. void Unit::setInitiative(double value) {
  77. initiative_ = value;
  78. }
  79. double Unit::getDamagePerHit() {
  80. return damage_per_hit_;
  81. }
  82. void Unit::setDamagePerHit(double value) {
  83. damage_per_hit_ = value;
  84. }
  85. double Unit::getIntelligence() {
  86. return intelligence_;
  87. }
  88. void Unit::setIntelligence(double value) {
  89. intelligence_ = value;
  90. }
  91. double Unit::getStrength() {
  92. return strength_;
  93. }
  94. void Unit::setStrength(double value) {
  95. strength_ = value;
  96. }
  97. double Unit::getAgility() {
  98. return agility_;
  99. }
  100. void Unit::setAgility(double value) {
  101. agility_ = value;
  102. }
  103. int Unit::getAttackPoints(){
  104. return attack_cost_;
  105. }
  106. void Unit::setAttackPoints(int value){
  107. attack_cost_ = value;
  108. }
  109. double Unit::getMagicDefence() {
  110. return magic_defence_;
  111. }
  112. void Unit::setMagicDefence(double value) {
  113. magic_defence_ = value;
  114. }
  115. double Unit::getPhysicDefence() {
  116. return physic_defence_;
  117. }
  118. void Unit::setPhysicDefence(double value) {
  119. physic_defence_ = value;
  120. }
  121. std::string Unit::getRace() {
  122. return race_;
  123. }
  124. void Unit::setRace(std::string new_race) {
  125. race_ = new_race;
  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. int decreasedValue = getMovementSpeed() - lenOfActualPath(destination);
  168. setMovementSpeed(decreasedValue);
  169. setLocation(destination);
  170. }
  171. }