Grass.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "Grass.h"
  2. #include "AddEntityUpdate.h"
  3. #include "Game.h"
  4. #include "ItemEntity.h"
  5. GrassBlock::GrassBlock(int typeId,
  6. const ItemType* zTool,
  7. Framework::Vec3<int> pos,
  8. int dimensionId)
  9. : Block(typeId, zTool, pos, dimensionId, 0)
  10. {
  11. tickSource = 1;
  12. }
  13. bool GrassBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  14. {
  15. // TODO: spread to neighbor blocks if light level is hight enought
  16. return 0;
  17. }
  18. void GrassBlock::onPostTick() {}
  19. void GrassBlock::onDestroy()
  20. {
  21. if (!deadAndRemoved)
  22. {
  23. for (int i = 0; i < 6; i++)
  24. {
  25. if (neighbourTypes[i] == BlockTypeEnum::NO_BLOCK)
  26. {
  27. Framework::Vec3<int> pos
  28. = getPos() + getDirection(getDirectionFromIndex(i));
  29. Game::INSTANCE->zDimension(getDimensionId())
  30. ->placeBlock(pos,
  31. Game::INSTANCE->zGenerator()->generateSingleBlock(
  32. pos, getDimensionId()));
  33. }
  34. }
  35. if ((double)rand() / RAND_MAX < 0.1)
  36. {
  37. ItemStack* spawnedItems = StaticRegistry<ItemType>::INSTANCE
  38. .zElement(ItemTypeEnum::WHEAT_SEED)
  39. ->createItemStack(1);
  40. if (spawnedItems)
  41. {
  42. ItemEntity* itemEntity
  43. = (ItemEntity*)StaticRegistry<EntityType>::INSTANCE
  44. .zElement(EntityTypeEnum::ITEM)
  45. ->createEntity(
  46. location
  47. + Framework::Vec3<float>(0.5f, 0.5f, 0.5f),
  48. getDimensionId(),
  49. Game::INSTANCE->getNextEntityId());
  50. itemEntity->unsaveAddItem(spawnedItems, NO_DIRECTION, 0);
  51. spawnedItems->release();
  52. Game::INSTANCE->requestWorldUpdate(
  53. new AddEntityUpdate(itemEntity, getDimensionId()));
  54. }
  55. }
  56. for (MultiblockStructure* structure : structures)
  57. structure->onBlockRemoved(this);
  58. Game::INSTANCE->zDimension(getDimensionId())
  59. ->placeBlock(
  60. getPos(), BlockTypeEnum::AIR); // this will be deleted here
  61. }
  62. }
  63. GrassBlockType::GrassBlockType(
  64. int typeId, int itemTypeId, ModelInfo model, const char* name, int mapColor)
  65. : BlockType(typeId, 0, model, 1, 10, 0, name, false, mapColor),
  66. itemType(itemTypeId),
  67. transparent(true),
  68. passable(true),
  69. hardness(0.1f),
  70. zTool(0),
  71. speedModifier(0.5f),
  72. interactable(1)
  73. {}
  74. void GrassBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  75. {
  76. GrassBlock* block = dynamic_cast<GrassBlock*>(zBlock);
  77. block->transparent = transparent;
  78. block->passable = passable;
  79. block->hp = (float)getInitialMaxHP();
  80. block->maxHP = (float)getInitialMaxHP();
  81. block->hardness = hardness;
  82. block->zTool = zTool;
  83. block->speedModifier = speedModifier;
  84. block->interactable = interactable;
  85. BlockType::createSuperBlock(zBlock, zItem);
  86. }
  87. void GrassBlockType::loadSuperBlock(
  88. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  89. {
  90. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  91. }
  92. void GrassBlockType::saveSuperBlock(
  93. Block* zBlock, Framework::StreamWriter* zWriter) const
  94. {
  95. BlockType::saveSuperBlock(zBlock, zWriter);
  96. }
  97. Item* GrassBlockType::createItem() const
  98. {
  99. return 0;
  100. }
  101. Block* GrassBlockType::createBlock(
  102. Framework::Vec3<int> position, int dimensionId) const
  103. {
  104. return new GrassBlock(getId(), zTool, position, dimensionId);
  105. }