Chest.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. Text uiml = "";
  77. uiml.append()
  78. << "<dialog id=\"" << getDialogId()
  79. << "\" title=\"Chest\" "
  80. "notifyOnClose=\""
  81. << getDimensionId() << "," << getPos().x << "," << getPos().y << ","
  82. << getPos().z
  83. << "\" "
  84. "width=\"610\" "
  85. "height=\"480\">"
  86. << "<inventory id=\"chest_inventory\" margin-bottom=\"18\" "
  87. "align-bottom=\"player_label\" align-left=\"start\" "
  88. "margin-left=\"9\" width=\"592\" height=\"172\" rowSize=\"10\" "
  89. "numSlots=\"30\" slotNameFilter=\"\" target=\""
  90. << getDimensionId() << "," << getPos().x << "," << getPos().y << ","
  91. << getPos().z << "\"/>"
  92. << "<text id=\"player_label\" width=\"100%\" height=\"auto\" style=\""
  93. << std::uppercase << std::hex
  94. << (TextFeld::Style::Text | TextFeld::Style::Center) << std::dec
  95. << std::nouppercase
  96. << "\" margin-bottom=\"9\" align-bottom=\"player_inventory\">Player "
  97. "Inventory</text>"
  98. << "<inventory id=\"player_inventory\" margin-bottom=\"18\" "
  99. "align-bottom=\"item_bar\" align-left=\"start\" "
  100. "margin-left=\"9\" width=\"592\" height=\"172\" rowSize=\"10\" "
  101. "numSlots=\"30\" slotNameFilter=\"Inventory\" target=\""
  102. << zActor->getId() << "\"/>"
  103. << "<inventory id=\"item_bar\" margin-bottom=\"9\" "
  104. "align-bottom=\"end\" align-left=\"start\" margin-left=\"9\" "
  105. "width=\"592\" height=\"52\" rowSize=\"10\" numSlots=\"10\" "
  106. "slotNameFilter=\"ItemBar\" target=\""
  107. << zActor->getId() << "\"/>"
  108. << "</dialog>";
  109. Game::INSTANCE->zUIController()->addDialog(
  110. new UIDialog(getDialogId(), zActor->getId(), new Framework::XML::Element(uiml)));
  111. NetworkMessage *msg = new NetworkMessage();
  112. msg->animateBlockBone(getDimensionId(),
  113. Game::getChunkCenter(getPos().x, getPos().y),
  114. Chunk::index(Dimension::chunkCoordinates(getPos())),
  115. 1,
  116. 1.0,
  117. Vec3<float>(0.5f, 0.f, 0.45f),
  118. Vec3<float>(
  119. 0.0f, (float)(PI / 2.0), 0.f)); // open the chest over 1 second
  120. Game::INSTANCE->broadcastMessage(msg);
  121. }
  122. unlock();
  123. }
  124. void Chest::sendModelInfo(NetworkMessage* zMessage)
  125. {
  126. if (open)
  127. {
  128. zMessage->animateBlockBone(getDimensionId(),
  129. Game::getChunkCenter(getPos().x, getPos().y),
  130. Chunk::index(Dimension::chunkCoordinates(getPos())),
  131. 1,
  132. 0.0,
  133. Vec3<float>(0.5f, 0.f, 0.45f),
  134. Vec3<float>(
  135. 0.0f, (float)(PI / 2.0), 0.f)); // open the chest instantly
  136. }
  137. }