Chest.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "Chest.h"
  2. #include <TextFeld.h>
  3. #include "Chunk.h"
  4. #include "Dimension.h"
  5. #include "Entity.h"
  6. #include "Game.h"
  7. #include "ItemSlot.h"
  8. #include "UIController.h"
  9. #include "UIDialog.h"
  10. Chest::Chest(int typeId, Framework::Vec3<int> pos, int dimensionId)
  11. : BasicBlock(typeId, pos, dimensionId, 1),
  12. open(0),
  13. userEntityId(0)
  14. {
  15. for (int i = 0; i < 30; i++)
  16. {
  17. ItemSlot* slot = new ItemSlot(
  18. "Inventory", 50, i, i, ANY_DIRECTION, ANY_DIRECTION, 0);
  19. addSlot(slot);
  20. }
  21. }
  22. void Chest::onDestroy()
  23. {
  24. for (ItemSlot* slot : *this)
  25. {
  26. if (!slot->isEmpty())
  27. {
  28. Game::INSTANCE->spawnItem(
  29. location + Framework::Vec3<float>(0.5f, 0.5f, 0.5f),
  30. getDimensionId(),
  31. slot->takeItemsOut(slot->getNumberOfItems(), NO_DIRECTION));
  32. }
  33. }
  34. BasicBlock::onDestroy();
  35. }
  36. void Chest::onDialogClosed(Framework::Text dialogId)
  37. {
  38. if (dialogId.istGleich(getDialogId()))
  39. {
  40. open = 0;
  41. userEntityId = 0;
  42. NetworkMessage* msg = new NetworkMessage();
  43. msg->animateBlockBone(getDimensionId(),
  44. Game::getChunkCenter(getPos().x, getPos().y),
  45. Chunk::index(Dimension::chunkCoordinates(getPos())),
  46. 1,
  47. 1.0,
  48. Framework::Vec3<float>(0.5f, 0.f, 0.45f),
  49. Framework::Vec3<float>(
  50. 0.0f, 0.f, 0.f)); // close the chest over one second
  51. broadcastMessage(msg);
  52. }
  53. }
  54. Framework::Text Chest::getDialogId() const
  55. {
  56. Framework::Text dialogId = "chest_inventory_";
  57. dialogId.append() << getDimensionId() << "," << getPos().x << ","
  58. << getPos().y << "," << getPos().z;
  59. return dialogId;
  60. }
  61. bool Chest::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  62. {
  63. if (open)
  64. {
  65. if (!Game::INSTANCE->zEntity(userEntityId))
  66. {
  67. onDialogClosed(getDialogId());
  68. }
  69. }
  70. return open;
  71. }
  72. bool Chest::interact(Item* zItem, Entity* zActor)
  73. {
  74. lock();
  75. if (open)
  76. {
  77. if (!Game::INSTANCE->zEntity(userEntityId)) open = 0;
  78. }
  79. if (!open)
  80. {
  81. userEntityId = zActor->getId();
  82. open = 1;
  83. Framework::Text uiml = "";
  84. uiml.append()
  85. << "<dialog id=\"" << getDialogId()
  86. << "\" title=\"Chest\" "
  87. "notifyOnClose=\""
  88. << getDimensionId() << "," << getPos().x << "," << getPos().y << ","
  89. << getPos().z
  90. << "\" "
  91. "width=\"610\" "
  92. "height=\"480\">"
  93. << "<inventory id=\"chest_inventory\" margin-bottom=\"18\" "
  94. "align-bottom=\"player_label\" align-left=\"start\" "
  95. "margin-left=\"9\" width=\"592\" height=\"172\" rowSize=\"10\" "
  96. "numSlots=\"30\" slotNameFilter=\"\" target=\""
  97. << getDimensionId() << "," << getPos().x << "," << getPos().y << ","
  98. << getPos().z << "\"/>"
  99. << "<text id=\"player_label\" width=\"100%\" height=\"auto\" "
  100. "style=\""
  101. << std::uppercase << std::hex
  102. << (Framework::TextFeld::Style::Text
  103. | Framework::TextFeld::Style::Center)
  104. << std::dec << std::nouppercase
  105. << "\" margin-bottom=\"9\" "
  106. "align-bottom=\"player_inventory\">Player "
  107. "Inventory</text>"
  108. << "<inventory id=\"player_inventory\" margin-bottom=\"18\" "
  109. "align-bottom=\"item_bar\" align-left=\"start\" "
  110. "margin-left=\"9\" width=\"592\" height=\"172\" rowSize=\"10\" "
  111. "numSlots=\"30\" slotNameFilter=\"Inventory\" target=\""
  112. << zActor->getId() << "\"/>"
  113. << "<inventory id=\"item_bar\" margin-bottom=\"9\" "
  114. "align-bottom=\"end\" align-left=\"start\" margin-left=\"9\" "
  115. "width=\"592\" height=\"52\" rowSize=\"10\" numSlots=\"10\" "
  116. "slotNameFilter=\"ItemBar\" target=\""
  117. << zActor->getId() << "\"/>"
  118. << "</dialog>";
  119. Game::INSTANCE->zUIController()->addDialog(new UIDialog(
  120. getDialogId(), zActor->getId(), new Framework::XML::Element(uiml)));
  121. NetworkMessage* msg = new NetworkMessage();
  122. msg->animateBlockBone(getDimensionId(),
  123. Game::getChunkCenter(getPos().x, getPos().y),
  124. Chunk::index(Dimension::chunkCoordinates(getPos())),
  125. 1,
  126. 1.0,
  127. Framework::Vec3<float>(0.5f, 0.f, 0.45f),
  128. Framework::Vec3<float>(
  129. 0.0f, (float)(PI / 2.0), 0.f)); // open the chest over 1 second
  130. broadcastMessage(msg);
  131. }
  132. unlock();
  133. return false; // item was not changed
  134. }
  135. void Chest::sendModelInfo(NetworkMessage* zMessage)
  136. {
  137. if (open)
  138. {
  139. zMessage->animateBlockBone(getDimensionId(),
  140. Game::getChunkCenter(getPos().x, getPos().y),
  141. Chunk::index(Dimension::chunkCoordinates(getPos())),
  142. 1,
  143. 0.0,
  144. Framework::Vec3<float>(0.5f, 0.f, 0.45f),
  145. Framework::Vec3<float>(
  146. 0.0f, (float)(PI / 2.0), 0.f)); // open the chest instantly
  147. }
  148. }
  149. ChestBlockType::ChestBlockType()
  150. : BasicBlockType()
  151. {}
  152. Block* ChestBlockType::createBlock(
  153. Framework::Vec3<int> position, int dimensionId) const
  154. {
  155. return new Chest(getItemTypeId(), position, dimensionId);
  156. }
  157. ChestBlockTypeFactory::ChestBlockTypeFactory()
  158. : SubTypeFactory(),
  159. super()
  160. {}
  161. ChestBlockType* ChestBlockTypeFactory::createValue(
  162. Framework::JSON::JSONObject* zJson) const
  163. {
  164. return new ChestBlockType();
  165. }
  166. void ChestBlockTypeFactory::fromJson(
  167. ChestBlockType* zResult, Framework::JSON::JSONObject* zJson) const
  168. {
  169. super.fromJson(zResult, zJson);
  170. }
  171. void ChestBlockTypeFactory::toJson(
  172. ChestBlockType* zObject, Framework::JSON::JSONObject* zResult) const
  173. {
  174. super.toJson(zObject, zResult);
  175. }
  176. JSONObjectValidationBuilder* ChestBlockTypeFactory::addToValidator(
  177. JSONObjectValidationBuilder* builder) const
  178. {
  179. return super.addToValidator(builder);
  180. }
  181. Framework::Text ChestBlockTypeFactory::getTypeToken() const
  182. {
  183. return "chest";
  184. }