Command.hpp
struct Command
{
Command();
std::function<void(SceneNode&, sf::Time)> action;
unsigned int category; //taking an enum from
};
template<typename GameObject, typename Function>
std::function<void(SceneNode& node, sf::Time dt)> derivedAction(Function func)
{
return [=] (SceneNode& node, sf::Time dt)
{
assert(dynamic_cast<GameObject*>(&node) != nullptr);
func(static_cast<GameObject&>(node), dt);
};
}
AircraftMover definition:
struct AircraftMover
{
AircraftMover(float vx, float vy)
: velocity(vx, vy)
{
}
void operator() (Aircraft& aircraft, sf::Time dt)
{
aircraft.accelerate(velocity);
}
sf::Vector2f velocity;
};
Calling a
Command, using
derivedAction(Function) and
AircraftMover:
/* Some code... */
Command moveLeft;
moveLeft.type = Category::PlayerAircraft;
moveLeft.action = derivedAction<Aircraft>(AircraftMover(-playerSpeed, 0));
/* Some code again... */