LightSources.cpp 4.4 KB

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