#include "ItemModifier.h" #include "Game.h" using namespace Framework; ItemModifier::ItemModifier() : ReferenceCounter() {} ConsumeItemModifier::ConsumeItemModifier() : ItemModifier() {} void ConsumeItemModifier::applyOn(Item* zItem) { zItem->setHp(0); } ConsumeItemModifierFactory::ConsumeItemModifierFactory() : SubTypeFactory() {} ConsumeItemModifier* ConsumeItemModifierFactory::fromJson( Framework::JSON::JSONObject* zJson) const { return new ConsumeItemModifier(); } Framework::JSON::JSONObject* ConsumeItemModifierFactory::toJsonObject( ConsumeItemModifier* zObject) const { return new Framework::JSON::JSONObject(); } JSONObjectValidationBuilder* ConsumeItemModifierFactory::addToValidator( JSONObjectValidationBuilder* builder) const { return builder; } const char* ConsumeItemModifierFactory::getTypeToken() const { return "consume"; } DoNothingModifier::DoNothingModifier() : ItemModifier() {} void DoNothingModifier::applyOn(Item* zItem) { // do nothing } DoNothingModifierFactory::DoNothingModifierFactory() : SubTypeFactory() {} DoNothingModifier* DoNothingModifierFactory::fromJson( Framework::JSON::JSONObject* zJson) const { return new DoNothingModifier(); } Framework::JSON::JSONObject* DoNothingModifierFactory::toJsonObject( DoNothingModifier* zObject) const { return new Framework::JSON::JSONObject(); } JSONObjectValidationBuilder* DoNothingModifierFactory::addToValidator( JSONObjectValidationBuilder* builder) const { return builder; } const char* DoNothingModifierFactory::getTypeToken() const { return "doNothing"; } AttributeItemModifier::AttributeItemModifier() : ItemModifier() {} void AttributeItemModifier::applyOn(Item* zItem) { auto attribute = attributes.begin(); auto value = values.begin(); while (attribute && value) { zItem->zItemType()->setItemAttribute( zItem, attribute->getText(), value); attribute++; value++; } } void AttributeItemModifier::addAttribute( Framework::Text name, Framework::JSON::JSONValue* value) { attributes.add(new Framework::Text(name)); values.add(value); } const Framework::RCArray& AttributeItemModifier::getAttributes() const { return attributes; } const Framework::RCArray& AttributeItemModifier::getValues() const { return values; } AttributeItemModifierFactory::AttributeItemModifierFactory() : SubTypeFactory() {} AttributeItemModifier* AttributeItemModifierFactory::fromJson( Framework::JSON::JSONObject* zJson) const { AttributeItemModifier* result = new AttributeItemModifier(); auto attribute = zJson->getFields(); auto value = zJson->getValues(); while (attribute, value) { if (!attribute.val().istGleich("type")) { result->addAttribute(attribute.val(), value); } attribute++; value++; } return result; } Framework::JSON::JSONObject* AttributeItemModifierFactory::toJsonObject( AttributeItemModifier* zObject) const { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); auto attribute = zObject->getAttributes().begin(); auto value = zObject->getValues().begin(); while (attribute && value) { result->addValue(attribute->getText(), dynamic_cast(value->getThis())); attribute++; value++; } return result; } JSONObjectValidationBuilder* AttributeItemModifierFactory::addToValidator( JSONObjectValidationBuilder* builder) const { return builder->allowAdditionalAttriutes(); } const char* AttributeItemModifierFactory::getTypeToken() const { return "attributes"; } CombinedItemModifier::CombinedItemModifier() : ItemModifier() {} void CombinedItemModifier::applyOn(Item* zItem) { for (ItemModifier* modifier : modifiers) { modifier->applyOn(zItem); } } void CombinedItemModifier::addModifier(ItemModifier* zModifier) { modifiers.add(zModifier); } const Framework::RCArray& CombinedItemModifier::getModifiers() const { return modifiers; } CombinedItemModifierFactory::CombinedItemModifierFactory() : SubTypeFactory() {} CombinedItemModifier* CombinedItemModifierFactory::fromJson( Framework::JSON::JSONObject* zJson) const { CombinedItemModifier* result = new CombinedItemModifier(); for (Framework::JSON::JSONValue* value : *zJson->zValue("modifiers")->asArray()) { result->addModifier( Game::INSTANCE->zTypeRegistry()->fromJson(value)); } return result; } Framework::JSON::JSONObject* CombinedItemModifierFactory::toJsonObject( CombinedItemModifier* zObject) const { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); Framework::JSON::JSONArray* modifiers = new Framework::JSON::JSONArray(); for (ItemModifier* modifier : zObject->getModifiers()) { modifiers->addValue(Game::INSTANCE->zTypeRegistry()->toJson(modifier)); } result->addValue("modifiers", modifiers); return result; } JSONObjectValidationBuilder* CombinedItemModifierFactory::addToValidator( JSONObjectValidationBuilder* builder) const { return builder->withRequiredArray("modifiers") ->addAcceptedTypeInArray( Game::INSTANCE->zTypeRegistry()->getValidator()) ->finishArray(); } const char* CombinedItemModifierFactory::getTypeToken() const { return "combined"; }