Item.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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() && b.getA()->zBlockType()->getId() == BlockTypeEnum::AIR)
  107. || (b.isB() && b.getB() == BlockTypeEnum::AIR);
  108. }
  109. void Item::onPlaced()
  110. {
  111. hp = 0;
  112. }
  113. Framework::Text Item::getTooltipUIML() const
  114. {
  115. return Framework::Text("<tip><text width=\"auto\" height=\"auto\">") + name
  116. + "</text></tip>";
  117. }
  118. void Item::applyInventoryEffects(Entity* zTarget)
  119. {}
  120. void Item::removeInventoryEffects(Entity* zTarget) {}
  121. void Item::applyEquippedEffects(Entity* zTarget) {}
  122. void Item::removeEquippedEffects(Entity* zTarget) {}
  123. bool Item::applyFoodEffects(Entity* zTarget)
  124. {
  125. return foodEffect(this, zTarget);
  126. }
  127. bool Item::canApplyFoodEffectsFully(Entity* zTarget) const
  128. {
  129. return foodEffectDestroysItemTest(this, zTarget);
  130. }
  131. ItomJsonType::ItomJsonType()
  132. : TypeFactory()
  133. {}
  134. Item* ItomJsonType::fromJson(Framework::JSON::JSONValue* zJson) const
  135. {
  136. const ItemType* type = ItemType::zByName(
  137. zJson->asObject()->zValue("type")->asString()->getString());
  138. Item *result = type->createItem();
  139. for (auto attribute = zJson->asObject()->getFields(); attribute;
  140. attribute++)
  141. {
  142. if (attribute.val().istGleich("type"))
  143. continue;
  144. type->setItemAttribute(
  145. result, attribute, zJson->asObject()->zValue(attribute));
  146. }
  147. return result;
  148. }
  149. Framework::JSON::JSONValue* ItomJsonType::toJson(Item* zObject) const {
  150. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  151. result->addValue("type", new Framework::JSON::JSONString(
  152. zObject->zItemType()->getName()));
  153. zObject->zItemType()->addItemAttributes(zObject, result);
  154. return result;
  155. }
  156. Framework::JSON::Validator::JSONValidator* ItomJsonType::getValidator() const {
  157. Framework::RCArray<Framework::Text> itemTypes;
  158. for (int index = 0; index < StaticRegistry<ItemType>::INSTANCE.getCount();
  159. index++)
  160. {
  161. if (StaticRegistry<ItemType>::INSTANCE.zElement(index))
  162. {
  163. itemTypes.add(new Framework::Text(
  164. StaticRegistry<ItemType>::INSTANCE.zElement(index)->getName()));
  165. }
  166. }
  167. return Framework::JSON::Validator::JSONValidator::buildForObject()
  168. ->withRequiredString("type")
  169. ->whichIsOneOf(itemTypes)
  170. ->finishString()
  171. ->allowAdditionalAttriutes()
  172. ->finishObject();
  173. }