QuestReward.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #include "QuestReward.h"
  2. #include "Entity.h"
  3. #include "Game.h"
  4. QuestReward::QuestReward(Framework::Text rewardId)
  5. : ReferenceCounter(),
  6. rewardId(rewardId)
  7. {}
  8. const Framework::Text& QuestReward::getRewardId() const
  9. {
  10. return rewardId;
  11. }
  12. ItemStackInfo::ItemStackInfo(Item* item, int count)
  13. : ReferenceCounter(),
  14. item(item),
  15. count(count)
  16. {}
  17. ItemStackInfo::~ItemStackInfo()
  18. {
  19. item->release();
  20. }
  21. Item* ItemStackInfo::zItem() const
  22. {
  23. return item;
  24. }
  25. int ItemStackInfo::getCount() const
  26. {
  27. return count;
  28. }
  29. ItemStackInfoType::ItemStackInfoType()
  30. : TypeFactory<ItemStackInfo>()
  31. {}
  32. ItemStackInfo* ItemStackInfoType::fromJson(
  33. Framework::JSON::JSONValue* zJson) const
  34. {
  35. return new ItemStackInfo(Game::INSTANCE->zTypeRegistry()->fromJson<Item>(
  36. zJson->asObject()->zValue("item")),
  37. (int)zJson->asObject()->zValue("count")->asNumber()->getNumber());
  38. }
  39. Framework::JSON::JSONValue* ItemStackInfoType::toJson(
  40. ItemStackInfo* zObject) const
  41. {
  42. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  43. result->addValue(
  44. "item", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zItem()));
  45. result->addValue(
  46. "count", new Framework::JSON::JSONNumber((double)zObject->getCount()));
  47. return result;
  48. }
  49. Framework::JSON::Validator::JSONValidator*
  50. ItemStackInfoType::getValidator() const
  51. {
  52. return Framework::JSON::Validator::JSONValidator::buildForObject()
  53. ->withRequiredAttribute(
  54. "item", Game::INSTANCE->zTypeRegistry()->getValidator<Item>())
  55. ->withRequiredNumber("count")
  56. ->withDefault(1.0)
  57. ->whichIsGreaterOrEqual(1.0)
  58. ->finishNumber()
  59. ->finishObject();
  60. }
  61. QuestRewardGiveItems::QuestRewardGiveItems(Framework::Text rewardId)
  62. : QuestReward(rewardId)
  63. {}
  64. void QuestRewardGiveItems::giveReward(Entity* zTargetEntity)
  65. {
  66. for (ItemStackInfo* info : items)
  67. {
  68. ItemStack* stack = new ItemStack(
  69. dynamic_cast<Item*>(info->zItem()->getThis()), info->getCount());
  70. zTargetEntity->unsaveAddItem(stack, Direction::NO_DIRECTION, 0);
  71. if (stack->getSize() > 0)
  72. {
  73. Game::INSTANCE->spawnItem(zTargetEntity->getLocation(),
  74. zTargetEntity->getDimensionId(),
  75. stack);
  76. }
  77. else
  78. {
  79. stack->release();
  80. }
  81. }
  82. }
  83. Framework::Text QuestRewardGiveItems::getRewardUIML()
  84. {
  85. Framework::Text result = "<questReward>";
  86. for (ItemStackInfo* info : items)
  87. {
  88. result.append() << "<itemStack count=\"" << info->getCount()
  89. << "\" itemImage=\""
  90. << info->zItem()->zItemType()->getId() << "\"><text>"
  91. << info->zItem()->getName() << "</text>"
  92. << info->zItem()->getTooltipUIML() << "</itemStack>";
  93. }
  94. result += "</questReward>";
  95. return result;
  96. }
  97. QuestRewardGiveItemsType::QuestRewardGiveItemsType()
  98. : SubTypeFactory<QuestReward, QuestRewardGiveItems>()
  99. {}
  100. QuestRewardGiveItems* QuestRewardGiveItemsType::fromJson(
  101. Framework::JSON::JSONObject* zJson) const
  102. {
  103. QuestRewardGiveItems* result = new QuestRewardGiveItems(
  104. zJson->zValue("rewardId")->asString()->getString());
  105. Framework::JSON::JSONArray* itemsJson = zJson->zValue("items")->asArray();
  106. for (Framework::JSON::JSONValue* itemJson : *itemsJson)
  107. {
  108. result->items.add(
  109. Game::INSTANCE->zTypeRegistry()->fromJson<ItemStackInfo>(itemJson));
  110. }
  111. return result;
  112. }
  113. Framework::JSON::JSONObject* QuestRewardGiveItemsType::toJson(
  114. QuestRewardGiveItems* zObject) const
  115. {
  116. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  117. result->addValue(
  118. "rewardId", new Framework::JSON::JSONString(zObject->getRewardId()));
  119. Framework::JSON::JSONArray* itemsJson = new Framework::JSON::JSONArray();
  120. for (ItemStackInfo* item : zObject->items)
  121. {
  122. itemsJson->addValue(Game::INSTANCE->zTypeRegistry()->toJson(item));
  123. }
  124. result->addValue("items", itemsJson);
  125. return result;
  126. }
  127. Framework::JSON::Validator::JSONValidator*
  128. QuestRewardGiveItemsType::getValidator(
  129. Framework::JSON::Validator::ObjectValidationBuilder<
  130. Framework::JSON::Validator::JSONValidator>* builder) const
  131. {
  132. return builder->withRequiredString("rewardId")
  133. ->finishString()
  134. ->withRequiredArray("items")
  135. ->addAcceptedTypeInArray(
  136. Game::INSTANCE->zTypeRegistry()->getValidator<ItemStackInfo>())
  137. ->finishArray()
  138. ->finishObject();
  139. }
  140. Framework::Text QuestRewardGiveItemsType::getTypeToken() const
  141. {
  142. return "give_items";
  143. }