You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
2.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
  1. #include "Character.hpp"
  2. #include <cstring>
  3. Character::Character(char *n, int l, int e, int h, int m, int a, int d)
  4. {
  5. this->level = l;
  6. this->experience = e;
  7. this->maxHP = h;
  8. this->curHP = maxHP;
  9. this->maxMP = m;
  10. this->curMP = maxMP;
  11. this->atk = a;
  12. this->def = d;
  13. strcpy(this->name, n);
  14. this->name[15] = 0;
  15. }
  16. Character::Character(const char *n, int l, int e, int h, int m, int a, int d)
  17. {
  18. this->level = l;
  19. this->experience = e;
  20. this->maxHP = h;
  21. this->curHP = maxHP;
  22. this->maxMP = m;
  23. this->curMP = maxMP;
  24. this->atk = a;
  25. this->def = d;
  26. strcpy(this->name, n);
  27. }
  28. char *Character::getName()
  29. {
  30. return this->name;
  31. }
  32. int Character::getLevel()
  33. {
  34. return this->level;
  35. }
  36. int Character::getExperience()
  37. {
  38. return this->experience;
  39. }
  40. int Character::getMaxHP()
  41. {
  42. return this->maxHP;
  43. }
  44. int Character::getCurHP()
  45. {
  46. return this->curHP;
  47. }
  48. int Character::getMaxMP()
  49. {
  50. return this->maxMP;
  51. }
  52. int Character::getCurMP()
  53. {
  54. return this->curMP;
  55. }
  56. int Character::getAtk()
  57. {
  58. return this->atk;
  59. }
  60. int Character::getDef()
  61. {
  62. return this->def;
  63. }
  64. void Character::levelUp()
  65. {
  66. if ((this->level <= maxLevel) && this->readytoLevelUp())
  67. {
  68. this->experience -= this->toNextLevel();
  69. this->level++;
  70. this->maxHP += 10;
  71. this->maxMP += 5;
  72. this->curHP = this->maxHP;
  73. this->curMP = this->maxMP;
  74. }
  75. }
  76. void Character::restoreHP(int n)
  77. {
  78. this->curHP += n;
  79. if (this->curHP > this->maxHP)
  80. {
  81. this->curHP = this->maxHP;
  82. }
  83. }
  84. void Character::restoreMP(int n)
  85. {
  86. this->curMP += n;
  87. if (this->curMP > this->maxMP)
  88. {
  89. this->curMP = this->maxMP;
  90. }
  91. }
  92. void Character::reduceHP(int n)
  93. {
  94. this->curHP -= n;
  95. }
  96. void Character::reduceMP(int n)
  97. {
  98. this->curMP -= n;
  99. }
  100. bool Character::isAlive()
  101. {
  102. return this->curHP > 0;
  103. }
  104. int Character::toNextLevel()
  105. {
  106. return (this->level + 1) * (this->level + 1);
  107. }
  108. bool Character::readytoLevelUp()
  109. {
  110. return (this->toNextLevel() <= this->experience);
  111. }
  112. void Character::increaseExperience(int n)
  113. {
  114. this->experience += n;
  115. if (this->experience > maxEperience)
  116. {
  117. this->experience = maxEperience;
  118. }
  119. while (this->readytoLevelUp())
  120. {
  121. this->levelUp();
  122. }
  123. }
  124. bool Character::canCastSpell(Spell *s)
  125. {
  126. return this->curMP >= s->getCost();
  127. }
  128. void Character::castSpell(Spell *s, Character *t)
  129. {
  130. this->reduceMP(s->getCost());
  131. t->reduceHP(s->getAmount());
  132. }
  133. void Character::rest()
  134. {
  135. this->curHP = this->maxHP;
  136. this->curMP = this->maxMP;
  137. }
  138. void Character::kill()
  139. {
  140. this->curHP = 0;
  141. }
  142. void Character::revive()
  143. {
  144. if (this->curHP <= 0)
  145. {
  146. this->curHP = 1;
  147. }
  148. }