I'm trying to solve multiple component modifications via a single event. Is there a better way of handling this?
I have an a label entity that cares about when a shield entity's hitpoints component is modified (damage, regeneration, etc).
The label will need to change
- its text to display the current % of shield remaining.
- its text color to display the severity of shield remaining.
When my ShieldSystem
resolves the hitPoints
component, the below code runs. For eah type of modification I plan to listen for, I need to add a new if
-statement to the for
-loop. This means I have a bunch of duplicated code between systems if more than one wants to modify any listening text component, sprite component, etc.
for(int i =0; i < listeners.size(); i++)
{
if(listeners.count(MessageType::CHANGE_TEXT) == 1)
{
std::vector<Entity*> entities = listeners[MessageType::CHANGE_TEXT];
for(int j; j < entities.size(); j++)
{
Entity* e = entities[j];
MessageInfo* messageInfo = new MessageInfo(MessageType::CHANGE_TEXT, e, "Some Text");
MessageCenter::Dispatch(messageInfo);
}
}
// Add an If(listeners.count(MessageType::CHANGE_TEXT_COLOR)...
// Add an If(listeners.count(MessageType::CHANGE_SPRITE_RECT)...
// etc
}