unit.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #pragma once
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cassert>
  5. #include <string>
  6. #include "unit.h"
  7. #include "AbstractFactory.h"
  8. int Unit::getCost(){
  9. return cost_;
  10. }
  11. void Unit::setCost(int value){
  12. cost_ = value;
  13. }
  14. std::string Unit::getParentSpec(){
  15. return parent_spec_;
  16. }
  17. void Unit::setParentSpec(std::string specId){
  18. parent_spec_ = specId;
  19. }
  20. std::vector<std::string> Unit::getUpgradeSpecs(){
  21. return upgrade_specs_;
  22. }
  23. void Unit::setUpgradeSpecs(std::vector<std::string> specs){
  24. upgrade_specs_ = specs;
  25. }
  26. double Unit::getExperience() {
  27. return experience_;
  28. }
  29. void Unit::setExperience(double value) {
  30. experience_ = value;
  31. }
  32. double Unit::getLevel() {
  33. return level_;
  34. };
  35. void Unit::setLevel(double value) {
  36. level_ = value;
  37. }
  38. double Unit::getHealthPoints() {
  39. return health_points_;
  40. };
  41. void Unit::setHealthPoints(double value) {
  42. health_points_ = value;
  43. }
  44. double Unit::getAttackRange() {
  45. return attack_range_;
  46. }
  47. void Unit::setAttackRange(double value) {
  48. attack_range_ = value;
  49. }
  50. int Unit::getActivityPoints(){
  51. return activity_points_;
  52. }
  53. void Unit::setActivityPoints(int value){
  54. activity_points_ = value;
  55. }
  56. Cell* Unit::getLocation() {
  57. return location_;
  58. }
  59. void Unit::setLocation(Cell* to) {
  60. location_ = to;
  61. }
  62. int Unit::getMovementSpeed() {
  63. return movement_speed_;
  64. }
  65. void Unit::setMovementSpeed(int value) {
  66. movement_speed_ = value;
  67. }
  68. int Unit::getAttackCost(){
  69. return attack_cost_;
  70. }
  71. void Unit::setAttackCost(int value){
  72. attack_cost_ = value;
  73. }
  74. double Unit::getInitiative() {
  75. return initiative_;
  76. }
  77. void Unit::setInitiative(double value) {
  78. initiative_ = value;
  79. }
  80. double Unit::getDamagePerHit() {
  81. return damage_per_hit_;
  82. }
  83. void Unit::setDamagePerHit(double value) {
  84. damage_per_hit_ = value;
  85. }
  86. double Unit::getIntelligence() {
  87. return intelligence_;
  88. }
  89. void Unit::setIntelligence(double value) {
  90. intelligence_ = value;
  91. }
  92. double Unit::getStrength() {
  93. return strength_;
  94. }
  95. void Unit::setStrength(double value) {
  96. strength_ = value;
  97. }
  98. double Unit::getAgility() {
  99. return agility_;
  100. }
  101. void Unit::setAgility(double value) {
  102. agility_ = value;
  103. }
  104. int Unit::getAttackPoints(){
  105. return attack_cost_;
  106. }
  107. void Unit::setAttackPoints(int value){
  108. attack_cost_ = value;
  109. }
  110. double Unit::getMagicDefence() {
  111. return magic_defence_;
  112. }
  113. void Unit::setMagicDefence(double value) {
  114. magic_defence_ = value;
  115. }
  116. double Unit::getPhysicDefence() {
  117. return physic_defence_;
  118. }
  119. void Unit::setPhysicDefence(double value) {
  120. physic_defence_ = value;
  121. }
  122. std::string Unit::getRace() {
  123. return race_;
  124. }
  125. void Unit::setRace(std::string new_race) {
  126. race_ = new_race;
  127. }
  128. double Unit::getRealX() {
  129. return real_x_;
  130. }
  131. void Unit::setRealX(double x) {
  132. real_x_ = x;
  133. }
  134. double Unit::getRealY() {
  135. return real_y_;
  136. }
  137. void Unit::setRealY(double y) {
  138. real_y_ = y;
  139. }
  140. void Unit::calculateDamagePerHit() {
  141. damage_per_hit_ = 0.5 * std::max(getAgility(), std::max(getStrength(), getIntelligence()));
  142. }
  143. double Unit::reduceIncomingDamage(std::string damageType, int damage) { //returns damage after reducing by defence
  144. assert("Incorrect damage type in call reduceIncomingDamage(), expected" &&
  145. damageType[0] == 'p' || damageType[0] == 'P' || damageType[0] == 'm' || damageType[0] == 'M');
  146. assert("Magic defence of unit is incorrectly high (>40), but must be" && magic_defence_ <= 40);
  147. assert("Physic defence of unit is incorrectly high (>40), but must be" && physic_defence_ <= 40);
  148. if (damageType[0] == 'p' || damageType[0] == 'P') {
  149. return (1 - 2.5 * physic_defence_ / 100) * damage;
  150. }
  151. else if (damageType[0] == 'm' || damageType[0] == 'M') {
  152. return (1 - 2.5 * magic_defence_ / 100) * damage;
  153. }
  154. }
  155. int Unit::lenOfActualPath(Cell* destination) {
  156. return getLocation()->actualPath(destination).size();
  157. }
  158. bool Unit::canMoveForDistance(int distance) {
  159. return (movement_speed_ >= distance);
  160. }
  161. bool Unit::canMoveToCell(Cell* destination) {
  162. return (destination->isEmpty() && lenOfActualPath(destination) > 0 && canMoveForDistance(lenOfActualPath(destination)));
  163. }
  164. void Unit::moveToCell(Cell* destination) {
  165. if (!canMoveToCell(destination))
  166. return; //here could be a gui-message about failed move (x-mark, for example)
  167. else {
  168. int decreasedValue = getMovementSpeed() - lenOfActualPath(destination);
  169. setMovementSpeed(decreasedValue);
  170. setLocation(destination);
  171. }
  172. }