BasicBlocks.cpp 12 KB

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