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.

87 lines
1.6 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. #ifndef _2EASY4ME_PLAYER_H_
  2. #define _2EASY4ME_PLAYER_H_
  3. #include "character.hpp"
  4. #include "string.h"
  5. class Player : public Character
  6. {
  7. protected:
  8. int xp;
  9. int level;
  10. public:
  11. Player(char *name)
  12. : Character(name)
  13. {
  14. this->xp = 0;
  15. this->level = 1;
  16. }
  17. Player(char *name, int hp, int att, int def, int level, int xp)
  18. : Character(name, hp, att, def)
  19. {
  20. this->xp = xp;
  21. this->level = level;
  22. }
  23. int getLevel()
  24. {
  25. return level;
  26. }
  27. bool checkLevelup()
  28. {
  29. return xp > (level * level);
  30. }
  31. bool levelUp(int stat)
  32. {
  33. switch (stat)
  34. {
  35. case 1:
  36. hp += 10;
  37. break;
  38. case 2:
  39. att += 2;
  40. break;
  41. case 3:
  42. def += 1;
  43. break;
  44. }
  45. xp -= level * level;
  46. }
  47. int generateCode(char *dst)
  48. {
  49. return sprintf(dst,
  50. "PlayerName:%15s;PlayerHp:%d;PlayerAtt:%d;PlayerDef:%d;PlayerLvl:%d;PlayerExp:%d;",
  51. this->name, this->hp, this->att, this->def, this->level, this->xp);
  52. }
  53. Player *loadCode(char *dst)
  54. {
  55. char name[16];
  56. int hp;
  57. int att;
  58. int def;
  59. int lvl;
  60. int exp;
  61. int result = sscanf(dst,
  62. "PlayerName:%15s;PlayerAtt:%d;PlayerDef:%d;PlayerLvl:%d;PlayerExp:%d;",
  63. name, &hp, &att, &def, &lvl, &exp);
  64. if (result == 5)
  65. {
  66. return new Player(name, hp, att, def, lvl, exp);
  67. }
  68. else
  69. {
  70. return nullptr;
  71. }
  72. }
  73. };
  74. #endif