BasicBlocks.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. #include "BasicBlocks.h"
  2. #include "Game.h"
  3. #include "ItemEntity.h"
  4. #include "ItemSlot.h"
  5. #include "ItemStack.h"
  6. #include "ModelInfo.h"
  7. #include "TreeSeblingBlock.h"
  8. BasicBlock::BasicBlock(int typeId, Framework::Vec3<int> pos, int dimensionId)
  9. : BasicBlock(typeId, pos, dimensionId, false)
  10. {}
  11. BasicBlock::BasicBlock(
  12. int typeId, Framework::Vec3<int> pos, int dimensionId, bool hasInventory)
  13. : Block(typeId, pos, dimensionId, hasInventory)
  14. {}
  15. void BasicBlock::addComponent(BlockComponent* component)
  16. {
  17. components.add(component);
  18. }
  19. Framework::XML::Element* BasicBlock::getTargetUIML() const
  20. {
  21. Framework::XML::Element* element
  22. = Block::getTargetUIML(); // Get the base element from Block class
  23. int index = 0;
  24. Framework::XML::Element* last
  25. = element->getChildCount() > 0
  26. ? element->zChild(element->getChildCount() - 1)
  27. : 0;
  28. for (BlockComponent* component : components)
  29. {
  30. Framework::XML::Element* compElement = component->getTooltipUIML();
  31. if (compElement)
  32. {
  33. if (last)
  34. {
  35. if (!last->hasAttribute("id"))
  36. {
  37. last->setAttribute(
  38. "id", Framework::Text("_component_") + (index - 1));
  39. }
  40. compElement->setAttribute(
  41. "alignTop", last->getAttributeValue("id"));
  42. compElement->setAttribute("marginTop", "5");
  43. }
  44. element->addChild(compElement);
  45. last = compElement;
  46. }
  47. index++;
  48. }
  49. return element;
  50. }
  51. bool BasicBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  52. {
  53. bool ative = false;
  54. for (BlockComponent* component : components)
  55. {
  56. ative |= component->tick(numTicks);
  57. }
  58. return ative;
  59. }
  60. void BasicBlock::onPostTick() {}
  61. void BasicBlock::getLightEmisionColor(unsigned char* result) const
  62. {
  63. Block::getLightEmisionColor(result);
  64. for (BlockComponent* component : components)
  65. {
  66. int color = component->getLightColor();
  67. result[0] = (unsigned char)MIN(result[0] + ((color >> 16) & 0xFF), 255);
  68. result[1] = (unsigned char)MIN(result[1] + ((color >> 8) & 0xFF), 255);
  69. result[2] = (unsigned char)MIN(result[2] + (color & 0xFF), 255);
  70. }
  71. }
  72. TickSourceType BasicBlock::isTickSource() const
  73. {
  74. return components.getEintragAnzahl() > 0 ? TickSourceType::EACH_TICK
  75. : TickSourceType::NONE;
  76. }
  77. void BasicBlock::onApiCall(char messageType,
  78. Framework::StreamReader* zRequest,
  79. NetworkMessage* zResponse,
  80. Entity* zSource)
  81. {
  82. switch (messageType)
  83. {
  84. case 2: // component request
  85. {
  86. int index;
  87. zRequest->lese((char*)&index, sizeof(int));
  88. if (index >= 0 && index < components.getEintragAnzahl())
  89. {
  90. components.z(index)->api(zRequest, zResponse, zSource);
  91. }
  92. break;
  93. }
  94. }
  95. }
  96. BasicBlockType::BasicBlockType()
  97. : BlockType(),
  98. itemTypeName(),
  99. transparent(0),
  100. passable(0),
  101. speedModifier(1.f),
  102. interactable(1)
  103. {}
  104. bool BasicBlockType::initialize(Game* zGame)
  105. {
  106. if (itemTypeName.getLength())
  107. {
  108. itemTypeId = zGame->getItemTypeId(itemTypeName);
  109. }
  110. else
  111. {
  112. itemTypeId = 0;
  113. }
  114. return itemTypeId >= 0 && BlockType::initialize(zGame);
  115. }
  116. void BasicBlockType::loadSuperBlock(
  117. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  118. {
  119. BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
  120. for (BlockComponent* component : block->components)
  121. {
  122. component->loadComponent(zReader);
  123. }
  124. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  125. }
  126. void BasicBlockType::saveSuperBlock(
  127. Block* zBlock, Framework::StreamWriter* zWriter) const
  128. {
  129. BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
  130. for (BlockComponent* component : block->components)
  131. {
  132. component->saveComponent(zWriter);
  133. }
  134. BlockType::saveSuperBlock(zBlock, zWriter);
  135. }
  136. void BasicBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  137. {
  138. BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
  139. block->transparent = transparent;
  140. block->passable = passable;
  141. block->hp = (float)getInitialMaxHP();
  142. block->maxHP = (float)getInitialMaxHP();
  143. block->hardness = getHardness();
  144. block->speedModifier = speedModifier;
  145. block->interactable = interactable;
  146. for (ItemSlot* slot : itemSlots)
  147. {
  148. block->addSlot(new ItemSlot(*slot));
  149. }
  150. for (Framework::JSON::JSONValue* component : components)
  151. {
  152. BlockComponent* blockComponent
  153. = Game::INSTANCE->zTypeRegistry()->fromJson<BlockComponent>(
  154. component);
  155. blockComponent->initialize(block);
  156. block->addComponent(blockComponent);
  157. }
  158. BlockType::createSuperBlock(zBlock, zItem);
  159. }
  160. Block* BasicBlockType::createBlock(
  161. Framework::Vec3<int> position, int dimensionId) const
  162. {
  163. return new BasicBlock(
  164. getId(), position, dimensionId, itemSlots.getEintragAnzahl() > 0);
  165. }
  166. Item* BasicBlockType::createItem() const
  167. {
  168. if (getItemTypeName().istGleich(""))
  169. {
  170. return 0;
  171. }
  172. return Game::INSTANCE->zItemType(itemTypeId)->createItem();
  173. }
  174. Framework::Text BasicBlockType::getItemTypeName() const
  175. {
  176. return itemTypeName;
  177. }
  178. ItemType* BasicBlockType::createItemType() const
  179. {
  180. if (getItemTypeName().istGleich(""))
  181. {
  182. return 0;
  183. }
  184. return new BasicBlockItemType(getItemTypeName(),
  185. new ModelInfo(zModel()->getModelPath(),
  186. zModel()->getTexturePaths(),
  187. zModel()->isTransparent(),
  188. zModel()->getSize() / 2.f),
  189. transparent,
  190. passable,
  191. getHardness(),
  192. speedModifier,
  193. getName(),
  194. 0,
  195. 50,
  196. getGroupNames());
  197. }
  198. void BasicBlockType::setItemTypeName(Framework::Text itemTypeName)
  199. {
  200. this->itemTypeName = itemTypeName;
  201. }
  202. int BasicBlockType::getItemTypeId() const
  203. {
  204. return itemTypeId;
  205. }
  206. void BasicBlockType::setTransparent(bool transparent)
  207. {
  208. this->transparent = transparent;
  209. }
  210. bool BasicBlockType::isTransparent() const
  211. {
  212. return transparent;
  213. }
  214. void BasicBlockType::setPassable(bool passable)
  215. {
  216. this->passable = passable;
  217. }
  218. bool BasicBlockType::isPassable() const
  219. {
  220. return passable;
  221. }
  222. void BasicBlockType::setSpeedModifier(float speedModifier)
  223. {
  224. this->speedModifier = speedModifier;
  225. }
  226. float BasicBlockType::getSpeedModifier() const
  227. {
  228. return speedModifier;
  229. }
  230. void BasicBlockType::setInteractable(bool interactable)
  231. {
  232. this->interactable = interactable;
  233. }
  234. bool BasicBlockType::isInteractable() const
  235. {
  236. return interactable;
  237. }
  238. const Framework::RCArray<ItemSlot>& BasicBlockType::getInventorySlots() const
  239. {
  240. return itemSlots;
  241. }
  242. void BasicBlockType::addInventorySlot(ItemSlot* slot)
  243. {
  244. itemSlots.add(slot);
  245. }
  246. const Framework::RCArray<Framework::JSON::JSONValue>&
  247. BasicBlockType::getComponents() const
  248. {
  249. return components;
  250. }
  251. void BasicBlockType::addComponent(Framework::JSON::JSONValue* component)
  252. {
  253. components.add(component);
  254. }