ItemModifier.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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::createValue(
  18. Framework::JSON::JSONObject* zJson) const
  19. {
  20. return new ConsumeItemModifier();
  21. }
  22. void ConsumeItemModifierFactory::fromJson(
  23. ConsumeItemModifier* zResult, Framework::JSON::JSONObject* zJson) const
  24. {}
  25. void ConsumeItemModifierFactory::toJson(
  26. ConsumeItemModifier* zObject, Framework::JSON::JSONObject* zResult) const
  27. {}
  28. JSONObjectValidationBuilder* ConsumeItemModifierFactory::addToValidator(
  29. JSONObjectValidationBuilder* builder) const
  30. {
  31. return builder;
  32. }
  33. Framework::Text ConsumeItemModifierFactory::getTypeToken() const
  34. {
  35. return "consume";
  36. }
  37. DoNothingModifier::DoNothingModifier()
  38. : ItemModifier()
  39. {}
  40. void DoNothingModifier::applyOn(Item* zItem)
  41. {
  42. // do nothing
  43. }
  44. DoNothingModifierFactory::DoNothingModifierFactory()
  45. : SubTypeFactory()
  46. {}
  47. DoNothingModifier* DoNothingModifierFactory::createValue(
  48. Framework::JSON::JSONObject* zJson) const
  49. {
  50. return new DoNothingModifier();
  51. }
  52. void DoNothingModifierFactory::fromJson(
  53. DoNothingModifier* zResult, Framework::JSON::JSONObject* zJson) const
  54. {}
  55. void DoNothingModifierFactory::toJson(
  56. DoNothingModifier* zObject, Framework::JSON::JSONObject* zResult) const
  57. {}
  58. JSONObjectValidationBuilder* DoNothingModifierFactory::addToValidator(
  59. JSONObjectValidationBuilder* builder) const
  60. {
  61. return builder;
  62. }
  63. Framework::Text DoNothingModifierFactory::getTypeToken() const
  64. {
  65. return "doNothing";
  66. }
  67. AttributeItemModifier::AttributeItemModifier()
  68. : ItemModifier()
  69. {}
  70. void AttributeItemModifier::applyOn(Item* zItem)
  71. {
  72. auto attribute = attributes.begin();
  73. auto value = values.begin();
  74. while (attribute && value)
  75. {
  76. zItem->zItemType()->setItemAttribute(
  77. zItem, attribute->getText(), value);
  78. attribute++;
  79. value++;
  80. }
  81. }
  82. void AttributeItemModifier::addAttribute(
  83. Framework::Text name, Framework::JSON::JSONValue* value)
  84. {
  85. attributes.add(new Framework::Text(name));
  86. values.add(value);
  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::createValue(
  102. Framework::JSON::JSONObject* zJson) const
  103. {
  104. return new AttributeItemModifier();
  105. }
  106. void AttributeItemModifierFactory::fromJson(
  107. AttributeItemModifier* zResult, Framework::JSON::JSONObject* zJson) const
  108. {
  109. auto attribute = zJson->getFields();
  110. auto value = zJson->getValues();
  111. while (attribute, value)
  112. {
  113. if (!attribute.val().istGleich("type"))
  114. {
  115. zResult->addAttribute(attribute.val(), value);
  116. }
  117. attribute++;
  118. value++;
  119. }
  120. }
  121. void AttributeItemModifierFactory::toJson(
  122. AttributeItemModifier* zObject, Framework::JSON::JSONObject* zResult) const
  123. {
  124. auto attribute = zObject->getAttributes().begin();
  125. auto value = zObject->getValues().begin();
  126. while (attribute && value)
  127. {
  128. zResult->addValue(attribute->getText(),
  129. dynamic_cast<Framework::JSON::JSONObject*>(value->getThis()));
  130. attribute++;
  131. value++;
  132. }
  133. }
  134. JSONObjectValidationBuilder* AttributeItemModifierFactory::addToValidator(
  135. JSONObjectValidationBuilder* builder) const
  136. {
  137. return builder->allowAdditionalAttriutes();
  138. }
  139. Framework::Text AttributeItemModifierFactory::getTypeToken() const
  140. {
  141. return "attributes";
  142. }
  143. CombinedItemModifier::CombinedItemModifier()
  144. : ItemModifier()
  145. {}
  146. void CombinedItemModifier::applyOn(Item* zItem)
  147. {
  148. for (ItemModifier* modifier : modifiers)
  149. {
  150. modifier->applyOn(zItem);
  151. }
  152. }
  153. void CombinedItemModifier::addModifier(ItemModifier* zModifier)
  154. {
  155. modifiers.add(zModifier);
  156. }
  157. const Framework::RCArray<ItemModifier>&
  158. CombinedItemModifier::getModifiers() const
  159. {
  160. return modifiers;
  161. }
  162. CombinedItemModifierFactory::CombinedItemModifierFactory()
  163. : SubTypeFactory()
  164. {}
  165. CombinedItemModifier* CombinedItemModifierFactory::createValue(
  166. Framework::JSON::JSONObject* zJson) const
  167. {
  168. return new CombinedItemModifier();
  169. }
  170. void CombinedItemModifierFactory::fromJson(
  171. CombinedItemModifier* zResult, Framework::JSON::JSONObject* zJson) const
  172. {
  173. for (Framework::JSON::JSONValue* value :
  174. *zJson->zValue("modifiers")->asArray())
  175. {
  176. zResult->addModifier(
  177. Game::INSTANCE->zTypeRegistry()->fromJson<ItemModifier>(value));
  178. }
  179. }
  180. void CombinedItemModifierFactory::toJson(
  181. CombinedItemModifier* zObject, Framework::JSON::JSONObject* zResult) const
  182. {
  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. zResult->addValue("modifiers", modifiers);
  189. }
  190. JSONObjectValidationBuilder* CombinedItemModifierFactory::addToValidator(
  191. JSONObjectValidationBuilder* builder) const
  192. {
  193. return builder->withRequiredArray("modifiers")
  194. ->addAcceptedTypeInArray(
  195. Game::INSTANCE->zTypeRegistry()->getValidator<ItemModifier>())
  196. ->finishArray();
  197. }
  198. Framework::Text CombinedItemModifierFactory::getTypeToken() const
  199. {
  200. return "combined";
  201. }