GrowingPlant.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 dimensionId,
  25. int maxTicks,
  26. const char* name,
  27. int blockTypeAfterGrowth)
  28. : Block(typeId, zTool, pos, dimensionId, 0),
  29. seblingTicks(0),
  30. seblingTicksMax(maxTicks),
  31. name(name),
  32. states(),
  33. blockTypeAfterGrowth(blockTypeAfterGrowth),
  34. plantSpawned(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)
  50. && state.percentage <= seblingTicks / (float)seblingTicksMax)
  51. {
  52. updateModel(state.model);
  53. }
  54. }
  55. return 1;
  56. }
  57. void GrowingPlantBlock::onPostTick()
  58. {
  59. if (seblingTicks >= (float)seblingTicksMax && !plantSpawned)
  60. {
  61. plantSpawned = 1;
  62. Game::INSTANCE->doLater([this]() {
  63. Game::INSTANCE->zDimension(getDimensionId())
  64. ->placeBlock(getPos(), blockTypeAfterGrowth);
  65. });
  66. }
  67. }
  68. void GrowingPlantBlock::sendModelInfo(NetworkMessage* zMessage)
  69. {
  70. const GrowthState* current = 0;
  71. for (const GrowthState& state : states)
  72. {
  73. if (state.percentage <= seblingTicks / (float)seblingTicksMax)
  74. {
  75. current = &state;
  76. }
  77. }
  78. if (current)
  79. {
  80. zMessage->addressBlock(this);
  81. InMemoryBuffer buffer;
  82. current->model.writeTo(&buffer);
  83. char* msg = new char[(int)buffer.getSize() + 1];
  84. msg[0] = 1; // hmodel change
  85. buffer.lese(msg + 1, (int)buffer.getSize());
  86. zMessage->setMessage(msg, (int)buffer.getSize() + 1);
  87. }
  88. }
  89. Framework::Text GrowingPlantBlock::getTargetUIML()
  90. {
  91. return Text("<targetInfo><text width=\"auto\" height=\"auto\">") + name
  92. + "\n" + "Growth: "
  93. + Text((int)(seblingTicks / (float)seblingTicksMax * 100.f))
  94. + "%</text></targetInfo>";
  95. }
  96. GrowingPlantBlock* GrowingPlantBlock::addGrowthState(GrowthState state)
  97. {
  98. int index = 0;
  99. for (const GrowthState& s : states)
  100. {
  101. if (s.percentage > state.percentage)
  102. {
  103. states.add(state, index);
  104. return this;
  105. }
  106. index++;
  107. }
  108. states.add(state);
  109. return this;
  110. }
  111. GrowingPlantBlockType::GrowingPlantBlockType(int typeId,
  112. ModelInfo model,
  113. const char* name,
  114. int blockTypeAfterGrowth,
  115. const char* readableName,
  116. int ticksNeeded,
  117. int mapColor)
  118. : BlockType(typeId, 0, model, 1, 10, 0, name, true, mapColor),
  119. transparent(1),
  120. passable(1),
  121. hardness(0.1f),
  122. zTool(0),
  123. speedModifier(0.3f),
  124. interactable(1),
  125. states(),
  126. blockTypeAfterGrowth(blockTypeAfterGrowth),
  127. readableName(readableName),
  128. ticksNeeded(ticksNeeded)
  129. {}
  130. GrowingPlantBlockType* GrowingPlantBlockType::setHardness(float hardness)
  131. {
  132. this->hardness = hardness;
  133. return this;
  134. }
  135. GrowingPlantBlockType* GrowingPlantBlockType::addGrowthState(
  136. float growthPercentage, ModelInfo model)
  137. {
  138. states.add(GrowthState(growthPercentage, model));
  139. return this;
  140. }
  141. void GrowingPlantBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  142. {
  143. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  144. block->transparent = transparent;
  145. block->passable = passable;
  146. block->hardness = hardness;
  147. block->zTool = zTool;
  148. block->speedModifier = speedModifier;
  149. block->interactable = interactable;
  150. BlockType::createSuperBlock(zBlock, zItem);
  151. }
  152. void GrowingPlantBlockType::loadSuperBlock(
  153. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  154. {
  155. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  156. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  157. zReader->lese((char*)&block->seblingTicks, 4);
  158. }
  159. void GrowingPlantBlockType::saveSuperBlock(
  160. Block* zBlock, Framework::StreamWriter* zWriter) const
  161. {
  162. BlockType::saveSuperBlock(zBlock, zWriter);
  163. GrowingPlantBlock* block = dynamic_cast<GrowingPlantBlock*>(zBlock);
  164. zWriter->schreibe((char*)&block->seblingTicks, 4);
  165. }
  166. Item* GrowingPlantBlockType::createItem() const
  167. {
  168. return 0;
  169. }
  170. Block* GrowingPlantBlockType::createBlock(
  171. Framework::Vec3<int> position, int dimensionId) const
  172. {
  173. GrowingPlantBlock* block = new GrowingPlantBlock(getId(),
  174. zTool,
  175. position,
  176. dimensionId,
  177. ticksNeeded,
  178. readableName,
  179. blockTypeAfterGrowth);
  180. for (const GrowthState& state : states)
  181. {
  182. block->addGrowthState(state);
  183. }
  184. return block;
  185. }