BasicItems.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. #include "BasicItems.h"
  2. #include "Entity.h"
  3. #include "Game.h"
  4. #include "Item.h"
  5. #include "ModelInfo.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. BasicItemType* BasicItemTypeFactory::fromJson(
  125. Framework::JSON::JSONObject* zJson) const
  126. {
  127. BasicItemType* result = ItemTypeFactoryBase::fromJson(zJson);
  128. result->setItemName(
  129. Framework::Text(zJson->zValue("itemName")->asString()->getString()));
  130. result->setHp((float)zJson->zValue("hp")->asNumber()->getNumber());
  131. result->setDurability(
  132. (float)zJson->zValue("durability")->asNumber()->getNumber());
  133. result->setSolid(zJson->zValue("solid")->asBool()->getBool());
  134. result->setHungerRecoveryPerHp(
  135. (float)zJson->zValue("hungerRecoveryPerHp")->asNumber()->getNumber());
  136. result->setThirstRecoveryPerHp(
  137. (float)zJson->zValue("thirstRecoveryPerHp")->asNumber()->getNumber());
  138. return result;
  139. }
  140. Framework::JSON::JSONObject* BasicItemTypeFactory::toJsonObject(
  141. BasicItemType* zObject) const
  142. {
  143. Framework::JSON::JSONObject* result
  144. = ItemTypeFactoryBase::toJsonObject(zObject);
  145. result->addValue(
  146. "itemName", new Framework::JSON::JSONString(zObject->getItemName()));
  147. result->addValue("hp", new Framework::JSON::JSONNumber(zObject->getHp()));
  148. result->addValue("durability",
  149. new Framework::JSON::JSONNumber(zObject->getDurability()));
  150. result->addValue(
  151. "solid", new Framework::JSON::JSONBool(zObject->isSolid()));
  152. result->addValue("hungerRecoveryPerHp",
  153. new Framework::JSON::JSONNumber(zObject->getHungerRecoveryPerHp()));
  154. result->addValue("thirstRecoveryPerHp",
  155. new Framework::JSON::JSONNumber(zObject->getThirstRecoveryPerHp()));
  156. return result;
  157. }
  158. JSONObjectValidationBuilder* BasicItemTypeFactory::addToValidator(
  159. JSONObjectValidationBuilder* builder) const
  160. {
  161. return ItemTypeFactoryBase::addToValidator(
  162. builder->withRequiredString("itemName")
  163. ->finishString()
  164. ->withRequiredNumber("hp")
  165. ->whichIsGreaterThen(0.0)
  166. ->withDefault(1.0)
  167. ->finishNumber()
  168. ->withRequiredNumber("durability")
  169. ->whichIsGreaterThen(0.0)
  170. ->withDefault(1.0)
  171. ->finishNumber()
  172. ->withRequiredBool("solid")
  173. ->withDefault(true)
  174. ->finishBool()
  175. ->withRequiredNumber("hungerRecoveryPerHp")
  176. ->whichIsGreaterOrEqual(0.0)
  177. ->withDefault(0.0)
  178. ->finishNumber()
  179. ->withRequiredNumber("thirstRecoveryPerHp")
  180. ->whichIsGreaterOrEqual(0.0)
  181. ->withDefault(0.0)
  182. ->finishNumber());
  183. }
  184. const char* BasicItemTypeFactory::getTypeToken() const
  185. {
  186. return "basic";
  187. }