Block.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #pragma once
  2. #include "EventListener.h"
  3. #include "BlockType.h"
  4. #include "ReferenceCounter.h"
  5. #include "EventThrower.h"
  6. #include "Item.h"
  7. #include "Inventory.h"
  8. #include <Trie.h>
  9. #include <Vec3.h>
  10. class BlockType;
  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. 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 *neighbours[ 6 ];
  32. int minTickTimeout;
  33. int maxTickTimeout;
  34. bool tickSource;
  35. /// <summary>
  36. /// executes block specific things
  37. /// </summary>
  38. /// <param name="zqueue">a queue to add neighbor blocks that should be ticked after this block</param>
  39. /// <param name="numTicks">the number of ticks passed since the last call (only for tickSources)</param>
  40. /// <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>
  41. /// <returns>true, iff the block needs to be ticked more often</returns>
  42. virtual bool onTick( TickQueue *zQueue, int numTicks, bool &blocked ) = 0;
  43. /// <summary>
  44. /// gets called after each block was tickt.
  45. /// the order of blocks called will be exactly the same as onTick
  46. /// </summary>
  47. virtual void onPostTick() = 0;
  48. public:
  49. Block( const BlockType *zType, ItemType *zTool, Framework::Vec3<int> pos );
  50. void tick( TickQueue *zQueue );
  51. void postTick();
  52. bool isTickSource() const;
  53. const BlockType *zBlockType() const;
  54. bool isTransparent() const;
  55. bool isPassable() const;
  56. float getHP() const;
  57. float getMaxHP() const;
  58. float getHardness() const;
  59. ItemType *zEffectiveTool() const;
  60. float getSpeedModifier() const;
  61. const Framework::Vec3<int> getPos() const;
  62. friend Chunk;
  63. friend BlockType;
  64. };
  65. class BasicBlockItem : public Item
  66. {
  67. protected:
  68. bool transparent;
  69. bool passable;
  70. float hp;
  71. float maxHP;
  72. float hardness;
  73. int toolId;
  74. float speedModifier;
  75. public:
  76. BasicBlockItem( const ItemType *zType, const char *name );
  77. virtual bool canBeStackedWith( Item *zItem ) const override;
  78. friend BasicBlockItemType;
  79. friend BlockType;
  80. };
  81. class BasicBlockItemType : public ItemType
  82. {
  83. protected:
  84. BasicBlockItemType( int id, ItemSkillLevelUpRule *levelUpRule, const ItemType *zBrokenType );
  85. virtual void loadSuperItem( Item *zItem, Framework::StreamReader *zReader ) const override;
  86. virtual void saveSuperItem( const Item *zItem, Framework::StreamWriter *zWriter ) const override;
  87. void initializeItem( BasicBlockItem *zItem, bool transparent, bool passable, float hp, float maxHP, float hardness, int toolId, float speedModifier ) const;
  88. };