TreeSeblingBlock.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. #include "TreeSeblingBlock.h"
  2. #include "BasicBlocks.h"
  3. #include "Dimension.h"
  4. #include "Game.h"
  5. #include "NoBlock.h"
  6. #include "RandNoise.h"
  7. #include "TreeTemplate.h"
  8. #include "WorldGenerator.h"
  9. TreeSeblingBlock::TreeSeblingBlock(int typeId,
  10. Framework::Vec3<int> pos,
  11. int dimensionId,
  12. const BlockType* wood,
  13. const BlockType* leaves)
  14. : Block(typeId, pos, dimensionId, 0),
  15. seblingTicks(0),
  16. seblingTicksMax(10000),
  17. wood(wood),
  18. leaves(leaves)
  19. {
  20. tickSource = 1;
  21. }
  22. bool TreeSeblingBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  23. {
  24. int lastPercentage = (int)(seblingTicks / (float)seblingTicksMax * 100.f);
  25. seblingTicks += 1;
  26. if ((int)(seblingTicks / (float)seblingTicksMax * 100.f) != lastPercentage)
  27. {
  28. Game::INSTANCE->blockTargetChanged(this);
  29. }
  30. return 0;
  31. }
  32. void TreeSeblingBlock::onPostTick()
  33. {
  34. if ((int)seblingTicks >= seblingTicksMax)
  35. {
  36. Game::INSTANCE->doLater([wood = wood,
  37. leaves = leaves,
  38. pos = getPos(),
  39. dim = getDimensionId()]() {
  40. // the tree sebling object will be deleted during this operation
  41. RandNoise noise((int)time(0));
  42. if (!Game::INSTANCE->zGenerator()->spawnStructure(pos,
  43. dim,
  44. [wood = wood, leaves = leaves](GeneratorTemplate* tmpl) {
  45. TreeTemplate* tree = dynamic_cast<TreeTemplate*>(tmpl);
  46. return tree && tree->zWoodType() == wood
  47. && tree->zLeavesType() == leaves;
  48. }))
  49. {
  50. Game::INSTANCE->zDimension(dim)->placeBlock(
  51. pos, BlockTypeEnum::AIR);
  52. }
  53. });
  54. }
  55. }
  56. Framework::Text TreeSeblingBlock::getTargetUIML()
  57. {
  58. return Framework::Text("<targetInfo><text width=\"auto\" height=\"auto\">")
  59. + Game::INSTANCE->zBlockType(typeId)->getName() + "\n" + "Growth: "
  60. + Framework::Text((int)(seblingTicks / (float)seblingTicksMax * 100.f))
  61. + "%</text></targetInfo>";
  62. }
  63. TreeSeblingBlockType::TreeSeblingBlockType()
  64. : BlockType(),
  65. transparent(true),
  66. passable(true),
  67. speedModifier(0.5f),
  68. interactable(1)
  69. {}
  70. bool TreeSeblingBlockType::initialize(Game* zGame)
  71. {
  72. if (itemTypeName.getLength())
  73. {
  74. itemTypeId = zGame->getItemTypeId(itemTypeName);
  75. }
  76. else
  77. {
  78. itemTypeId = 0;
  79. }
  80. woodTypeId = zGame->getBlockTypeId(woodTypeName);
  81. leavesTypeId = zGame->getBlockTypeId(leavesTypeName);
  82. return itemTypeId >= 0 && BlockType::initialize(zGame);
  83. }
  84. void TreeSeblingBlockType::setItemTypeName(Framework::Text itemTypeName)
  85. {
  86. this->itemTypeName = itemTypeName;
  87. }
  88. Framework::Text TreeSeblingBlockType::getItemTypeName() const
  89. {
  90. return itemTypeName;
  91. }
  92. void TreeSeblingBlockType::setWoodTypeName(Framework::Text woodTypeName)
  93. {
  94. this->woodTypeName = woodTypeName;
  95. }
  96. Framework::Text TreeSeblingBlockType::getWoodTypeName() const
  97. {
  98. return woodTypeName;
  99. }
  100. void TreeSeblingBlockType::setLeavesTypeName(Framework::Text leavesTypeName)
  101. {
  102. this->leavesTypeName = leavesTypeName;
  103. }
  104. Framework::Text TreeSeblingBlockType::getLeavesTypeName() const
  105. {
  106. return leavesTypeName;
  107. }
  108. void TreeSeblingBlockType::setTransparent(bool transparent)
  109. {
  110. this->transparent = transparent;
  111. }
  112. bool TreeSeblingBlockType::isTransparent() const
  113. {
  114. return transparent;
  115. }
  116. void TreeSeblingBlockType::setPassable(bool passable)
  117. {
  118. this->passable = passable;
  119. }
  120. bool TreeSeblingBlockType::isPassable() const
  121. {
  122. return passable;
  123. }
  124. void TreeSeblingBlockType::setSpeedModifier(float speedModifier)
  125. {
  126. this->speedModifier = speedModifier;
  127. }
  128. float TreeSeblingBlockType::getSpeedModifier() const
  129. {
  130. return speedModifier;
  131. }
  132. void TreeSeblingBlockType::setInteractable(bool interactable)
  133. {
  134. this->interactable = interactable;
  135. }
  136. bool TreeSeblingBlockType::isInteractable() const
  137. {
  138. return interactable;
  139. }
  140. ItemType* TreeSeblingBlockType::createItemType() const
  141. {
  142. return new BasicBlockItemType(getItemTypeName(),
  143. new ModelInfo(zModel()->getModelPath(),
  144. zModel()->getTexturePaths(),
  145. zModel()->isTransparent(),
  146. zModel()->getSize() / 2.f),
  147. transparent,
  148. passable,
  149. getHardness(),
  150. speedModifier,
  151. getName(),
  152. 0,
  153. 50,
  154. getGroupNames());
  155. }
  156. void TreeSeblingBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  157. {
  158. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  159. block->transparent = transparent;
  160. block->passable = passable;
  161. block->hp = (float)getInitialMaxHP();
  162. block->maxHP = (float)getInitialMaxHP();
  163. block->hardness = getHardness();
  164. block->speedModifier = speedModifier;
  165. block->interactable = interactable;
  166. BlockType::createSuperBlock(zBlock, zItem);
  167. }
  168. void TreeSeblingBlockType::loadSuperBlock(
  169. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  170. {
  171. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  172. zReader->lese((char*)&block->seblingTicks, 4);
  173. zReader->lese((char*)&block->seblingTicksMax, 4);
  174. int id;
  175. zReader->lese((char*)&id, 4);
  176. block->wood = Game::INSTANCE->zBlockType(id);
  177. zReader->lese((char*)&id, 4);
  178. block->leaves = Game::INSTANCE->zBlockType(id);
  179. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  180. }
  181. void TreeSeblingBlockType::saveSuperBlock(
  182. Block* zBlock, Framework::StreamWriter* zWriter) const
  183. {
  184. TreeSeblingBlock* block = dynamic_cast<TreeSeblingBlock*>(zBlock);
  185. zWriter->schreibe((char*)&block->seblingTicks, 4);
  186. zWriter->schreibe((char*)&block->seblingTicksMax, 4);
  187. int id = block->wood->getId();
  188. zWriter->schreibe((char*)&id, 4);
  189. id = block->leaves->getId();
  190. zWriter->schreibe((char*)&id, 4);
  191. BlockType::saveSuperBlock(zBlock, zWriter);
  192. }
  193. Item* TreeSeblingBlockType::createItem() const
  194. {
  195. return Game::INSTANCE->zItemType(itemTypeId)->createItem();
  196. }
  197. Block* TreeSeblingBlockType::createBlock(
  198. Framework::Vec3<int> position, int dimensionId) const
  199. {
  200. return new TreeSeblingBlock(getId(),
  201. position,
  202. dimensionId,
  203. Game::INSTANCE->zBlockType(woodTypeId),
  204. Game::INSTANCE->zBlockType(leavesTypeId));
  205. }
  206. TreeSeblingBlockTypeFactory::TreeSeblingBlockTypeFactory()
  207. : BlockTypeFactoryBase()
  208. {}
  209. TreeSeblingBlockType* TreeSeblingBlockTypeFactory::createValue(
  210. Framework::JSON::JSONObject* zJson) const
  211. {
  212. return new TreeSeblingBlockType();
  213. }
  214. void TreeSeblingBlockTypeFactory::fromJson(
  215. TreeSeblingBlockType* zResult, Framework::JSON::JSONObject* zJson) const
  216. {
  217. zResult->setItemTypeName(
  218. zJson->zValue("itemType")->asString()->getString());
  219. zResult->setWoodTypeName(
  220. zJson->zValue("woodType")->asString()->getString());
  221. zResult->setLeavesTypeName(
  222. zJson->zValue("leavesType")->asString()->getString());
  223. zResult->setTransparent(zJson->zValue("transparent")->asBool()->getBool());
  224. zResult->setPassable(zJson->zValue("passable")->asBool()->getBool());
  225. zResult->setSpeedModifier(
  226. (float)zJson->zValue("speedModifier")->asNumber()->getNumber());
  227. zResult->setInteractable(
  228. zJson->zValue("interactable")->asBool()->getBool());
  229. BlockTypeFactoryBase::fromJson(zResult, zJson);
  230. }
  231. void TreeSeblingBlockTypeFactory::toJson(
  232. TreeSeblingBlockType* zObject, Framework::JSON::JSONObject* zResult) const
  233. {
  234. zResult->addValue("itemType",
  235. new Framework::JSON::JSONString(zObject->getItemTypeName()));
  236. zResult->addValue("woodType",
  237. new Framework::JSON::JSONString(zObject->getWoodTypeName()));
  238. zResult->addValue("leavesType",
  239. new Framework::JSON::JSONString(zObject->getLeavesTypeName()));
  240. zResult->addValue("transparent",
  241. new Framework::JSON::JSONNumber(zObject->isTransparent()));
  242. zResult->addValue(
  243. "passable", new Framework::JSON::JSONNumber(zObject->isPassable()));
  244. zResult->addValue("speedModifier",
  245. new Framework::JSON::JSONNumber(zObject->getSpeedModifier()));
  246. zResult->addValue("interactable",
  247. new Framework::JSON::JSONNumber(zObject->isInteractable()));
  248. BlockTypeFactoryBase::toJson(zObject, zResult);
  249. }
  250. JSONObjectValidationBuilder* TreeSeblingBlockTypeFactory::addToValidator(
  251. JSONObjectValidationBuilder* builder) const
  252. {
  253. return BlockTypeFactoryBase::addToValidator(
  254. builder->withRequiredString("itemType")
  255. ->finishString()
  256. ->withRequiredString("woodType")
  257. ->finishString()
  258. ->withRequiredString("leavesType")
  259. ->finishString()
  260. ->withRequiredBool("transparent")
  261. ->withDefault(true)
  262. ->finishBool()
  263. ->withRequiredBool("passable")
  264. ->withDefault(true)
  265. ->finishBool()
  266. ->withRequiredNumber("speedModifier")
  267. ->withDefault(0.5)
  268. ->finishNumber()
  269. ->withRequiredBool("interactable")
  270. ->withDefault(true)
  271. ->finishBool());
  272. }
  273. Framework::Text TreeSeblingBlockTypeFactory::getTypeToken() const
  274. {
  275. return "treeSapling";
  276. }