123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- #pragma once
- #include <Array.h>
- #include <JSON.h>
- #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 TypeFactory<RecipieInput>
- {
- public:
- RecipieInputFactory();
- RecipieInput* createValue(
- Framework::JSON::JSONObject* zJson) const override;
- void fromJson(RecipieInput* zResult,
- Framework::JSON::JSONObject* zJson) const override;
- void toJson(RecipieInput* zObject,
- Framework::JSON::JSONObject* zResult) 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 TypeFactory<RecipieOutput>
- {
- public:
- RecipieOutputFactory();
- RecipieOutput* createValue(
- Framework::JSON::JSONObject* zJson) const override;
- void fromJson(RecipieOutput* zResult,
- Framework::JSON::JSONObject* zJson) const override;
- void toJson(RecipieOutput* zObject,
- Framework::JSON::JSONObject* zResult) const override;
- JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const override;
- };
- class Recipie : public virtual Framework::ReferenceCounter
- {
- protected:
- Framework::Text groupName;
- Framework::RCArray<RecipieOutput> 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<ItemInfo> getOutput() const;
- bool createsOutput(int itemTypeId);
- void setGroupName(Framework::Text groupName);
- const Framework::RCArray<RecipieOutput>& getOutputs() const;
- Framework::Text getGroupName() const;
- };
- template<typename S> class RecipieFactory : public SubTypeFactory<Recipie, S>
- {
- public:
- RecipieFactory()
- : SubTypeFactory<Recipie, S>()
- {}
- void fromJson(S* zResult, Framework::JSON::JSONObject* zJson) const override
- {
- Recipie* zRecipie = dynamic_cast<Recipie*>(zResult);
- for (Framework::JSON::JSONValue* output :
- *zJson->zValue("outputs")->asArray())
- {
- zRecipie->addOutput(
- Game::INSTANCE->zTypeRegistry()->fromJson<RecipieOutput>(
- output));
- }
- zRecipie->setGroupName(zJson->zValue("group")->asString()->getString());
- }
- void toJson(S* zObject, Framework::JSON::JSONObject* zResult) const override
- {
- Recipie* zRecipie = dynamic_cast<Recipie*>(zResult);
- Framework::JSON::JSONArray* outputs = new Framework::JSON::JSONArray();
- for (RecipieOutput* output : zRecipie->getOutputs())
- {
- outputs->addValue(Game::INSTANCE->zTypeRegistry()->toJson(output));
- }
- zResult->addValue("outputs", outputs);
- zResult->addValue(
- "group", new Framework::JSON::JSONString(zRecipie->getGroupName()));
- };
- JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const override
- {
- return builder->withRequiredArray("outputs")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()->getValidator<RecipieOutput>())
- ->finishArray()
- ->withRequiredString("group")
- ->finishString();
- }
- };
- class UnshapedRecipie : public Recipie
- {
- private:
- Framework::RCArray<RecipieInput> inputs;
- public:
- UnshapedRecipie();
- bool testApplicability(CraftingStorage* zStorage) override;
- void apply(CraftingStorage* zStorage) override;
- Framework::Text getRecipieUIML() override;
- void addInput(RecipieInput* input);
- const Framework::RCArray<RecipieInput>& getInputs() const;
- };
- class UnshapedRecipieFactory : public RecipieFactory<UnshapedRecipie>
- {
- public:
- UnshapedRecipieFactory();
- UnshapedRecipie* createValue(
- Framework::JSON::JSONObject* zJson) const override;
- void fromJson(UnshapedRecipie* zResult,
- Framework::JSON::JSONObject* zJson) const override;
- void toJson(UnshapedRecipie* zObject,
- Framework::JSON::JSONObject* zResult) const override;
- JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const override;
- Framework::Text getTypeToken() const override;
- };
- class ShapedRecipie : public Recipie
- {
- private:
- Framework::RCArray<RecipieInput> 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<RecipieInput>& getInputs() const;
- };
- class ShapedRecipieFactory : public RecipieFactory<ShapedRecipie>
- {
- public:
- ShapedRecipieFactory();
- ShapedRecipie* createValue(
- Framework::JSON::JSONObject* zJson) const override;
- void fromJson(ShapedRecipie* zResult,
- Framework::JSON::JSONObject* zJson) const override;
- void toJson(ShapedRecipie* zObject,
- Framework::JSON::JSONObject* zResult) const override;
- JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const override;
- Framework::Text getTypeToken() const override;
- };
|