BasicBlocks.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. #include "BasicBlocks.h"
  2. #include "Game.h"
  3. #include "ItemEntity.h"
  4. #include "ItemStack.h"
  5. #include "ModelInfo.h"
  6. #include "TreeSeblingBlock.h"
  7. BasicBlock::BasicBlock(int typeId, Framework::Vec3<int> pos, int dimensionId)
  8. : BasicBlock(typeId, pos, dimensionId, false)
  9. {}
  10. BasicBlock::BasicBlock(
  11. int typeId, Framework::Vec3<int> pos, int dimensionId, bool hasInventory)
  12. : Block(typeId, pos, dimensionId, hasInventory)
  13. {}
  14. bool BasicBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  15. {
  16. return 0;
  17. }
  18. void BasicBlock::onPostTick() {}
  19. BasicBlockType::BasicBlockType()
  20. : BlockType(),
  21. itemTypeName(),
  22. transparent(0),
  23. passable(0),
  24. speedModifier(1.f),
  25. interactable(1)
  26. {}
  27. bool BasicBlockType::initialize(Game* zGame)
  28. {
  29. if (itemTypeName.getLength())
  30. {
  31. itemTypeId = zGame->getItemTypeId(itemTypeName);
  32. }
  33. else
  34. {
  35. itemTypeId = 0;
  36. }
  37. return itemTypeId >= 0 && BlockType::initialize(zGame);
  38. }
  39. void BasicBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  40. {
  41. BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
  42. block->transparent = transparent;
  43. block->passable = passable;
  44. block->hp = (float)getInitialMaxHP();
  45. block->maxHP = (float)getInitialMaxHP();
  46. block->hardness = getHardness();
  47. block->speedModifier = speedModifier;
  48. block->interactable = interactable;
  49. BlockType::createSuperBlock(zBlock, zItem);
  50. }
  51. Block* BasicBlockType::createBlock(
  52. Framework::Vec3<int> position, int dimensionId) const
  53. {
  54. return new BasicBlock(getId(), position, dimensionId);
  55. }
  56. Item* BasicBlockType::createItem() const
  57. {
  58. return Game::INSTANCE->zItemType(itemTypeId)->createItem();
  59. }
  60. Framework::Text BasicBlockType::getItemTypeName() const
  61. {
  62. return itemTypeName;
  63. }
  64. ItemType* BasicBlockType::createItemType() const
  65. {
  66. return new BasicBlockItemType(getItemTypeName(),
  67. new ModelInfo(zModel()->getModelPath(),
  68. zModel()->getTexturePaths(),
  69. zModel()->isTransparent(),
  70. zModel()->getSize() / 2.f),
  71. transparent,
  72. passable,
  73. getHardness(),
  74. speedModifier,
  75. getItemTypeName(),
  76. 0,
  77. 50,
  78. getGroupNames());
  79. }
  80. void BasicBlockType::setItemTypeName(Framework::Text itemTypeName)
  81. {
  82. this->itemTypeName = itemTypeName;
  83. }
  84. int BasicBlockType::getItemTypeId() const
  85. {
  86. return itemTypeId;
  87. }
  88. void BasicBlockType::setTransparent(bool transparent)
  89. {
  90. this->transparent = transparent;
  91. }
  92. bool BasicBlockType::isTransparent() const
  93. {
  94. return transparent;
  95. }
  96. void BasicBlockType::setPassable(bool passable)
  97. {
  98. this->passable = passable;
  99. }
  100. bool BasicBlockType::isPassable() const
  101. {
  102. return passable;
  103. }
  104. void BasicBlockType::setSpeedModifier(float speedModifier)
  105. {
  106. this->speedModifier = speedModifier;
  107. }
  108. float BasicBlockType::getSpeedModifier() const
  109. {
  110. return speedModifier;
  111. }
  112. void BasicBlockType::setInteractable(bool interactable)
  113. {
  114. this->interactable = interactable;
  115. }
  116. bool BasicBlockType::isInteractable() const
  117. {
  118. return interactable;
  119. }
  120. BasicBlockTypeFactory::BasicBlockTypeFactory()
  121. : BlockTypeFactoryBase()
  122. {}
  123. BasicBlockType* BasicBlockTypeFactory::createValue(
  124. Framework::JSON::JSONObject* zJson) const
  125. {
  126. return new BasicBlockType();
  127. }
  128. BasicBlockType* BasicBlockTypeFactory::fromJson(
  129. Framework::JSON::JSONObject* zJson) const
  130. {
  131. BasicBlockType* result = BlockTypeFactoryBase::fromJson(zJson);
  132. result->setItemTypeName(zJson->zValue("itemType")->asString()->getString());
  133. result->setTransparent(zJson->zValue("transparent")->asBool()->getBool());
  134. result->setPassable(zJson->zValue("passable")->asBool()->getBool());
  135. result->setSpeedModifier(
  136. (float)zJson->zValue("speedModifier")->asNumber()->getNumber());
  137. result->setInteractable(
  138. (float)zJson->zValue("interactable")->asBool()->getBool());
  139. return result;
  140. }
  141. Framework::JSON::JSONObject* BasicBlockTypeFactory::toJsonObject(
  142. BasicBlockType* zObject) const
  143. {
  144. Framework::JSON::JSONObject* result
  145. = BlockTypeFactoryBase::toJsonObject(zObject);
  146. result->addValue("itemType",
  147. new Framework::JSON::JSONString(zObject->getItemTypeName()));
  148. result->addValue(
  149. "transparent", new Framework::JSON::JSONBool(zObject->isTransparent()));
  150. result->addValue(
  151. "passable", new Framework::JSON::JSONBool(zObject->isPassable()));
  152. result->addValue("speedModifier",
  153. new Framework::JSON::JSONNumber(zObject->getSpeedModifier()));
  154. result->addValue("interactable",
  155. new Framework::JSON::JSONBool(zObject->isInteractable()));
  156. return result;
  157. }
  158. JSONObjectValidationBuilder* BasicBlockTypeFactory::addToValidator(
  159. JSONObjectValidationBuilder* builder) const
  160. {
  161. return BlockTypeFactoryBase::addToValidator(
  162. builder
  163. ->withRequiredAttribute("itemType",
  164. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  165. ItemTypeNameFactory::TYPE_ID))
  166. ->withRequiredBool("transparent")
  167. ->withDefault(false)
  168. ->finishBool()
  169. ->withRequiredBool("passable")
  170. ->withDefault(false)
  171. ->finishBool()
  172. ->withRequiredNumber("speedModifier")
  173. ->withDefault(1.0)
  174. ->finishNumber()
  175. ->withRequiredBool("interactable")
  176. ->withDefault(true)
  177. ->finishBool());
  178. }
  179. const char* BasicBlockTypeFactory::getTypeToken() const
  180. {
  181. return "basicBlock";
  182. }
  183. AdditionalItemSpawningBlock::AdditionalItemSpawningBlock(
  184. int typeId, Framework::Vec3<int> pos, int dimensionId)
  185. : BasicBlock(typeId, pos, dimensionId)
  186. {}
  187. void AdditionalItemSpawningBlock::addSpawn(SpawnConfig config)
  188. {
  189. spawns.add(config);
  190. }
  191. void AdditionalItemSpawningBlock::onDestroy()
  192. {
  193. for (const SpawnConfig& config : spawns)
  194. {
  195. if ((double)rand() / RAND_MAX < config.chance)
  196. {
  197. int amount = config.min
  198. + (int)((config.max - config.min)
  199. * ((double)rand() / RAND_MAX));
  200. if (amount > 0)
  201. {
  202. ItemStack* spawnedItems
  203. = Game::INSTANCE->zItemType(config.typeId)
  204. ->createItemStack(amount);
  205. if (spawnedItems)
  206. {
  207. Game::INSTANCE->spawnItem(
  208. location + Framework::Vec3<float>(0.5f, 0.5f, 0.5f),
  209. getDimensionId(),
  210. spawnedItems);
  211. }
  212. }
  213. }
  214. }
  215. BasicBlock::onDestroy();
  216. }
  217. AdditionalItemSpawningBlockType::AdditionalItemSpawningBlockType()
  218. : BasicBlockType(),
  219. spawns()
  220. {}
  221. void AdditionalItemSpawningBlockType::createSuperBlock(
  222. Block* zBlock, Item* zItem) const
  223. {
  224. AdditionalItemSpawningBlock* block
  225. = dynamic_cast<AdditionalItemSpawningBlock*>(zBlock);
  226. if (block)
  227. {
  228. for (const SpawnConfig& config : spawns)
  229. {
  230. block->addSpawn(config);
  231. }
  232. }
  233. BasicBlockType::createSuperBlock(zBlock, zItem);
  234. }
  235. bool AdditionalItemSpawningBlockType::initialize(Game* zGame)
  236. {
  237. for (auto iterator = spawns.begin(); iterator; iterator++)
  238. {
  239. int itemTypeId
  240. = Game::INSTANCE->getItemTypeId(iterator.val().itemTypeName);
  241. if (itemTypeId < 0)
  242. {
  243. return false;
  244. }
  245. iterator.set({iterator.val().min,
  246. iterator.val().max,
  247. iterator.val().chance,
  248. iterator.val().itemTypeName,
  249. itemTypeId});
  250. }
  251. return BasicBlockType::initialize(zGame);
  252. }
  253. Block* AdditionalItemSpawningBlockType::createBlock(
  254. Framework::Vec3<int> position, int dimensionId) const
  255. {
  256. AdditionalItemSpawningBlock* block
  257. = new AdditionalItemSpawningBlock(getId(), position, dimensionId);
  258. return block;
  259. }
  260. void AdditionalItemSpawningBlockType::addSpawn(SpawnConfig config)
  261. {
  262. spawns.add(config);
  263. }
  264. const Framework::Array<SpawnConfig>&
  265. AdditionalItemSpawningBlockType::getSpawns() const
  266. {
  267. return spawns;
  268. }
  269. AdditionalItemSpawningBlockTypeFactory::AdditionalItemSpawningBlockTypeFactory()
  270. : BasicBlockTypeFactory()
  271. {}
  272. BasicBlockType* AdditionalItemSpawningBlockTypeFactory::createValue(
  273. Framework::JSON::JSONObject* zJson) const
  274. {
  275. return new AdditionalItemSpawningBlockType();
  276. }
  277. BasicBlockType* AdditionalItemSpawningBlockTypeFactory::fromJson(
  278. Framework::JSON::JSONObject* zJson) const
  279. {
  280. AdditionalItemSpawningBlockType* result
  281. = dynamic_cast<AdditionalItemSpawningBlockType*>(
  282. BasicBlockTypeFactory::fromJson(zJson));
  283. Framework::JSON::JSONArray* spawnsJson = zJson->zValue("spawns")->asArray();
  284. for (int i = 0; i < spawnsJson->getLength(); i++)
  285. {
  286. Framework::JSON::JSONObject* spawnJson
  287. = spawnsJson->zValue(i)->asObject();
  288. result->addSpawn(SpawnConfig{
  289. (int)spawnJson->zValue("min")->asNumber()->getNumber(),
  290. (int)spawnJson->zValue("max")->asNumber()->getNumber(),
  291. (float)spawnJson->zValue("chance")->asNumber()->getNumber(),
  292. spawnJson->zValue("itemType")->asString()->getString(),
  293. 0,
  294. });
  295. }
  296. return result;
  297. }
  298. Framework::JSON::JSONObject*
  299. AdditionalItemSpawningBlockTypeFactory::toJsonObject(
  300. BasicBlockType* zObject) const
  301. {
  302. AdditionalItemSpawningBlockType* zType
  303. = dynamic_cast<AdditionalItemSpawningBlockType*>(zObject);
  304. Framework::JSON::JSONObject* result
  305. = BasicBlockTypeFactory::toJsonObject(zObject);
  306. Framework::JSON::JSONArray* spawns = new Framework::JSON::JSONArray();
  307. for (const SpawnConfig& config : zType->getSpawns())
  308. {
  309. Framework::JSON::JSONObject* spawn = new Framework::JSON::JSONObject();
  310. spawn->addValue(
  311. "itemType", new Framework::JSON::JSONString(config.itemTypeName));
  312. spawn->addValue(
  313. "chance", new Framework::JSON::JSONNumber(config.chance));
  314. spawn->addValue("min", new Framework::JSON::JSONNumber(config.min));
  315. spawn->addValue("max", new Framework::JSON::JSONNumber(config.max));
  316. spawns->addValue(spawn);
  317. }
  318. result->addValue("spawns", spawns);
  319. return result;
  320. }
  321. JSONObjectValidationBuilder*
  322. AdditionalItemSpawningBlockTypeFactory::addToValidator(
  323. JSONObjectValidationBuilder* builder) const
  324. {
  325. return BasicBlockTypeFactory::addToValidator(builder->withRequiredAttribute(
  326. "spawns",
  327. Framework::Validator::DataValidator::buildForArray()
  328. ->addAcceptedObjectInArray()
  329. ->withRequiredAttribute("itemType",
  330. Game::INSTANCE->zTypeRegistry()->getValidator<Framework::Text>(
  331. ItemTypeNameFactory::TYPE_ID))
  332. ->withRequiredNumber("chance")
  333. ->finishNumber()
  334. ->withRequiredNumber("min")
  335. ->finishNumber()
  336. ->withRequiredNumber("max")
  337. ->finishNumber()
  338. ->finishObject()
  339. ->finishArray()));
  340. }
  341. const char* AdditionalItemSpawningBlockTypeFactory::getTypeToken() const
  342. {
  343. return "additionalItemsBlockType";
  344. }
  345. const char* AdditionalItemSpawningBlockTypeFactory::getTypeName() const
  346. {
  347. return typeid(AdditionalItemSpawningBlockType).name();
  348. }