BasicItems.cpp 6.0 KB

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