Block.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #pragma once
  2. #include "BlockType.h"
  3. #include "ReferenceCounter.h"
  4. #include "Item.h"
  5. #include "Inventory.h"
  6. #include "NetworkResponse.h"
  7. #include <Trie.h>
  8. #include <Vec3.h>
  9. #include <Either.h>
  10. #define CONST_BLOCK(maybeBlock, type) (maybeBlock ? maybeBlock : StaticRegistry<BlockType>::INSTANCE.zElement((int)type)->zDefault())
  11. class ItemType;
  12. class Chunk;
  13. class BasicBlockItemType;
  14. class TickQueue;
  15. class Block : public Inventory
  16. {
  17. private:
  18. int ticksLeftCounter;
  19. int currentTickTimeout;
  20. bool wasTicked;
  21. bool onTickCalled;
  22. int dimensionId;
  23. protected:
  24. bool transparent;
  25. bool passable;
  26. float hp;
  27. float maxHP;
  28. float hardness;
  29. const BlockType* zType;
  30. ItemType* zTool;
  31. float speedModifier;
  32. Block* zNeighbours[6];
  33. int neighbourTypes[6];
  34. int minTickTimeout;
  35. int maxTickTimeout;
  36. bool tickSource;
  37. bool interactable;
  38. bool transmissionRequested;
  39. bool deadAndRemoved;
  40. /// <summary>
  41. /// executes block specific things
  42. /// </summary>
  43. /// <param name="zqueue">a queue to add neighbor blocks that should be ticked after this block</param>
  44. /// <param name="numTicks">the number of ticks passed since the last call (only for tickSources)</param>
  45. /// <param name="blocked">can be set to one to tell that this block needs to be tickt again later in the queue of this tick</param>
  46. /// <returns>true, iff the block needs to be ticked more often</returns>
  47. virtual bool onTick(TickQueue* zQueue, int numTicks, bool& blocked) = 0;
  48. /// <summary>
  49. /// gets called after each block was tickt.
  50. /// the order of blocks called will be exactly the same as onTick
  51. /// </summary>
  52. virtual void onPostTick() = 0;
  53. virtual void onDestroy();
  54. public:
  55. Block(const BlockType* zType, ItemType* zTool, Framework::Vec3<int> pos, bool hasInventory);
  56. virtual ~Block();
  57. void tick(TickQueue* zQueue);
  58. void postTick();
  59. void setDimensionId(int id);
  60. virtual void setNeighbour(Direction dir, Framework::Either<Block*, int> neighbor);
  61. virtual void setNeighbourBlock(Direction dir, Block* zN);
  62. virtual void setNeighbourType(Direction dir, int type);
  63. void api(Framework::StreamReader* zRequest, NetworkResponse* zResponse);
  64. bool isTickSource() const;
  65. const BlockType* zBlockType() const;
  66. bool isTransparent() const;
  67. bool isPassable() const;
  68. bool isInteractable() const;
  69. float getHP() const;
  70. float getMaxHP() const;
  71. float getHardness() const;
  72. ItemType* zEffectiveTool() const;
  73. float getSpeedModifier() const;
  74. const Framework::Vec3<int> getPos() const;
  75. int getDimensionId() const;
  76. bool isVisible() const;
  77. void setHP(float hp);
  78. bool isDeadAndRemoved() const;
  79. friend BlockType;
  80. };
  81. class BasicBlockItem : public Item
  82. {
  83. protected:
  84. bool transparent;
  85. bool passable;
  86. float hp;
  87. float maxHP;
  88. float hardness;
  89. int toolId;
  90. float speedModifier;
  91. bool interactable;
  92. public:
  93. BasicBlockItem(const ItemType* zType, const BlockType* zPlacedBlockType, const char* name);
  94. virtual bool canBeStackedWith(Item* zItem) const override;
  95. friend BasicBlockItemType;
  96. friend BlockType;
  97. };
  98. class BasicBlockItemType : public ItemType
  99. {
  100. protected:
  101. BasicBlockItemType(int id, const char* name, ItemSkillLevelUpRule* levelUpRule, const ItemType* zBrokenType, ModelInfo model);
  102. virtual void loadSuperItem(Item* zItem, Framework::StreamReader* zReader) const override;
  103. virtual void saveSuperItem(const Item* zItem, Framework::StreamWriter* zWriter) const override;
  104. void initializeItem(BasicBlockItem* zItem, bool transparent, bool passable, float hp, float maxHP, float hardness, int toolId, float speedModifier) const;
  105. };