123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- #include "BasicItems.h"
- #include "Game.h"
- #include "ModelInfo.h"
- #include "Item.h"
- #include "Entity.h"
- BasicItemType::BasicItemType()
- : ItemType(),
- itemName(),
- hp(1.f),
- durability(1.f),
- solid(true),
- hungerRecoveryPerHp(0.f),
- thirstRecoveryPerHp(0.f)
- {}
- 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;
- }
- void BasicItemType::setItemName(Framework::Text itemName)
- {
- this->itemName = itemName;
- }
- Framework::Text BasicItemType::getItemName() const
- {
- return itemName;
- }
- void BasicItemType::setHp(float hp)
- {
- this->hp = hp;
- }
- float BasicItemType::getHp() const
- {
- return hp;
- }
- void BasicItemType::setDurability(float durability)
- {
- this->durability = durability;
- }
- float BasicItemType::getDurability() const
- {
- return durability;
- }
- void BasicItemType::setSolid(bool solid)
- {
- this->solid = solid;
- }
- bool BasicItemType::isSolid() const
- {
- return solid;
- }
- void BasicItemType::setHungerRecoveryPerHp(float hungerRecoveryPerHp)
- {
- this->hungerRecoveryPerHp = hungerRecoveryPerHp;
- }
- float BasicItemType::getHungerRecoveryPerHp() const
- {
- return hungerRecoveryPerHp;
- }
- void BasicItemType::setThirstRecoveryPerHp(float thirstRecoveryPerHp)
- {
- this->thirstRecoveryPerHp = thirstRecoveryPerHp;
- }
- float BasicItemType::getThirstRecoveryPerHp() const
- {
- return thirstRecoveryPerHp;
- }
- BasicItemTypeFactory::BasicItemTypeFactory()
- : ItemTypeFactoryBase()
- {}
- BasicItemType* BasicItemTypeFactory::createValue(
- Framework::JSON::JSONObject* zJson) const
- {
- return new BasicItemType();
- }
- void BasicItemTypeFactory::fromJson(
- BasicItemType* zResult, Framework::JSON::JSONObject* zJson) const
- {
- zResult->setItemName(
- Framework::Text(zJson->zValue("itemName")->asString()->getString()));
- zResult->setHp((float)zJson->zValue("hp")->asNumber()->getNumber());
- zResult->setDurability(
- (float)zJson->zValue("durability")->asNumber()->getNumber());
- zResult->setSolid(zJson->zValue("solid")->asBool()->getBool());
- zResult->setHungerRecoveryPerHp(
- (float)zJson->zValue("hungerRecoveryPerHp")->asNumber()->getNumber());
- zResult->setThirstRecoveryPerHp(
- (float)zJson->zValue("thirstRecoveryPerHp")->asNumber()->getNumber());
- ItemTypeFactoryBase::fromJson(zResult, zJson);
- }
- void BasicItemTypeFactory::toJson(
- BasicItemType* zObject, Framework::JSON::JSONObject* zResult) const
- {
- zResult->addValue(
- "itemName", new Framework::JSON::JSONString(zObject->getItemName()));
- zResult->addValue("hp", new Framework::JSON::JSONNumber(zObject->getHp()));
- zResult->addValue("durability",
- new Framework::JSON::JSONNumber(zObject->getDurability()));
- zResult->addValue(
- "solid", new Framework::JSON::JSONBool(zObject->isSolid()));
- zResult->addValue("hungerRecoveryPerHp",
- new Framework::JSON::JSONNumber(zObject->getHungerRecoveryPerHp()));
- zResult->addValue("thirstRecoveryPerHp",
- new Framework::JSON::JSONNumber(zObject->getThirstRecoveryPerHp()));
- }
- JSONObjectValidationBuilder* BasicItemTypeFactory::addToValidator(
- JSONObjectValidationBuilder* builder) const
- {
- return ItemTypeFactoryBase::addToValidator(
- builder->withRequiredString("itemName")
- ->finishString()
- ->withRequiredNumber("hp")
- ->whichIsGreaterThen(0.0)
- ->withDefault(1.0)
- ->finishNumber()
- ->withRequiredNumber("durability")
- ->whichIsGreaterThen(0.0)
- ->withDefault(1.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());
- }
- Framework::Text BasicItemTypeFactory::getTypeToken() const
- {
- return "basic";
- }
|