Item.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #pragma once
  2. #include "ItemType.h"
  3. #include "Reader.h"
  4. #include "TypeRegistry.h"
  5. class ItemType;
  6. class BlockType;
  7. class StoneToolItemType;
  8. class Item : public virtual Framework::ReferenceCounter
  9. {
  10. protected:
  11. int itemTypeId;
  12. int blockTypeId;
  13. float hp; // an item will be removed once its hp reaches 0
  14. float maxHp;
  15. float durability; // an item will break into another item once its
  16. // durability reaches 0
  17. float maxDurability;
  18. bool eatable;
  19. bool placeable;
  20. bool equippable;
  21. bool solid;
  22. bool usable;
  23. Framework::Text name;
  24. std::function<bool(Item*, Entity*)> foodEffect;
  25. std::function<bool(const Item*, Entity*)> foodEffectDestroysItemTest;
  26. Item(int itemTypeId, Framework::Text name);
  27. public:
  28. void setHp(float hp);
  29. void setDurability(float durability);
  30. virtual void tick();
  31. void setFoodEffect(std::function<bool(Item*, Entity*)> foodEffect,
  32. std::function<bool(const Item*, Entity*)> destroysItemTest);
  33. const ItemType* zItemType() const;
  34. int getTypeId() const;
  35. virtual const BlockType* zPlacedBlockType() const;
  36. float getHp() const;
  37. float getDurability() const;
  38. bool isUsable() const;
  39. bool isEatable() const;
  40. bool isPlaceable() const;
  41. bool isEquippable() const;
  42. bool isSolid() const;
  43. void setMaxDurability(float maxDurability);
  44. float getMaxDurability() const;
  45. int getMaxStackSize() const;
  46. float getMaxHp() const;
  47. const Framework::Text& getName() const;
  48. virtual bool canBeStackedWith(const Item* zItem) const;
  49. virtual bool canBePlacedAt(
  50. int dimensionId, Framework::Vec3<int> worldPos) const;
  51. virtual void onPlaced();
  52. virtual Framework::Text getTooltipUIML() const;
  53. virtual void applyInventoryEffects(Entity* zTarget);
  54. virtual void removeInventoryEffects(Entity* zTarget);
  55. virtual void applyEquippedEffects(Entity* zTarget);
  56. virtual void removeEquippedEffects(Entity* zTarget);
  57. virtual bool applyFoodEffects(Entity* zTarget);
  58. virtual bool canApplyFoodEffectsFully(Entity* zTarget) const;
  59. friend ItemType;
  60. };
  61. class ItemJsonType : public TypeFactory<Item>
  62. {
  63. public:
  64. ItemJsonType();
  65. Item* createValue(Framework::JSON::JSONObject* zJson) const override;
  66. void fromJson(
  67. Item* zResult, Framework::JSON::JSONObject* zJson) const override;
  68. void toJson(
  69. Item* zObject, Framework::JSON::JSONObject* zResult) const override;
  70. JSONObjectValidationBuilder* addToValidator(
  71. JSONObjectValidationBuilder* builder) const override;
  72. };