#include "BasicItems.h" #include "Game.h" #include "ModelInfo.h" BasicItemType::BasicItemType(Framework::Text name, ModelInfo* model, Framework::Text itemName, float hp, float durability, int maxStack, bool solid, float hungerRecoveryPerHp, float thirstRecoveryPerHp) : ItemType(name, model, maxStack), itemName(itemName), hp(hp), durability(durability), solid(solid), hungerRecoveryPerHp(hungerRecoveryPerHp), thirstRecoveryPerHp(thirstRecoveryPerHp) {} Item* BasicItemType::createItem() const { Item* result = createBasicItem(getId(), itemName, hp, hp, durability, durability, hungerRecoveryPerHp > 0 || thirstRecoveryPerHp > 0, 0, 0, solid, 0); if (hungerRecoveryPerHp > 0 || thirstRecoveryPerHp > 0) { result->setFoodEffect( [this](Item* zItem, Entity* zEntity) { float added = zItem->getHp(); if (zEntity->getHunger() + added * hungerRecoveryPerHp > zEntity->getMaxHunger() && zEntity->getThirst() + added * thirstRecoveryPerHp > zEntity->getMaxThirst()) { added = MAX((zEntity->getMaxHunger() - zEntity->getHunger()) / hungerRecoveryPerHp, (zEntity->getMaxThirst() - zEntity->getThirst()) / thirstRecoveryPerHp); } zEntity->setHunger( zEntity->getHunger() + added * hungerRecoveryPerHp); zEntity->setThirst( zEntity->getThirst() + added * thirstRecoveryPerHp); zItem->setHp(zItem->getHp() - added); return added != 0.f; }, [this](const Item* zItem, Entity* zEntity) { float addable = zItem->getHp(); if (zEntity->getHunger() + addable * hungerRecoveryPerHp > zEntity->getMaxHunger() && zEntity->getThirst() + addable * thirstRecoveryPerHp > zEntity->getMaxThirst()) { addable = MAX((zEntity->getMaxHunger() - zEntity->getHunger()) / hungerRecoveryPerHp, (zEntity->getMaxThirst() - zEntity->getThirst()) / thirstRecoveryPerHp); } return addable >= zItem->getHp(); }); } return result; } Framework::Text BasicItemType::getItemName() const { return itemName; } float BasicItemType::getHp() const { return hp; } float BasicItemType::getDurability() const { return durability; } bool BasicItemType::isSolid() const { return solid; } float BasicItemType::getHungerRecoveryPerHp() const { return hungerRecoveryPerHp; } float BasicItemType::getThirstRecoveryPerHp() const { return thirstRecoveryPerHp; } BasicItemTypeFactory::BasicItemTypeFactory() : SubTypeFactory() {} BasicItemType* BasicItemTypeFactory::fromJson( Framework::JSON::JSONObject* zJson) const { return new BasicItemType(zJson->zValue("name")->asString()->getString(), Game::INSTANCE->zTypeRegistry()->fromJson( zJson->zValue("model")), zJson->zValue("itemName")->asString()->getString(), (float)zJson->zValue("hp")->asNumber()->getNumber(), (float)zJson->zValue("durability")->asNumber()->getNumber(), (int)zJson->zValue("maxStack")->asNumber()->getNumber(), zJson->zValue("solid")->asBool()->getBool(), (float)zJson->zValue("hungerRecoveryPerHp")->asNumber()->getNumber(), (float)zJson->zValue("thirstRecoveryPerHp")->asNumber()->getNumber()); } Framework::JSON::JSONObject* BasicItemTypeFactory::toJson( BasicItemType* zObject) const { Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject(); result->addValue( "name", new Framework::JSON::JSONString(zObject->getName())); result->addValue( "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel())); result->addValue( "itemName", new Framework::JSON::JSONString(zObject->getItemName())); result->addValue("hp", new Framework::JSON::JSONNumber(zObject->getHp())); result->addValue("durability", new Framework::JSON::JSONNumber(zObject->getDurability())); result->addValue( "maxStack", new Framework::JSON::JSONNumber(zObject->getMaxStackSize())); result->addValue( "solid", new Framework::JSON::JSONBool(zObject->isSolid())); result->addValue("hungerRecoveryPerHp", new Framework::JSON::JSONNumber(zObject->getHungerRecoveryPerHp())); result->addValue("thirstRecoveryPerHp", new Framework::JSON::JSONNumber(zObject->getThirstRecoveryPerHp())); return result; } Framework::JSON::Validator::JSONValidator* BasicItemTypeFactory::getValidator( Framework::JSON::Validator::ObjectValidationBuilder< Framework::JSON::Validator::JSONValidator>* builder) const { return builder->withRequiredString("name") ->finishString() ->withRequiredAttribute( "model", Game::INSTANCE->zTypeRegistry()->getValidator()) ->withRequiredString("itemName") ->finishString() ->withRequiredNumber("hp") ->whichIsGreaterThen(0.0) ->finishNumber() ->withRequiredNumber("durability") ->whichIsGreaterThen(0.0) ->finishNumber() ->withRequiredNumber("maxStack") ->whichIsGreaterThen(0.0) ->withDefault(50.0) ->finishNumber() ->withRequiredBool("solid") ->withDefault(true) ->finishBool() ->withRequiredNumber("hungerRecoveryPerHp") ->whichIsGreaterOrEqual(0.0) ->withDefault(0.0) ->finishNumber() ->withRequiredNumber("thirstRecoveryPerHp") ->whichIsGreaterOrEqual(0.0) ->withDefault(0.0) ->finishNumber() ->finishObject(); } Framework::Text BasicItemTypeFactory::getTypeToken() const { return "basic"; }