LightSources.cpp 8.2 KB

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