BasicItems.cpp 6.1 KB

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