unit.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <cassert>
  4. #include <string>
  5. #include "AbstractFactory.h"
  6. #include "units/unit.h"
  7. Unit::Unit(QString unit_name) {
  8. }
  9. int Unit::getCost(){
  10. return cost_;
  11. }
  12. std::string Unit::getParentSpec(){
  13. return parent_spec_;
  14. }
  15. std::vector<std::string> Unit::getUpgradeSpecs(){
  16. return upgrade_specs_;
  17. }
  18. double Unit::getExperience() {
  19. return experience_;
  20. }
  21. double Unit::getLevel() {
  22. return level_;
  23. }
  24. double Unit::getHealthPoints() {
  25. return health_points_;
  26. }
  27. double Unit::getAttackRange() {
  28. return attack_range_;
  29. }
  30. int Unit::getActivityPoints(){
  31. return activity_points_;
  32. }
  33. Cell* Unit::getLocation() {
  34. return location_;
  35. }
  36. void Unit::setLocation(Cell* to) {
  37. location_ = to;
  38. }
  39. int Unit::getMovementSpeed() {
  40. return movement_speed_;
  41. }
  42. int Unit::getAttackCost(){
  43. return attack_cost_;
  44. }
  45. double Unit::getInitiative() {
  46. return initiative_;
  47. }
  48. double Unit::getDamagePerHit() {
  49. return damage_per_hit_;
  50. }
  51. double Unit::getIntelligence() {
  52. return intelligence_;
  53. }
  54. double Unit::getStrength() {
  55. return strength_;
  56. }
  57. double Unit::getAgility() {
  58. return agility_;
  59. }
  60. int Unit::getAttackPoints(){
  61. return attack_cost_;
  62. }
  63. double Unit::getMagicDefence() {
  64. return magic_defence_;
  65. }
  66. double Unit::getPhysicDefence() {
  67. return physic_defence_;
  68. }
  69. double Unit::getRealX() {
  70. return real_x_;
  71. }
  72. void Unit::setRealX(double x) {
  73. real_x_ = x;
  74. }
  75. double Unit::getRealY() {
  76. return real_y_;
  77. }
  78. void Unit::setRealY(double y) {
  79. real_y_ = y;
  80. }
  81. void Unit::calculateDamagePerHit() {
  82. damage_per_hit_ = 0.5 * std::max(getAgility(), std::max(getStrength(), getIntelligence()));
  83. }
  84. double Unit::reduceIncomingDamage(std::string damageType, int damage) { //returns damage after reducing by defence
  85. assert("Incorrect damage type in call reduceIncomingDamage(), expected" &&
  86. damageType[0] == 'p' || damageType[0] == 'P' || damageType[0] == 'm' || damageType[0] == 'M');
  87. assert("Magic defence of unit is incorrectly high (>40), but must be" && magic_defence_ <= 40);
  88. assert("Physic defence of unit is incorrectly high (>40), but must be" && physic_defence_ <= 40);
  89. if (damageType[0] == 'p' || damageType[0] == 'P') {
  90. return (1 - 2.5 * physic_defence_ / 100) * damage;
  91. }
  92. else if (damageType[0] == 'm' || damageType[0] == 'M') {
  93. return (1 - 2.5 * magic_defence_ / 100) * damage;
  94. }
  95. }
  96. int Unit::lenOfActualPath(Cell* destination) {
  97. return getLocation()->actualPath(destination).size();
  98. }
  99. bool Unit::canMoveForDistance(int distance) {
  100. return (movement_speed_ >= distance);
  101. }
  102. bool Unit::canMoveToCell(Cell* destination) {
  103. return (destination->isEmpty() && lenOfActualPath(destination) > 0 && canMoveForDistance(lenOfActualPath(destination)));
  104. }
  105. void Unit::moveToCell(Cell* destination) {
  106. if (!canMoveToCell(destination))
  107. return; //here could be a gui-message about failed move (x-mark, for example)
  108. else {
  109. movement_speed_ -= lenOfActualPath(destination);
  110. setLocation(destination);
  111. }
  112. }