summaryrefslogtreecommitdiff
path: root/include/SSVOpenHexagon/Components/SpeedData.hpp
blob: 9a47aeec04a967564091cec8c6dd0621c257cd68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright (c) 2013-2020 Vittorio Romeo
// License: Academic Free License ("AFL") v. 3.0
// AFL License page: https://opensource.org/licenses/AFL-3.0

#pragma once

#include <SSVUtils/Core/Common/Frametime.hpp>

namespace hg {

struct SpeedData
{
    float _speed;
    float _accel;
    float _min;
    float _max;
    float _pingPong;

    explicit SpeedData(float speed = 0, float accel = 0.f, float min = 0.f,
        float max = 0.f, bool pingPong = false) noexcept
        : _speed{speed},
          _accel{accel},
          _min{min},
          _max{max},
          _pingPong{pingPong ? -1.f : 1.f}
    {}

    void update(const ssvu::FT ft) noexcept
    {
        if(_accel == 0.f)
        {
            return;
        }

        _speed += _accel * ft;

        if(_speed > _max)
        {
            _speed = _max;
            _accel *= _pingPong;
        }
        else if(_speed < _min)
        {
            _speed = _min;
            _accel *= _pingPong;
        }
    }
};

} // namespace hg