LightSources.cpp 4.3 KB

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