QuestReward.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. QuestRewardGiveItemsType::QuestRewardGiveItemsType()
  84. : SubTypeFactory<QuestReward, QuestRewardGiveItems>()
  85. {}
  86. QuestRewardGiveItems* QuestRewardGiveItemsType::fromJson(
  87. Framework::JSON::JSONObject* zJson) const
  88. {
  89. QuestRewardGiveItems* result = new QuestRewardGiveItems(
  90. zJson->zValue("rewardId")->asString()->getString());
  91. Framework::JSON::JSONArray* itemsJson = zJson->zValue("items")->asArray();
  92. for (Framework::JSON::JSONValue* itemJson : *itemsJson)
  93. {
  94. result->items.add(
  95. Game::INSTANCE->zTypeRegistry()->fromJson<ItemStackInfo>(itemJson));
  96. }
  97. return result;
  98. }
  99. Framework::JSON::JSONObject* QuestRewardGiveItemsType::toJson(
  100. QuestRewardGiveItems* zObject) const
  101. {
  102. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  103. result->addValue(
  104. "rewardId", new Framework::JSON::JSONString(zObject->getRewardId()));
  105. Framework::JSON::JSONArray* itemsJson = new Framework::JSON::JSONArray();
  106. for (ItemStackInfo* item : zObject->items)
  107. {
  108. itemsJson->addValue(Game::INSTANCE->zTypeRegistry()->toJson(item));
  109. }
  110. result->addValue("items", itemsJson);
  111. return result;
  112. }
  113. Framework::JSON::Validator::JSONValidator*
  114. QuestRewardGiveItemsType::getValidator(
  115. Framework::JSON::Validator::ObjectValidationBuilder<
  116. Framework::JSON::Validator::JSONValidator>* builder) const
  117. {
  118. return builder->withRequiredString("rewardId")
  119. ->finishString()
  120. ->withRequiredArray("items")
  121. ->addAcceptedTypeInArray(
  122. Game::INSTANCE->zTypeRegistry()->getValidator<ItemStackInfo>())
  123. ->finishArray()
  124. ->finishObject();
  125. }
  126. Framework::Text QuestRewardGiveItemsType::getTypeToken() const
  127. {
  128. return "give_items";
  129. }