LightSources.cpp 4.1 KB

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