Item.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 = [](Entity* e) {};
  21. }
  22. void Item::setHp(float hp)
  23. {
  24. this->hp = hp;
  25. }
  26. void Item::setDurability(float durability)
  27. {
  28. this->durability = durability;
  29. }
  30. void Item::tick() {}
  31. void Item::setFoodEffect(std::function<void(Entity*)> foodEffect)
  32. {
  33. this->foodEffect = foodEffect;
  34. }
  35. const ItemType* Item::zItemType() const
  36. {
  37. return StaticRegistry<ItemType>::INSTANCE.zElement(itemTypeId);
  38. }
  39. int Item::getTypeId() const
  40. {
  41. return itemTypeId;
  42. }
  43. const BlockType* Item::zPlacedBlockType() const
  44. {
  45. return StaticRegistry<BlockType>::INSTANCE.zElement(blockTypeId);
  46. }
  47. float Item::getHp() const
  48. {
  49. return hp;
  50. }
  51. float Item::getDurability() const
  52. {
  53. return durability;
  54. }
  55. bool Item::isUsable() const
  56. {
  57. return usable;
  58. }
  59. bool Item::isEatable() const
  60. {
  61. return eatable;
  62. }
  63. bool Item::isPlaceable() const
  64. {
  65. return placeable;
  66. }
  67. bool Item::isEquippable() const
  68. {
  69. return equippable;
  70. }
  71. bool Item::isSolid() const
  72. {
  73. return solid;
  74. }
  75. float Item::getMaxDurability() const
  76. {
  77. return maxDurability;
  78. }
  79. int Item::getMaxStackSize() const
  80. {
  81. return maxStackSize;
  82. }
  83. float Item::getMaxHp() const
  84. {
  85. return maxHp;
  86. }
  87. const Framework::Text& Item::getName() const
  88. {
  89. return name;
  90. }
  91. bool Item::canBeStackedWith(const Item* zItem) const
  92. {
  93. return itemTypeId == zItem->itemTypeId && durability == maxDurability
  94. && zItem->durability == zItem->durability && maxHp == zItem->maxHp
  95. && eatable == zItem->eatable && placeable == zItem->placeable
  96. && equippable == zItem->eatable && solid == zItem->solid
  97. && usable == zItem->usable && maxStackSize == zItem->maxStackSize
  98. && name.istGleich(zItem->name);
  99. }
  100. bool Item::canBePlacedAt(int dimensionId, Vec3<int> worldPos) const
  101. {
  102. auto b = Game::INSTANCE->zBlockAt(worldPos, dimensionId);
  103. return (b.isA() && b.getA()->zBlockType()->getId() == BlockTypeEnum::AIR)
  104. || (b.isB() && b.getB() == BlockTypeEnum::AIR);
  105. }
  106. void Item::onPlaced()
  107. {
  108. hp = 0;
  109. }
  110. Framework::Text Item::getTooltipUIML() const
  111. {
  112. return Framework::Text("<tip><text width=\"auto\" height=\"auto\">") + name
  113. + "</text></tip>";
  114. }
  115. void Item::applyInventoryEffects(Entity* zTarget)
  116. {
  117. foodEffect(zTarget);
  118. }
  119. void Item::removeInventoryEffects(Entity* zTarget) {}
  120. void Item::applyEquippedEffects(Entity* zTarget) {}
  121. void Item::removeEquippedEffects(Entity* zTarget) {}
  122. void Item::applyFoodEffects(Entity* zTarget) {}
  123. ItomJsonType::ItomJsonType()
  124. : TypeFactory()
  125. {}
  126. Item* ItomJsonType::fromJson(Framework::JSON::JSONValue* zJson) const
  127. {
  128. const ItemType* type = ItemType::zByName(
  129. zJson->asObject()->zValue("type")->asString()->getString());
  130. Item *result = type->createItem();
  131. for (auto attribute = zJson->asObject()->getFields(); attribute;
  132. attribute++)
  133. {
  134. if (attribute.val().istGleich("type"))
  135. continue;
  136. type->setItemAttribute(
  137. result, attribute, zJson->asObject()->zValue(attribute));
  138. }
  139. return result;
  140. }
  141. Framework::JSON::JSONValue* ItomJsonType::toJson(Item* zObject) const {
  142. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  143. result->addValue("type", new Framework::JSON::JSONString(
  144. zObject->zItemType()->getName()));
  145. zObject->zItemType()->addItemAttributes(zObject, result);
  146. return result;
  147. }
  148. Framework::JSON::Validator::JSONValidator* ItomJsonType::getValidator() const {
  149. Framework::RCArray<Framework::Text> itemTypes;
  150. for (int index = 0; index < StaticRegistry<ItemType>::INSTANCE.getCount();
  151. index++)
  152. {
  153. if (StaticRegistry<ItemType>::INSTANCE.zElement(index))
  154. {
  155. itemTypes.add(new Framework::Text(
  156. StaticRegistry<ItemType>::INSTANCE.zElement(index)->getName()));
  157. }
  158. }
  159. return Framework::JSON::Validator::JSONValidator::buildForObject()
  160. ->withRequiredString("type")
  161. ->whichIsOneOf(itemTypes)
  162. ->finishString()
  163. ->allowAdditionalAttriutes()
  164. ->finishObject();
  165. }