ItemType.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #pragma once
  2. #include <JSON.h>
  3. #include <Text.h>
  4. #include <Trie.h>
  5. #include <Writer.h>
  6. #include "BlockType.h"
  7. #include "Effect.h"
  8. #include "Game.h"
  9. #include "ModelInfo.h"
  10. class Item;
  11. class ItemStack;
  12. class ItemSkill;
  13. class ItemSkillLevelUpRule;
  14. class ItemTypeEnum
  15. {
  16. public:
  17. static const int PLAYER_HAND = 0;
  18. };
  19. class ItemType : public virtual Framework::ReferenceCounter
  20. {
  21. protected:
  22. int id;
  23. Framework::Text name;
  24. Framework::Text tooltipUIML;
  25. ModelInfo* model;
  26. int maxStackSize;
  27. Framework::RCArray<Framework::Text> groups;
  28. public:
  29. ItemType();
  30. ~ItemType();
  31. protected:
  32. virtual void loadSuperItem(
  33. Item* zItem, Framework::StreamReader* zReader) const;
  34. virtual void saveSuperItem(
  35. const Item* zItem, Framework::StreamWriter* zWriter) const;
  36. public:
  37. virtual bool initialize(Game* zGame);
  38. virtual const ItemType* zBrokenItemType() const;
  39. virtual Item* createItem() const = 0;
  40. virtual ItemStack* createItemStack(int size) const;
  41. virtual ItemSkill* createDefaultItemSkill() const;
  42. virtual void levelUpItemSkill(ItemSkill* zSkill) const;
  43. virtual Item* cloneItem(const Item* zItem) const;
  44. virtual Item* breakItem(const Item* zItem) const;
  45. virtual Item* loadItem(Framework::StreamReader* zReader) const;
  46. virtual void saveItem(
  47. const Item* zItem, Framework::StreamWriter* zWriter) const;
  48. virtual void setItemAttribute(Item* zItem,
  49. Framework::Text name,
  50. Framework::JSON::JSONValue* zValue) const;
  51. virtual void addItemAttributes(
  52. Item* zItem, Framework::JSON::JSONObject* zItemObjet) const;
  53. void setTypeId(int id);
  54. int getId() const;
  55. void setName(Framework::Text name);
  56. const Framework::Text& getName() const;
  57. void setTooltipUIML(Framework::Text tooltipUIML);
  58. const Framework::Text& getTooltipUIML() const;
  59. void setModel(ModelInfo* model);
  60. ModelInfo* zModel() const;
  61. void setMaxStackSize(int maxStackSize);
  62. int getMaxStackSize() const;
  63. void addGroup(Framework::Text group);
  64. const Framework::RCArray<Framework::Text>& getGroups() const;
  65. static Item* createBasicItem(int id,
  66. const char* name,
  67. float hp,
  68. float maxHp,
  69. float durability,
  70. float maxDurability,
  71. bool eatable,
  72. bool placeable,
  73. bool equippable,
  74. bool solid,
  75. bool usable);
  76. static int getTypeId(const char* name);
  77. static const ItemType* zByName(const char* name);
  78. };
  79. template<typename S> class ItemTypeFactoryBase
  80. : public SubTypeFactory<ItemType, S>
  81. {
  82. public:
  83. ItemTypeFactoryBase()
  84. : SubTypeFactory<ItemType, S>()
  85. {}
  86. virtual void fromJson(
  87. S* zResult, Framework::JSON::JSONObject* zJson) const override
  88. {
  89. ItemType* zType = dynamic_cast<ItemType*>(zResult);
  90. zType->setModel(Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  91. zJson->zValue("model")->asObject()));
  92. zType->setName(zJson->zValue("name")->asString()->getString());
  93. if (zJson->hasValue("tooltipUIML"))
  94. {
  95. zType->setTooltipUIML(
  96. zJson->zValue("tooltipUIML")->asString()->getString());
  97. }
  98. zType->setMaxStackSize(
  99. (int)zJson->zValue("maxStack")->asNumber()->getNumber());
  100. for (Framework::JSON::JSONValue* value :
  101. *zJson->zValue("groupNames")->asArray())
  102. {
  103. zType->addGroup(value->asString()->getString());
  104. }
  105. }
  106. virtual void toJson(
  107. S* zObject, Framework::JSON::JSONObject* zResult) const override
  108. {
  109. ItemType* zType = dynamic_cast<ItemType*>(zResult);
  110. zResult->addValue("model",
  111. Game::INSTANCE->zTypeRegistry()->toJson<ModelInfo>(
  112. zType->zModel()));
  113. zResult->addValue(
  114. "name", new Framework::JSON::JSONString(zType->getName()));
  115. zResult->addValue("tooltipUIML",
  116. new Framework::JSON::JSONString(zType->getTooltipUIML()));
  117. zResult->addValue("maxStack",
  118. new Framework::JSON::JSONNumber(zType->getMaxStackSize()));
  119. Framework::JSON::JSONArray* groupNames
  120. = new Framework::JSON::JSONArray();
  121. for (Framework::Text* groupName : zType->getGroups())
  122. {
  123. groupNames->addValue(new Framework::JSON::JSONString(*groupName));
  124. }
  125. zResult->addValue("groupNames", groupNames);
  126. }
  127. virtual JSONObjectValidationBuilder* addToValidator(
  128. JSONObjectValidationBuilder* builder) const override
  129. {
  130. return builder
  131. ->withRequiredAttribute("model",
  132. Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
  133. ->withRequiredString("name")
  134. ->finishString()
  135. ->withRequiredString("tooltipUIML")
  136. ->whichIsOptional()
  137. ->finishString()
  138. ->withRequiredNumber("maxStack")
  139. ->withDefault(50)
  140. ->finishNumber()
  141. ->withRequiredArray("groupNames")
  142. ->withDefault(new Framework::JSON::JSONArray())
  143. ->addAcceptedStringInArray()
  144. ->finishString()
  145. ->finishArray();
  146. }
  147. };