I need a changing value that can be manually stepped with step() that goes back and forth a min and a max, moving by speed every step().
This is my current code:
template<typename T> struct PingPongValue {
        T value, min, max, speed, dir{1};
        PingPongValue(T mMin, T mMax, T mSpeed) 
           : value(mMin), min(mMin), max(mMax), speed(mSpeed) { }
        void step()
        {
            value += speed * dir;
                 if(value > max) { value = max; dir = -1; }
            else if(value < min) { value = min; dir = +1; }
        }
};
Example:
PingPongValue v{0, 5, 1};
v.step(); // v.value == 1
v.step(); // v.value == 2
v.step(); // v.value == 3
v.step(); // v.value == 4
v.step(); // v.value == 5
v.step(); // v.value == 4
v.step(); // v.value == 3
v.step(); // v.value == 2
// etc...
I suppose there's a mathematical way to represent this as a branchless function, but I cannot figure it out. I tried using modulo but I still need a dir variable to change step direction.