ItemModifier.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. #include "ItemModifier.h"
  2. #include "Game.h"
  3. using namespace Framework;
  4. ItemModifier::ItemModifier()
  5. : ReferenceCounter()
  6. {}
  7. ConsumeItemModifier::ConsumeItemModifier()
  8. : ItemModifier()
  9. {}
  10. void ConsumeItemModifier::applyOn(Item* zItem)
  11. {
  12. zItem->setHp(0);
  13. }
  14. ConsumeItemModifierFactory::ConsumeItemModifierFactory()
  15. : SubTypeFactory()
  16. {}
  17. ConsumeItemModifier* ConsumeItemModifierFactory::fromJson(
  18. Framework::JSON::JSONObject* zJson) const
  19. {
  20. return new ConsumeItemModifier();
  21. }
  22. Framework::JSON::JSONObject* ConsumeItemModifierFactory::toJson(
  23. ConsumeItemModifier* zObject) const
  24. {
  25. return new Framework::JSON::JSONObject();
  26. }
  27. Framework::JSON::Validator::JSONValidator*
  28. ConsumeItemModifierFactory::getValidator(
  29. Framework::JSON::Validator::ObjectValidationBuilder<
  30. Framework::JSON::Validator::JSONValidator>* builder) const
  31. {
  32. return builder->finishObject();
  33. }
  34. Framework::Text ConsumeItemModifierFactory::getTypeToken() const
  35. {
  36. return "consume";
  37. }
  38. DoNothingModifier::DoNothingModifier()
  39. : ItemModifier()
  40. {}
  41. void DoNothingModifier::applyOn(Item* zItem)
  42. {
  43. // do nothing
  44. }
  45. DoNothingModifierFactory::DoNothingModifierFactory()
  46. : SubTypeFactory()
  47. {}
  48. DoNothingModifier* DoNothingModifierFactory::fromJson(
  49. Framework::JSON::JSONObject* zJson) const
  50. {
  51. return new DoNothingModifier();
  52. }
  53. Framework::JSON::JSONObject* DoNothingModifierFactory::toJson(
  54. DoNothingModifier* zObject) const
  55. {
  56. return new Framework::JSON::JSONObject();
  57. }
  58. Framework::JSON::Validator::JSONValidator*
  59. DoNothingModifierFactory::getValidator(
  60. Framework::JSON::Validator::ObjectValidationBuilder<
  61. Framework::JSON::Validator::JSONValidator>* builder) const
  62. {
  63. return builder->finishObject();
  64. }
  65. Framework::Text DoNothingModifierFactory::getTypeToken() const
  66. {
  67. return "doNothing";
  68. }
  69. AttributeItemModifier::AttributeItemModifier(
  70. Framework::RCArray<Framework::Text> attribute,
  71. Framework::RCArray<Framework::JSON::JSONValue> value)
  72. : ItemModifier(),
  73. attributes(attribute),
  74. values(value)
  75. {}
  76. void AttributeItemModifier::applyOn(Item* zItem)
  77. {
  78. auto attribute = attributes.begin();
  79. auto value = values.begin();
  80. while (attribute && value)
  81. {
  82. zItem->zItemType()->setItemAttribute(
  83. zItem, attribute->getText(), value);
  84. attribute++;
  85. value++;
  86. }
  87. }
  88. const Framework::RCArray<Framework::Text>&
  89. AttributeItemModifier::getAttributes() const
  90. {
  91. return attributes;
  92. }
  93. const Framework::RCArray<Framework::JSON::JSONValue>&
  94. AttributeItemModifier::getValues() const
  95. {
  96. return values;
  97. }
  98. AttributeItemModifierFactory::AttributeItemModifierFactory()
  99. : SubTypeFactory()
  100. {}
  101. AttributeItemModifier* AttributeItemModifierFactory::fromJson(
  102. Framework::JSON::JSONObject* zJson) const
  103. {
  104. Framework::RCArray<Framework::Text> attributes;
  105. Framework::RCArray<Framework::JSON::JSONValue> values;
  106. auto attribute = zJson->getFields();
  107. auto value = zJson->getValues();
  108. while (attribute, value)
  109. {
  110. if (!attribute.val().istGleich("type"))
  111. {
  112. attributes.add(new Framework::Text(attribute.val()));
  113. values.add(
  114. dynamic_cast<Framework::JSON::JSONValue*>(value->getThis()));
  115. }
  116. attribute++;
  117. value++;
  118. }
  119. return new AttributeItemModifier(attributes, values);
  120. }
  121. Framework::JSON::JSONObject* AttributeItemModifierFactory::toJson(
  122. AttributeItemModifier* zObject) const
  123. {
  124. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  125. auto attribute = zObject->getAttributes().begin();
  126. auto value = zObject->getValues().begin();
  127. while (attribute && value)
  128. {
  129. result->addValue(attribute->getText(),
  130. dynamic_cast<Framework::JSON::JSONObject*>(value->getThis()));
  131. attribute++;
  132. value++;
  133. }
  134. return result;
  135. }
  136. Framework::JSON::Validator::JSONValidator*
  137. AttributeItemModifierFactory::getValidator(
  138. Framework::JSON::Validator::ObjectValidationBuilder<
  139. Framework::JSON::Validator::JSONValidator>* builder) const
  140. {
  141. return builder->allowAdditionalAttriutes()->finishObject();
  142. }
  143. Framework::Text AttributeItemModifierFactory::getTypeToken() const
  144. {
  145. return "attributes";
  146. }
  147. CombinedItemModifier::CombinedItemModifier(
  148. Framework::RCArray<ItemModifier> modifiers)
  149. : ItemModifier(),
  150. modifiers()
  151. {}
  152. void CombinedItemModifier::applyOn(Item* zItem)
  153. {
  154. for (ItemModifier* modifier : modifiers)
  155. {
  156. modifier->applyOn(zItem);
  157. }
  158. }
  159. const Framework::RCArray<ItemModifier>&
  160. CombinedItemModifier::getModifiers() const
  161. {
  162. return modifiers;
  163. }
  164. CombinedItemModifierFactory::CombinedItemModifierFactory()
  165. : SubTypeFactory()
  166. {}
  167. CombinedItemModifier* CombinedItemModifierFactory::fromJson(
  168. Framework::JSON::JSONObject* zJson) const
  169. {
  170. Framework::RCArray<ItemModifier> modifiers;
  171. for (Framework::JSON::JSONValue* value :
  172. *zJson->zValue("modifiers")->asArray())
  173. {
  174. modifiers.add(
  175. Game::INSTANCE->zTypeRegistry()->fromJson<ItemModifier>(value));
  176. }
  177. return new CombinedItemModifier(modifiers);
  178. }
  179. Framework::JSON::JSONObject* CombinedItemModifierFactory::toJson(
  180. CombinedItemModifier* zObject) const
  181. {
  182. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  183. Framework::JSON::JSONArray* modifiers = new Framework::JSON::JSONArray();
  184. for (ItemModifier* modifier : zObject->getModifiers())
  185. {
  186. modifiers->addValue(Game::INSTANCE->zTypeRegistry()->toJson(modifier));
  187. }
  188. result->addValue("modifiers", modifiers);
  189. return result;
  190. }
  191. Framework::JSON::Validator::JSONValidator*
  192. CombinedItemModifierFactory::getValidator(
  193. Framework::JSON::Validator::ObjectValidationBuilder<
  194. Framework::JSON::Validator::JSONValidator>* builder) const
  195. {
  196. return builder->withRequiredArray("modifiers")
  197. ->addAcceptedTypeInArray(
  198. Game::INSTANCE->zTypeRegistry()->getValidator<ItemModifier>())
  199. ->finishArray()
  200. ->finishObject();
  201. }
  202. Framework::Text CombinedItemModifierFactory::getTypeToken() const
  203. {
  204. return "combined";
  205. }