LightSources.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include "LightSources.h"
  2. LightSourceItem::LightSourceItem(const ItemType* zType, const BlockType* zPlacedBlockType, const char* name)
  3. : BasicBlockItem(zType, zPlacedBlockType, name),
  4. color(0XFFFFFFFF)
  5. {}
  6. bool LightSourceItem::canBeStackedWith(const Item* zItem) const
  7. {
  8. const LightSourceItem* other = dynamic_cast<const LightSourceItem*>(zItem);
  9. return BasicBlockItem::canBeStackedWith(zItem) && other->color == color;
  10. }
  11. LightSourceItemType::LightSourceItemType(int id, const char* name, const ModelInfo model)
  12. :BasicBlockItemType(id, name, 0, 0, model)
  13. {}
  14. void LightSourceItemType::loadSuperItem(Item* zItem, Framework::StreamReader* zReader) const
  15. {
  16. LightSourceItem* item = dynamic_cast<LightSourceItem*>(zItem);
  17. zReader->lese((char*)&item->color, 4);
  18. BasicBlockItemType::loadSuperItem(zItem, zReader);
  19. }
  20. void LightSourceItemType::saveSuperItem(const Item* zItem, Framework::StreamWriter* zWriter) const
  21. {
  22. const LightSourceItem* item = dynamic_cast<const LightSourceItem*>(zItem);
  23. zWriter->schreibe((char*)&item->color, 4);
  24. BasicBlockItemType::saveSuperItem(zItem, zWriter);
  25. }
  26. void LightSourceItemType::initializeItem(LightSourceItem* zItem, int color, bool transparent, bool passable, float hardness, int toolId, float speedModifier) const
  27. {
  28. zItem->color = color;
  29. BasicBlockItemType::initializeItem(zItem, transparent, passable, hardness, toolId, speedModifier);
  30. }
  31. BasicLightSource::BasicLightSource(const BlockType* zType, Framework::Vec3<int> pos)
  32. : Block(zType, 0, pos, 0)
  33. {}
  34. void BasicLightSource::setLight(int light)
  35. {
  36. lightEmisionColor[0] = (unsigned char)((light >> 16) & 0xFF);
  37. lightEmisionColor[1] = (unsigned char)((light >> 8) & 0xFF);
  38. lightEmisionColor[2] = (unsigned char)(light & 0xFF);
  39. }
  40. bool BasicLightSource::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  41. {
  42. return 0;
  43. }
  44. void BasicLightSource::onPostTick()
  45. {}
  46. BasicLightSourceBlockType::BasicLightSourceBlockType(int typeId, int itemTypeId, ModelInfo model)
  47. : BlockType(typeId, 0, model, 1, 1, 1),
  48. itemType(itemTypeId),
  49. transparent(1),
  50. passable(1),
  51. hardness(1.f),
  52. interactable(1)
  53. {}
  54. void BasicLightSourceBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  55. {
  56. BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
  57. if (!block)
  58. throw "BasicLightSourceBlockType::createSuperBlock was called with a block witch is not an instance of BasicLightSource";
  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 = 0;
  65. block->speedModifier = 1;
  66. block->interactable = interactable;
  67. block->setLight(color);
  68. BlockType::createSuperBlock(zBlock, zItem);
  69. if (zItem)
  70. {
  71. dynamic_cast<BasicLightSource*>(zBlock)->setLight(dynamic_cast<LightSourceItem*>(zItem)->color);
  72. }
  73. }
  74. void BasicLightSourceBlockType::loadSuperBlock(Block* zBlock, Framework::StreamReader* zReader) const
  75. {
  76. BlockType::loadSuperBlock(zBlock, zReader);
  77. BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
  78. if (!block)
  79. throw "BasicLightSourceBlockType::loadSuperBlock was called with a block witch is not an instance of BasicLightSource";
  80. zReader->lese((char*)&block->lightEmisionColor, 4);
  81. }
  82. void BasicLightSourceBlockType::saveSuperBlock(Block* zBlock, Framework::StreamWriter* zWriter) const
  83. {
  84. BlockType::saveSuperBlock(zBlock, zWriter);
  85. BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
  86. if (!block)
  87. throw "BasicLightSourceBlockType::saveSuperBlock was called with a block witch is not an instance of BasicLightSource";
  88. zWriter->schreibe((char*)&block->lightEmisionColor, 4);
  89. }
  90. Block* BasicLightSourceBlockType::createBlock(Framework::Vec3<int> position) const
  91. {
  92. return new BasicLightSource(this, position);
  93. }
  94. Item* BasicLightSourceBlockType::createItem() const
  95. {
  96. return StaticRegistry<ItemType>::INSTANCE.zElement(itemType)->createItem();
  97. }
  98. TorchBlockItemType::TorchBlockItemType()
  99. : LightSourceItemType(ID, "Torch", ModelInfo("items.m3/stick", "blocks.ltdb/torch.png", 6))
  100. {}
  101. Item* TorchBlockItemType::createItem() const
  102. {
  103. LightSourceItem* item = new LightSourceItem(this, TorchBlockType::INSTANCE, "Torch");
  104. initializeItem(item, 0x00F69A54);
  105. return item;
  106. }
  107. TorchBlockType::TorchBlockType()
  108. : BasicLightSourceBlockType(ID, TorchBlockItemType::ID, ModelInfo("blocks.m3/torch", "blocks.ltdb/torch.png", 6))
  109. {
  110. hardness = 0.f;
  111. color = 0x00F69A54;
  112. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  113. }