LightSources.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include "LightSources.h"
  2. #include "Game.h"
  3. LightSourceItem::LightSourceItem(
  4. int itemType, int blockType, Framework::Text name)
  5. : BasicBlockItem(itemType, blockType, name, 0),
  6. color(0XFFFFFFFF)
  7. {}
  8. bool LightSourceItem::canBeStackedWith(const Item* zItem) const
  9. {
  10. const LightSourceItem* other = dynamic_cast<const LightSourceItem*>(zItem);
  11. return BasicBlockItem::canBeStackedWith(zItem) && other->color == color;
  12. }
  13. LightSourceItemType::LightSourceItemType(Framework::Text name,
  14. ModelInfo* model,
  15. Framework::Text blockTypeName,
  16. int maxStackSize,
  17. Framework::RCArray<Framework::Text> groups)
  18. : BasicBlockItemType(name,
  19. model,
  20. true,
  21. true,
  22. 0.f,
  23. 1.f,
  24. blockTypeName,
  25. 0,
  26. maxStackSize,
  27. groups)
  28. {}
  29. void LightSourceItemType::loadSuperItem(
  30. Item* zItem, Framework::StreamReader* zReader) const
  31. {
  32. LightSourceItem* item = dynamic_cast<LightSourceItem*>(zItem);
  33. zReader->lese((char*)&item->color, 4);
  34. BasicBlockItemType::loadSuperItem(zItem, zReader);
  35. }
  36. void LightSourceItemType::saveSuperItem(
  37. const Item* zItem, Framework::StreamWriter* zWriter) const
  38. {
  39. const LightSourceItem* item = dynamic_cast<const LightSourceItem*>(zItem);
  40. zWriter->schreibe((char*)&item->color, 4);
  41. BasicBlockItemType::saveSuperItem(zItem, zWriter);
  42. }
  43. Item* LightSourceItemType::createItem() const
  44. {
  45. LightSourceItem* item = new LightSourceItem(id, getBlockTypeId(), name);
  46. item->color = color;
  47. return item;
  48. }
  49. LightSourceItemType* LightSourceItemType::setColor(int color)
  50. {
  51. this->color = color;
  52. return this;
  53. }
  54. BasicLightSource::BasicLightSource(
  55. int typeId, Framework::Vec3<int> pos, int dimensionId)
  56. : Block(typeId, pos, dimensionId, false)
  57. {}
  58. void BasicLightSource::setLight(int light)
  59. {
  60. lightEmisionColor[0] = (unsigned char)((light >> 16) & 0xFF);
  61. lightEmisionColor[1] = (unsigned char)((light >> 8) & 0xFF);
  62. lightEmisionColor[2] = (unsigned char)(light & 0xFF);
  63. }
  64. bool BasicLightSource::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  65. {
  66. return 0;
  67. }
  68. void BasicLightSource::onPostTick() {}
  69. BasicLightSourceBlockType::BasicLightSourceBlockType()
  70. : BlockType(),
  71. itemTypeName(),
  72. itemTypeId(0),
  73. transparent(1),
  74. passable(1),
  75. interactable(1),
  76. lightColor(0XFFFFFFFF)
  77. {}
  78. void BasicLightSourceBlockType::createSuperBlock(
  79. Block* zBlock, Item* zItem) const
  80. {
  81. BlockType::createSuperBlock(zBlock, zItem);
  82. BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
  83. block->transparent = transparent;
  84. block->passable = passable;
  85. block->hp = (float)getInitialMaxHP();
  86. block->maxHP = (float)getInitialMaxHP();
  87. block->hardness = getHardness();
  88. block->speedModifier = 1;
  89. block->interactable = interactable;
  90. block->setLight(lightColor);
  91. if (zItem)
  92. {
  93. dynamic_cast<BasicLightSource*>(zBlock)->setLight(
  94. dynamic_cast<LightSourceItem*>(zItem)->color);
  95. }
  96. }
  97. void BasicLightSourceBlockType::loadSuperBlock(
  98. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  99. {
  100. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  101. BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
  102. if (!block)
  103. throw "BasicLightSourceBlockType::loadSuperBlock was called with a "
  104. "block witch is not an instance of BasicLightSource";
  105. zReader->lese((char*)&block->lightEmisionColor, 4);
  106. }
  107. void BasicLightSourceBlockType::saveSuperBlock(
  108. Block* zBlock, Framework::StreamWriter* zWriter) const
  109. {
  110. BlockType::saveSuperBlock(zBlock, zWriter);
  111. BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
  112. if (!block)
  113. throw "BasicLightSourceBlockType::saveSuperBlock was called with a "
  114. "block witch is not an instance of BasicLightSource";
  115. zWriter->schreibe((char*)&block->lightEmisionColor, 4);
  116. }
  117. bool BasicLightSourceBlockType::initialize(Game* zGame)
  118. {
  119. if (itemTypeName.getLength())
  120. {
  121. itemTypeId = zGame->getItemTypeId(itemTypeName);
  122. }
  123. else
  124. {
  125. itemTypeId = 0;
  126. }
  127. return itemTypeId >= 0 && BlockType::initialize(zGame);
  128. }
  129. Block* BasicLightSourceBlockType::createBlock(
  130. Framework::Vec3<int> position, int dimensionId) const
  131. {
  132. return new BasicLightSource(getId(), position, dimensionId);
  133. }
  134. Item* BasicLightSourceBlockType::createItem() const
  135. {
  136. return Game::INSTANCE->zItemType(itemTypeId)->createItem();
  137. }
  138. ItemType* BasicLightSourceBlockType::createItemType() const
  139. {
  140. return (new LightSourceItemType(getItemTypeName(),
  141. new ModelInfo(zModel()->getModelPath(),
  142. zModel()->getTexturePaths(),
  143. zModel()->isTransparent(),
  144. zModel()->getSize() / 2.f),
  145. getName(),
  146. 50,
  147. getGroupNames()))
  148. ->setColor(0x00F69A54);
  149. }
  150. void BasicLightSourceBlockType::setItemTypeName(Framework::Text itemTypeName)
  151. {
  152. this->itemTypeName = itemTypeName;
  153. }
  154. Framework::Text BasicLightSourceBlockType::getItemTypeName() const
  155. {
  156. return itemTypeName;
  157. }
  158. void BasicLightSourceBlockType::setTransparent(bool transparent)
  159. {
  160. this->transparent = transparent;
  161. }
  162. bool BasicLightSourceBlockType::isTransparent() const
  163. {
  164. return transparent;
  165. }
  166. void BasicLightSourceBlockType::setPassable(bool passable)
  167. {
  168. this->passable = passable;
  169. }
  170. bool BasicLightSourceBlockType::isPassable() const
  171. {
  172. return passable;
  173. }
  174. void BasicLightSourceBlockType::setInteractable(bool interactable)
  175. {
  176. this->interactable = interactable;
  177. }
  178. bool BasicLightSourceBlockType::isInteractable() const
  179. {
  180. return interactable;
  181. }
  182. void BasicLightSourceBlockType::setLightColor(int lightColor)
  183. {
  184. this->lightColor = lightColor;
  185. }
  186. int BasicLightSourceBlockType::getLightColor() const
  187. {
  188. return lightColor;
  189. }
  190. BasicLightSourceBlockTypeFactory::BasicLightSourceBlockTypeFactory()
  191. : BlockTypeFactoryBase()
  192. {}
  193. BasicLightSourceBlockType* BasicLightSourceBlockTypeFactory::createValue(
  194. Framework::JSON::JSONObject* zJson) const
  195. {
  196. return new BasicLightSourceBlockType();
  197. }
  198. void BasicLightSourceBlockTypeFactory::fromJson(
  199. BasicLightSourceBlockType* zResult,
  200. Framework::JSON::JSONObject* zJson) const
  201. {
  202. zResult->setItemTypeName(zJson->zValue("itemType")->asString()->getString());
  203. zResult->setTransparent(zJson->zValue("transparent")->asBool()->getBool());
  204. zResult->setPassable(zJson->zValue("passable")->asBool()->getBool());
  205. zResult->setInteractable(
  206. zJson->zValue("interactable")->asBool()->getBool());
  207. zResult->setLightColor(
  208. (int)zJson->zValue("lightColor")->asString()->getString());
  209. BlockTypeFactoryBase::fromJson(zResult, zJson);
  210. }
  211. void BasicLightSourceBlockTypeFactory::toJson(
  212. BasicLightSourceBlockType* zObject,
  213. Framework::JSON::JSONObject* zResult) const
  214. {
  215. zResult->addValue("itemType",
  216. new Framework::JSON::JSONString(zObject->getItemTypeName()));
  217. zResult->addValue(
  218. "transparent", new Framework::JSON::JSONBool(zObject->isTransparent()));
  219. zResult->addValue(
  220. "passable", new Framework::JSON::JSONBool(zObject->isPassable()));
  221. zResult->addValue("interactable",
  222. new Framework::JSON::JSONBool(zObject->isInteractable()));
  223. zResult->addValue("lightColor",
  224. new Framework::JSON::JSONString(zObject->getLightColor()));
  225. BlockTypeFactoryBase::toJson(zObject, zResult);
  226. }
  227. JSONObjectValidationBuilder* BasicLightSourceBlockTypeFactory::addToValidator(
  228. JSONObjectValidationBuilder* builder) const
  229. {
  230. return BlockTypeFactoryBase::addToValidator(
  231. builder->withRequiredString("itemType")
  232. ->finishString()
  233. ->withRequiredBool("transparent")
  234. ->withDefault(true)
  235. ->finishBool()
  236. ->withRequiredBool("passable")
  237. ->withDefault(true)
  238. ->finishBool()
  239. ->withRequiredBool("interactable")
  240. ->withDefault(true)
  241. ->finishBool()
  242. ->withRequiredString("lightColor")
  243. ->finishString());
  244. }
  245. Framework::Text BasicLightSourceBlockTypeFactory::getTypeToken() const
  246. {
  247. return "lightSource";
  248. }