123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- #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::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- return new ConsumeItemModifier();
- }
- void ConsumeItemModifierFactory::fromJson(
- ConsumeItemModifier* zResult, Framework::JSON::JSONObject* zJson) const
- {}
- void ConsumeItemModifierFactory::toJson(
- ConsumeItemModifier* zObject, Framework::JSON::JSONObject* zResult) const
- {}
- JSONObjectValidationBuilder* ConsumeItemModifierFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return builder;
- }
- Framework::Text ConsumeItemModifierFactory::getTypeToken() const
- {
- return "consume";
- }
- DoNothingModifier::DoNothingModifier()
- : ItemModifier()
- {}
- void DoNothingModifier::applyOn(Item* zItem)
- {
- // do nothing
- }
- DoNothingModifierFactory::DoNothingModifierFactory()
- : SubTypeFactory()
- {}
- DoNothingModifier* DoNothingModifierFactory::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- return new DoNothingModifier();
- }
- void DoNothingModifierFactory::fromJson(
- DoNothingModifier* zResult, Framework::JSON::JSONObject* zJson) const
- {}
- void DoNothingModifierFactory::toJson(
- DoNothingModifier* zObject, Framework::JSON::JSONObject* zResult) const
- {}
- JSONObjectValidationBuilder* DoNothingModifierFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return builder;
- }
- Framework::Text 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<Framework::Text>&
- AttributeItemModifier::getAttributes() const
- {
- return attributes;
- }
- const Framework::RCArray<Framework::JSON::JSONValue>&
- AttributeItemModifier::getValues() const
- {
- return values;
- }
- AttributeItemModifierFactory::AttributeItemModifierFactory()
- : SubTypeFactory()
- {}
- AttributeItemModifier* AttributeItemModifierFactory::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- return new AttributeItemModifier();
- }
- void AttributeItemModifierFactory::fromJson(
- AttributeItemModifier* zResult, Framework::JSON::JSONObject* zJson) const
- {
- auto attribute = zJson->getFields();
- auto value = zJson->getValues();
- while (attribute, value)
- {
- if (!attribute.val().istGleich("type"))
- {
- zResult->addAttribute(attribute.val(), value);
- }
- attribute++;
- value++;
- }
- }
- void AttributeItemModifierFactory::toJson(
- AttributeItemModifier* zObject, Framework::JSON::JSONObject* zResult) const
- {
- auto attribute = zObject->getAttributes().begin();
- auto value = zObject->getValues().begin();
- while (attribute && value)
- {
- zResult->addValue(attribute->getText(),
- dynamic_cast<Framework::JSON::JSONObject*>(value->getThis()));
- attribute++;
- value++;
- }
- }
- JSONObjectValidationBuilder* AttributeItemModifierFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return builder->allowAdditionalAttriutes();
- }
- Framework::Text 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<ItemModifier>&
- CombinedItemModifier::getModifiers() const
- {
- return modifiers;
- }
- CombinedItemModifierFactory::CombinedItemModifierFactory()
- : SubTypeFactory()
- {}
- CombinedItemModifier* CombinedItemModifierFactory::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- return new CombinedItemModifier();
- }
- void CombinedItemModifierFactory::fromJson(
- CombinedItemModifier* zResult, Framework::JSON::JSONObject* zJson) const
- {
- for (Framework::JSON::JSONValue* value :
- *zJson->zValue("modifiers")->asArray())
- {
- zResult->addModifier(
- Game::INSTANCE->zTypeRegistry()->fromJson<ItemModifier>(value));
- }
- }
- void CombinedItemModifierFactory::toJson(
- CombinedItemModifier* zObject, Framework::JSON::JSONObject* zResult) const
- {
- Framework::JSON::JSONArray* modifiers = new Framework::JSON::JSONArray();
- for (ItemModifier* modifier : zObject->getModifiers())
- {
- modifiers->addValue(Game::INSTANCE->zTypeRegistry()->toJson(modifier));
- }
- zResult->addValue("modifiers", modifiers);
- }
- JSONObjectValidationBuilder* CombinedItemModifierFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return builder->withRequiredArray("modifiers")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()->getValidator<ItemModifier>())
- ->finishArray();
- }
- Framework::Text CombinedItemModifierFactory::getTypeToken() const
- {
- return "combined";
- }
|