ItemSkill.h 2.6 KB

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