TreeSeblingBlock.cpp 9.6 KB

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