BasicItems.cpp 6.9 KB

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