123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- #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,
- Framework::RCArray<Framework::Text> groups)
- : ItemType(name, model, maxStack, groups),
- 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
- {
- Framework::RCArray<Framework::Text> groups;
- for (Framework::JSON::JSONValue* group :
- *zJson->zValue("groupNames")->asArray())
- {
- groups.add(new Framework::Text(group->asString()->getString()));
- }
- return new BasicItemType(zJson->zValue("name")->asString()->getString(),
- Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
- 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(), groups);
- }
- 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()));
- Framework::JSON::JSONArray* groups = new Framework::JSON::JSONArray();
- for (Framework::Text* group : zObject->getGroups())
- {
- groups->addValue(new Framework::JSON::JSONString(group->getText()));
- }
- result->addValue("groupNames", groups);
- 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<ModelInfo>())
- ->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()
- ->withRequiredArray("groupNames")
- ->addAcceptedStringInArray()
- ->finishString()
- ->withDefault(new Framework::JSON::JSONArray())
- ->finishArray()
- ->finishObject();
- }
- Framework::Text BasicItemTypeFactory::getTypeToken() const
- {
- return "basic";
- }
|