Currently using C++20, GCC 11.1.0
I'm coding for simple movement in a game loop.
Following the abstract pseudocode below, how would I be able to translate this into code? I was thinking of using either goto
to just skip right into the scope that uses the values, or std::optional
to check whether the values exist or not.
The reason I'm trying to do this instead of just adding the bottom if statement into the A...D if statements is because the bottom if statement could become very large, and may add redundancy. Or should I just refactor the if statement into a separate function?
if (direction is left && ...)
{
int xVelocity {left_calculation...};
}
else if (direction is right && ...)
{
int xVelocity {right_calculation...};
}
else if (direction is up && ...)
{
int yVelocity {up_calculation...};
}
else if (direction is down && ...)
{
int yVelocity {down_calculation...};
}
if (x has a value or y has a value)
{
// Do something with those values...
}