Item.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. #include "Item.h"
  2. #include <Text.h>
  3. #include "Block.h"
  4. #include "Game.h"
  5. Item::Item(int itemTypeId, Framework::Text name)
  6. : ReferenceCounter(),
  7. itemTypeId(itemTypeId),
  8. blockTypeId(0),
  9. hp(1),
  10. maxHp(1),
  11. durability(1),
  12. maxDurability(1),
  13. eatable(0),
  14. placeable(0),
  15. equippable(0),
  16. solid(1),
  17. usable(0),
  18. name(name)
  19. {
  20. foodEffect = [](Item* i, Entity* e) { return false; };
  21. foodEffectDestroysItemTest = [](const Item* i, Entity* e) { return false; };
  22. }
  23. void Item::setHp(float hp)
  24. {
  25. this->hp = hp;
  26. }
  27. void Item::setDurability(float durability)
  28. {
  29. this->durability = durability;
  30. }
  31. void Item::tick() {}
  32. void Item::setFoodEffect(std::function<bool(Item*, Entity*)> foodEffect,
  33. std::function<bool(const Item*, Entity*)> destroysItemTest)
  34. {
  35. this->foodEffect = foodEffect;
  36. this->foodEffectDestroysItemTest = destroysItemTest;
  37. }
  38. const ItemType* Item::zItemType() const
  39. {
  40. return Game::INSTANCE->zItemType(itemTypeId);
  41. }
  42. int Item::getTypeId() const
  43. {
  44. return itemTypeId;
  45. }
  46. const BlockType* Item::zPlacedBlockType() const
  47. {
  48. return Game::INSTANCE->zBlockType(blockTypeId);
  49. }
  50. float Item::getHp() const
  51. {
  52. return hp;
  53. }
  54. float Item::getDurability() const
  55. {
  56. return durability;
  57. }
  58. bool Item::isUsable() const
  59. {
  60. return usable;
  61. }
  62. bool Item::isEatable() const
  63. {
  64. return eatable;
  65. }
  66. bool Item::isPlaceable() const
  67. {
  68. return placeable;
  69. }
  70. bool Item::isEquippable() const
  71. {
  72. return equippable;
  73. }
  74. bool Item::isSolid() const
  75. {
  76. return solid;
  77. }
  78. void Item::setMaxDurability(float maxDurability)
  79. {
  80. if (maxDurability != this->maxDurability)
  81. {
  82. float durabilityPercentage = durability / this->maxDurability;
  83. this->maxDurability = maxDurability;
  84. durability = maxDurability * durabilityPercentage;
  85. }
  86. }
  87. float Item::getMaxDurability() const
  88. {
  89. return maxDurability;
  90. }
  91. int Item::getMaxStackSize() const
  92. {
  93. return zItemType()->getMaxStackSize();
  94. }
  95. float Item::getMaxHp() const
  96. {
  97. return maxHp;
  98. }
  99. const Framework::Text& Item::getName() const
  100. {
  101. return name;
  102. }
  103. bool Item::canBeStackedWith(const Item* zItem) const
  104. {
  105. return itemTypeId == zItem->itemTypeId && durability == maxDurability
  106. && zItem->durability == zItem->durability && maxHp == zItem->maxHp
  107. && eatable == zItem->eatable && placeable == zItem->placeable
  108. && equippable == zItem->eatable && solid == zItem->solid
  109. && usable == zItem->usable && name.istGleich(zItem->name);
  110. }
  111. bool Item::canBePlacedAt(int dimensionId, Framework::Vec3<int> worldPos) const
  112. {
  113. auto b = Game::INSTANCE->zBlockAt(worldPos, dimensionId);
  114. return (b.isA()
  115. && (b.getA()->zBlockType()->getId() == BlockTypeEnum::AIR
  116. || b.getA()->zBlockType()->isFluid()))
  117. || (b.isB() && b.getB() == BlockTypeEnum::AIR);
  118. }
  119. void Item::onPlaced()
  120. {
  121. hp = 0;
  122. }
  123. Framework::Text Item::getTooltipUIML() const
  124. {
  125. return Framework::Text("<tip><text width=\"auto\" height=\"auto\">") + name
  126. + "</text></tip>";
  127. }
  128. void Item::applyInventoryEffects(Entity* zTarget) {}
  129. void Item::removeInventoryEffects(Entity* zTarget) {}
  130. void Item::applyEquippedEffects(Entity* zTarget) {}
  131. void Item::removeEquippedEffects(Entity* zTarget) {}
  132. bool Item::applyFoodEffects(Entity* zTarget)
  133. {
  134. return foodEffect(this, zTarget);
  135. }
  136. bool Item::canApplyFoodEffectsFully(Entity* zTarget) const
  137. {
  138. return foodEffectDestroysItemTest(this, zTarget);
  139. }
  140. ItemJsonType::ItemJsonType()
  141. : TypeFactory()
  142. {}
  143. Item* ItemJsonType::createValue(Framework::JSON::JSONObject* zJson) const
  144. {
  145. const ItemType* type = ItemType::zByName(
  146. zJson->asObject()->zValue("type")->asString()->getString());
  147. return type->createItem();
  148. }
  149. void ItemJsonType::fromJson(
  150. Item* zResult, Framework::JSON::JSONObject* zJson) const
  151. {
  152. const ItemType* type = ItemType::zByName(
  153. zJson->asObject()->zValue("type")->asString()->getString());
  154. for (auto attribute = zJson->asObject()->getFields(); attribute;
  155. attribute++)
  156. {
  157. if (attribute.val().istGleich("type")) continue;
  158. type->setItemAttribute(
  159. zResult, attribute, zJson->asObject()->zValue(attribute));
  160. }
  161. }
  162. void ItemJsonType::toJson(
  163. Item* zObject, Framework::JSON::JSONObject* zResult) const
  164. {
  165. zResult->addValue("type",
  166. new Framework::JSON::JSONString(zObject->zItemType()->getName()));
  167. zObject->zItemType()->addItemAttributes(zObject, zResult);
  168. }
  169. JSONObjectValidationBuilder* ItemJsonType::addToValidator(
  170. JSONObjectValidationBuilder *builder) const
  171. {
  172. Framework::RCArray<Framework::Text> itemTypes;
  173. for (int index = 0; index < Game::INSTANCE->getItemTypeCount(); index++)
  174. {
  175. if (Game::INSTANCE->zItemType(index))
  176. {
  177. itemTypes.add(new Framework::Text(
  178. Game::INSTANCE->zItemType(index)->getName()));
  179. }
  180. }
  181. return builder
  182. ->withRequiredString("type")
  183. ->whichIsOneOf(itemTypes)
  184. ->finishString()
  185. ->allowAdditionalAttriutes();
  186. }