I want to make a new way of handling animations for my game. Currently I detriment object animation states with two variables _physics_State
& _direction_State
used in a switch case and nested switch case respectfully. I want to move away from this method of using switch cases.
switch (_physics_State)
{
case OBJECT_PHYSICS_STATE_GROUNDED:
switch(_direction_State)
{
case OBJECT_DIRECTION_UP: _animation_State = ANIMATION_STATE_YS_IDLE_UP; break;
case OBJECT_DIRECTION_UP_RIGHT: _animation_State = ANIMATION_STATE_YS_IDLE_UP_RIGHT; break;
case OBJECT_DIRECTION_UP_LEFT: _animation_State = ANIMATION_STATE_YS_IDLE_UP_LEFT; break;
case OBJECT_DIRECTION_RIGHT: _animation_State = ANIMATION_STATE_YS_IDLE_RIGHT; break;
case OBJECT_DIRECTION_RIGHT_DOWN: _animation_State = ANIMATION_STATE_YS_IDLE_RIGHT_DOWN; break;
case OBJECT_DIRECTION_DOWN: _animation_State = ANIMATION_STATE_YS_IDLE_DOWN; break;
case OBJECT_DIRECTION_DOWN_LEFT: _animation_State = ANIMATION_STATE_YS_IDLE_DOWN_LEFT; break;
case OBJECT_DIRECTION_LEFT: _animation_State = ANIMATION_STATE_YS_IDLE_LEFT; break;
default: _animation_State = ANIMATION_STATE_YS_IDLE_DOWN; break;
}
break;
}
I have come up with the solution of generating a string by reading variables in a specific order and storing it in a hash map. This seemed to work except with the last variable _speed
. _speed
is continuous not discrete and will likely have a range of values that will still make the object reach the same animation state. I cannot simply store speed as a simple value. How would you handle this continuous data in a way that allow me to search it up with the discrete data?
std::string animation_state_string_results = ""
+ std::to_string(_physics_State)
+ "-" + std::to_string(_direction_State);
+ "-" + std::to_string(G_button_Press_Value);
+ "-" + std::to_string(_speed);
It’s easy enough to convert a “continuous” quantity into a coarse “label”. The usual idea is to divide the range into intervals; then use a variant of binary search to identify the interval that contains a query value. Store the index of the largest value known to be no larger than the input and of the smallest value known to be larger. Note that you can consider n values to delimit n+1 intervals (two of which are half-infinite); if that’s appropriate for your data, you have to initialize one or both bounding indices to indices beyond the outermost values.
You can do that as a preprocessing step on the continuous variable, or you can store (for each appropriate combination of the other variables) a data structure that contains the interval boundaries in parallel with the values to use for each interval.
Meanwhile, don’t make a compound key by assembling a string—you’ll end up doing silly things like reparsing it later. Instead, use a tuple
(or a custom class). You do then have to provide a hash function, but that’s not difficult (and there exist libraries to make it trivial).