ItemModifier.cpp 5.5 KB

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