LightSources.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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),
  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. if (!block)
  72. throw "BasicLightSourceBlockType::createSuperBlock was called with a "
  73. "block witch is not an instance of BasicLightSource";
  74. block->transparent = transparent;
  75. block->passable = passable;
  76. block->hp = (float)getInitialMaxHP();
  77. block->maxHP = (float)getInitialMaxHP();
  78. block->hardness = hardness;
  79. block->zTool = 0;
  80. block->speedModifier = 1;
  81. block->interactable = interactable;
  82. block->setLight(color);
  83. BlockType::createSuperBlock(zBlock, zItem);
  84. if (zItem)
  85. {
  86. dynamic_cast<BasicLightSource*>(zBlock)->setLight(
  87. dynamic_cast<LightSourceItem*>(zItem)->color);
  88. }
  89. }
  90. void BasicLightSourceBlockType::loadSuperBlock(
  91. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  92. {
  93. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  94. BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
  95. if (!block)
  96. throw "BasicLightSourceBlockType::loadSuperBlock was called with a "
  97. "block witch is not an instance of BasicLightSource";
  98. zReader->lese((char*)&block->lightEmisionColor, 4);
  99. }
  100. void BasicLightSourceBlockType::saveSuperBlock(
  101. Block* zBlock, Framework::StreamWriter* zWriter) const
  102. {
  103. BlockType::saveSuperBlock(zBlock, zWriter);
  104. BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
  105. if (!block)
  106. throw "BasicLightSourceBlockType::saveSuperBlock was called with a "
  107. "block witch is not an instance of BasicLightSource";
  108. zWriter->schreibe((char*)&block->lightEmisionColor, 4);
  109. }
  110. Block* BasicLightSourceBlockType::createBlock(
  111. Framework::Vec3<int> position) const
  112. {
  113. return new BasicLightSource(getId(), position);
  114. }
  115. Item* BasicLightSourceBlockType::createItem() const
  116. {
  117. return StaticRegistry<ItemType>::INSTANCE.zElement(itemType)->createItem();
  118. }
  119. BasicLightSourceBlockType* BasicLightSourceBlockType::setHardness(
  120. float hardness)
  121. {
  122. this->hardness = hardness;
  123. return this;
  124. }
  125. BasicLightSourceBlockType* BasicLightSourceBlockType::setColor(int color)
  126. {
  127. this->color = color;
  128. return this;
  129. }