| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- #pragma once
- #include <Either.h>
- #include <ReferenceCounter.h>
- #include "Game.h"
- #include "ItemFilter.h"
- #include "TypeRegistry.h"
- class Block;
- class Entity;
- class Item;
- class InteractionConfig : public virtual Framework::ReferenceCounter
- {
- private:
- ItemFilter* filter;
- public:
- InteractionConfig();
- ~InteractionConfig();
- void setItemFilter(ItemFilter* filter);
- ItemFilter* zItemFilter() const;
- bool doInteraction(Framework::Either<Block*, Entity*> target,
- Item* zItem,
- Entity* actor,
- bool& itemChanged);
- protected:
- virtual bool onInteraction(Framework::Either<Block*, Entity*> target,
- Item* zItem,
- Entity* actor,
- bool& itemChanged)
- = 0;
- };
- template<class T> class InteractionConfigFactory
- : public SubTypeFactory<InteractionConfig, T>
- {
- public:
- virtual JSONObjectValidationBuilder* addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return builder->withRequiredAttribute("itemFilter",
- Game::INSTANCE->zTypeRegistry()->getValidator<ItemFilter>(),
- false,
- true);
- }
- virtual T* fromJson(Framework::JSON::JSONObject* zJson) const
- {
- T* result = createValue(zJson);
- if (zJson->hasValue("itemFilter"))
- {
- ItemFilter* itemFilter
- = Game::INSTANCE->zTypeRegistry()->fromJson<ItemFilter>(
- zJson->zValue("itemFilter"));
- result->setItemFilter(itemFilter);
- }
- return result;
- }
- virtual Framework::JSON::JSONObject* toJsonObject(T* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- if (zObject->zItemFilter())
- {
- result->addValue("itemFilter",
- Game::INSTANCE->zTypeRegistry()->toJson(
- zObject->zItemFilter()));
- }
- return result;
- }
- protected:
- virtual T* createValue(Framework::JSON::JSONObject* zJson) const = 0;
- };
|