YourGameLib
Loading...
Searching...
No Matches
motion.h
Go to the documentation of this file.
1/*
2Copyright (c) 2019-2024 Alexander Scholz
3
4This software is provided 'as-is', without any express or implied
5warranty. In no event will the authors be held liable for any damages
6arising from the use of this software.
7
8Permission is granted to anyone to use this software for any purpose,
9including commercial applications, and to alter it and redistribute it
10freely, subject to the following restrictions:
11
121. The origin of this software must not be misrepresented; you must not
13 claim that you wrote the original software. If you use this software
14 in a product, an acknowledgment in the product documentation would be
15 appreciated but is not required.
162. Altered source versions must be plainly marked as such, and must not be
17 misrepresented as being the original software.
183. This notice may not be removed or altered from any source distribution.
19*/
20#ifndef YOURGAME_MOTION_H
21#define YOURGAME_MOTION_H
22
23#include <functional>
24#include <vector>
25
26namespace yourgame
27{
28 namespace util
29 {
30 class Motion
31 {
32 public:
33 enum class FlowType
34 {
35 ONCE,
36 REPEAT,
38 };
39
40 enum class SegmentEase
41 {
42 NONE,
43 SQUARE,
45 CUBE,
47 SINE,
49 SMOOTH,
51 };
52
53 Motion(FlowType flowType);
54 Motion &addIdle(float dt, float val);
55 Motion &addRamp(float dt, float val0, float val1, SegmentEase ease);
56
57 void tick(double dt);
58 float val();
59
60 void pause(bool pause);
61 void restart();
62 void setDirection(bool forward);
63
64 bool isFinished();
65 bool isPaused();
66 bool getDirection();
67
68 private:
69 struct Segment
70 {
71 Segment(float dt,
72 std::function<float(float, const Segment &)> calcFn,
73 std::function<float(float, float)> easeFn,
74 float val0,
75 float val1,
76 float val2) : dt(dt), calcFn(calcFn), easeFn(easeFn), val0(val0), val1(val1), val2(val2) {}
77
78 float dt;
79 std::function<float(float, const Segment &)> calcFn;
80 std::function<float(float, float)> easeFn;
81 float val0;
82 float val1;
83 float val2;
84 };
85
86 std::vector<Segment> m_segments;
87 FlowType m_flowType;
88 double m_t;
89 double m_direction;
90 double m_duration;
91 float m_currentValue;
92 bool m_finished;
93 bool m_paused;
94
95 static float calcIdle(float t, const Segment &segment);
96 static float calcRamp(float t, const Segment &segment);
97 static std::function<float(float, float)> getEaseFn(SegmentEase ease);
98 static float calcEaseNone(float t, float dt);
99 static float calcEaseSquare(float t, float dt);
100 static float calcEaseSquareDown(float t, float dt);
101 static float calcEaseCube(float t, float dt);
102 static float calcEaseCubeDown(float t, float dt);
103 static float calcEaseSine(float t, float dt);
104 static float calcEaseSineDown(float t, float dt);
105 static float calcEaseSmooth(float t, float dt);
106 static float calcEaseSmooth2(float t, float dt);
107 };
108 } // namespace util
109} // namespace yourgame
110
111#endif
Definition motion.h:31
FlowType
Definition motion.h:34
void pause(bool pause)
Definition motion.cpp:120
Motion & addIdle(float dt, float val)
Definition motion.cpp:35
bool getDirection()
Definition motion.cpp:148
SegmentEase
Definition motion.h:41
bool isFinished()
Definition motion.cpp:138
Motion & addRamp(float dt, float val0, float val1, SegmentEase ease)
Definition motion.cpp:42
bool isPaused()
Definition motion.cpp:143
float val()
Definition motion.cpp:115
void restart()
Definition motion.cpp:125
void tick(double dt)
Definition motion.cpp:49
void setDirection(bool forward)
Definition motion.cpp:133
Definition audio.h:27