Block.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #pragma once
  2. #include <Vec3.h>
  3. #include "Inventory.h"
  4. #include "Item.h"
  5. #include "ItemType.h"
  6. #include "MultiblockStructure.h"
  7. #include "NetworkMessage.h"
  8. #include "Tickable.h"
  9. #include "TickSourceType.h"
  10. #define CONST_BLOCK(maybeBlock, type) \
  11. ((maybeBlock) ? (maybeBlock) \
  12. : Game::INSTANCE->zBlockType((int)(type))->zDefault())
  13. class ItemType;
  14. class Chunk;
  15. class BasicBlockItemType;
  16. class PlaceableProof;
  17. class TickQueue;
  18. class Block : public Inventory,
  19. public Tickable
  20. {
  21. private:
  22. int ticksLeftCounter;
  23. int currentTickTimeout;
  24. bool wasTicked;
  25. bool onTickCalled;
  26. protected:
  27. Direction frontDirection;
  28. bool transparent;
  29. bool passable;
  30. float hp;
  31. float maxHP;
  32. float hardness;
  33. int typeId;
  34. float speedModifier;
  35. int minTickTimeout;
  36. int maxTickTimeout;
  37. bool interactable;
  38. bool transmissionRequested;
  39. bool deadAndRemoved;
  40. unsigned char lightEmisionColor[3];
  41. int mapColor;
  42. Framework::RCArray<MultiblockStructure> structures;
  43. /// <summary>
  44. /// executes block specific things
  45. /// </summary>
  46. /// <param name="zqueue">a queue to add neighbor blocks that should be
  47. /// ticked after this block</param> <param name="numTicks">the number of
  48. /// ticks passed since the last call (only for tickSources)</param> <param
  49. /// name="blocked">can be set to one to tell that this block needs to be
  50. /// tickt again later in the queue of this tick</param> <returns>true, iff
  51. /// the block needs to be ticked more often</returns>
  52. virtual bool onTick(TickQueue* zQueue, int numTicks, bool& blocked) = 0;
  53. /// <summary>
  54. /// gets called after each block was tickt.
  55. /// the order of blocks called will be exactly the same as onTick
  56. /// </summary>
  57. virtual void onPostTick() = 0;
  58. virtual void onDestroy(
  59. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill);
  60. virtual void onDialogClosed(Framework::Text dialogId);
  61. void broadcastModelInfoChange();
  62. void broadcastMessage(NetworkMessage* message);
  63. void broadcastPassableSpeedModifierChange();
  64. virtual void onApiCall(char messageType,
  65. Framework::StreamReader* zRequest,
  66. NetworkMessage* zResponse,
  67. Entity* zSource);
  68. public:
  69. Block(int typeId,
  70. Framework::Vec3<int> pos,
  71. int dimensionId,
  72. bool hasInventory);
  73. Block(int typeId,
  74. Framework::Vec3<int> pos,
  75. int dimensionId,
  76. bool hasInventory,
  77. Direction frontDirection);
  78. virtual ~Block();
  79. void tick(TickQueue* zQueue) override;
  80. void postTick() override;
  81. void addToStructure(MultiblockStructure* structure);
  82. virtual void onLoaded();
  83. virtual void onUnloaded();
  84. virtual Framework::XML::Element* getTargetUIML() const;
  85. virtual void sendModelInfo(NetworkMessage* zMessage);
  86. virtual bool interact(Item* zItem, Entity* zActor, bool& itemChanged);
  87. void api(Framework::StreamReader* zRequest,
  88. NetworkMessage* zResponse,
  89. Entity* zSource);
  90. virtual TickSourceType isTickSource() const;
  91. virtual bool needsTick() const;
  92. const BlockType* zBlockType() const;
  93. bool isTransparent() const;
  94. bool isPassable() const;
  95. virtual bool isInteractable(const Item* zItem) const;
  96. float getHP() const;
  97. float getMaxHP() const;
  98. float getHardness() const;
  99. float getSpeedModifier() const;
  100. const Framework::Vec3<int> getPos() const;
  101. void setHP(
  102. Entity* zActor, Item* zUsedItem, ItemSkill* zUsedSkill, float hp);
  103. bool isDeadAndRemoved() const;
  104. virtual void getLightEmisionColor(unsigned char* result) const;
  105. virtual void filterPassingLight(unsigned char rgb[3]) const;
  106. void updateModel(ModelInfo* zInfo) const;
  107. int getMapColor() const;
  108. void setFrontDirection(Direction frontDirection);
  109. Direction getFrontDirection() const;
  110. friend BlockType;
  111. };
  112. class BasicBlockItem : public Item
  113. {
  114. protected:
  115. bool transparent;
  116. bool passable;
  117. float hardness;
  118. float speedModifier;
  119. bool interactable;
  120. PlaceableProof* placeableProof;
  121. public:
  122. BasicBlockItem(int itemTypeId,
  123. int blockTypeId,
  124. Framework::Text name,
  125. PlaceableProof* placeableProof);
  126. ~BasicBlockItem();
  127. virtual bool canBeStackedWith(const Item* zItem) const override;
  128. virtual bool canBePlacedAt(
  129. int dimensionId, Framework::Vec3<int> worldPos) const override;
  130. friend BasicBlockItemType;
  131. friend BlockType;
  132. };
  133. class BasicBlockItemType : public ItemType
  134. {
  135. private:
  136. bool transparent;
  137. bool passable;
  138. float hardness;
  139. float speedModifier;
  140. Framework::Text blockTypeName;
  141. int blockTypeId;
  142. PlaceableProof* placeableProof;
  143. public:
  144. BasicBlockItemType();
  145. BasicBlockItemType(Framework::Text name,
  146. ModelInfo* model,
  147. bool transparent,
  148. bool passable,
  149. float hardness,
  150. float speedModifier,
  151. Framework::Text blockTypeName,
  152. PlaceableProof* placeableProof,
  153. int maxStackSize,
  154. Framework::RCArray<Framework::Text> groups);
  155. ~BasicBlockItemType();
  156. protected:
  157. virtual void loadSuperItem(
  158. Item* zItem, Framework::StreamReader* zReader) const override;
  159. virtual void saveSuperItem(
  160. const Item* zItem, Framework::StreamWriter* zWriter) const override;
  161. virtual Item* createItem() const override;
  162. public:
  163. virtual bool initialize(Game* zGame) override;
  164. int getBlockTypeId() const;
  165. void setTransparent(bool transparent);
  166. bool isTransparent() const;
  167. void setPassable(bool passable);
  168. bool isPassable() const;
  169. void setHardness(float hardness);
  170. float getHardness() const;
  171. void setSpeedModifier(float speedModifier);
  172. float getSpeedModifier() const;
  173. void setBlockTypeName(Framework::Text blockTypeName);
  174. Framework::Text getBlockTypeName() const;
  175. void setPlaceableProof(PlaceableProof* placeableProof);
  176. PlaceableProof* zPlaceableProof() const;
  177. };
  178. class BasicBlockItemTypeFactory : public ItemTypeFactoryBase<BasicBlockItemType>
  179. {
  180. public:
  181. BasicBlockItemTypeFactory();
  182. BasicBlockItemType* createValue(
  183. Framework::JSON::JSONObject* zJson) const override;
  184. BasicBlockItemType* fromJson(
  185. Framework::JSON::JSONObject* zJson) const override;
  186. Framework::JSON::JSONObject* toJsonObject(
  187. BasicBlockItemType* zObject) const override;
  188. JSONObjectValidationBuilder* addToValidator(
  189. JSONObjectValidationBuilder* builder) const override;
  190. const char* getTypeToken() const override;
  191. };