GrowingPlant.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. #include "GrowingPlant.h"
  2. #include "Game.h"
  3. GrowthState::GrowthState(float percentage, ModelInfo model)
  4. : percentage(percentage),
  5. model(model)
  6. {}
  7. GrowthState::GrowthState()
  8. : percentage(1),
  9. model("", "", 0)
  10. {}
  11. GrowthState& GrowthState::operator=(const GrowthState& right)
  12. {
  13. percentage = right.percentage;
  14. model = ModelInfo(right.model);
  15. return *this;
  16. }
  17. GrowingPlantBlock::GrowingPlantBlock(int typeId,
  18. const ItemType* zTool,
  19. Framework::Vec3<int> pos,
  20. int maxTicks,
  21. const char* name,
  22. int blockTypeAfterGrowth)
  23. : Block(typeId, zTool, pos, 0),
  24. seblingTicks(0),
  25. seblingTicksMax(maxTicks),
  26. name(name),
  27. states(),
  28. blockTypeAfterGrowth(blockTypeAfterGrowth),
  29. plantSpawned(0),
  30. modelUpdated(0)
  31. {
  32. tickSource = 1;
  33. }
  34. bool GrowingPlantBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  35. {
  36. float beforePercentage = seblingTicks / (float)seblingTicksMax;
  37. seblingTicks += (float)numTicks;
  38. if ((int)(seblingTicks / (float)seblingTicksMax * 100.f)
  39. != (int)(beforePercentage * 100.f))
  40. {
  41. Game::INSTANCE->blockTargetChanged(this);
  42. }
  43. for (const GrowthState& state : states)
  44. {
  45. if ((state.percentage > beforePercentage || !modelUpdated)
  46. && state.percentage <= seblingTicks / (float)seblingTicksMax)
  47. {
  48. updateModel(state.model);
  49. modelUpdated = 1;
  50. }
  51. }
  52. return 1;
  53. }
  54. void GrowingPlantBlock::onPostTick()
  55. {
  56. if (seblingTicks >= (float)seblingTicksMax && !plantSpawned)
  57. {
  58. plantSpawned = 1;
  59. Game::INSTANCE->doLater([this]() {
  60. Game::INSTANCE->zDimension(getDimensionId())
  61. ->placeBlock(getPos(), blockTypeAfterGrowth);
  62. });
  63. }
  64. }
  65. Framework::Text GrowingPlantBlock::getTargetUIML()
  66. {
  67. return Text("<targetInfo><text width=\"auto\" height=\"auto\">") + name
  68. + "\n" + "Growth: "
  69. + Text((int)(seblingTicks / (float)seblingTicksMax * 100.f))
  70. + "%</text></targetInfo>";
  71. }
  72. GrowingPlantBlock* GrowingPlantBlock::addGrowthState(GrowthState state)
  73. {
  74. int index = 0;
  75. for (const GrowthState& s : states)
  76. {
  77. if (s.percentage > state.percentage)
  78. {
  79. states.add(state, index);
  80. return this;
  81. }
  82. index++;
  83. }
  84. states.add(state);
  85. return this;
  86. }
  87. GrowingPlantBlockType::GrowingPlantBlockType(int typeId,
  88. ModelInfo model,
  89. const char* name,
  90. int blockTypeAfterGrowth,
  91. const char* readableName,
  92. int ticksNeeded)
  93. : BlockType(typeId, 0, model, 1, 10.f, 0, name),
  94. transparent(1),
  95. passable(1),
  96. hardness(0.1f),
  97. zTool(0),
  98. speedModifier(0.3f),
  99. interactable(1),
  100. states(),
  101. blockTypeAfterGrowth(blockTypeAfterGrowth),
  102. readableName(readableName),
  103. ticksNeeded(ticksNeeded)
  104. {}
  105. GrowingPlantBlockType* GrowingPlantBlockType::setHardness(float hardness)
  106. {
  107. this->hardness = hardness;
  108. return this;
  109. }
  110. GrowingPlantBlockType* GrowingPlantBlockType::addGrowthState(
  111. float growthPercentage, ModelInfo model)
  112. {
  113. states.add(GrowthState(growthPercentage, model));
  114. return this;
  115. }
  116. void GrowingPlantBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  117. {
  118. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  119. block->transparent = transparent;
  120. block->passable = passable;
  121. block->hardness = hardness;
  122. block->zTool = zTool;
  123. block->speedModifier = speedModifier;
  124. block->interactable = interactable;
  125. BlockType::createSuperBlock(zBlock, zItem);
  126. }
  127. void GrowingPlantBlockType::loadSuperBlock(
  128. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  129. {
  130. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  131. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  132. zReader->lese((char*)&block->seblingTicks, 4);
  133. }
  134. void GrowingPlantBlockType::saveSuperBlock(
  135. Block* zBlock, Framework::StreamWriter* zWriter) const
  136. {
  137. BlockType::saveSuperBlock(zBlock, zWriter);
  138. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  139. zWriter->schreibe((char*)&block->seblingTicks, 4);
  140. }
  141. Item* GrowingPlantBlockType::createItem() const
  142. {
  143. return 0;
  144. }
  145. Block* GrowingPlantBlockType::createBlock(Framework::Vec3<int> position) const
  146. {
  147. GrowingPlantBlock* block = new GrowingPlantBlock(getId(),
  148. zTool,
  149. position,
  150. ticksNeeded,
  151. readableName,
  152. blockTypeAfterGrowth);
  153. for (const GrowthState& state : states)
  154. {
  155. block->addGrowthState(state);
  156. }
  157. return block;
  158. }