Chest.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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, bool& itemChanged)
  52. {
  53. lock();
  54. bool result = 0;
  55. if (open)
  56. {
  57. if (!Game::INSTANCE->zEntity(userEntityId)) open = 0;
  58. }
  59. if (!open)
  60. {
  61. userEntityId = zActor->getId();
  62. open = 1;
  63. result = Block::interact(zItem, zActor, itemChanged);
  64. NetworkMessage* msg = new NetworkMessage();
  65. msg->animateBlockBone(getDimensionId(),
  66. Game::getChunkCenter(getPos().x, getPos().y),
  67. Chunk::index(Dimension::chunkCoordinates(getPos())),
  68. 1,
  69. 1.0,
  70. Framework::Vec3<float>(0.5f, 0.f, 0.45f),
  71. Framework::Vec3<float>(
  72. 0.0f, (float)(PI / 2.0), 0.f)); // open the chest over 1 second
  73. broadcastMessage(msg);
  74. }
  75. unlock();
  76. return result;
  77. }
  78. void Chest::sendModelInfo(NetworkMessage* zMessage)
  79. {
  80. if (open)
  81. {
  82. zMessage->animateBlockBone(getDimensionId(),
  83. Game::getChunkCenter(getPos().x, getPos().y),
  84. Chunk::index(Dimension::chunkCoordinates(getPos())),
  85. 1,
  86. 0.0,
  87. Framework::Vec3<float>(0.5f, 0.f, 0.45f),
  88. Framework::Vec3<float>(
  89. 0.0f, (float)(PI / 2.0), 0.f)); // open the chest instantly
  90. }
  91. }
  92. ChestBlockType::ChestBlockType()
  93. : BasicBlockType()
  94. {}
  95. Block* ChestBlockType::createBlock(
  96. Framework::Vec3<int> position, int dimensionId) const
  97. {
  98. return new Chest(getItemTypeId(), position, dimensionId);
  99. }
  100. ChestBlockTypeFactory::ChestBlockTypeFactory()
  101. : BasicBlockTypeFactory()
  102. {}
  103. ChestBlockType* ChestBlockTypeFactory::createValue(
  104. Framework::JSON::JSONObject* zJson) const
  105. {
  106. return new ChestBlockType();
  107. }
  108. ChestBlockType* ChestBlockTypeFactory::fromJson(
  109. Framework::JSON::JSONObject* zJson) const
  110. {
  111. return BasicBlockTypeFactory::fromJson(zJson);
  112. }
  113. Framework::JSON::JSONObject* ChestBlockTypeFactory::toJsonObject(
  114. ChestBlockType* zObject) const
  115. {
  116. return BasicBlockTypeFactory::toJsonObject(zObject);
  117. }
  118. JSONObjectValidationBuilder* ChestBlockTypeFactory::addToValidator(
  119. JSONObjectValidationBuilder* builder) const
  120. {
  121. return BasicBlockTypeFactory::addToValidator(builder);
  122. }
  123. const char* ChestBlockTypeFactory::getTypeToken() const
  124. {
  125. return "chest";
  126. }
  127. const char* ChestBlockTypeFactory::getTypeName() const
  128. {
  129. return typeid(ChestBlockType).name();
  130. }