unit.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. void Unit::calculateDamagePerHit() {
  99. damage_per_hit_ = 0.5 * std::max(getAgility(), std::max(getStrength(), getIntelligence()));
  100. }
  101. double Unit::reduceIncomingDamage(std::string damageType, int damage) { //returns damage after reducing by defence
  102. assert("Incorrect damage type in call reduceIncomingDamage(), expected" &&
  103. damageType[0] == 'p' || damageType[0] == 'P' || damageType[0] == 'm' || damageType[0] == 'M');
  104. assert("Magic defence of unit is incorrectly high (>40), but must be" && magic_defence_ <= 40);
  105. assert("Physic defence of unit is incorrectly high (>40), but must be" && physic_defence_ <= 40);
  106. if (damageType[0] == 'p' || damageType[0] == 'P') {
  107. return (1 - 2.5 * physic_defence_ / 100) * damage;
  108. }
  109. else if (damageType[0] == 'm' || damageType[0] == 'M') {
  110. return (1 - 2.5 * magic_defence_ / 100) * damage;
  111. }
  112. }
  113. int Unit::lenOfActualPath(Cell* destination) {
  114. return getLocation()->actualPath(destination).size();
  115. }
  116. bool Unit::canMoveForDistance(int distance) {
  117. return (movement_points_ >= distance);
  118. }
  119. bool Unit::canMoveToCell(Cell* destination) {
  120. return (destination->isEmpty() && canMoveForDistance(lenOfActualPath(destination)));
  121. }
  122. void Unit::moveToCell(Cell* destination) {
  123. if (!canMoveToCell)
  124. return; //here could be a gui-message about failed move (x-mark, for example)
  125. else {
  126. int decreasedValue = getMovementPoints() - lenOfActualPath(destination);
  127. setMovementPoints(decreasedValue);
  128. setLocation(destination);
  129. }
  130. }
  131. /*bool canAttack(int distance) {
  132. }*/
  133. //TODO: real_x_, real_y_, ptr to player