ICT290 / src / scene / AIController / Messaging / MessageDispatcher.h
MessageDispatcher.h
Raw
#pragma once

// why?
//#pragma warning (disable:4786)

#include <SDL.h>
#include <set>
#include <string>

// to make life easier...
#define Dispatcher MessageDispatcher::Instance()

#include "../BasicEntity.h"
#include "Telegram.h"

// to make code easier to read
const Uint32 SEND_MSG_IMMEDIATELY = 0;
const int NO_ADDITIONAL_INFO = 0;
const int SENDER_ID_IRRELEVANT = -1;

class MessageDispatcher {
   public:
    static MessageDispatcher* Instance();

    // send a message to another agent. Receiving agent is referenced by ID.
    void DispatchMsg(Uint32 delay,
                     int sender,
                     int receiver,
                     int msg,
                     void* ExtraInfo);

    // send out any delayed messages. This method is called each time through
    // the main game loop.
    void DispatchDelayedMessages();

   protected:
   private:
    // a std::set is used as the container for the delayed messages
    // because of the benefit of automatic sorting and avoidance
    // of duplicates. Messages are sorted by their dispatch time.
    std::set<Telegram> PriorityQ;

    // this method is utilized by DispatchMsg or DispatchDelayedMessages.
    // This method calls the message handling member function of the receiving
    // entity, pReceiver, with the newly created telegram
    void Discharge(BasicEntity* Receiver, const Telegram& msg);

    MessageDispatcher() = default;

    // copy ctor and assignment should be private
    MessageDispatcher(const MessageDispatcher&);
    MessageDispatcher& operator=(const MessageDispatcher&);
};