Chest.cpp 3.5 KB

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