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.

227 lines
4.3 KiB

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