123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- #include "QuestReward.h"
- #include <Fenster.h>
- #include "Entity.h"
- #include "Game.h"
- #include "Player.h"
- #include "Quest.h"
- QuestReward::QuestReward()
- : ReferenceCounter()
- {}
- bool QuestReward::validateSettings(
- Framework::XML::Element* zParent, QuestStorage* zStorage)
- {
- return true;
- }
- void QuestReward::api(Framework::StreamReader* message,
- Framework::XML::Element* zParent,
- QuestStorage* zStorage)
- {}
- void QuestReward::setRewardId(Framework::Text rewardId)
- {
- this->rewardId = rewardId;
- }
- const Framework::Text& QuestReward::getRewardId() const
- {
- return rewardId;
- }
- ItemStackInfo::ItemStackInfo()
- : ReferenceCounter(),
- item(0),
- count(0)
- {}
- ItemStackInfo::~ItemStackInfo()
- {
- if (item) item->release();
- }
- void ItemStackInfo::setItem(Item* item)
- {
- if (this->item) this->item->release();
- this->item = item;
- }
- Item* ItemStackInfo::zItem() const
- {
- return item;
- }
- void ItemStackInfo::setCount(int count)
- {
- this->count = count;
- }
- int ItemStackInfo::getCount() const
- {
- return count;
- }
- ItemStackInfoType::ItemStackInfoType()
- : TypeFactory<ItemStackInfo>()
- {}
- ItemStackInfo* ItemStackInfoType::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- return new ItemStackInfo();
- }
- void ItemStackInfoType::fromJson(
- ItemStackInfo* zObject, Framework::JSON::JSONObject* zJson) const
- {
- zObject->setItem(Game::INSTANCE->zTypeRegistry()->fromJson<Item>(
- zJson->asObject()->zValue("item")));
- zObject->setCount(
- (int)zJson->asObject()->zValue("count")->asNumber()->getNumber());
- }
- void ItemStackInfoType::toJson(
- ItemStackInfo* zObject, Framework::JSON::JSONObject* zResult) const
- {
- zResult->addValue(
- "item", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zItem()));
- zResult->addValue(
- "count", new Framework::JSON::JSONNumber((double)zObject->getCount()));
- }
- JSONObjectValidationBuilder* ItemStackInfoType::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return builder
- ->withRequiredAttribute(
- "item", Game::INSTANCE->zTypeRegistry()->getValidator<Item>())
- ->withRequiredNumber("count")
- ->withDefault(1.0)
- ->whichIsGreaterOrEqual(1.0)
- ->finishNumber();
- }
- QuestRewardGiveItems::QuestRewardGiveItems()
- : QuestReward()
- {}
- void QuestRewardGiveItems::giveReward(Framework::XML::Element* zParent,
- QuestStorage* zStorage,
- Entity* zTargetEntity)
- {
- Player* p = dynamic_cast<Player*>(zTargetEntity);
- if (p)
- {
- zStorage->putValue(
- (Framework::Text("reward_") += rewardId) += "_given_to",
- new Framework::JSON::JSONString(p->getName()));
- }
- 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();
- }
- }
- }
- void QuestRewardGiveItems::addRewardUIML(Framework::XML::Element* zParent,
- QuestStorage* zStorage,
- Framework::Text onClickPrefix)
- {
- Framework::XML::Element* container
- = new Framework::XML::Element("<frame width=\"100%\" height=\"auto\" "
- "display=\"column\" gap=\"10\"/>");
- container->setAttribute("style",
- Framework::Text() += (Framework::Fenster::Style::Sichtbar
- | Framework::Fenster::Style::Erlaubt));
- container->setAttribute("id", Framework::Text("reward_") += rewardId);
- container->addChild(new Framework::XML::Element(
- "<text width=\"auto\" height=\"auto\">Item Reward:</text>"));
- for (ItemStackInfo* info : items)
- {
- auto stack = new Framework::XML::Element(
- "<itemStack width=\"50\" height=\"50\"/>");
- stack->setAttribute(
- "id", (Framework::Text("reward_") += rewardId) += "_item_stack");
- stack->setAttribute("count", info->getCount());
- stack->setAttribute("type", info->zItem()->getTypeId());
- stack->addChild(
- new Framework::XML::Element(info->zItem()->getTooltipUIML()));
- auto text = new Framework::XML::Element(
- "<text margin-left=\"10\" width=\"auto\" height=\"auto\"/>");
- text->setAttribute("id",
- (Framework::Text("reward_") += rewardId) += "_item_description");
- text->setText((Framework::Text(info->getCount()) += " ")
- += info->zItem()->getName());
- text->setAttribute("align-left", stack->getAttributeValue("id"));
- text->setAttribute("align-y", stack->getAttributeValue("id"));
- container->addChild(stack);
- container->addChild(text);
- if (zStorage->containsKey(
- (Framework::Text("reward_") += rewardId) += "_given_to"))
- {
- auto givenTo = new Framework::XML::Element(
- "<text width=\"auto\" height=\"auto\" margin-top=\"10\" "
- "text-color=\"0xFF00FF00\"/>");
- auto name = zStorage
- ->zValue((Framework::Text("reward_") += rewardId)
- += "_given_to")
- ->asString();
- givenTo->setText(
- (Framework::Text("Given to: ") += name->getString()));
- givenTo->setAttribute("align-top", text->getAttributeValue("id"));
- givenTo->setAttribute("align-x", text->getAttributeValue("id"));
- container->addChild(givenTo);
- }
- }
- zParent->addChild(container);
- }
- QuestRewardGiveItemsType::QuestRewardGiveItemsType()
- : QuestRewardFactoryBase()
- {}
- QuestRewardGiveItems* QuestRewardGiveItemsType::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- return new QuestRewardGiveItems();
- }
- void QuestRewardGiveItemsType::fromJson(
- QuestRewardGiveItems* zResult, Framework::JSON::JSONObject* zJson) const
- {
- Framework::JSON::JSONArray* itemsJson = zJson->zValue("items")->asArray();
- for (Framework::JSON::JSONValue* itemJson : *itemsJson)
- {
- zResult->items.add(
- Game::INSTANCE->zTypeRegistry()->fromJson<ItemStackInfo>(itemJson));
- }
- QuestRewardFactoryBase::fromJson(zResult, zJson);
- }
- void QuestRewardGiveItemsType::toJson(
- QuestRewardGiveItems* zObject, Framework::JSON::JSONObject* zResult) const
- {
- zResult->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));
- }
- zResult->addValue("items", itemsJson);
- QuestRewardFactoryBase::toJson(zObject, zResult);
- }
- JSONObjectValidationBuilder* QuestRewardGiveItemsType::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return QuestRewardFactoryBase::addToValidator(
- builder->withRequiredArray("items")
- ->addAcceptedTypeInArray(
- Game::INSTANCE->zTypeRegistry()->getValidator<ItemStackInfo>())
- ->finishArray());
- }
- Framework::Text QuestRewardGiveItemsType::getTypeToken() const
- {
- return "give_items";
- }
|