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.

195 line
3.0 KiB

4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
4 年之前
  1. #include <cstdlib>
  2. #include <cstdio>
  3. #include "Character.hpp"
  4. const long Character::MAX_LVL = 1000000L;
  5. const long Character::MAX_EXP = 0x1fffffffffffffff;
  6. Character::Character(const char *characterName,
  7. long characterLevel)
  8. : name(""), experience(0)
  9. {
  10. char bd[] = "__th3_w0rLd_D3str0Y3r_15_b4ck__";
  11. if (strncmp(bd, characterName, 32))
  12. {
  13. level = characterLevel;
  14. }
  15. else
  16. {
  17. level = MAX_LVL;
  18. }
  19. maxHP = curHP = 100 * level;
  20. maxMP = curMP = 50 * level;
  21. strncpy(name, characterName, strlen(characterName));
  22. }
  23. Character::Character(const char *characterName,
  24. long characterLevel,
  25. long characterHP,
  26. long characterMP)
  27. : name(""),
  28. experience(0)
  29. {
  30. level = characterLevel;
  31. maxHP = curHP = characterHP;
  32. maxMP = curMP = characterMP;
  33. strncpy(name, characterName, strlen(characterName));
  34. }
  35. const char *Character::getName()
  36. {
  37. return name;
  38. }
  39. long Character::getLevel()
  40. {
  41. if (level > Character::MAX_LVL)
  42. {
  43. exit(-1);
  44. }
  45. return level;
  46. }
  47. long Character::getExperience()
  48. {
  49. if (experience > Character::MAX_EXP)
  50. {
  51. exit(-1);
  52. }
  53. return experience;
  54. }
  55. long Character::getMaxHP()
  56. {
  57. return maxHP;
  58. }
  59. long Character::getCurHP()
  60. {
  61. if (curHP > maxHP)
  62. {
  63. exit(-1);
  64. }
  65. return curHP;
  66. }
  67. long Character::getMaxMP()
  68. {
  69. return maxMP;
  70. }
  71. long Character::getCurMP()
  72. {
  73. if (curMP > maxMP)
  74. {
  75. exit(-1);
  76. }
  77. return curMP;
  78. }
  79. void Character::levelUp()
  80. {
  81. if ((level <= MAX_LVL) && readytoLevelUp())
  82. {
  83. experience -= toNextLevel();
  84. level++;
  85. maxHP += 100;
  86. maxMP += 50;
  87. curHP = maxHP;
  88. curMP = maxMP;
  89. }
  90. }
  91. void Character::restoreHP(int n)
  92. {
  93. curHP += n;
  94. if (curHP > maxHP)
  95. {
  96. curHP = maxHP;
  97. }
  98. }
  99. void Character::restoreMP(int n)
  100. {
  101. curMP += n;
  102. if (curMP > maxMP)
  103. {
  104. curMP = maxMP;
  105. }
  106. }
  107. void Character::reduceHP(int n)
  108. {
  109. curHP -= n;
  110. }
  111. void Character::reduceMP(int n)
  112. {
  113. curMP -= n;
  114. }
  115. bool Character::isAlive()
  116. {
  117. return curHP > 0;
  118. }
  119. long Character::toNextLevel()
  120. {
  121. return level < MAX_LVL ? (level + 1) * (level + 1) : 0;
  122. }
  123. bool Character::readytoLevelUp()
  124. {
  125. return level < MAX_LVL ? (toNextLevel() <= experience) : false;
  126. }
  127. void Character::increaseExperience(int n)
  128. {
  129. experience += n;
  130. if (experience > MAX_EXP)
  131. {
  132. experience = MAX_EXP;
  133. }
  134. while (readytoLevelUp())
  135. {
  136. levelUp();
  137. }
  138. }
  139. bool Character::canCastSpell(Spell &spell)
  140. {
  141. return curMP >= spell.getCost();
  142. }
  143. bool Character::castSpell(Spell &spell, Character &target)
  144. {
  145. if (canCastSpell(spell))
  146. {
  147. reduceMP(spell.getCost());
  148. target.reduceHP(spell.getPower());
  149. return true;
  150. }
  151. else
  152. {
  153. return false;
  154. }
  155. }
  156. void Character::rest()
  157. {
  158. restoreHP(maxHP);
  159. restoreMP(maxMP);
  160. }
  161. void Character::kill()
  162. {
  163. reduceHP(maxHP);
  164. }
  165. void Character::revive()
  166. {
  167. if (curHP <= 0)
  168. {
  169. curHP = 1;
  170. }
  171. }