TreeSeblingBlock.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #include "TreeSeblingBlock.h"
  2. #include "BasicBlocks.h"
  3. #include "Game.h"
  4. #include "NoBlock.h"
  5. #include "RandNoise.h"
  6. #include "TreeTemplate.h"
  7. TreeSeblingBlock::TreeSeblingBlock(int typeId,
  8. const ItemType* zTool,
  9. Framework::Vec3<int> pos,
  10. const BlockType* wood,
  11. const BlockType* leaves)
  12. : Block(typeId, zTool, pos, 0),
  13. wood(wood),
  14. leaves(leaves)
  15. {
  16. tickSource = 1;
  17. }
  18. bool TreeSeblingBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  19. {
  20. seblingTicks += 1;
  21. return 0;
  22. }
  23. void TreeSeblingBlock::onPostTick()
  24. {
  25. if ((int)seblingTicks >= seblingTicksMax)
  26. {
  27. Game::INSTANCE->doLater([wood = wood,
  28. leaves = leaves,
  29. pos = getPos(),
  30. dim = getDimensionId()]() {
  31. // the tree sebling object will be deleted during this operation
  32. RandNoise noise((int)time(0));
  33. if (!Game::INSTANCE->zGenerator()->spawnStructure(pos,
  34. dim,
  35. [wood = wood, leaves = leaves](GenerationTemplate* tmpl) {
  36. TreeTemplate* tree = dynamic_cast<TreeTemplate*>(tmpl);
  37. return tree && tree->getWoodType() == wood
  38. && tree->getLeavesType() == leaves;
  39. }))
  40. {
  41. Game::INSTANCE->zDimension(dim)->placeBlock(
  42. pos, BlockTypeEnum::AIR);
  43. }
  44. });
  45. }
  46. }
  47. TreeSeblingBlockType::TreeSeblingBlockType(
  48. int typeId, int itemTypeId, ModelInfo model, int woodType, int leavesType)
  49. : BlockType(typeId, 0, model, 1, 10, 0),
  50. itemType(itemTypeId),
  51. transparent(true),
  52. passable(true),
  53. hardness(0.1f),
  54. zTool(0),
  55. speedModifier(0.5f),
  56. interactable(1),
  57. woodType(woodType),
  58. leavesType(leavesType)
  59. {}
  60. void TreeSeblingBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  61. {
  62. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  63. if (!block)
  64. throw "TreeSeblingBlockType::createSuperBlock was called with a block "
  65. "witch is not an instance of TreeSeblingBlock";
  66. block->transparent = transparent;
  67. block->passable = passable;
  68. block->hp = (float)getInitialMaxHP();
  69. block->maxHP = (float)getInitialMaxHP();
  70. block->hardness = hardness;
  71. block->zTool = zTool;
  72. block->speedModifier = speedModifier;
  73. block->interactable = interactable;
  74. BlockType::createSuperBlock(zBlock, zItem);
  75. }
  76. void TreeSeblingBlockType::loadSuperBlock(
  77. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  78. {
  79. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  80. if (!block)
  81. throw "TreeSeblingBlockType::loadSuperBlock was called with a block "
  82. "witch is not an instance of TreeSeblingBlock";
  83. zReader->lese((char*)&block->seblingTicks, 4);
  84. zReader->lese((char*)&block->seblingTicksMax, 4);
  85. int id;
  86. zReader->lese((char*)&id, 4);
  87. block->wood = StaticRegistry<BlockType>::INSTANCE.zElement(id);
  88. zReader->lese((char*)&id, 4);
  89. block->leaves = StaticRegistry<BlockType>::INSTANCE.zElement(id);
  90. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  91. }
  92. void TreeSeblingBlockType::saveSuperBlock(
  93. Block* zBlock, Framework::StreamWriter* zWriter) const
  94. {
  95. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  96. if (!block)
  97. throw "TreeSeblingBlockType::saveSuperBlock was called with a block "
  98. "witch is not an instance of TreeSeblingBlock";
  99. zWriter->schreibe((char*)&block->seblingTicks, 4);
  100. zWriter->schreibe((char*)&block->seblingTicksMax, 4);
  101. int id = block->wood->getId();
  102. zWriter->schreibe((char*)&id, 4);
  103. id = block->leaves->getId();
  104. zWriter->schreibe((char*)&id, 4);
  105. BlockType::saveSuperBlock(zBlock, zWriter);
  106. }
  107. Item* TreeSeblingBlockType::createItem() const
  108. {
  109. return StaticRegistry<ItemType>::INSTANCE.zElement(itemType)->createItem();
  110. }
  111. Block* TreeSeblingBlockType::createBlock(Framework::Vec3<int> position) const
  112. {
  113. return new TreeSeblingBlock(getId(),
  114. zTool,
  115. position,
  116. StaticRegistry<BlockType>::INSTANCE.zElement(woodType),
  117. StaticRegistry<BlockType>::INSTANCE.zElement(leavesType));
  118. }
  119. TreeSeblingBlockType* TreeSeblingBlockType::setHardness(float hardness)
  120. {
  121. this->hardness = hardness;
  122. return this;
  123. }