Chest.cpp 3.6 KB

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