Item.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. bool Item::canBeStackedWith(const Item* zItem) const
  88. {
  89. return itemTypeId == zItem->itemTypeId && durability == maxDurability
  90. && zItem->durability == zItem->durability && maxHp == zItem->maxHp
  91. && eatable == zItem->eatable && placeable == zItem->placeable
  92. && equippable == zItem->eatable && solid == zItem->solid
  93. && usable == zItem->usable && maxStackSize == zItem->maxStackSize
  94. && name.istGleich(zItem->name);
  95. }
  96. bool Item::canBePlacedAt(int dimensionId, Vec3<int> worldPos) const
  97. {
  98. auto b = Game::INSTANCE->zBlockAt(worldPos, dimensionId);
  99. return (b.isA() && b.getA()->zBlockType()->getId() == BlockTypeEnum::AIR)
  100. || (b.isB() && b.getB() == BlockTypeEnum::AIR);
  101. }
  102. void Item::onPlaced()
  103. {
  104. hp = 0;
  105. }
  106. Framework::Text Item::getTooltipUIML() const
  107. {
  108. return Framework::Text("<tip><text width=\"auto\" height=\"auto\">") + name
  109. + "</text></tip>";
  110. }
  111. void Item::applyInventoryEffects(Entity* zTarget)
  112. {
  113. foodEffect(zTarget);
  114. }
  115. void Item::removeInventoryEffects(Entity* zTarget) {}
  116. void Item::applyEquippedEffects(Entity* zTarget) {}
  117. void Item::removeEquippedEffects(Entity* zTarget) {}
  118. void Item::applyFoodEffects(Entity* zTarget) {}