Block.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. /// <summary>
  40. /// executes block specific things
  41. /// </summary>
  42. /// <param name="zqueue">a queue to add neighbor blocks that should be ticked after this block</param>
  43. /// <param name="numTicks">the number of ticks passed since the last call (only for tickSources)</param>
  44. /// <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>
  45. /// <returns>true, iff the block needs to be ticked more often</returns>
  46. virtual bool onTick( TickQueue* zQueue, int numTicks, bool& blocked ) = 0;
  47. /// <summary>
  48. /// gets called after each block was tickt.
  49. /// the order of blocks called will be exactly the same as onTick
  50. /// </summary>
  51. virtual void onPostTick() = 0;
  52. virtual void onDestroy();
  53. public:
  54. Block( const BlockType* zType, ItemType* zTool, Framework::Vec3<int> pos, bool hasInventory );
  55. virtual ~Block();
  56. void tick( TickQueue* zQueue );
  57. void postTick();
  58. void setDimensionId( int id );
  59. virtual void setNeighbour( Direction dir, Framework::Either<Block*, int> neighbor );
  60. virtual void setNeighbourBlock( Direction dir, Block* zN );
  61. virtual void setNeighbourType( Direction dir, int type );
  62. void api( Framework::StreamReader* zRequest, NetworkResponse* zResponse );
  63. bool isTickSource() const;
  64. const BlockType* zBlockType() const;
  65. bool isTransparent() const;
  66. bool isPassable() const;
  67. bool isInteractable() const;
  68. float getHP() const;
  69. float getMaxHP() const;
  70. float getHardness() const;
  71. ItemType* zEffectiveTool() const;
  72. float getSpeedModifier() const;
  73. const Framework::Vec3<int> getPos() const;
  74. int getDimensionId() const;
  75. bool isVisible() const;
  76. void setHP( float hp );
  77. void onAfterTransmission();
  78. void requestTransmission();
  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. public:
  92. BasicBlockItem( const ItemType* zType, const BlockType* zPlacedBlockType, const char* name );
  93. virtual bool canBeStackedWith( Item* zItem ) const override;
  94. friend BasicBlockItemType;
  95. friend BlockType;
  96. };
  97. class BasicBlockItemType : public ItemType
  98. {
  99. protected:
  100. BasicBlockItemType( int id, ItemSkillLevelUpRule* levelUpRule, const ItemType* zBrokenType );
  101. virtual void loadSuperItem( Item* zItem, Framework::StreamReader* zReader ) const override;
  102. virtual void saveSuperItem( const Item* zItem, Framework::StreamWriter* zWriter ) const override;
  103. void initializeItem( BasicBlockItem* zItem, bool transparent, bool passable, float hp, float maxHP, float hardness, int toolId, float speedModifier ) const;
  104. };