| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- #pragma once
- #include <cmath>
- #include <utility>
- float lerp(float a, float b, float r);
- float linearRemap(float val, float fromMin, float fromMax, float toMin, float toMax);
- struct EasingEngine
- {
- float timer = 0;
- float deltaTime = 0;
- float runDeltaTime = 0;
- float result_ = 0;
- bool hasRestarted = 0;
- EasingEngine &update(float deltaTime)
- {
- this->deltaTime += deltaTime;
- this->runDeltaTime = this->deltaTime;
- this->result_ = 0;
- hasRestarted = 0;
- return *this;
- }
- EasingEngine &constant(float length, float val = 0)
- {
- if (runDeltaTime == 0) { return *this; }
- if (runDeltaTime > length)
- {
- runDeltaTime -= length;
- result_ += val;
- }
- else
- {
- //float ratio = runDeltaTime / length;
- result_ += val;
- runDeltaTime = 0;
- }
- return *this;
- }
- EasingEngine &goTowards(float length, float val = 0)
- {
- if (runDeltaTime == 0) { return *this; }
- if (runDeltaTime > length)
- {
- runDeltaTime -= length;
- result_ = val;
- }
- else
- {
- float ratio = runDeltaTime / length;
- result_ = lerp(result_, val, ratio);
- runDeltaTime = 0;
- }
- return *this;
- }
- EasingEngine &linear(float length, float start = 0, float end = 1)
- {
- if (runDeltaTime == 0) { return *this; }
- if (runDeltaTime > length)
- {
- runDeltaTime -= length;
- result_ += end;
- }
- else
- {
- float ratio = runDeltaTime / length;
- result_ += linearRemap(ratio, 0, 1, start, end);
- runDeltaTime = 0;
- }
-
- return *this;
- }
- EasingEngine &sin(float length, float speed = 1, float start = 0, float end = 1)
- {
- if (runDeltaTime == 0) { return *this; }
- if (runDeltaTime > length)
- {
- runDeltaTime -= length;
- result_ += linearRemap((std::sinf(length * speed) + 1.f)/2.f, 0, 1, start, end);
- }
- else
- {
- result_ += linearRemap((std::sinf(runDeltaTime * speed) + 1.f)/2.f, 0, 1, start, end);
- runDeltaTime = 0;
- }
- return *this;
- }
- EasingEngine &clamp(float min = 0, float max = 1)
- {
- result_ = std::min(std::max(result_, min), max);
- return *this;
- }
- //goes up and down using sin
- EasingEngine &burst(float length, float start = 0, float end = 1, float power = 1)
- {
- if (runDeltaTime == 0) { return *this; }
- if (runDeltaTime > length)
- {
- runDeltaTime -= length;
- result_ += start;
- }
- else
- {
- float ratio = runDeltaTime / length;
- result_ += linearRemap(std::powf(std::sinf(ratio*3.1415926), power)
- , 0, 1, start, end);
- runDeltaTime = 0;
- }
- return *this;
- }
- float result()
- {
- if (runDeltaTime != 0) { deltaTime = 0; hasRestarted = true; } //reset animation
- return result_;
- }
- };
|