BasicBlock.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #include "AddEntityUpdate.h"
  2. #include "BasicBlocks.h"
  3. #include "Game.h"
  4. #include "ItemEntity.h"
  5. #include "TreeSeblingBlock.h"
  6. BasicBlock::BasicBlock(int typeId, ItemType* zTool, Framework::Vec3<int> pos)
  7. : Block(typeId, zTool, pos, false)
  8. {}
  9. bool BasicBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  10. {
  11. return 0;
  12. }
  13. void BasicBlock::onPostTick() {}
  14. AdditionalItemSpawningBlock::AdditionalItemSpawningBlock(
  15. int typeId, ItemType* zTool, Framework::Vec3<int> pos)
  16. : BasicBlock(typeId, 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
  29. + (int)((config.max - config.min)
  30. * ((double)rand() / RAND_MAX));
  31. if (amount > 0)
  32. {
  33. ItemStack* spawnedItems = StaticRegistry<ItemType>::INSTANCE
  34. .zElement(config.itemType)
  35. ->createItemStack(amount);
  36. if (spawnedItems)
  37. {
  38. ItemEntity* itemEntity
  39. = (ItemEntity*)StaticRegistry<EntityType>::INSTANCE
  40. .zElement(EntityTypeEnum::ITEM)
  41. ->createEntity(location
  42. + Framework::Vec3<float>(
  43. 0.5f, 0.5f, 0.5f),
  44. getDimensionId(),
  45. Game::INSTANCE->getNextEntityId());
  46. itemEntity->unsaveAddItem(spawnedItems, NO_DIRECTION, 0);
  47. spawnedItems->release();
  48. Game::INSTANCE->requestWorldUpdate(
  49. new AddEntityUpdate(itemEntity, getDimensionId()));
  50. }
  51. }
  52. }
  53. }
  54. BasicBlock::onDestroy();
  55. }
  56. BasicBlockType::BasicBlockType(
  57. int typeId, int itemTypeId, ModelInfo model, const char* name, int mapColor)
  58. : BasicBlockType(
  59. typeId,
  60. itemTypeId,
  61. model,
  62. [typeId](Framework::Vec3<int> pos) {
  63. return new BasicBlock(typeId, 0, pos);
  64. },
  65. name,
  66. mapColor)
  67. {}
  68. BasicBlockType::BasicBlockType(int typeId,
  69. int itemTypeId,
  70. ModelInfo model,
  71. std::function<Block*(Framework::Vec3<int>)> creatBlockCustom,
  72. const char* name,
  73. int mapColor)
  74. : BlockType(typeId, 0, model, 1, 100, 0, name, false, mapColor),
  75. itemType(itemTypeId),
  76. transparent(0),
  77. passable(0),
  78. hardness(1.f),
  79. zTool(0),
  80. speedModifier(1.f),
  81. interactable(1),
  82. creatBlockCustom(creatBlockCustom)
  83. {}
  84. void BasicBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  85. {
  86. BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
  87. block->transparent = transparent;
  88. block->passable = passable;
  89. block->hp = (float)getInitialMaxHP();
  90. block->maxHP = (float)getInitialMaxHP();
  91. block->hardness = hardness;
  92. block->zTool = zTool;
  93. block->speedModifier = speedModifier;
  94. block->interactable = interactable;
  95. BlockType::createSuperBlock(zBlock, zItem);
  96. }
  97. Block* BasicBlockType::createBlock(Framework::Vec3<int> position) const
  98. {
  99. return creatBlockCustom(position);
  100. }
  101. Item* BasicBlockType::createItem() const
  102. {
  103. return StaticRegistry<ItemType>::INSTANCE.zElement(itemType)->createItem();
  104. }
  105. BasicBlockType* BasicBlockType::setHardness(float hardness)
  106. {
  107. this->hardness = hardness;
  108. return this;
  109. }
  110. BasicBlockType* BasicBlockType::setTransparent(bool transparent)
  111. {
  112. this->transparent = transparent;
  113. return this;
  114. }