My engine is almost fully exposed to AS, but I can't quite figure out the best way to approach using my message handler.
Here's how it works:
I have a singleton class called "MessageHandler" defined as follows:
template <typename T>
class MessageHandlerClass : public MessageHandlerPublisher
, private NoCopy, public Singleton<MessageHandlerClass> {
public:
void publish(const T *msg);
void subscribe(MessageInterface<T> *l);
void unsubscribe(const MessageInterface<T> *l);
private:
MessageHandlerClass();
~MessageHandlerClass();
std::vector<MessageInterface<T> *> listeners;
std::queue<T *> messages;
};
(there's actually more things like delegates and delayed sending, but those aren't important for this discussion).
Next there's the MessageInterface Interface/Class:
template <typename T>
struct MessageInterface {
virtual ~MessageInterface() {
MessageHandler<T>::Instance().unsubscribe(this);
}
virtual void receiveMessage(const T *msg) = 0;
};
Basically how this works is you can create any message whatsoever.. so for example a player input message like such:
struct PlayerIPMsg {
PlayerIPMsg() { move_up = move_down = move_left = move_right = false; }
bool move_up, move_down, move_left, move_right;
};
Then any class can listen for that message... so for example the player:
struct Player : public Entity, public MessageInterface<PlayerIPMsg> {
Player() { MessageHandler<PlayerIPMsg>::Instance().subscribe(this); }
void receiveMessage(const PlayerIPMsg *msg) {
//handle movement here;
}
};
This basically allows any data to pass between objects that have no idea about each other... I can't think of the best way to approach this in AS.
I have about 25 base engine Message classes, not including any game-specific ones, so it'd be impractical for each IEntity to implement all message interfaces if it doesn't use them...
Any ideas on how I should approach this?