Block.h 3.3 KB

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