LightSources.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include "LightSources.h"
  2. #include "Game.h"
  3. LightSourceItem::LightSourceItem(int itemType, int blockType, Framework::Text name)
  4. : BasicBlockItem(itemType, blockType, name, 0),
  5. color(0XFFFFFFFF)
  6. {}
  7. bool LightSourceItem::canBeStackedWith(const Item* zItem) const
  8. {
  9. const LightSourceItem* other = dynamic_cast<const LightSourceItem*>(zItem);
  10. return BasicBlockItem::canBeStackedWith(zItem) && other->color == color;
  11. }
  12. LightSourceItemType::LightSourceItemType(Framework::Text name,
  13. ModelInfo* model,
  14. Framework::Text blockTypeName,
  15. int maxStackSize)
  16. : BasicBlockItemType(
  17. name, model, true, true, 0.f, 1.f, blockTypeName, 0, maxStackSize)
  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, getBlockTypeId(), 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, pos, dimensionId, false)
  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. Framework::Text itemTypeName,
  61. ModelInfo* model,
  62. Framework::Text name,
  63. float hardness,
  64. int mapColor,
  65. int lightColor,
  66. Framework::RCArray<Framework::Text> groupNames)
  67. : BlockType(0, model, 1, 1, 1, name, false, mapColor, groupNames, hardness),
  68. itemTypeName(itemTypeName),
  69. transparent(1),
  70. passable(1),
  71. interactable(1),
  72. lightColor(lightColor)
  73. {}
  74. void BasicLightSourceBlockType::createSuperBlock(
  75. Block* zBlock, Item* zItem) const
  76. {
  77. BlockType::createSuperBlock(zBlock, zItem);
  78. BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
  79. block->transparent = transparent;
  80. block->passable = passable;
  81. block->hp = (float)getInitialMaxHP();
  82. block->maxHP = (float)getInitialMaxHP();
  83. block->hardness = getHardness();
  84. block->speedModifier = 1;
  85. block->interactable = interactable;
  86. block->setLight(lightColor);
  87. if (zItem)
  88. {
  89. dynamic_cast<BasicLightSource*>(zBlock)->setLight(
  90. dynamic_cast<LightSourceItem*>(zItem)->color);
  91. }
  92. }
  93. void BasicLightSourceBlockType::loadSuperBlock(
  94. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  95. {
  96. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  97. BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
  98. if (!block)
  99. throw "BasicLightSourceBlockType::loadSuperBlock was called with a "
  100. "block witch is not an instance of BasicLightSource";
  101. zReader->lese((char*)&block->lightEmisionColor, 4);
  102. }
  103. void BasicLightSourceBlockType::saveSuperBlock(
  104. Block* zBlock, Framework::StreamWriter* zWriter) const
  105. {
  106. BlockType::saveSuperBlock(zBlock, zWriter);
  107. BasicLightSource* block = dynamic_cast<BasicLightSource*>(zBlock);
  108. if (!block)
  109. throw "BasicLightSourceBlockType::saveSuperBlock was called with a "
  110. "block witch is not an instance of BasicLightSource";
  111. zWriter->schreibe((char*)&block->lightEmisionColor, 4);
  112. }
  113. bool BasicLightSourceBlockType::initialize(Game* zGame)
  114. {
  115. if (itemTypeName.getLength())
  116. {
  117. itemTypeId = zGame->getItemTypeId(itemTypeName);
  118. }
  119. else
  120. {
  121. itemTypeId = 0;
  122. }
  123. return itemTypeId >= 0 && BlockType::initialize(zGame);
  124. }
  125. Block* BasicLightSourceBlockType::createBlock(
  126. Framework::Vec3<int> position, int dimensionId) const
  127. {
  128. return new BasicLightSource(getId(), position, dimensionId);
  129. }
  130. Item* BasicLightSourceBlockType::createItem() const
  131. {
  132. return Game::INSTANCE->zItemType(itemTypeId)->createItem();
  133. }
  134. int BasicLightSourceBlockType::getLightColor() const
  135. {
  136. return lightColor;
  137. }
  138. Framework::Text BasicLightSourceBlockType::getItemTypeName() const
  139. {
  140. return itemTypeName;
  141. }
  142. ItemType* BasicLightSourceBlockType::createItemType() const
  143. {
  144. return (new LightSourceItemType(getItemTypeName(),
  145. new ModelInfo(zModel()->getModelPath(),
  146. zModel()->getTexturePaths(),
  147. zModel()->isTransparent(),
  148. zModel()->getSize() / 2.f),
  149. getName(), 50))
  150. ->setColor(0x00F69A54);
  151. }
  152. BasicLightSourceBlockTypeFactory::BasicLightSourceBlockTypeFactory()
  153. : SubTypeFactory()
  154. {}
  155. BasicLightSourceBlockType* BasicLightSourceBlockTypeFactory::fromJson(
  156. Framework::JSON::JSONObject* zJson) const
  157. {
  158. Framework::RCArray<Framework::Text> groupNames;
  159. for (Framework::JSON::JSONValue* value :
  160. *zJson->zValue("groupNames")->asArray())
  161. {
  162. groupNames.add(new Framework::Text(value->asString()->getString()));
  163. }
  164. return new BasicLightSourceBlockType(
  165. zJson->zValue("itemType")->asString()->getString(),
  166. Game::INSTANCE->zTypeRegistry()->fromJson<ModelInfo>(
  167. zJson->zValue("model")),
  168. zJson->zValue("name")->asString()->getString(),
  169. (float)zJson->zValue("hardness")->asNumber()->getNumber(),
  170. (int)zJson->zValue("mapColor")->asString()->getString(),
  171. (int)zJson->zValue("lightColor")->asString()->getString(),
  172. groupNames);
  173. }
  174. Framework::JSON::JSONObject* BasicLightSourceBlockTypeFactory::toJson(
  175. BasicLightSourceBlockType* zObject) const
  176. {
  177. Framework::JSON::JSONObject* result = new Framework::JSON::JSONObject();
  178. result->addValue("itemType",
  179. new Framework::JSON::JSONString(zObject->getItemTypeName()));
  180. result->addValue(
  181. "model", Game::INSTANCE->zTypeRegistry()->toJson(zObject->zModel()));
  182. result->addValue(
  183. "name", new Framework::JSON::JSONString(zObject->getName()));
  184. result->addValue(
  185. "hardness", new Framework::JSON::JSONNumber(zObject->getHardness()));
  186. result->addValue(
  187. "mapColor", new Framework::JSON::JSONString(zObject->getMapColor()));
  188. result->addValue("lightColor",
  189. new Framework::JSON::JSONString(zObject->getLightColor()));
  190. Framework::JSON::JSONArray* groupNames = new Framework::JSON::JSONArray();
  191. for (Framework::Text* groupName : zObject->getGroupNames())
  192. {
  193. groupNames->addValue(new Framework::JSON::JSONString(*groupName));
  194. }
  195. result->addValue("groupNames", groupNames);
  196. return result;
  197. }
  198. Framework::JSON::Validator::JSONValidator*
  199. BasicLightSourceBlockTypeFactory::getValidator(
  200. Framework::JSON::Validator::ObjectValidationBuilder<
  201. Framework::JSON::Validator::JSONValidator>* builder) const
  202. {
  203. return builder->withRequiredString("itemType")
  204. ->finishString()
  205. ->withRequiredAttribute(
  206. "model", Game::INSTANCE->zTypeRegistry()->getValidator<ModelInfo>())
  207. ->withRequiredString("name")
  208. ->finishString()
  209. ->withRequiredNumber("hardness")
  210. ->withDefault(1.0)
  211. ->finishNumber()
  212. ->withRequiredString("mapColor")
  213. ->finishString()
  214. ->withRequiredString("lightColor")
  215. ->finishString()
  216. ->withRequiredArray("groupNames")
  217. ->withDefault(new Framework::JSON::JSONArray())
  218. ->addAcceptedStringInArray()
  219. ->finishString()
  220. ->finishArray()
  221. ->finishObject();
  222. }
  223. Framework::Text BasicLightSourceBlockTypeFactory::getTypeToken() const
  224. {
  225. return "lightSource";
  226. }