GrowingPlant.cpp 5.2 KB

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