Item.cpp 4.7 KB

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