TreeSeblingBlock.cpp 4.3 KB

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