Block.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. class ItemType;
  10. class Chunk;
  11. class BasicBlockItemType;
  12. class TickQueue;
  13. #define AIR_BLOCK -1
  14. #define IS_BLOCK(b) (((__int64)b) > 0)
  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 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. void api( Framework::StreamReader *zRequest, NetworkResponse *zResponse );
  57. bool isTickSource() const;
  58. const BlockType *zBlockType() const;
  59. bool isTransparent() const;
  60. bool isPassable() const;
  61. float getHP() const;
  62. float getMaxHP() const;
  63. float getHardness() const;
  64. ItemType *zEffectiveTool() const;
  65. float getSpeedModifier() const;
  66. const Framework::Vec3<int> getPos() const;
  67. int getDimensionId() const;
  68. bool isVisible() const;
  69. friend BlockType;
  70. };
  71. class BasicBlockItem : public Item
  72. {
  73. protected:
  74. bool transparent;
  75. bool passable;
  76. float hp;
  77. float maxHP;
  78. float hardness;
  79. int toolId;
  80. float speedModifier;
  81. public:
  82. BasicBlockItem( const ItemType *zType, const char *name );
  83. virtual bool canBeStackedWith( Item *zItem ) const override;
  84. friend BasicBlockItemType;
  85. friend BlockType;
  86. };
  87. class BasicBlockItemType : public ItemType
  88. {
  89. protected:
  90. BasicBlockItemType( int id, ItemSkillLevelUpRule *levelUpRule, const ItemType *zBrokenType );
  91. virtual void loadSuperItem( Item *zItem, Framework::StreamReader *zReader ) const override;
  92. virtual void saveSuperItem( const Item *zItem, Framework::StreamWriter *zWriter ) const override;
  93. void initializeItem( BasicBlockItem *zItem, bool transparent, bool passable, float hp, float maxHP, float hardness, int toolId, float speedModifier ) const;
  94. };