Item.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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) {}