TreeSeblingBlock.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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(int typeId,
  48. int itemTypeId,
  49. ModelInfo model,
  50. int woodType,
  51. int leavesType,
  52. const char* name)
  53. : BlockType(typeId, 0, model, 1, 10, 0, name),
  54. itemType(itemTypeId),
  55. transparent(true),
  56. passable(true),
  57. hardness(0.1f),
  58. zTool(0),
  59. speedModifier(0.5f),
  60. interactable(1),
  61. woodType(woodType),
  62. leavesType(leavesType)
  63. {}
  64. void TreeSeblingBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  65. {
  66. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  67. if (!block)
  68. throw "TreeSeblingBlockType::createSuperBlock was called with a block "
  69. "witch is not an instance of TreeSeblingBlock";
  70. block->transparent = transparent;
  71. block->passable = passable;
  72. block->hp = (float)getInitialMaxHP();
  73. block->maxHP = (float)getInitialMaxHP();
  74. block->hardness = hardness;
  75. block->zTool = zTool;
  76. block->speedModifier = speedModifier;
  77. block->interactable = interactable;
  78. BlockType::createSuperBlock(zBlock, zItem);
  79. }
  80. void TreeSeblingBlockType::loadSuperBlock(
  81. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  82. {
  83. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  84. if (!block)
  85. throw "TreeSeblingBlockType::loadSuperBlock was called with a block "
  86. "witch is not an instance of TreeSeblingBlock";
  87. zReader->lese((char*)&block->seblingTicks, 4);
  88. zReader->lese((char*)&block->seblingTicksMax, 4);
  89. int id;
  90. zReader->lese((char*)&id, 4);
  91. block->wood = StaticRegistry<BlockType>::INSTANCE.zElement(id);
  92. zReader->lese((char*)&id, 4);
  93. block->leaves = StaticRegistry<BlockType>::INSTANCE.zElement(id);
  94. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  95. }
  96. void TreeSeblingBlockType::saveSuperBlock(
  97. Block* zBlock, Framework::StreamWriter* zWriter) const
  98. {
  99. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  100. if (!block)
  101. throw "TreeSeblingBlockType::saveSuperBlock was called with a block "
  102. "witch is not an instance of TreeSeblingBlock";
  103. zWriter->schreibe((char*)&block->seblingTicks, 4);
  104. zWriter->schreibe((char*)&block->seblingTicksMax, 4);
  105. int id = block->wood->getId();
  106. zWriter->schreibe((char*)&id, 4);
  107. id = block->leaves->getId();
  108. zWriter->schreibe((char*)&id, 4);
  109. BlockType::saveSuperBlock(zBlock, zWriter);
  110. }
  111. Item* TreeSeblingBlockType::createItem() const
  112. {
  113. return StaticRegistry<ItemType>::INSTANCE.zElement(itemType)->createItem();
  114. }
  115. Block* TreeSeblingBlockType::createBlock(Framework::Vec3<int> position) const
  116. {
  117. return new TreeSeblingBlock(getId(),
  118. zTool,
  119. position,
  120. StaticRegistry<BlockType>::INSTANCE.zElement(woodType),
  121. StaticRegistry<BlockType>::INSTANCE.zElement(leavesType));
  122. }
  123. TreeSeblingBlockType* TreeSeblingBlockType::setHardness(float hardness)
  124. {
  125. this->hardness = hardness;
  126. return this;
  127. }