Chest.cpp 5.6 KB

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