-
Notifications
You must be signed in to change notification settings - Fork 19
Directors
Unlike entities, components or systems a Director
class acts as
an observer of a scene, rather than interact with it directly. Inheriting the
xy::Director
interface provides thre functions:
Director::handleMessage(const xy::Message& msg);
Director::handleEvent(const sf::Event& evt);
Director::process(float);
The idea of the Director
class is to provide a way to observe and react to
events such as user input or messages emitted from the scene, by
further processing the information, or using it to command or 'direct' scene
entities. For example a director can be used to take user input from multiple
sources, process it and send it to the player entity. Directors can also be used
to observe in-game events such as the destruction of specific entities, and trigger
particle effects like smoke or explosions in response. A simple input director
may look like:
class InputDirector final : public xy::Director
{
public:
void handleMessage(const xy::Message&) override {}
void handleEvent(const sf::Event& evt) override
{
if(evt.type == sf::Event::KeyPressed)
{
switch(evt.key.code)
{
default: break;
case sf::Keyboard::Left:
m_currentInput |= 0x4;
break;
case sf::Keyboard::Right:
m_currentInput |= 0x8;
break;
}
}
else if(evt.type == sf::JoystickButtonPressed)
{
if(evt.joystickButton.button == 2)
{
m_currentInput |= 0x10;
}
}
}
void process(float dt) override
{
xy::Command cmd;
cmd.targetFlags = CommandFlag::PlayerOne;
cmd.action = [&](xy::Entity entity, float)
{
entity.getComponent<Player>().currenInput = m_currentInput;
};
sendCommand(cmd);
m_currentInput = 0;
}
private:
sf::Uint8 m_currentInput = 0;
}
The above works by assessing any input events and using them to set flags within a bitmask. The input is then sent to a player entity using the command system, one of which one is automatically created when a director is added to a scene, if it does not yet exist.
Directors should be added to a scene when the scene is first initialised.
m_scene.addDirector<InputDirector>();
Only one director of each type can be added to a single scene. See scene for more details.