-1

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);
Laurent
  • 69
  • 1
  • 8
  • Instead of using a `switch`, why not just use a table (at least for the block of code you posted)? I'm not sure I understand what you mean about the `_speed` variable, so I don't have an answer to that. – user1118321 Jan 12 '19 at 17:44
  • Read this "I want to move away from this method of using switch cases." "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 issue is speed isn't discrete. Its like a unit. if speed was 4 or 11 the animation state would still be the same for all value in between these numbers. – Laurent Jan 12 '19 at 17:57
  • I don't understand. There is no way to search continuous data as discreet data. But you seem to understand that. So what do you want? You can stay with `swtich case` if speed is _really_ the issue, but i would just `std::map` and use find. – KamilCuk Jan 12 '19 at 19:32
  • I didn't say I understood anything. I was just asking if there was a way to search continuous data. I have four different variables to deal with. "_speed" is one of them. I need to confirm something within a range not a value. – Laurent Jan 12 '19 at 21:20

1 Answers1

0

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).

Davis Herring
  • 36,443
  • 4
  • 48
  • 76