TreeSeblingBlock.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. int mapColor)
  68. : BlockType(typeId, 0, model, 1, 10, 0, name, false, mapColor),
  69. itemType(itemTypeId),
  70. transparent(true),
  71. passable(true),
  72. hardness(0.1f),
  73. zTool(0),
  74. speedModifier(0.5f),
  75. interactable(1),
  76. woodType(woodType),
  77. leavesType(leavesType)
  78. {}
  79. void TreeSeblingBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  80. {
  81. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  82. block->transparent = transparent;
  83. block->passable = passable;
  84. block->hp = (float)getInitialMaxHP();
  85. block->maxHP = (float)getInitialMaxHP();
  86. block->hardness = hardness;
  87. block->zTool = zTool;
  88. block->speedModifier = speedModifier;
  89. block->interactable = interactable;
  90. BlockType::createSuperBlock(zBlock, zItem);
  91. }
  92. void TreeSeblingBlockType::loadSuperBlock(
  93. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  94. {
  95. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  96. zReader->lese((char*)&block->seblingTicks, 4);
  97. zReader->lese((char*)&block->seblingTicksMax, 4);
  98. int id;
  99. zReader->lese((char*)&id, 4);
  100. block->wood = StaticRegistry<BlockType>::INSTANCE.zElement(id);
  101. zReader->lese((char*)&id, 4);
  102. block->leaves = StaticRegistry<BlockType>::INSTANCE.zElement(id);
  103. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  104. }
  105. void TreeSeblingBlockType::saveSuperBlock(
  106. Block* zBlock, Framework::StreamWriter* zWriter) const
  107. {
  108. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  109. zWriter->schreibe((char*)&block->seblingTicks, 4);
  110. zWriter->schreibe((char*)&block->seblingTicksMax, 4);
  111. int id = block->wood->getId();
  112. zWriter->schreibe((char*)&id, 4);
  113. id = block->leaves->getId();
  114. zWriter->schreibe((char*)&id, 4);
  115. BlockType::saveSuperBlock(zBlock, zWriter);
  116. }
  117. Item* TreeSeblingBlockType::createItem() const
  118. {
  119. return StaticRegistry<ItemType>::INSTANCE.zElement(itemType)->createItem();
  120. }
  121. Block* TreeSeblingBlockType::createBlock(Framework::Vec3<int> position) const
  122. {
  123. return new TreeSeblingBlock(getId(),
  124. zTool,
  125. position,
  126. StaticRegistry<BlockType>::INSTANCE.zElement(woodType),
  127. StaticRegistry<BlockType>::INSTANCE.zElement(leavesType));
  128. }
  129. TreeSeblingBlockType* TreeSeblingBlockType::setHardness(float hardness)
  130. {
  131. this->hardness = hardness;
  132. return this;
  133. }