ItemSkill.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. #pragma once
  2. #include <JSON.h>
  3. #include <Reader.h>
  4. #include <ReferenceCounter.h>
  5. #include "TypeRegistry.h"
  6. class ItemType;
  7. class ItemSkill;
  8. class Block;
  9. class Entity;
  10. class Dimension;
  11. class Item;
  12. class ItemSkillLevelUpRule : public virtual Framework::ReferenceCounter
  13. {
  14. public:
  15. virtual void applyOn(ItemSkill* zSkill) = 0;
  16. };
  17. class ItemSkill : public virtual Framework::ReferenceCounter
  18. {
  19. private:
  20. float xp;
  21. float maxXP;
  22. float level;
  23. int itemTypeId;
  24. public:
  25. ItemSkill();
  26. virtual void load(Framework::StreamReader* zReader);
  27. virtual void save(Framework::StreamWriter* zWriter);
  28. virtual bool use(Entity* zActor, Item* zUsedItem, Block* zTarget) = 0;
  29. virtual bool use(Entity* zActor, Item* zUsedItem, Entity* zTarget) = 0;
  30. virtual bool interact(Entity* zActor, Item* zUsedItem, Block* zTarget);
  31. virtual bool interact(Entity* zActor, Item* zUsedItem, Entity* zTarget);
  32. void setXp(float xp);
  33. float getXp() const;
  34. void setMaxXp(float maxXp);
  35. float getMaxXp() const;
  36. void setLevel(float level);
  37. float getLevel() const;
  38. void setItemTypeId(int itemTypeId);
  39. int getItemTypeId() const;
  40. };
  41. template<typename S> class ItemSkillFactoryBase
  42. : public SubTypeFactory<ItemSkill, S>
  43. {
  44. public:
  45. ItemSkillFactoryBase()
  46. : SubTypeFactory<ItemSkill, S>(){};
  47. virtual void fromJson(
  48. S* zResult, Framework::JSON::JSONObject* zJson) const override
  49. {
  50. ItemSkill* zSkill = dynamic_cast<ItemSkill*>(zResult);
  51. zSkill->setMaxXp(
  52. (float)zJson->zValue("maxXp")->asNumber()->getNumber());
  53. zSkill->setLevel(
  54. (float)zJson->zValue("level")->asNumber()->getNumber());
  55. }
  56. virtual void toJson(
  57. S* zObject, Framework::JSON::JSONObject* zResult) const override
  58. {
  59. ItemSkill* zSkill = dynamic_cast<ItemSkill*>(zObject);
  60. zResult->addValue(
  61. "maxXp", new Framework::JSON::JSONNumber(zSkill->getMaxXp()));
  62. zResult->addValue(
  63. "level", new Framework::JSON::JSONNumber(zSkill->getLevel()));
  64. }
  65. virtual JSONObjectValidationBuilder* addToValidator(
  66. JSONObjectValidationBuilder* builder) const override
  67. {
  68. return builder->withRequiredNumber("maxXp")
  69. ->withDefault(10.f)
  70. ->finishNumber()
  71. ->withRequiredNumber("level")
  72. ->withDefault(1.f)
  73. ->finishNumber();
  74. }
  75. };