Recipie.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #pragma once
  2. #include <Array.h>
  3. #include <JSON.h>
  4. #include "ItemFilter.h"
  5. #include "ItemModifier.h"
  6. #include "TypeRegistry.h"
  7. class CraftingStorage;
  8. class ShapedCraftingStorage;
  9. struct ItemInfo
  10. {
  11. int type;
  12. int count;
  13. float hp;
  14. float maxHp;
  15. float durability;
  16. float maxDurability;
  17. };
  18. class RecipieInput : public Framework::ReferenceCounter
  19. {
  20. private:
  21. ItemFilter* filter;
  22. ItemModifier* modifier;
  23. int amount;
  24. public:
  25. RecipieInput();
  26. ~RecipieInput();
  27. void setFilter(ItemFilter* filter);
  28. ItemFilter* zFilter() const;
  29. void setModifier(ItemModifier* modifier);
  30. ItemModifier* zModifier() const;
  31. void setAmount(int amount);
  32. int getAmount() const;
  33. };
  34. class RecipieInputFactory : public TypeFactory<RecipieInput>
  35. {
  36. public:
  37. RecipieInputFactory();
  38. RecipieInput* createValue(
  39. Framework::JSON::JSONObject* zJson) const override;
  40. void fromJson(RecipieInput* zResult,
  41. Framework::JSON::JSONObject* zJson) const override;
  42. void toJson(RecipieInput* zObject,
  43. Framework::JSON::JSONObject* zResult) const override;
  44. JSONObjectValidationBuilder* addToValidator(
  45. JSONObjectValidationBuilder* builder) const override;
  46. };
  47. class RecipieOutput : public Framework::ReferenceCounter
  48. {
  49. private:
  50. int itemTypeId;
  51. int amount;
  52. ItemModifier* modifier;
  53. public:
  54. RecipieOutput();
  55. ~RecipieOutput();
  56. void setItemTypeId(int itemTypeId);
  57. int getItemTypeId() const;
  58. void setAmount(int amount);
  59. int getAmount() const;
  60. void setModifier(ItemModifier* modifier);
  61. ItemModifier* zModifier() const;
  62. Item* createItem() const;
  63. };
  64. class RecipieOutputFactory : public TypeFactory<RecipieOutput>
  65. {
  66. public:
  67. RecipieOutputFactory();
  68. RecipieOutput* createValue(
  69. Framework::JSON::JSONObject* zJson) const override;
  70. void fromJson(RecipieOutput* zResult,
  71. Framework::JSON::JSONObject* zJson) const override;
  72. void toJson(RecipieOutput* zObject,
  73. Framework::JSON::JSONObject* zResult) const override;
  74. JSONObjectValidationBuilder* addToValidator(
  75. JSONObjectValidationBuilder* builder) const override;
  76. };
  77. class Recipie : public virtual Framework::ReferenceCounter
  78. {
  79. protected:
  80. Framework::Text groupName;
  81. Framework::RCArray<RecipieOutput> outputs;
  82. public:
  83. Recipie();
  84. virtual bool testApplicability(CraftingStorage* zStorage) = 0;
  85. virtual void apply(CraftingStorage* zStorage) = 0;
  86. virtual Framework::Text getRecipieUIML() = 0;
  87. void addOutput(RecipieOutput* outputs);
  88. virtual Framework::Array<ItemInfo> getOutput() const;
  89. bool createsOutput(int itemTypeId);
  90. void setGroupName(Framework::Text groupName);
  91. const Framework::RCArray<RecipieOutput>& getOutputs() const;
  92. Framework::Text getGroupName() const;
  93. };
  94. template<typename S> class RecipieFactory : public SubTypeFactory<Recipie, S>
  95. {
  96. public:
  97. RecipieFactory()
  98. : SubTypeFactory<Recipie, S>()
  99. {}
  100. void fromJson(S* zResult, Framework::JSON::JSONObject* zJson) const override
  101. {
  102. Recipie* zRecipie = dynamic_cast<Recipie*>(zResult);
  103. for (Framework::JSON::JSONValue* output :
  104. *zJson->zValue("outputs")->asArray())
  105. {
  106. zRecipie->addOutput(
  107. Game::INSTANCE->zTypeRegistry()->fromJson<RecipieOutput>(
  108. output));
  109. }
  110. zRecipie->setGroupName(zJson->zValue("group")->asString()->getString());
  111. }
  112. void toJson(S* zObject, Framework::JSON::JSONObject* zResult) const override
  113. {
  114. Recipie* zRecipie = dynamic_cast<Recipie*>(zResult);
  115. Framework::JSON::JSONArray* outputs = new Framework::JSON::JSONArray();
  116. for (RecipieOutput* output : zRecipie->getOutputs())
  117. {
  118. outputs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(output));
  119. }
  120. zResult->addValue("outputs", outputs);
  121. zResult->addValue(
  122. "group", new Framework::JSON::JSONString(zRecipie->getGroupName()));
  123. };
  124. JSONObjectValidationBuilder* addToValidator(
  125. JSONObjectValidationBuilder* builder) const override
  126. {
  127. return builder->withRequiredArray("outputs")
  128. ->addAcceptedTypeInArray(
  129. Game::INSTANCE->zTypeRegistry()->getValidator<RecipieOutput>())
  130. ->finishArray()
  131. ->withRequiredString("group")
  132. ->finishString();
  133. }
  134. };
  135. class UnshapedRecipie : public Recipie
  136. {
  137. private:
  138. Framework::RCArray<RecipieInput> inputs;
  139. public:
  140. UnshapedRecipie();
  141. bool testApplicability(CraftingStorage* zStorage) override;
  142. void apply(CraftingStorage* zStorage) override;
  143. Framework::Text getRecipieUIML() override;
  144. void addInput(RecipieInput* input);
  145. const Framework::RCArray<RecipieInput>& getInputs() const;
  146. };
  147. class UnshapedRecipieFactory : public RecipieFactory<UnshapedRecipie>
  148. {
  149. public:
  150. UnshapedRecipieFactory();
  151. UnshapedRecipie* createValue(
  152. Framework::JSON::JSONObject* zJson) const override;
  153. void fromJson(UnshapedRecipie* zResult,
  154. Framework::JSON::JSONObject* zJson) const override;
  155. void toJson(UnshapedRecipie* zObject,
  156. Framework::JSON::JSONObject* zResult) const override;
  157. JSONObjectValidationBuilder* addToValidator(
  158. JSONObjectValidationBuilder* builder) const override;
  159. Framework::Text getTypeToken() const override;
  160. };
  161. class ShapedRecipie : public Recipie
  162. {
  163. private:
  164. Framework::RCArray<RecipieInput> inputs;
  165. int width;
  166. int height;
  167. public:
  168. ShapedRecipie();
  169. bool testApplicability(CraftingStorage* zStorage) override;
  170. void apply(CraftingStorage* zStorage) override;
  171. Framework::Text getRecipieUIML() override;
  172. void setWidth(int width);
  173. int getWidth() const;
  174. void setHeight(int height);
  175. int getHeight() const;
  176. void addInput(RecipieInput* input);
  177. void setInput(int index, RecipieInput* input);
  178. const Framework::RCArray<RecipieInput>& getInputs() const;
  179. };
  180. class ShapedRecipieFactory : public RecipieFactory<ShapedRecipie>
  181. {
  182. public:
  183. ShapedRecipieFactory();
  184. ShapedRecipie* createValue(
  185. Framework::JSON::JSONObject* zJson) const override;
  186. void fromJson(ShapedRecipie* zResult,
  187. Framework::JSON::JSONObject* zJson) const override;
  188. void toJson(ShapedRecipie* zObject,
  189. Framework::JSON::JSONObject* zResult) const override;
  190. JSONObjectValidationBuilder* addToValidator(
  191. JSONObjectValidationBuilder* builder) const override;
  192. Framework::Text getTypeToken() const override;
  193. };