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.

82 lines
1.6 KiB

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. bool checkLevelup()
  24. {
  25. return xp > (level * level);
  26. }
  27. bool levelUp(int stat)
  28. {
  29. switch (stat)
  30. {
  31. case 1:
  32. hp += 10;
  33. break;
  34. case 2:
  35. att += 2;
  36. break;
  37. case 3:
  38. def += 1;
  39. break;
  40. }
  41. xp -= level * level;
  42. }
  43. int generateCode(char *dst)
  44. {
  45. return sprintf(dst,
  46. "PlayerName:%15s;PlayerHp:%d;PlayerAtt:%d;PlayerDef:%d;PlayerLvl:%d;PlayerExp:%d;",
  47. this->name, this->hp, this->att, this->def, this->level, this->xp);
  48. }
  49. Player *loadCode(char *dst)
  50. {
  51. char name[16];
  52. int hp;
  53. int att;
  54. int def;
  55. int lvl;
  56. int exp;
  57. int result = sscanf(dst,
  58. "PlayerName:%15s;PlayerAtt:%d;PlayerDef:%d;PlayerLvl:%d;PlayerExp:%d;",
  59. name, &hp, &att, &def, &lvl, &exp);
  60. if (result == 5)
  61. {
  62. return new Player(name, hp, att, def, lvl, exp);
  63. }
  64. else
  65. {
  66. return nullptr;
  67. }
  68. }
  69. };
  70. #endif