Chest.cpp 5.8 KB

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