Recipie.h 6.3 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 ObjectTypeFactory<RecipieInput>
  35. {
  36. public:
  37. RecipieInputFactory();
  38. RecipieInput* fromJson(Framework::JSON::JSONObject* zJson) const override;
  39. Framework::JSON::JSONObject* toJsonObject(
  40. RecipieInput* zObject) const override;
  41. JSONObjectValidationBuilder* addToValidator(
  42. JSONObjectValidationBuilder* builder) const override;
  43. };
  44. class RecipieOutput : public Framework::ReferenceCounter
  45. {
  46. private:
  47. int itemTypeId;
  48. int amount;
  49. ItemModifier* modifier;
  50. public:
  51. RecipieOutput();
  52. ~RecipieOutput();
  53. void setItemTypeId(int itemTypeId);
  54. int getItemTypeId() const;
  55. void setAmount(int amount);
  56. int getAmount() const;
  57. void setModifier(ItemModifier* modifier);
  58. ItemModifier* zModifier() const;
  59. Item* createItem() const;
  60. };
  61. class RecipieOutputFactory : public ObjectTypeFactory<RecipieOutput>
  62. {
  63. public:
  64. RecipieOutputFactory();
  65. RecipieOutput* fromJson(Framework::JSON::JSONObject* zJson) const override;
  66. Framework::JSON::JSONObject* toJsonObject(
  67. RecipieOutput* zObject) const override;
  68. JSONObjectValidationBuilder* addToValidator(
  69. JSONObjectValidationBuilder* builder) const override;
  70. };
  71. class Recipie : public virtual Framework::ReferenceCounter
  72. {
  73. protected:
  74. Framework::Text groupName;
  75. Framework::RCArray<RecipieOutput> outputs;
  76. public:
  77. Recipie();
  78. virtual bool testApplicability(CraftingStorage* zStorage) = 0;
  79. virtual void apply(CraftingStorage* zStorage) = 0;
  80. virtual Framework::Text getRecipieUIML() = 0;
  81. void addOutput(RecipieOutput* outputs);
  82. virtual Framework::Array<ItemInfo> getOutput() const;
  83. bool createsOutput(int itemTypeId);
  84. void setGroupName(Framework::Text groupName);
  85. const Framework::RCArray<RecipieOutput>& getOutputs() const;
  86. Framework::Text getGroupName() const;
  87. };
  88. template<typename S> class RecipieFactory : public SubTypeFactory<Recipie, S>
  89. {
  90. public:
  91. RecipieFactory()
  92. : SubTypeFactory<Recipie, S>()
  93. {}
  94. S* fromJson(Framework::JSON::JSONObject* zJson) const override
  95. {
  96. S* result = createValue(zJson);
  97. Recipie* zRecipie = dynamic_cast<Recipie*>(result);
  98. for (Framework::JSON::JSONValue* output :
  99. *zJson->zValue("outputs")->asArray())
  100. {
  101. zRecipie->addOutput(
  102. Game::INSTANCE->zTypeRegistry()->fromJson<RecipieOutput>(
  103. output));
  104. }
  105. zRecipie->setGroupName(zJson->zValue("group")->asString()->getString());
  106. return result;
  107. }
  108. Framework::JSON::JSONObject* toJsonObject(S* zObject) const override
  109. {
  110. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  111. Recipie* zRecipie = dynamic_cast<Recipie*>(result);
  112. Framework::JSON::JSONArray* outputs = new Framework::JSON::JSONArray();
  113. for (RecipieOutput* output : zRecipie->getOutputs())
  114. {
  115. outputs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(output));
  116. }
  117. result->addValue("outputs", outputs);
  118. result->addValue(
  119. "group", new Framework::JSON::JSONString(zRecipie->getGroupName()));
  120. return result;
  121. };
  122. JSONObjectValidationBuilder* addToValidator(
  123. JSONObjectValidationBuilder* builder) const override
  124. {
  125. return builder->withRequiredArray("outputs")
  126. ->addAcceptedTypeInArray(
  127. Game::INSTANCE->zTypeRegistry()->getValidator<RecipieOutput>())
  128. ->finishArray()
  129. ->withRequiredString("group")
  130. ->finishString();
  131. }
  132. protected:
  133. virtual S* createValue(Framework::JSON::JSONObject* zJson) const = 0;
  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. UnshapedRecipie* fromJson(
  154. Framework::JSON::JSONObject* zJson) const override;
  155. Framework::JSON::JSONObject* toJsonObject(
  156. UnshapedRecipie* zObject) const override;
  157. JSONObjectValidationBuilder* addToValidator(
  158. JSONObjectValidationBuilder* builder) const override;
  159. const char* 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. ShapedRecipie* fromJson(Framework::JSON::JSONObject* zJson) const override;
  187. Framework::JSON::JSONObject* toJsonObject(
  188. ShapedRecipie* zObject) const override;
  189. JSONObjectValidationBuilder* addToValidator(
  190. JSONObjectValidationBuilder* builder) const override;
  191. const char* getTypeToken() const override;
  192. };