#include "Item.h" #include #include "Block.h" #include "Game.h" Item::Item(int itemTypeId, Framework::Text name) : ReferenceCounter(), itemTypeId(itemTypeId), blockTypeId(0), hp(1), maxHp(1), durability(1), maxDurability(1), eatable(0), placeable(0), equippable(0), solid(1), usable(0), 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 foodEffect, std::function destroysItemTest) { this->foodEffect = foodEffect; this->foodEffectDestroysItemTest = destroysItemTest; } const ItemType* Item::zItemType() const { return Game::INSTANCE->zItemType(itemTypeId); } int Item::getTypeId() const { return itemTypeId; } const BlockType* Item::zPlacedBlockType() const { return Game::INSTANCE->zBlockType(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; } void Item::setMaxDurability(float maxDurability) { if (maxDurability != this->maxDurability) { float durabilityPercentage = durability / this->maxDurability; this->maxDurability = maxDurability; durability = maxDurability * durabilityPercentage; } } float Item::getMaxDurability() const { return maxDurability; } int Item::getMaxStackSize() const { return zItemType()->getMaxStackSize(); } 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 && name.istGleich(zItem->name); } bool Item::canBePlacedAt(int dimensionId, Framework::Vec3 worldPos) const { auto b = Game::INSTANCE->zBlockAt(worldPos, dimensionId, 0); return (b.isA() && (b.getA()->zBlockType()->getId() == BlockTypeEnum::AIR || b.getA()->zBlockType()->isFluid())) || (b.isB() && b.getB() == BlockTypeEnum::AIR); } void Item::onPlaced() { hp = 0; } Framework::Text Item::getTooltipUIML() const { return Framework::Text("") + name + ""; } 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); } ItemJsonType::ItemJsonType() : TypeFactory() {} Item* ItemJsonType::createValue(Framework::JSON::JSONObject* zJson) const { const ItemType* type = ItemType::zByName( zJson->asObject()->zValue("type")->asString()->getString()); return type->createItem(); } void ItemJsonType::fromJson( Item* zResult, Framework::JSON::JSONObject* zJson) const { const ItemType* type = ItemType::zByName( zJson->asObject()->zValue("type")->asString()->getString()); for (auto attribute = zJson->asObject()->getFields(); attribute; attribute++) { if (attribute.val().istGleich("type")) continue; type->setItemAttribute( zResult, attribute, zJson->asObject()->zValue(attribute)); } } void ItemJsonType::toJson( Item* zObject, Framework::JSON::JSONObject* zResult) const { zResult->addValue("type", new Framework::JSON::JSONString(zObject->zItemType()->getName())); zObject->zItemType()->addItemAttributes(zObject, zResult); } JSONObjectValidationBuilder* ItemJsonType::addToValidator( JSONObjectValidationBuilder* builder) const { Framework::RCArray itemTypes; for (int index = 0; index < Game::INSTANCE->getItemTypeCount(); index++) { if (Game::INSTANCE->zItemType(index)) { itemTypes.add(new Framework::Text( Game::INSTANCE->zItemType(index)->getName())); } } return builder->withRequiredString("type") ->whichIsOneOf(itemTypes) ->finishString() ->allowAdditionalAttriutes(); }