TreeSeblingBlock.cpp 4.5 KB

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