Block.h 3.0 KB

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