123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- #include "QuestReward.h"
- #include "Entity.h"
- #include "Game.h"
- QuestReward::QuestReward(Framework::Text rewardId)
- : ReferenceCounter(),
- rewardId(rewardId)
- {}
- const Framework::Text& QuestReward::getRewardId() const
- {
- return rewardId;
- }
- ItemStackInfo::ItemStackInfo(Item* item, int count)
- : ReferenceCounter(),
- item(item),
- count(count)
- {}
- ItemStackInfo::~ItemStackInfo()
- {
- item->release();
- }
- Item* ItemStackInfo::zItem() const
- {
- return item;
- }
- int ItemStackInfo::getCount() const
- {
- return count;
- }
- ItemStackInfoType::ItemStackInfoType()
- : TypeFactory<ItemStackInfo>()
- {}
- ItemStackInfo* ItemStackInfoType::fromJson(
- Framework::JSON::JSONValue* zJson) const
- {
- return new ItemStackInfo(Game::INSTANCE->zTypeRegistry()->fromJson<Item>(
- zJson->asObject()->zValue("item")),
- (int)zJson->asObject()->zValue("count")->asNumber()->getNumber());
- }
- Framework::JSON::JSONValue* ItemStackInfoType::toJson(
- ItemStackInfo* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue(
- "item", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zItem()));
- result->addValue(
- "count", new Framework::JSON::JSONNumber((double)zObject->getCount()));
- return result;
- }
- Framework::JSON::Validator::JSONValidator*
- ItemStackInfoType::getValidator() const
- {
- return Framework::JSON::Validator::JSONValidator::buildForObject()
- ->withRequiredAttribute(
- "item", Game::INSTANCE->zTypeRegistry()->getValidator<Item>())
- ->withRequiredNumber("count")
- ->withDefault(1.0)
- ->whichIsGreaterOrEqual(1.0)
- ->finishNumber()
- ->finishObject();
- }
- QuestRewardGiveItems::QuestRewardGiveItems(Framework::Text rewardId)
- : QuestReward(rewardId)
- {}
- void QuestRewardGiveItems::giveReward(Entity* zTargetEntity)
- {
- for (ItemStackInfo* info : items)
- {
- ItemStack* stack = new ItemStack(
- dynamic_cast<Item*>(info->zItem()->getThis()), info->getCount());
- zTargetEntity->unsaveAddItem(stack, Direction::NO_DIRECTION, 0);
- if (stack->getSize() > 0)
- {
- Game::INSTANCE->spawnItem(zTargetEntity->getLocation(),
- zTargetEntity->getDimensionId(),
- stack);
- }
- else
- {
- stack->release();
- }
- }
- }
- Framework::Text QuestRewardGiveItems::getRewardUIML()
- {
- Framework::Text result = "<questReward>";
- for (ItemStackInfo* info : items)
- {
- result.append() << "<itemStack count=\"" << info->getCount()
- << "\" itemImage=\""
- << info->zItem()->zItemType()->getId() << "\"><text>"
- << info->zItem()->getName() << "</text>"
- << info->zItem()->getTooltipUIML() << "</itemStack>";
- }
- result += "</questReward>";
- return result;
- }
- QuestRewardGiveItemsType::QuestRewardGiveItemsType()
- : SubTypeFactory<QuestReward, QuestRewardGiveItems>()
- {}
- QuestRewardGiveItems* QuestRewardGiveItemsType::fromJson(
- Framework::JSON::JSONObject* zJson) const
- {
- QuestRewardGiveItems* result = new QuestRewardGiveItems(
- zJson->zValue("rewardId")->asString()->getString());
- Framework::JSON::JSONArray* itemsJson = zJson->zValue("items")->asArray();
- for (Framework::JSON::JSONValue* itemJson : *itemsJson)
- {
- result->items.add(
- Game::INSTANCE->zTypeRegistry()->fromJson<ItemStackInfo>(itemJson));
- }
- return result;
- }
- Framework::JSON::JSONObject* QuestRewardGiveItemsType::toJson(
- QuestRewardGiveItems* zObject) const
- {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue(
- "rewardId", new Framework::JSON::JSONString(zObject->getRewardId()));
- Framework::JSON::JSONArray* itemsJson = new Framework::JSON::JSONArray();
- for (ItemStackInfo* item : zObject->items)
- {
- itemsJson->addValue(Game::INSTANCE->zTypeRegistry()->toJson(item));
- }
- result->addValue("items", itemsJson);
- return result;
- }
- Framework::JSON::Validator::JSONValidator*
- QuestRewardGiveItemsType::getValidator(
- Framework::JSON::Validator::ObjectValidationBuilder<
- Framework::JSON::Validator::JSONValidator>* builder) const
- {
- return builder->withRequiredString("rewardId")
- ->finishString()
- ->withRequiredArray("items")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()->getValidator<ItemStackInfo>())
- ->finishArray()
- ->finishObject();
- }
- Framework::Text QuestRewardGiveItemsType::getTypeToken() const
- {
- return "give_items";
- }
|