InformationObserver.cpp 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "InformationObserver.h"
  2. #include "Game.h"
  3. InformationObserver::InformationObserver(int entityId)
  4. : ReferenceCounter(),
  5. entityId(entityId),
  6. ready(false)
  7. {}
  8. InformationObserver::~InformationObserver()
  9. {
  10. cs.lock();
  11. waitingMessages.leeren();
  12. cs.unlock();
  13. }
  14. int InformationObserver::getEntityId() const
  15. {
  16. return entityId;
  17. }
  18. bool InformationObserver::sendMessage(NetworkMessage* message)
  19. {
  20. cs.lock();
  21. if (ready)
  22. {
  23. cs.unlock();
  24. Entity* e = Game::INSTANCE->zEntity(entityId);
  25. if (e)
  26. {
  27. Game::INSTANCE->sendMessage(message, e);
  28. return 1;
  29. }
  30. else
  31. {
  32. message->release();
  33. return 0;
  34. }
  35. }
  36. else
  37. {
  38. waitingMessages.add(message);
  39. cs.unlock();
  40. return 1;
  41. }
  42. }
  43. void InformationObserver::setReady()
  44. {
  45. cs.lock();
  46. ready = 1;
  47. Entity* e = Game::INSTANCE->zEntity(entityId);
  48. if (e)
  49. {
  50. for (NetworkMessage* msg : waitingMessages)
  51. {
  52. Game::INSTANCE->sendMessage(dynamic_cast<NetworkMessage*>(msg->getThis()), e);
  53. }
  54. }
  55. waitingMessages.leeren();
  56. cs.unlock();
  57. }