unit.cpp 4.5 KB

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