Item.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #include "Item.h"
  2. #include <Text.h>
  3. #include "Game.h"
  4. Item::Item(int itemTypeId, Framework::Text 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. name(name)
  18. {
  19. foodEffect = [](Item* i, Entity* e) { return false; };
  20. foodEffectDestroysItemTest = [](const Item* i, Entity* e) { return false; };
  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<bool(Item*, Entity*)> foodEffect,
  32. std::function<bool(const Item*, Entity*)> destroysItemTest)
  33. {
  34. this->foodEffect = foodEffect;
  35. this->foodEffectDestroysItemTest = destroysItemTest;
  36. }
  37. const ItemType* Item::zItemType() const
  38. {
  39. return Game::INSTANCE->zItemType(itemTypeId);
  40. }
  41. int Item::getTypeId() const
  42. {
  43. return itemTypeId;
  44. }
  45. const BlockType* Item::zPlacedBlockType() const
  46. {
  47. return Game::INSTANCE->zBlockType(blockTypeId);
  48. }
  49. float Item::getHp() const
  50. {
  51. return hp;
  52. }
  53. float Item::getDurability() const
  54. {
  55. return durability;
  56. }
  57. bool Item::isUsable() const
  58. {
  59. return usable;
  60. }
  61. bool Item::isEatable() const
  62. {
  63. return eatable;
  64. }
  65. bool Item::isPlaceable() const
  66. {
  67. return placeable;
  68. }
  69. bool Item::isEquippable() const
  70. {
  71. return equippable;
  72. }
  73. bool Item::isSolid() const
  74. {
  75. return solid;
  76. }
  77. void Item::setMaxDurability(float maxDurability)
  78. {
  79. if (maxDurability != this->maxDurability)
  80. {
  81. float durabilityPercentage = durability / this->maxDurability;
  82. this->maxDurability = maxDurability;
  83. durability = maxDurability * durabilityPercentage;
  84. }
  85. }
  86. float Item::getMaxDurability() const
  87. {
  88. return maxDurability;
  89. }
  90. int Item::getMaxStackSize() const
  91. {
  92. return zItemType()->getMaxStackSize();
  93. }
  94. float Item::getMaxHp() const
  95. {
  96. return maxHp;
  97. }
  98. const Framework::Text& Item::getName() const
  99. {
  100. return name;
  101. }
  102. bool Item::canBeStackedWith(const Item* zItem) const
  103. {
  104. return itemTypeId == zItem->itemTypeId && durability == maxDurability
  105. && zItem->durability == zItem->durability && maxHp == zItem->maxHp
  106. && eatable == zItem->eatable && placeable == zItem->placeable
  107. && equippable == zItem->eatable && solid == zItem->solid
  108. && usable == zItem->usable
  109. && name.istGleich(zItem->name);
  110. }
  111. bool Item::canBePlacedAt(int dimensionId, 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::fromJson(Framework::JSON::JSONValue* zJson) const
  144. {
  145. const ItemType* type = ItemType::zByName(
  146. zJson->asObject()->zValue("type")->asString()->getString());
  147. Item* result = type->createItem();
  148. for (auto attribute = zJson->asObject()->getFields(); attribute;
  149. attribute++)
  150. {
  151. if (attribute.val().istGleich("type")) continue;
  152. type->setItemAttribute(
  153. result, attribute, zJson->asObject()->zValue(attribute));
  154. }
  155. return result;
  156. }
  157. Framework::JSON::JSONValue* ItemJsonType::toJson(Item* zObject) const
  158. {
  159. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  160. result->addValue("type",
  161. new Framework::JSON::JSONString(zObject->zItemType()->getName()));
  162. zObject->zItemType()->addItemAttributes(zObject, result);
  163. return result;
  164. }
  165. Framework::JSON::Validator::JSONValidator* ItemJsonType::getValidator() const
  166. {
  167. Framework::RCArray<Framework::Text> itemTypes;
  168. for (int index = 0; index < Game::INSTANCE->getItemTypeCount(); index++)
  169. {
  170. if (Game::INSTANCE->zItemType(index))
  171. {
  172. itemTypes.add(new Framework::Text(
  173. Game::INSTANCE->zItemType(index)->getName()));
  174. }
  175. }
  176. return Framework::JSON::Validator::JSONValidator::buildForObject()
  177. ->withRequiredString("type")
  178. ->whichIsOneOf(itemTypes)
  179. ->finishString()
  180. ->allowAdditionalAttriutes()
  181. ->finishObject();
  182. }