BasicBlock.cpp 13 KB

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