BasicItems.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "BasicItems.h"
  2. #include "Game.h"
  3. #include "ModelInfo.h"
  4. BasicItemType::BasicItemType(Framework::Text name,
  5. ModelInfo* model,
  6. Framework::Text itemName,
  7. float hp,
  8. float durability,
  9. int maxStack,
  10. bool solid,
  11. float hungerRecoveryPerHp,
  12. float thirstRecoveryPerHp)
  13. : ItemType(name, model, maxStack),
  14. itemName(itemName),
  15. hp(hp),
  16. durability(durability),
  17. solid(solid),
  18. hungerRecoveryPerHp(hungerRecoveryPerHp),
  19. thirstRecoveryPerHp(thirstRecoveryPerHp)
  20. {}
  21. Item* BasicItemType::createItem() const
  22. {
  23. Item* result = createBasicItem(getId(),
  24. itemName,
  25. hp,
  26. hp,
  27. durability,
  28. durability,
  29. hungerRecoveryPerHp > 0 || thirstRecoveryPerHp > 0,
  30. 0,
  31. 0,
  32. solid,
  33. 0);
  34. if (hungerRecoveryPerHp > 0 || thirstRecoveryPerHp > 0)
  35. {
  36. result->setFoodEffect(
  37. [this](Item* zItem, Entity* zEntity) {
  38. float added = zItem->getHp();
  39. if (zEntity->getHunger() + added * hungerRecoveryPerHp
  40. > zEntity->getMaxHunger()
  41. && zEntity->getThirst() + added * thirstRecoveryPerHp
  42. > zEntity->getMaxThirst())
  43. {
  44. added = MAX((zEntity->getMaxHunger() - zEntity->getHunger())
  45. / hungerRecoveryPerHp,
  46. (zEntity->getMaxThirst() - zEntity->getThirst())
  47. / thirstRecoveryPerHp);
  48. }
  49. zEntity->setHunger(
  50. zEntity->getHunger() + added * hungerRecoveryPerHp);
  51. zEntity->setThirst(
  52. zEntity->getThirst() + added * thirstRecoveryPerHp);
  53. zItem->setHp(zItem->getHp() - added);
  54. return added != 0.f;
  55. },
  56. [this](const Item* zItem, Entity* zEntity) {
  57. float addable = zItem->getHp();
  58. if (zEntity->getHunger() + addable * hungerRecoveryPerHp
  59. > zEntity->getMaxHunger()
  60. && zEntity->getThirst() + addable * thirstRecoveryPerHp
  61. > zEntity->getMaxThirst())
  62. {
  63. addable
  64. = MAX((zEntity->getMaxHunger() - zEntity->getHunger())
  65. / hungerRecoveryPerHp,
  66. (zEntity->getMaxThirst() - zEntity->getThirst())
  67. / thirstRecoveryPerHp);
  68. }
  69. return addable >= zItem->getHp();
  70. });
  71. }
  72. return result;
  73. }
  74. Framework::Text BasicItemType::getItemName() const
  75. {
  76. return itemName;
  77. }
  78. float BasicItemType::getHp() const
  79. {
  80. return hp;
  81. }
  82. float BasicItemType::getDurability() const
  83. {
  84. return durability;
  85. }
  86. bool BasicItemType::isSolid() const
  87. {
  88. return solid;
  89. }
  90. float BasicItemType::getHungerRecoveryPerHp() const
  91. {
  92. return hungerRecoveryPerHp;
  93. }
  94. float BasicItemType::getThirstRecoveryPerHp() const
  95. {
  96. return thirstRecoveryPerHp;
  97. }
  98. BasicItemTypeFactory::BasicItemTypeFactory()
  99. : SubTypeFactory()
  100. {}
  101. BasicItemType* BasicItemTypeFactory::fromJson(
  102. Framework::JSON::JSONObject* zJson) const
  103. {
  104. return new BasicItemType(zJson->zValue("name")->asString()->getString(),
  105. Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  106. zJson->zValue("model")),
  107. zJson->zValue("itemName")->asString()->getString(),
  108. (float)zJson->zValue("hp")->asNumber()->getNumber(),
  109. (float)zJson->zValue("durability")->asNumber()->getNumber(),
  110. (int)zJson->zValue("maxStack")->asNumber()->getNumber(),
  111. zJson->zValue("solid")->asBool()->getBool(),
  112. (float)zJson->zValue("hungerRecoveryPerHp")->asNumber()->getNumber(),
  113. (float)zJson->zValue("thirstRecoveryPerHp")->asNumber()->getNumber());
  114. }
  115. Framework::JSON::JSONObject* BasicItemTypeFactory::toJson(
  116. BasicItemType* zObject) const
  117. {
  118. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  119. result->addValue(
  120. "name", new Framework::JSON::JSONString(zObject->getName()));
  121. result->addValue(
  122. "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
  123. result->addValue(
  124. "itemName", new Framework::JSON::JSONString(zObject->getItemName()));
  125. result->addValue("hp", new Framework::JSON::JSONNumber(zObject->getHp()));
  126. result->addValue("durability",
  127. new Framework::JSON::JSONNumber(zObject->getDurability()));
  128. result->addValue(
  129. "maxStack", new Framework::JSON::JSONNumber(zObject->getMaxStackSize()));
  130. result->addValue(
  131. "solid", new Framework::JSON::JSONBool(zObject->isSolid()));
  132. result->addValue("hungerRecoveryPerHp",
  133. new Framework::JSON::JSONNumber(zObject->getHungerRecoveryPerHp()));
  134. result->addValue("thirstRecoveryPerHp",
  135. new Framework::JSON::JSONNumber(zObject->getThirstRecoveryPerHp()));
  136. return result;
  137. }
  138. Framework::JSON::Validator::JSONValidator* BasicItemTypeFactory::getValidator(
  139. Framework::JSON::Validator::ObjectValidationBuilder<
  140. Framework::JSON::Validator::JSONValidator>* builder) const
  141. {
  142. return builder->withRequiredString("name")
  143. ->finishString()
  144. ->withRequiredAttribute(
  145. "model", Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
  146. ->withRequiredString("itemName")
  147. ->finishString()
  148. ->withRequiredNumber("hp")
  149. ->whichIsGreaterThen(0.0)
  150. ->finishNumber()
  151. ->withRequiredNumber("durability")
  152. ->whichIsGreaterThen(0.0)
  153. ->finishNumber()
  154. ->withRequiredNumber("maxStack")
  155. ->whichIsGreaterThen(0.0)
  156. ->withDefault(50.0)
  157. ->finishNumber()
  158. ->withRequiredBool("solid")
  159. ->withDefault(true)
  160. ->finishBool()
  161. ->withRequiredNumber("hungerRecoveryPerHp")
  162. ->whichIsGreaterOrEqual(0.0)
  163. ->withDefault(0.0)
  164. ->finishNumber()
  165. ->withRequiredNumber("thirstRecoveryPerHp")
  166. ->whichIsGreaterOrEqual(0.0)
  167. ->withDefault(0.0)
  168. ->finishNumber()
  169. ->finishObject();
  170. }
  171. Framework::Text BasicItemTypeFactory::getTypeToken() const
  172. {
  173. return "basic";
  174. }