Chest.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. open = 0;
  18. userEntityId = 0;
  19. NetworkMessage* msg = new NetworkMessage();
  20. auto pos = Dimension::chunkCoordinates(getPos());
  21. msg->animateBlockBone(getDimensionId(),
  22. Game::getChunkCenter(getPos().x, getPos().y),
  23. Chunk::index(pos.x, pos.y) * WORLD_HEIGHT + pos.z,
  24. 1,
  25. 1.0,
  26. Framework::Vec3<float>(0.5f, 0.f, 0.45f),
  27. Framework::Vec3<float>(
  28. 0.0f, 0.f, 0.f)); // close the chest over one second
  29. broadcastMessage(msg);
  30. }
  31. bool Chest::onTick(TickQueue* zQueue, int numTicks, bool& blocked)
  32. {
  33. if (open)
  34. {
  35. if (!Game::INSTANCE->zEntity(userEntityId))
  36. {
  37. onDialogClosed("");
  38. }
  39. }
  40. return open;
  41. }
  42. bool Chest::interact(Item* zItem, Entity* zActor, bool& itemChanged)
  43. {
  44. lock();
  45. bool result = 0;
  46. if (open)
  47. {
  48. if (!Game::INSTANCE->zEntity(userEntityId)) open = 0;
  49. }
  50. if (!open)
  51. {
  52. result = Block::interact(zItem, zActor, itemChanged);
  53. if (result)
  54. {
  55. userEntityId = zActor->getId();
  56. open = 1;
  57. NetworkMessage* msg = new NetworkMessage();
  58. auto pos = Dimension::chunkCoordinates(getPos());
  59. msg->animateBlockBone(getDimensionId(),
  60. Game::getChunkCenter(getPos().x, getPos().y),
  61. Chunk::index(pos.x, pos.y) * WORLD_HEIGHT + pos.z,
  62. 1,
  63. 1.0,
  64. Framework::Vec3<float>(0.5f, 0.f, 0.45f),
  65. Framework::Vec3<float>(0.0f,
  66. (float)(PI / 2.0),
  67. 0.f)); // open the chest over 1 second
  68. broadcastMessage(msg);
  69. }
  70. }
  71. unlock();
  72. return result;
  73. }
  74. void Chest::sendModelInfo(NetworkMessage* zMessage)
  75. {
  76. if (open)
  77. {
  78. auto pos = Dimension::chunkCoordinates(getPos());
  79. zMessage->animateBlockBone(getDimensionId(),
  80. Game::getChunkCenter(getPos().x, getPos().y),
  81. Chunk::index(pos.x, pos.y) * WORLD_HEIGHT + pos.z,
  82. 1,
  83. 0.0,
  84. Framework::Vec3<float>(0.5f, 0.f, 0.45f),
  85. Framework::Vec3<float>(
  86. 0.0f, (float)(PI / 2.0), 0.f)); // open the chest instantly
  87. }
  88. }
  89. ChestBlockType::ChestBlockType()
  90. : BasicBlockType()
  91. {}
  92. Block* ChestBlockType::createBlock(
  93. Framework::Vec3<int> position, int dimensionId) const
  94. {
  95. return new Chest(getItemTypeId(), position, dimensionId);
  96. }
  97. ChestBlockTypeFactory::ChestBlockTypeFactory()
  98. : BasicBlockTypeFactory()
  99. {}
  100. ChestBlockType* ChestBlockTypeFactory::createValue(
  101. Framework::JSON::JSONObject* zJson) const
  102. {
  103. return new ChestBlockType();
  104. }
  105. ChestBlockType* ChestBlockTypeFactory::fromJson(
  106. Framework::JSON::JSONObject* zJson) const
  107. {
  108. return BasicBlockTypeFactory::fromJson(zJson);
  109. }
  110. Framework::JSON::JSONObject* ChestBlockTypeFactory::toJsonObject(
  111. ChestBlockType* zObject) const
  112. {
  113. return BasicBlockTypeFactory::toJsonObject(zObject);
  114. }
  115. JSONObjectValidationBuilder* ChestBlockTypeFactory::addToValidator(
  116. JSONObjectValidationBuilder* builder) const
  117. {
  118. return BasicBlockTypeFactory::addToValidator(builder);
  119. }
  120. const char* ChestBlockTypeFactory::getTypeToken() const
  121. {
  122. return "chest";
  123. }
  124. const char* ChestBlockTypeFactory::getTypeName() const
  125. {
  126. return typeid(ChestBlockType).name();
  127. }