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.

215 lines
3.5 KiB

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