Item.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "Item.h"
  2. #include <Text.h>
  3. #include "Game.h"
  4. Item::Item(int itemTypeId, const char* name)
  5. : ReferenceCounter(),
  6. itemTypeId(itemTypeId),
  7. blockTypeId(0),
  8. hp(1),
  9. maxHp(1),
  10. durability(1),
  11. maxDurability(1),
  12. eatable(0),
  13. placeable(0),
  14. equippable(0),
  15. solid(1),
  16. usable(0),
  17. maxStackSize(50),
  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 StaticRegistry<ItemType>::INSTANCE.zElement(itemTypeId);
  41. }
  42. int Item::getTypeId() const
  43. {
  44. return itemTypeId;
  45. }
  46. const BlockType* Item::zPlacedBlockType() const
  47. {
  48. return StaticRegistry<BlockType>::INSTANCE.zElement(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. float Item::getMaxDurability() const
  79. {
  80. return maxDurability;
  81. }
  82. int Item::getMaxStackSize() const
  83. {
  84. return maxStackSize;
  85. }
  86. float Item::getMaxHp() const
  87. {
  88. return maxHp;
  89. }
  90. const Framework::Text& Item::getName() const
  91. {
  92. return name;
  93. }
  94. bool Item::canBeStackedWith(const Item* zItem) const
  95. {
  96. return itemTypeId == zItem->itemTypeId && durability == maxDurability
  97. && zItem->durability == zItem->durability && maxHp == zItem->maxHp
  98. && eatable == zItem->eatable && placeable == zItem->placeable
  99. && equippable == zItem->eatable && solid == zItem->solid
  100. && usable == zItem->usable && maxStackSize == zItem->maxStackSize
  101. && name.istGleich(zItem->name);
  102. }
  103. bool Item::canBePlacedAt(int dimensionId, Vec3<int> worldPos) const
  104. {
  105. auto b = Game::INSTANCE->zBlockAt(worldPos, dimensionId);
  106. return (b.isA()
  107. && (b.getA()->zBlockType()->getId() == BlockTypeEnum::AIR
  108. || b.getA()->zBlockType()->isFluid()))
  109. || (b.isB() && b.getB() == BlockTypeEnum::AIR);
  110. }
  111. void Item::onPlaced()
  112. {
  113. hp = 0;
  114. }
  115. Framework::Text Item::getTooltipUIML() const
  116. {
  117. return Framework::Text("<tip><text width=\"auto\" height=\"auto\">") + name
  118. + "</text></tip>";
  119. }
  120. void Item::applyInventoryEffects(Entity* zTarget)
  121. {}
  122. void Item::removeInventoryEffects(Entity* zTarget) {}
  123. void Item::applyEquippedEffects(Entity* zTarget) {}
  124. void Item::removeEquippedEffects(Entity* zTarget) {}
  125. bool Item::applyFoodEffects(Entity* zTarget)
  126. {
  127. return foodEffect(this, zTarget);
  128. }
  129. bool Item::canApplyFoodEffectsFully(Entity* zTarget) const
  130. {
  131. return foodEffectDestroysItemTest(this, zTarget);
  132. }
  133. ItomJsonType::ItomJsonType()
  134. : TypeFactory()
  135. {}
  136. Item* ItomJsonType::fromJson(Framework::JSON::JSONValue* zJson) const
  137. {
  138. const ItemType* type = ItemType::zByName(
  139. zJson->asObject()->zValue("type")->asString()->getString());
  140. Item *result = type->createItem();
  141. for (auto attribute = zJson->asObject()->getFields(); attribute;
  142. attribute++)
  143. {
  144. if (attribute.val().istGleich("type"))
  145. continue;
  146. type->setItemAttribute(
  147. result, attribute, zJson->asObject()->zValue(attribute));
  148. }
  149. return result;
  150. }
  151. Framework::JSON::JSONValue* ItomJsonType::toJson(Item* zObject) const {
  152. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  153. result->addValue("type", new Framework::JSON::JSONString(
  154. zObject->zItemType()->getName()));
  155. zObject->zItemType()->addItemAttributes(zObject, result);
  156. return result;
  157. }
  158. Framework::JSON::Validator::JSONValidator* ItomJsonType::getValidator() const {
  159. Framework::RCArray<Framework::Text> itemTypes;
  160. for (int index = 0; index < StaticRegistry<ItemType>::INSTANCE.getCount();
  161. index++)
  162. {
  163. if (StaticRegistry<ItemType>::INSTANCE.zElement(index))
  164. {
  165. itemTypes.add(new Framework::Text(
  166. StaticRegistry<ItemType>::INSTANCE.zElement(index)->getName()));
  167. }
  168. }
  169. return Framework::JSON::Validator::JSONValidator::buildForObject()
  170. ->withRequiredString("type")
  171. ->whichIsOneOf(itemTypes)
  172. ->finishString()
  173. ->allowAdditionalAttriutes()
  174. ->finishObject();
  175. }