unit.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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::getAttackRange() {
  27. return attack_range_;
  28. }
  29. void Unit::setAttackRange(double value) {
  30. attack_range_ = value;
  31. }
  32. int Unit::getActivityPoints(){
  33. return activity_points_;
  34. }
  35. void Unit::setActivityPoints(int value){
  36. activity_points_ = value;
  37. }
  38. Cell* Unit::getLocation() {
  39. return location_;
  40. }
  41. void Unit::setLocation(Cell* to) {
  42. location_ = to;
  43. }
  44. int Unit::getMovementSpeed() {
  45. return movement_speed_;
  46. }
  47. void Unit::setMovementSpeed(int value) {
  48. movement_speed_ = value;
  49. }
  50. int Unit::getAttackSpeed(){
  51. return attack_speed_;
  52. }
  53. void Unit::setAttackSpeed(int value){
  54. attack_speed_ = 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. int Unit::getAttackPoints(){
  87. return attack_speed_;
  88. }
  89. void Unit::setAttackPoints(int value){
  90. attack_speed_ = value;
  91. }
  92. double Unit::getMagicDefence() {
  93. return magic_defence_;
  94. }
  95. void Unit::setMagicDefence(double value) {
  96. magic_defence_ = value;
  97. }
  98. double Unit::getPhysicDefence() {
  99. return physic_defence_;
  100. }
  101. void Unit::setPhysicDefence(double value) {
  102. physic_defence_ = value;
  103. }
  104. std::string Unit::getRace() {
  105. return race_;
  106. }
  107. void Unit::setRace(std::string new_race) {
  108. race_ = new_race;
  109. }
  110. double Unit::getRealX() {
  111. return real_x_;
  112. }
  113. void Unit::setRealX(double x) {
  114. real_x_ = x;
  115. }
  116. double Unit::getRealY() {
  117. return real_y_;
  118. }
  119. void Unit::setRealY(double y) {
  120. real_y_ = y;
  121. }
  122. void Unit::calculateDamagePerHit() {
  123. damage_per_hit_ = 0.5 * std::max(getAgility(), std::max(getStrength(), getIntelligence()));
  124. }
  125. double Unit::reduceIncomingDamage(std::string damageType, int damage) { //returns damage after reducing by defence
  126. assert("Incorrect damage type in call reduceIncomingDamage(), expected" &&
  127. damageType[0] == 'p' || damageType[0] == 'P' || damageType[0] == 'm' || damageType[0] == 'M');
  128. assert("Magic defence of unit is incorrectly high (>40), but must be" && magic_defence_ <= 40);
  129. assert("Physic defence of unit is incorrectly high (>40), but must be" && physic_defence_ <= 40);
  130. if (damageType[0] == 'p' || damageType[0] == 'P') {
  131. return (1 - 2.5 * physic_defence_ / 100) * damage;
  132. }
  133. else if (damageType[0] == 'm' || damageType[0] == 'M') {
  134. return (1 - 2.5 * magic_defence_ / 100) * damage;
  135. }
  136. }
  137. int Unit::lenOfActualPath(Cell* destination) {
  138. return getLocation()->actualPath(destination).size();
  139. }
  140. bool Unit::canMoveForDistance(int distance) {
  141. return (movement_speed_ >= distance);
  142. }
  143. bool Unit::canMoveToCell(Cell* destination) {
  144. return (destination->isEmpty() && lenOfActualPath(destination) > 0 && canMoveForDistance(lenOfActualPath(destination)));
  145. }
  146. void Unit::moveToCell(Cell* destination) {
  147. if (!canMoveToCell(destination))
  148. return; //here could be a gui-message about failed move (x-mark, for example)
  149. else {
  150. int decreasedValue = getMovementSpeed() - lenOfActualPath(destination);
  151. setMovementSpeed(decreasedValue);
  152. setLocation(destination);
  153. }
  154. }
  155. int main() {
  156. std::cout << "Hello, world!\n";
  157. }
  158. bool MeleeUnit::canAttackForDistance(int distance) {
  159. // if
  160. }