123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- #include "InformationObserver.h"
- #include "Game.h"
- InformationObserver::InformationObserver(int entityId)
- : ReferenceCounter(),
- ready(false),
- entityId(entityId)
- {}
- InformationObserver::~InformationObserver()
- {
- cs.lock();
- waitingMessages.leeren();
- cs.unlock();
- }
- int InformationObserver::getEntityId() const
- {
- return entityId;
- }
- bool InformationObserver::sendMessage(NetworkMessage* message)
- {
- cs.lock();
- if (ready)
- {
- cs.unlock();
- Entity* e = Game::INSTANCE->zEntity(entityId);
- if (e)
- {
- Game::INSTANCE->sendMessage(message, e);
- return 1;
- }
- else
- {
- message->release();
- return 0;
- }
- }
- else
- {
- waitingMessages.add(message);
- cs.unlock();
- return 1;
- }
- }
- void InformationObserver::setReady()
- {
- cs.lock();
- ready = 1;
- Entity* e = Game::INSTANCE->zEntity(entityId);
- if (e)
- {
- for (NetworkMessage* msg : waitingMessages)
- {
- Game::INSTANCE->sendMessage(dynamic_cast<NetworkMessage*>(msg->getThis()), e);
- }
- }
- waitingMessages.leeren();
- cs.unlock();
- }
|