Grass.cpp 3.8 KB

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