#pragma once #include #include #include "ItemFilter.h" #include "ItemModifier.h" #include "TypeRegistry.h" class CraftingStorage; class ShapedCraftingStorage; struct ItemInfo { int type; int count; float hp; float maxHp; float durability; float maxDurability; }; class RecipieInput : public Framework::ReferenceCounter { private: ItemFilter* filter; ItemModifier* modifier; int amount; public: RecipieInput(); ~RecipieInput(); void setFilter(ItemFilter* filter); ItemFilter* zFilter() const; void setModifier(ItemModifier* modifier); ItemModifier* zModifier() const; void setAmount(int amount); int getAmount() const; }; class RecipieInputFactory : public ObjectTypeFactory { public: RecipieInputFactory(); RecipieInput* fromJson(Framework::JSON::JSONObject* zJson) const override; Framework::JSON::JSONObject* toJsonObject( RecipieInput* zObject) const override; JSONObjectValidationBuilder* addToValidator( JSONObjectValidationBuilder* builder) const override; }; class RecipieOutput : public Framework::ReferenceCounter { private: int itemTypeId; int amount; ItemModifier* modifier; public: RecipieOutput(); ~RecipieOutput(); void setItemTypeId(int itemTypeId); int getItemTypeId() const; void setAmount(int amount); int getAmount() const; void setModifier(ItemModifier* modifier); ItemModifier* zModifier() const; Item* createItem() const; }; class RecipieOutputFactory : public ObjectTypeFactory { public: RecipieOutputFactory(); RecipieOutput* fromJson(Framework::JSON::JSONObject* zJson) const override; Framework::JSON::JSONObject* toJsonObject( RecipieOutput* zObject) const override; JSONObjectValidationBuilder* addToValidator( JSONObjectValidationBuilder* builder) const override; }; class Recipie : public virtual Framework::ReferenceCounter { protected: Framework::Text groupName; Framework::RCArray outputs; public: Recipie(); virtual bool testApplicability(CraftingStorage* zStorage) = 0; virtual void apply(CraftingStorage* zStorage) = 0; virtual Framework::Text getRecipieUIML() = 0; void addOutput(RecipieOutput* outputs); virtual Framework::Array getOutput() const; bool createsOutput(int itemTypeId); void setGroupName(Framework::Text groupName); const Framework::RCArray& getOutputs() const; Framework::Text getGroupName() const; }; template class RecipieFactory : public SubTypeFactory { public: RecipieFactory() : SubTypeFactory() {} S* fromJson(Framework::JSON::JSONObject* zJson) const override { S* result = createValue(zJson); Recipie* zRecipie = dynamic_cast(result); for (Framework::JSON::JSONValue* output : *zJson->zValue("outputs")->asArray()) { zRecipie->addOutput( Game::INSTANCE->zTypeRegistry()->fromJson( output)); } zRecipie->setGroupName(zJson->zValue("group")->asString()->getString()); return result; } Framework::JSON::JSONObject* toJsonObject(S* zObject) const override { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); Recipie* zRecipie = dynamic_cast(result); Framework::JSON::JSONArray* outputs = new Framework::JSON::JSONArray(); for (RecipieOutput* output : zRecipie->getOutputs()) { outputs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(output)); } result->addValue("outputs", outputs); result->addValue( "group", new Framework::JSON::JSONString(zRecipie->getGroupName())); return result; }; JSONObjectValidationBuilder* addToValidator( JSONObjectValidationBuilder* builder) const override { return builder->withRequiredArray("outputs") ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry()->getValidator()) ->finishArray() ->withRequiredString("group") ->finishString(); } protected: virtual S* createValue(Framework::JSON::JSONObject* zJson) const = 0; }; class UnshapedRecipie : public Recipie { private: Framework::RCArray inputs; public: UnshapedRecipie(); bool testApplicability(CraftingStorage* zStorage) override; void apply(CraftingStorage* zStorage) override; Framework::Text getRecipieUIML() override; void addInput(RecipieInput* input); const Framework::RCArray& getInputs() const; }; class UnshapedRecipieFactory : public RecipieFactory { public: UnshapedRecipieFactory(); UnshapedRecipie* createValue( Framework::JSON::JSONObject* zJson) const override; UnshapedRecipie* fromJson( Framework::JSON::JSONObject* zJson) const override; Framework::JSON::JSONObject* toJsonObject( UnshapedRecipie* zObject) const override; JSONObjectValidationBuilder* addToValidator( JSONObjectValidationBuilder* builder) const override; const char* getTypeToken() const override; }; class ShapedRecipie : public Recipie { private: Framework::RCArray inputs; int width; int height; public: ShapedRecipie(); bool testApplicability(CraftingStorage* zStorage) override; void apply(CraftingStorage* zStorage) override; Framework::Text getRecipieUIML() override; void setWidth(int width); int getWidth() const; void setHeight(int height); int getHeight() const; void addInput(RecipieInput* input); void setInput(int index, RecipieInput* input); const Framework::RCArray& getInputs() const; }; class ShapedRecipieFactory : public RecipieFactory { public: ShapedRecipieFactory(); ShapedRecipie* createValue( Framework::JSON::JSONObject* zJson) const override; ShapedRecipie* fromJson(Framework::JSON::JSONObject* zJson) const override; Framework::JSON::JSONObject* toJsonObject( ShapedRecipie* zObject) const override; JSONObjectValidationBuilder* addToValidator( JSONObjectValidationBuilder* builder) const override; const char* getTypeToken() const override; };