123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- #include "Item.h"
- #include <Text.h>
- #include "Game.h"
- Item::Item(int itemTypeId, const char* name)
- : ReferenceCounter(),
- itemTypeId(itemTypeId),
- blockTypeId(0),
- hp(1),
- maxHp(1),
- durability(1),
- maxDurability(1),
- eatable(0),
- placeable(0),
- equippable(0),
- solid(1),
- usable(0),
- maxStackSize(50),
- name(name)
- {
- foodEffect = [](Item* i, Entity* e) { return false; };
- foodEffectDestroysItemTest = [](const Item* i, Entity *e) { return false; };
- }
- void Item::setHp(float hp)
- {
- this->hp = hp;
- }
- void Item::setDurability(float durability)
- {
- this->durability = durability;
- }
- void Item::tick() {}
- void Item::setFoodEffect(std::function<bool(Item*, Entity*)> foodEffect,
- std::function<bool(const Item*, Entity*)> destroysItemTest)
- {
- this->foodEffect = foodEffect;
- this->foodEffectDestroysItemTest = destroysItemTest;
- }
- const ItemType* Item::zItemType() const
- {
- return StaticRegistry<ItemType>::INSTANCE.zElement(itemTypeId);
- }
- int Item::getTypeId() const
- {
- return itemTypeId;
- }
- const BlockType* Item::zPlacedBlockType() const
- {
- return StaticRegistry<BlockType>::INSTANCE.zElement(blockTypeId);
- }
- float Item::getHp() const
- {
- return hp;
- }
- float Item::getDurability() const
- {
- return durability;
- }
- bool Item::isUsable() const
- {
- return usable;
- }
- bool Item::isEatable() const
- {
- return eatable;
- }
- bool Item::isPlaceable() const
- {
- return placeable;
- }
- bool Item::isEquippable() const
- {
- return equippable;
- }
- bool Item::isSolid() const
- {
- return solid;
- }
- float Item::getMaxDurability() const
- {
- return maxDurability;
- }
- int Item::getMaxStackSize() const
- {
- return maxStackSize;
- }
- float Item::getMaxHp() const
- {
- return maxHp;
- }
- const Framework::Text& Item::getName() const
- {
- return name;
- }
- bool Item::canBeStackedWith(const Item* zItem) const
- {
- return itemTypeId == zItem->itemTypeId && durability == maxDurability
- && zItem->durability == zItem->durability && maxHp == zItem->maxHp
- && eatable == zItem->eatable && placeable == zItem->placeable
- && equippable == zItem->eatable && solid == zItem->solid
- && usable == zItem->usable && maxStackSize == zItem->maxStackSize
- && name.istGleich(zItem->name);
- }
- bool Item::canBePlacedAt(int dimensionId, Vec3<int> worldPos) const
- {
- auto b = Game::INSTANCE->zBlockAt(worldPos, dimensionId);
- return (b.isA() && b.getA()->zBlockType()->getId() == BlockTypeEnum::AIR)
- || (b.isB() && b.getB() == BlockTypeEnum::AIR);
- }
- void Item::onPlaced()
- {
- hp = 0;
- }
- Framework::Text Item::getTooltipUIML() const
- {
- return Framework::Text("<tip><text width=\"auto\" height=\"auto\">") + name
- + "</text></tip>";
- }
- void Item::applyInventoryEffects(Entity* zTarget)
- {}
- void Item::removeInventoryEffects(Entity* zTarget) {}
- void Item::applyEquippedEffects(Entity* zTarget) {}
- void Item::removeEquippedEffects(Entity* zTarget) {}
- bool Item::applyFoodEffects(Entity* zTarget)
- {
- return foodEffect(this, zTarget);
- }
- bool Item::canApplyFoodEffectsFully(Entity* zTarget) const
- {
- return foodEffectDestroysItemTest(this, zTarget);
- }
- ItomJsonType::ItomJsonType()
- : TypeFactory()
- {}
- Item* ItomJsonType::fromJson(Framework::JSON::JSONValue* zJson) const
- {
- const ItemType* type = ItemType::zByName(
- zJson->asObject()->zValue("type")->asString()->getString());
- Item *result = type->createItem();
- for (auto attribute = zJson->asObject()->getFields(); attribute;
- attribute++)
- {
- if (attribute.val().istGleich("type"))
- continue;
- type->setItemAttribute(
- result, attribute, zJson->asObject()->zValue(attribute));
- }
- return result;
- }
- Framework::JSON::JSONValue* ItomJsonType::toJson(Item* zObject) const {
- Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
- result->addValue("type", new Framework::JSON::JSONString(
- zObject->zItemType()->getName()));
- zObject->zItemType()->addItemAttributes(zObject, result);
- return result;
- }
- Framework::JSON::Validator::JSONValidator* ItomJsonType::getValidator() const {
- Framework::RCArray<Framework::Text> itemTypes;
- for (int index = 0; index < StaticRegistry<ItemType>::INSTANCE.getCount();
- index++)
- {
- if (StaticRegistry<ItemType>::INSTANCE.zElement(index))
- {
- itemTypes.add(new Framework::Text(
- StaticRegistry<ItemType>::INSTANCE.zElement(index)->getName()));
- }
- }
- return Framework::JSON::Validator::JSONValidator::buildForObject()
- ->withRequiredString("type")
- ->whichIsOneOf(itemTypes)
- ->finishString()
- ->allowAdditionalAttriutes()
- ->finishObject();
- }
|