BasicBlock.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. #include "BasicBlocks.h"
  2. #include "ItemEntity.h"
  3. #include "Game.h"
  4. #include "AddEntityUpdate.h"
  5. BasicBlock::BasicBlock(const BlockType* zType, ItemType* zTool, Framework::Vec3<int> pos)
  6. : Block(zType, zTool, pos, false)
  7. {}
  8. bool BasicBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  9. {
  10. return 0;
  11. }
  12. void BasicBlock::onPostTick()
  13. {}
  14. AdditionalItemSpawningBlock::AdditionalItemSpawningBlock(const BlockType* zType, ItemType* zTool, Framework::Vec3<int> pos)
  15. : BasicBlock(zType, zTool, pos)
  16. {}
  17. void AdditionalItemSpawningBlock::addSpawn(SpawnConfig config)
  18. {
  19. spawns.add(config);
  20. }
  21. void AdditionalItemSpawningBlock::onDestroy()
  22. {
  23. for (const SpawnConfig& config : spawns)
  24. {
  25. if ((double)rand() / RAND_MAX < config.chance)
  26. {
  27. int amount = config.min + (int)((config.max - config.min) * ((double)rand() / RAND_MAX));
  28. if (amount > 0)
  29. {
  30. ItemStack* spawnedItems = StaticRegistry<ItemType>::INSTANCE.zElement(config.itemType)->createItemStack(amount);
  31. if (spawnedItems)
  32. {
  33. ItemEntity* itemEntity = (ItemEntity*)ItemEntityType::INSTANCE->createEntity(location + Framework::Vec3<float>(0.5f, 0.5f, 0.5f), getDimensionId(), Game::INSTANCE->getNextEntityId());
  34. itemEntity->unsaveAddItem(spawnedItems, NO_DIRECTION);
  35. spawnedItems->release();
  36. Game::INSTANCE->requestWorldUpdate(new AddEntityUpdate(itemEntity, getDimensionId()));
  37. }
  38. }
  39. }
  40. }
  41. BasicBlock::onDestroy();
  42. }
  43. BasicBlockType::BasicBlockType(int typeId, int itemTypeId, ModelInfo model)
  44. : BlockType(typeId, 0, model, 1, 100, 0),
  45. itemType(itemTypeId),
  46. transparent(0),
  47. passable(0),
  48. hardness(1.f),
  49. zTool(0),
  50. speedModifier(1.f),
  51. interactable(1)
  52. {}
  53. void BasicBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  54. {
  55. BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
  56. if (!block)
  57. throw "DirtBlockType::createSuperBlock was called with a block witch is not an instance of BasicBlock";
  58. block->transparent = transparent;
  59. block->passable = passable;
  60. block->hp = (float)getInitialMaxHP();
  61. block->maxHP = (float)getInitialMaxHP();
  62. block->hardness = hardness;
  63. block->zTool = zTool;
  64. block->speedModifier = speedModifier;
  65. block->interactable = interactable;
  66. BlockType::createSuperBlock(zBlock, zItem);
  67. }
  68. Block* BasicBlockType::createBlock(Framework::Vec3<int> position) const
  69. {
  70. return new BasicBlock(this, 0, position);
  71. }
  72. Item* BasicBlockType::createItem() const
  73. {
  74. return StaticRegistry<ItemType>::INSTANCE.zElement(itemType)->createItem();
  75. }
  76. // Dirt
  77. DirtBlockType::DirtBlockType()
  78. : BasicBlockType(ID, DirtBlockItemType::ID, ModelInfo("cube", { "blocks.ltdb/dirt.png", "blocks.ltdb/dirt.png", "blocks.ltdb/dirt.png", "blocks.ltdb/dirt.png", "blocks.ltdb/lawn.png", "blocks.ltdb/dirt.png" }))
  79. {
  80. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  81. }
  82. DirtBlockItemType::DirtBlockItemType()
  83. : BasicBlockItemType(ID, "Dirt", 0, 0, ModelInfo("itemCube", "blocks.ltdb/dirt.png", 6))
  84. {}
  85. Item* DirtBlockItemType::createItem() const
  86. {
  87. BasicBlockItem* item = new BasicBlockItem(this, DirtBlockType::INSTANCE, "Dirt");
  88. initializeItem(item, 0, 0, 1, 0, 1);
  89. return item;
  90. }
  91. // Stone
  92. StoneBlockType::StoneBlockType()
  93. : BasicBlockType(ID, StoneBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/stone.png", 6))
  94. {
  95. hardness = 2.f;
  96. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  97. }
  98. StoneBlockItemType::StoneBlockItemType()
  99. : BasicBlockItemType(ID, "Stone", 0, 0, ModelInfo("itemCube", "blocks.ltdb/stone.png", 6))
  100. {}
  101. Item* StoneBlockItemType::createItem() const
  102. {
  103. BasicBlockItem* item = new BasicBlockItem(this, StoneBlockType::INSTANCE, "Stone");
  104. initializeItem(item, 0, 0, 2, 0, 1);
  105. return item;
  106. }
  107. // Sand
  108. SandBlockType::SandBlockType()
  109. : BasicBlockType(ID, SandBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/sand.png", 6))
  110. {
  111. hardness = 0.5f;
  112. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  113. }
  114. SandBlockItemType::SandBlockItemType()
  115. : BasicBlockItemType(ID, "Sand", 0, 0, ModelInfo("itemCube", "blocks.ltdb/sand.png", 6))
  116. {}
  117. Item* SandBlockItemType::createItem() const
  118. {
  119. BasicBlockItem* item = new BasicBlockItem(this, SandBlockType::INSTANCE, "Sand");
  120. initializeItem(item, 0, 0, 0.5f, 0, 1);
  121. return item;
  122. }
  123. // Oak Wood
  124. OakBlockType::OakBlockType()
  125. : BasicBlockType(ID, OakBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/oak.png", 6))
  126. {
  127. hardness = 1.5f;
  128. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  129. }
  130. OakBlockItemType::OakBlockItemType()
  131. : BasicBlockItemType(ID, "Oak", 0, 0, ModelInfo("itemCube", "blocks.ltdb/oak.png", 6))
  132. {}
  133. Item* OakBlockItemType::createItem() const
  134. {
  135. BasicBlockItem* item = new BasicBlockItem(this, OakBlockType::INSTANCE, "Oak");
  136. initializeItem(item, 0, 0, 1.5f, 0, 1);
  137. return item;
  138. }
  139. // Oak Leaves
  140. OakLeavesBlockType::OakLeavesBlockType()
  141. : BasicBlockType(ID, OakLeavesBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/leaves.png", 6))
  142. {
  143. hardness = 0.1f;
  144. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  145. }
  146. Block* OakLeavesBlockType::createBlock(Framework::Vec3<int> position) const
  147. {
  148. AdditionalItemSpawningBlock* block = new AdditionalItemSpawningBlock(this, 0, position);
  149. block->addSpawn({ 1, 1, 0.015, OakSeblingBlockItemType::ID });
  150. return block;
  151. }
  152. OakLeavesBlockItemType::OakLeavesBlockItemType()
  153. : BasicBlockItemType(ID, "Leaves", 0, 0, ModelInfo("itemCube", "blocks.ltdb/leaves.png", 6))
  154. {}
  155. Item* OakLeavesBlockItemType::createItem() const
  156. {
  157. BasicBlockItem* item = new BasicBlockItem(this, OakLeavesBlockType::INSTANCE, "Oak Leaves");
  158. initializeItem(item, 0, 0, 0.1f, 0, 1);
  159. return item;
  160. }
  161. // Oak Sebling
  162. OakSeblingBlockType::OakSeblingBlockType()
  163. : BasicBlockType(ID, OakSeblingBlockItemType::ID, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
  164. {
  165. hardness = 0.1f;
  166. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  167. }
  168. Block* OakSeblingBlockType::createBlock(Framework::Vec3<int> position) const
  169. {
  170. // TODO: add sebling block
  171. return BasicBlockType::createBlock(position);
  172. }
  173. OakSeblingBlockItemType::OakSeblingBlockItemType()
  174. : BasicBlockItemType(ID, "Oak Sebling", 0, 0, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
  175. {}
  176. Item* OakSeblingBlockItemType::createItem() const
  177. {
  178. BasicBlockItem* item = new BasicBlockItem(this, OakSeblingBlockType::INSTANCE, "Oak Sebling");
  179. initializeItem(item, 0, 0, 0.1f, 0, 1);
  180. return item;
  181. }
  182. // Gravel
  183. GravelBlockType::GravelBlockType()
  184. : BasicBlockType(ID, GravelBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/gravel.png", 6))
  185. {
  186. hardness = 0.75f;
  187. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  188. }
  189. GravelBlockItemType::GravelBlockItemType()
  190. : BasicBlockItemType(ID, "Gravel", 0, 0, ModelInfo("itemCube", "blocks.ltdb/gravel.png", 6))
  191. {}
  192. Item* GravelBlockItemType::createItem() const
  193. {
  194. BasicBlockItem* item = new BasicBlockItem(this, GravelBlockType::INSTANCE, "Gravel");
  195. initializeItem(item, 0, 0, 0.75f, 0, 1);
  196. return item;
  197. }
  198. // Granite
  199. GraniteBlockType::GraniteBlockType()
  200. : BasicBlockType(ID, GraniteBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/granite.png", 6))
  201. {
  202. hardness = 3.f;
  203. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  204. }
  205. GraniteBlockItemType::GraniteBlockItemType()
  206. : BasicBlockItemType(ID, "Granite", 0, 0, ModelInfo("itemCube", "blocks.ltdb/granite.png", 6))
  207. {}
  208. Item* GraniteBlockItemType::createItem() const
  209. {
  210. BasicBlockItem* item = new BasicBlockItem(this, GraniteBlockType::INSTANCE, "Granite");
  211. initializeItem(item, 0, 0, 3.f, 0, 1);
  212. return item;
  213. }
  214. // Cobble
  215. CobbleBlockType::CobbleBlockType()
  216. : BasicBlockType(ID, CobbleBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/cobble.png", 6))
  217. {
  218. hardness = 1.f;
  219. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  220. }
  221. CobbleBlockItemType::CobbleBlockItemType()
  222. : BasicBlockItemType(ID, "Cobble", 0, 0, ModelInfo("itemCube", "blocks.ltdb/cobble.png", 6))
  223. {}
  224. Item* CobbleBlockItemType::createItem() const
  225. {
  226. BasicBlockItem* item = new BasicBlockItem(this, CobbleBlockType::INSTANCE, "Cobble");
  227. initializeItem(item, 0, 0, 1.f, 0, 1);
  228. return item;
  229. }
  230. // Birch Wood
  231. BirchBlockType::BirchBlockType()
  232. : BasicBlockType(ID, BirchBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/birch.png", 6))
  233. {
  234. hardness = 1.5f;
  235. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  236. }
  237. BirchBlockItemType::BirchBlockItemType()
  238. : BasicBlockItemType(ID, "Birch", 0, 0, ModelInfo("itemCube", "blocks.ltdb/birch.png", 6))
  239. {}
  240. Item* BirchBlockItemType::createItem() const
  241. {
  242. BasicBlockItem* item = new BasicBlockItem(this, BirchBlockType::INSTANCE, "Birch");
  243. initializeItem(item, 0, 0, 1.5f, 0, 1);
  244. return item;
  245. }
  246. // Birch Leaves
  247. BirchLeavesBlockType::BirchLeavesBlockType()
  248. : BasicBlockType(ID, BirchLeavesBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/leaves.png", 6))
  249. {
  250. hardness = 0.1f;
  251. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  252. }
  253. Block* BirchLeavesBlockType::createBlock(Framework::Vec3<int> position) const
  254. {
  255. AdditionalItemSpawningBlock* block = new AdditionalItemSpawningBlock(this, 0, position);
  256. block->addSpawn({ 1, 1, 0.03, BirchSeblingBlockItemType::ID });
  257. return block;
  258. }
  259. BirchLeavesBlockItemType::BirchLeavesBlockItemType()
  260. : BasicBlockItemType(ID, "Leaves", 0, 0, ModelInfo("itemCube", "blocks.ltdb/leaves.png", 6))
  261. {}
  262. Item* BirchLeavesBlockItemType::createItem() const
  263. {
  264. BasicBlockItem* item = new BasicBlockItem(this, BirchLeavesBlockType::INSTANCE, "Birch Leaves");
  265. initializeItem(item, 0, 0, 0.1f, 0, 1);
  266. return item;
  267. }
  268. // Birch Sebling
  269. BirchSeblingBlockType::BirchSeblingBlockType()
  270. : BasicBlockType(ID, BirchSeblingBlockItemType::ID, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
  271. {
  272. hardness = 0.1f;
  273. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  274. }
  275. Block* BirchSeblingBlockType::createBlock(Framework::Vec3<int> position) const
  276. {
  277. // TODO: add sebling block
  278. return BasicBlockType::createBlock(position);
  279. }
  280. BirchSeblingBlockItemType::BirchSeblingBlockItemType()
  281. : BasicBlockItemType(ID, "Birch Sebling", 0, 0, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
  282. {}
  283. Item* BirchSeblingBlockItemType::createItem() const
  284. {
  285. BasicBlockItem* item = new BasicBlockItem(this, BirchSeblingBlockType::INSTANCE, "Birch Sebling");
  286. initializeItem(item, 0, 0, 0.1f, 0, 1);
  287. return item;
  288. }
  289. // Beech Wood
  290. BeechBlockType::BeechBlockType()
  291. : BasicBlockType(ID, BeechBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/beech.png", 6))
  292. {
  293. hardness = 1.5f;
  294. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  295. }
  296. BeechBlockItemType::BeechBlockItemType()
  297. : BasicBlockItemType(ID, "Beech", 0, 0, ModelInfo("itemCube", "blocks.ltdb/beech.png", 6))
  298. {}
  299. Item* BeechBlockItemType::createItem() const
  300. {
  301. BasicBlockItem* item = new BasicBlockItem(this, BeechBlockType::INSTANCE, "Beech");
  302. initializeItem(item, 0, 0, 1.5f, 0, 1);
  303. return item;
  304. }
  305. // Beech Leaves
  306. BeechLeavesBlockType::BeechLeavesBlockType()
  307. : BasicBlockType(ID, BeechLeavesBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/leaves.png", 6))
  308. {
  309. hardness = 0.1f;
  310. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  311. }
  312. Block* BeechLeavesBlockType::createBlock(Framework::Vec3<int> position) const
  313. {
  314. AdditionalItemSpawningBlock* block = new AdditionalItemSpawningBlock(this, 0, position);
  315. block->addSpawn({ 1, 1, 0.02, BeechSeblingBlockItemType::ID });
  316. return block;
  317. }
  318. BeechLeavesBlockItemType::BeechLeavesBlockItemType()
  319. : BasicBlockItemType(ID, "Leaves", 0, 0, ModelInfo("itemCube", "blocks.ltdb/leaves.png", 6))
  320. {}
  321. Item* BeechLeavesBlockItemType::createItem() const
  322. {
  323. BasicBlockItem* item = new BasicBlockItem(this, BeechLeavesBlockType::INSTANCE, "Beech Leaves");
  324. initializeItem(item, 0, 0, 0.1f, 0, 1);
  325. return item;
  326. }
  327. // Beech Sebling
  328. BeechSeblingBlockType::BeechSeblingBlockType()
  329. : BasicBlockType(ID, BeechSeblingBlockItemType::ID, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
  330. {
  331. hardness = 0.1f;
  332. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  333. }
  334. Block* BeechSeblingBlockType::createBlock(Framework::Vec3<int> position) const
  335. {
  336. // TODO: add sebling block
  337. return BasicBlockType::createBlock(position);
  338. }
  339. BeechSeblingBlockItemType::BeechSeblingBlockItemType()
  340. : BasicBlockItemType(ID, "Beech Sebling", 0, 0, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
  341. {}
  342. Item* BeechSeblingBlockItemType::createItem() const
  343. {
  344. BasicBlockItem* item = new BasicBlockItem(this, BeechSeblingBlockType::INSTANCE, "Beech Sebling");
  345. initializeItem(item, 0, 0, 0.1f, 0, 1);
  346. return item;
  347. }
  348. // Basalt
  349. BasaltBlockType::BasaltBlockType()
  350. : BasicBlockType(ID, BasaltBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/basalt.png", 6))
  351. {
  352. hardness = 2.f;
  353. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  354. }
  355. BasaltBlockItemType::BasaltBlockItemType()
  356. : BasicBlockItemType(ID, "Basalt", 0, 0, ModelInfo("itemCube", "blocks.ltdb/basalt.png", 6))
  357. {}
  358. Item* BasaltBlockItemType::createItem() const
  359. {
  360. BasicBlockItem* item = new BasicBlockItem(this, BasaltBlockType::INSTANCE, "Basalt");
  361. initializeItem(item, 0, 0, 2.f, 0, 1);
  362. return item;
  363. }
  364. // Pine Wood
  365. PineBlockType::PineBlockType()
  366. : BasicBlockType(ID, PineBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/pine.png", 6))
  367. {
  368. hardness = 1.4f;
  369. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  370. }
  371. PineBlockItemType::PineBlockItemType()
  372. : BasicBlockItemType(ID, "Pine", 0, 0, ModelInfo("itemCube", "blocks.ltdb/pine.png", 6))
  373. {}
  374. Item* PineBlockItemType::createItem() const
  375. {
  376. BasicBlockItem* item = new BasicBlockItem(this, PineBlockType::INSTANCE, "Pine");
  377. initializeItem(item, 0, 0, 1.4f, 0, 1);
  378. return item;
  379. }
  380. // Pine Leaves
  381. PineLeavesBlockType::PineLeavesBlockType()
  382. : BasicBlockType(ID, PineLeavesBlockItemType::ID, ModelInfo("cube", "blocks.ltdb/leaves.png", 6))
  383. {
  384. hardness = 0.1f;
  385. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  386. }
  387. PineLeavesBlockItemType::PineLeavesBlockItemType()
  388. : BasicBlockItemType(ID, "Leaves", 0, 0, ModelInfo("itemCube", "blocks.ltdb/leaves.png", 6))
  389. {}
  390. Item* PineLeavesBlockItemType::createItem() const
  391. {
  392. BasicBlockItem* item = new BasicBlockItem(this, PineLeavesBlockType::INSTANCE, "Pine Leaves");
  393. initializeItem(item, 0, 0, 0.1f, 0, 1);
  394. return item;
  395. }
  396. Block* PineLeavesBlockType::createBlock(Framework::Vec3<int> position) const
  397. {
  398. AdditionalItemSpawningBlock* block = new AdditionalItemSpawningBlock(this, 0, position);
  399. block->addSpawn({ 1, 1, 0.025, PineSeblingBlockItemType::ID });
  400. return block;
  401. }
  402. // Pine Sebling
  403. PineSeblingBlockType::PineSeblingBlockType()
  404. : BasicBlockType(ID, PineSeblingBlockItemType::ID, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
  405. {
  406. hardness = 0.1f;
  407. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  408. }
  409. Block* PineSeblingBlockType::createBlock(Framework::Vec3<int> position) const
  410. {
  411. // TODO: add sebling block
  412. return BasicBlockType::createBlock(position);
  413. }
  414. PineSeblingBlockItemType::PineSeblingBlockItemType()
  415. : BasicBlockItemType(ID, "Pine Sebling", 0, 0, ModelInfo("blocks.m3/sebling", "blocks.ltdb/sebling.png", 1))
  416. {}
  417. Item* PineSeblingBlockItemType::createItem() const
  418. {
  419. BasicBlockItem* item = new BasicBlockItem(this, PineSeblingBlockType::INSTANCE, "Pine Sebling");
  420. initializeItem(item, 0, 0, 0.1f, 0, 1);
  421. return item;
  422. }