123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- #include "QuestReward.h"
- #include <Fenster.h>
- #include "Entity.h"
- #include "Game.h"
- #include "Player.h"
- QuestReward::QuestReward(Framework::Text rewardId)
- : ReferenceCounter(),
- rewardId(rewardId)
- {}
- bool QuestReward::validateSettings(
- Framework::XML::Element* zParent, QuestStorage* zStorage)
- {
- return true;
- }
- void QuestReward::api(Framework::StreamReader* message,
- Framework::XML::Element* zParent,
- QuestStorage* zStorage)
- {}
- 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(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()
- : SubTypeFactory()
- {}
- 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";
- }
|