LightSources.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 hp, float maxHP, float hardness, int toolId, float speedModifier) const
  27. {
  28. zItem->color = color;
  29. BasicBlockItemType::initializeItem(zItem, transparent, passable, hp, maxHP, 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. if (zItem)
  57. {
  58. BlockType::createSuperBlock(zBlock, zItem);
  59. dynamic_cast<BasicLightSource*>(zBlock)->setLight(dynamic_cast<LightSourceItem*>(zItem)->color);
  60. }
  61. else
  62. {
  63. BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
  64. if (!block)
  65. throw "BasicLightSourceBlockType::createSuperBlock was called with a block witch is not an instance of BasicLightSource";
  66. block->transparent = transparent;
  67. block->passable = passable;
  68. block->hp = (float)getInitialMaxHP();
  69. block->maxHP = (float)getInitialMaxHP();
  70. block->hardness = hardness;
  71. block->zTool = 0;
  72. block->speedModifier = 1;
  73. block->interactable = interactable;
  74. block->setLight(color);
  75. }
  76. }
  77. void BasicLightSourceBlockType::loadSuperBlock(Block* zBlock, Framework::StreamReader* zReader) const
  78. {
  79. BlockType::loadSuperBlock(zBlock, zReader);
  80. BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
  81. if (!block)
  82. throw "BasicLightSourceBlockType::loadSuperBlock was called with a block witch is not an instance of BasicLightSource";
  83. zReader->lese((char*)&block->lightEmisionColor, 4);
  84. }
  85. void BasicLightSourceBlockType::saveSuperBlock(Block* zBlock, Framework::StreamWriter* zWriter) const
  86. {
  87. BlockType::saveSuperBlock(zBlock, zWriter);
  88. BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
  89. if (!block)
  90. throw "BasicLightSourceBlockType::saveSuperBlock was called with a block witch is not an instance of BasicLightSource";
  91. zWriter->schreibe((char*)&block->lightEmisionColor, 4);
  92. }
  93. Block* BasicLightSourceBlockType::createBlock(Framework::Vec3<int> position) const
  94. {
  95. return new BasicLightSource(this, position);
  96. }
  97. Item* BasicLightSourceBlockType::createItem() const
  98. {
  99. return StaticRegistry<ItemType>::INSTANCE.zElement(itemType)->createItem();
  100. }
  101. TorchBlockItemType::TorchBlockItemType()
  102. : LightSourceItemType(ID, "Torch", ModelInfo("items.m3/stick", "blocks.ltdb/torch.png", 6))
  103. {}
  104. Item* TorchBlockItemType::createItem() const
  105. {
  106. LightSourceItem* item = new LightSourceItem(this, TorchBlockType::INSTANCE, "Torch");
  107. initializeItem(item, 0x00F69A54);
  108. return item;
  109. }
  110. TorchBlockType::TorchBlockType()
  111. : BasicLightSourceBlockType(ID, TorchBlockItemType::ID, ModelInfo("blocks.m3/torch", "blocks.ltdb/torch.png", 6))
  112. {
  113. hardness = 0.f;
  114. color = 0x00F69A54;
  115. defaultBlock = createBlockAt({ 0, 0, 0 }, 0);
  116. }