Chest.cpp 4.3 KB

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