Block.h 3.4 KB

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