Block.h 3.6 KB

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