unit.cpp 3.7 KB

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