BasicBlocks.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. bool BasicBlock::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  20. {
  21. bool ative = false;
  22. for (BlockComponent* component : components)
  23. {
  24. ative |= component->tick(numTicks);
  25. }
  26. return ative;
  27. }
  28. void BasicBlock::onPostTick() {}
  29. void BasicBlock::getLightEmisionColor(unsigned char* result) const
  30. {
  31. Block::getLightEmisionColor(result);
  32. for (BlockComponent* component : components)
  33. {
  34. int color = component->getLightColor();
  35. result[0] = (unsigned char)MIN(result[0] + ((color >> 16) & 0xFF), 255);
  36. result[1] = (unsigned char)MIN(result[1] + ((color >> 8) & 0xFF), 255);
  37. result[2] = (unsigned char)MIN(result[2] + (color & 0xFF), 255);
  38. }
  39. }
  40. TickSourceType BasicBlock::isTickSource() const
  41. {
  42. return components.getEintragAnzahl() > 0 ? TickSourceType::EACH_TICK
  43. : TickSourceType::NONE;
  44. }
  45. BasicBlockType::BasicBlockType()
  46. : BlockType(),
  47. itemTypeName(),
  48. transparent(0),
  49. passable(0),
  50. speedModifier(1.f),
  51. interactable(1)
  52. {}
  53. bool BasicBlockType::initialize(Game* zGame)
  54. {
  55. if (itemTypeName.getLength())
  56. {
  57. itemTypeId = zGame->getItemTypeId(itemTypeName);
  58. }
  59. else
  60. {
  61. itemTypeId = 0;
  62. }
  63. return itemTypeId >= 0 && BlockType::initialize(zGame);
  64. }
  65. void BasicBlockType::loadSuperBlock(
  66. Block* zBlock, Framework::StreamReader* zReader, int dimensionId) const
  67. {
  68. BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
  69. for (BlockComponent* component : block->components)
  70. {
  71. component->loadComponent(zReader);
  72. }
  73. BlockType::loadSuperBlock(zBlock, zReader, dimensionId);
  74. }
  75. void BasicBlockType::saveSuperBlock(
  76. Block* zBlock, Framework::StreamWriter* zWriter) const
  77. {
  78. BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
  79. for (BlockComponent* component : block->components)
  80. {
  81. component->saveComponent(zWriter);
  82. }
  83. BlockType::saveSuperBlock(zBlock, zWriter);
  84. }
  85. void BasicBlockType::createSuperBlock(Block* zBlock, Item* zItem) const
  86. {
  87. BasicBlock* block = dynamic_cast<BasicBlock*>(zBlock);
  88. block->transparent = transparent;
  89. block->passable = passable;
  90. block->hp = (float)getInitialMaxHP();
  91. block->maxHP = (float)getInitialMaxHP();
  92. block->hardness = getHardness();
  93. block->speedModifier = speedModifier;
  94. block->interactable = interactable;
  95. for (ItemSlot* slot : itemSlots)
  96. {
  97. block->addSlot(new ItemSlot(*slot));
  98. }
  99. for (Framework::JSON::JSONValue* component : components)
  100. {
  101. BlockComponent* blockComponent
  102. = Game::INSTANCE->zTypeRegistry()->fromJson<BlockComponent>(
  103. component);
  104. blockComponent->initialize(block);
  105. block->addComponent(blockComponent);
  106. }
  107. BlockType::createSuperBlock(zBlock, zItem);
  108. }
  109. Block* BasicBlockType::createBlock(
  110. Framework::Vec3<int> position, int dimensionId) const
  111. {
  112. return new BasicBlock(
  113. getId(), position, dimensionId, itemSlots.getEintragAnzahl() > 0);
  114. }
  115. Item* BasicBlockType::createItem() const
  116. {
  117. if (getItemTypeName().istGleich(""))
  118. {
  119. return 0;
  120. }
  121. return Game::INSTANCE->zItemType(itemTypeId)->createItem();
  122. }
  123. Framework::Text BasicBlockType::getItemTypeName() const
  124. {
  125. return itemTypeName;
  126. }
  127. ItemType* BasicBlockType::createItemType() const
  128. {
  129. if (getItemTypeName().istGleich(""))
  130. {
  131. return 0;
  132. }
  133. return new BasicBlockItemType(getItemTypeName(),
  134. new ModelInfo(zModel()->getModelPath(),
  135. zModel()->getTexturePaths(),
  136. zModel()->isTransparent(),
  137. zModel()->getSize() / 2.f),
  138. transparent,
  139. passable,
  140. getHardness(),
  141. speedModifier,
  142. getName(),
  143. 0,
  144. 50,
  145. getGroupNames());
  146. }
  147. void BasicBlockType::setItemTypeName(Framework::Text itemTypeName)
  148. {
  149. this->itemTypeName = itemTypeName;
  150. }
  151. int BasicBlockType::getItemTypeId() const
  152. {
  153. return itemTypeId;
  154. }
  155. void BasicBlockType::setTransparent(bool transparent)
  156. {
  157. this->transparent = transparent;
  158. }
  159. bool BasicBlockType::isTransparent() const
  160. {
  161. return transparent;
  162. }
  163. void BasicBlockType::setPassable(bool passable)
  164. {
  165. this->passable = passable;
  166. }
  167. bool BasicBlockType::isPassable() const
  168. {
  169. return passable;
  170. }
  171. void BasicBlockType::setSpeedModifier(float speedModifier)
  172. {
  173. this->speedModifier = speedModifier;
  174. }
  175. float BasicBlockType::getSpeedModifier() const
  176. {
  177. return speedModifier;
  178. }
  179. void BasicBlockType::setInteractable(bool interactable)
  180. {
  181. this->interactable = interactable;
  182. }
  183. bool BasicBlockType::isInteractable() const
  184. {
  185. return interactable;
  186. }
  187. const Framework::RCArray<ItemSlot>& BasicBlockType::getInventorySlots() const
  188. {
  189. return itemSlots;
  190. }
  191. void BasicBlockType::addInventorySlot(ItemSlot* slot)
  192. {
  193. itemSlots.add(slot);
  194. }
  195. const Framework::RCArray<Framework::JSON::JSONValue>&
  196. BasicBlockType::getComponents() const
  197. {
  198. return components;
  199. }
  200. void BasicBlockType::addComponent(Framework::JSON::JSONValue* component)
  201. {
  202. components.add(component);
  203. }