Chest.cpp 4.6 KB

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