GrowingPlant.cpp 4.6 KB

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