123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239 |
- #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::toJson(
- ConsumeItemModifier* zObject) const
- {
- return new Framework::JSON::JSONObject();
- }
- Framework::JSON::Validator::JSONValidator*
- ConsumeItemModifierFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->finishObject();
- }
- Framework::Text 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::toJson(
- DoNothingModifier* zObject) const
- {
- return new Framework::JSON::JSONObject();
- }
- Framework::JSON::Validator::JSONValidator*
- DoNothingModifierFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->finishObject();
- }
- Framework::Text DoNothingModifierFactory::getTypeToken() const
- {
- return "doNothing";
- }
- AttributeItemModifier::AttributeItemModifier(
- Framework::RCArray<Framework::Text> attribute,
- Framework::RCArray<Framework::JSON::JSONValue> value)
- : ItemModifier(),
- attributes(attribute),
- values(value)
- {}
- 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++;
- }
- }
- 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::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- Framework::RCArray<Framework::Text> attributes;
- Framework::RCArray<Framework::JSON::JSONValue> values;
- auto attribute = zJson->getFields();
- auto value = zJson->getValues();
- while (attribute, value)
- {
- if (!attribute.val().istGleich("type"))
- {
- attributes.add(new Framework::Text(attribute.val()));
- values.add(
- dynamic_cast<Framework::JSON::JSONValue*>(value->getThis()));
- }
- attribute++;
- value++;
- }
- return new AttributeItemModifier(attributes, values);
- }
- Framework::JSON::JSONObject* AttributeItemModifierFactory::toJson(
- 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<Framework::JSON::JSONObject*>(value->getThis()));
- attribute++;
- value++;
- }
- return result;
- }
- Framework::JSON::Validator::JSONValidator*
- AttributeItemModifierFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->allowAdditionalAttriutes()->finishObject();
- }
- Framework::Text AttributeItemModifierFactory::getTypeToken() const
- {
- return "attributes";
- }
- CombinedItemModifier::CombinedItemModifier(
- Framework::RCArray<ItemModifier> modifiers)
- : ItemModifier(),
- modifiers()
- {}
- void CombinedItemModifier::applyOn(Item* zItem)
- {
- for (ItemModifier* modifier : modifiers)
- {
- modifier->applyOn(zItem);
- }
- }
- const Framework::RCArray<ItemModifier>&
- CombinedItemModifier::getModifiers() const
- {
- return modifiers;
- }
- CombinedItemModifierFactory::CombinedItemModifierFactory()
- : SubTypeFactory()
- {}
- CombinedItemModifier* CombinedItemModifierFactory::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- Framework::RCArray<ItemModifier> modifiers;
- for (Framework::JSON::JSONValue* value :
- *zJson->zValue("modifiers")->asArray())
- {
- modifiers.add(
- Game::INSTANCE->zTypeRegistry()->fromJson<ItemModifier>(value));
- }
- return new CombinedItemModifier(modifiers);
- }
- Framework::JSON::JSONObject* CombinedItemModifierFactory::toJson(
- 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;
- }
- Framework::JSON::Validator::JSONValidator*
- CombinedItemModifierFactory::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->withRequiredArray("modifiers")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()->getValidator<ItemModifier>())
- ->finishArray()
- ->finishObject();
- }
- Framework::Text CombinedItemModifierFactory::getTypeToken() const
- {
- return "combined";
- }
|