Chest.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "Chest.h"
  2. #include <TextFeld.h>
  3. #include "Game.h"
  4. Chest::Chest(
  5. int typeId, ItemType* zTool, Framework::Vec3<int> pos, int dimensionId)
  6. : BasicBlock(typeId, zTool, pos, dimensionId, 1),
  7. open(0),
  8. userEntityId(0)
  9. {
  10. for (int i = 0; i < 30; i++)
  11. {
  12. ItemSlot* slot = new ItemSlot(
  13. "Inventory", 50, i, i, ANY_DIRECTION, ANY_DIRECTION, 0);
  14. addSlot(slot);
  15. }
  16. }
  17. void Chest::onDestroy()
  18. {
  19. for (ItemSlot* slot : *this)
  20. {
  21. if (!slot->isEmpty())
  22. {
  23. Game::INSTANCE->spawnItem(location + Vec3<float>(0.5f, 0.5f, 0.5f),
  24. getDimensionId(),
  25. slot->takeItemsOut(slot->getNumberOfItems(), NO_DIRECTION));
  26. }
  27. }
  28. BasicBlock::onDestroy();
  29. }
  30. void Chest::onDialogClosed(Framework::Text dialogId)
  31. {
  32. if (dialogId.istGleich(getDialogId()))
  33. {
  34. open = 0;
  35. userEntityId = 0;
  36. NetworkMessage* msg = new NetworkMessage();
  37. msg->animateBlockBone(getDimensionId(),
  38. Game::getChunkCenter(getPos().x, getPos().y),
  39. Chunk::index(Dimension::chunkCoordinates(getPos())),
  40. 1,
  41. 1.0,
  42. Vec3<float>(0.5f, 0.f, 0.45f),
  43. Vec3<float>(0.0f, 0.f, 0.f)); // close the chest over one second
  44. Game::INSTANCE->broadcastMessage(msg);
  45. }
  46. }
  47. Framework::Text Chest::getDialogId() const
  48. {
  49. Text dialogId = "chest_inventory_";
  50. dialogId.append() << getDimensionId() << "," << getPos().x << ","
  51. << getPos().y << "," << getPos().z;
  52. return dialogId;
  53. }
  54. bool Chest::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  55. {
  56. if (open)
  57. {
  58. if (!Game::INSTANCE->zEntity(userEntityId))
  59. {
  60. onDialogClosed(getDialogId());
  61. }
  62. }
  63. return open;
  64. }
  65. void Chest::interact(Item* zItem, Entity* zActor)
  66. {
  67. lock();
  68. if (open)
  69. {
  70. if (!Game::INSTANCE->zEntity(userEntityId)) open = 0;
  71. }
  72. if (!open)
  73. {
  74. userEntityId = zActor->getId();
  75. open = 1;
  76. NetworkMessage* msg = new NetworkMessage();
  77. msg->openDialog(getDialogId());
  78. Text uiml = "";
  79. uiml.append()
  80. << "<dialog id=\"" << getDialogId()
  81. << "\" title=\"Chest\" "
  82. "notifyOnClose=\""
  83. << getDimensionId() << "," << getPos().x << "," << getPos().y << ","
  84. << getPos().z
  85. << "\" "
  86. "width=\"610\" "
  87. "height=\"480\">"
  88. << "<inventory id=\"chest_inventory\" margin-bottom=\"18\" "
  89. "align-bottom=\"player_label\" align-left=\"start\" "
  90. "margin-left=\"9\" width=\"592\" height=\"172\" rowSize=\"10\" "
  91. "numSlots=\"30\" slotNameFilter=\"\" target=\""
  92. << getDimensionId() << "," << getPos().x << "," << getPos().y << ","
  93. << getPos().z << "\"/>"
  94. << "<text id=\"player_label\" width=\"100%\" height=\"auto\" style=\""
  95. << std::uppercase << std::hex
  96. << (TextFeld::Style::Text | TextFeld::Style::Center) << std::dec
  97. << std::nouppercase
  98. << "\" margin-bottom=\"9\" align-bottom=\"player_inventory\">Player "
  99. "Inventory</text>"
  100. << "<inventory id=\"player_inventory\" margin-bottom=\"18\" "
  101. "align-bottom=\"item_bar\" align-left=\"start\" "
  102. "margin-left=\"9\" width=\"592\" height=\"172\" rowSize=\"10\" "
  103. "numSlots=\"30\" slotNameFilter=\"Inventory\" target=\""
  104. << zActor->getId() << "\"/>"
  105. << "<inventory id=\"item_bar\" margin-bottom=\"9\" "
  106. "align-bottom=\"end\" align-left=\"start\" margin-left=\"9\" "
  107. "width=\"592\" height=\"52\" rowSize=\"10\" numSlots=\"10\" "
  108. "slotNameFilter=\"ItemBar\" target=\""
  109. << zActor->getId() << "\"/>"
  110. << "</dialog>";
  111. int msgSize = 4 + uiml.getLength();
  112. char* data = new char[msgSize];
  113. *(int*)data = uiml.getLength();
  114. memcpy(data + 4, uiml.getText(), uiml.getLength());
  115. msg->setMessage(data, msgSize);
  116. Game::INSTANCE->sendMessage(msg, zActor);
  117. msg = new NetworkMessage();
  118. msg->animateBlockBone(getDimensionId(),
  119. Game::getChunkCenter(getPos().x, getPos().y),
  120. Chunk::index(Dimension::chunkCoordinates(getPos())),
  121. 1,
  122. 1.0,
  123. Vec3<float>(0.5f, 0.f, 0.45f),
  124. Vec3<float>(
  125. 0.0f, (float)(PI / 2.0), 0.f)); // open the chest over 1 second
  126. Game::INSTANCE->broadcastMessage(msg);
  127. }
  128. unlock();
  129. }
  130. void Chest::sendModelInfo(NetworkMessage* zMessage)
  131. {
  132. if (open)
  133. {
  134. zMessage->animateBlockBone(getDimensionId(),
  135. Game::getChunkCenter(getPos().x, getPos().y),
  136. Chunk::index(Dimension::chunkCoordinates(getPos())),
  137. 1,
  138. 0.0,
  139. Vec3<float>(0.5f, 0.f, 0.45f),
  140. Vec3<float>(
  141. 0.0f, (float)(PI / 2.0), 0.f)); // open the chest instantly
  142. }
  143. }