From 142e00794097dfb78c4b758a2d39d26fae070092 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 13 Mar 2021 07:25:23 -0500 Subject: Created Interpolation abstraction This simplifies EffectSystem quite a bit, and will be useful in other classes. --- CMakeLists.txt | 1 + src/effect_system.cpp | 44 ++++++-------------------------------------- src/effect_system.h | 39 +++++++++++++-------------------------- src/interpolation.cpp | 19 +++++++++++++++++++ src/interpolation.h | 26 ++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 64 deletions(-) create mode 100644 src/interpolation.cpp create mode 100644 src/interpolation.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c5c7767..c8a1753 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,6 +46,7 @@ add_executable(tanetane src/game.cpp src/map.cpp src/font.cpp + src/interpolation.cpp src/transform_system.cpp src/camera_system.cpp src/animation_system.cpp diff --git a/src/effect_system.cpp b/src/effect_system.cpp index 4dff13c..9b24d3e 100644 --- a/src/effect_system.cpp +++ b/src/effect_system.cpp @@ -4,23 +4,9 @@ void EffectSystem::tick(double dt) { if (game_.isGameplayPaused()) return; - if (screenFade_ != screenFadeDest_) { - screenFadeThus_ += dt; - if (screenFadeThus_ >= screenFadeLength_) { - screenFadeThus_ = screenFadeLength_; - } - - screenFade_ = (screenFadeDest_ - screenFadeStart_) / screenFadeLength_ * screenFadeThus_ + screenFadeStart_; - } - - if (mapFade_ != mapFadeDest_) { - mapFadeThus_ += dt; - if (mapFadeThus_ >= mapFadeLength_) { - mapFadeThus_ = mapFadeLength_; - } - - mapFade_ = (mapFadeDest_ - mapFadeStart_) / mapFadeLength_ * mapFadeThus_ + mapFadeStart_; - } + screenFade_.tick(dt); + mapFade_.tick(dt); + circleEffect_.tick(dt); if (cameraShaking_) { cameraShakeTimer_.accumulate(dt); @@ -37,29 +23,14 @@ void EffectSystem::tick(double dt) { cameraShakeOffset_.y() = 0; } } - - if (circleEffect_ != circleEffectDest_) { - circleEffectThus_ += dt; - if (circleEffectThus_ >= circleEffectLength_) { - circleEffectThus_ = circleEffectLength_; - } - - circleEffect_ = (circleEffectDest_ - circleEffectStart_) / circleEffectLength_ * circleEffectThus_ + circleEffectStart_; - } } void EffectSystem::fadeScreen(int length, double amount) { - screenFadeStart_ = screenFade_; - screenFadeDest_ = amount; - screenFadeLength_ = length; - screenFadeThus_ = 0; + screenFade_.start(length, amount); } void EffectSystem::fadeMap(int length, double amount) { - mapFadeStart_ = mapFade_; - mapFadeDest_ = amount; - mapFadeLength_ = length; - mapFadeThus_ = 0; + mapFade_.start(length, amount); } void EffectSystem::shakeCamera(int period) { @@ -75,8 +46,5 @@ void EffectSystem::stopShakingCamera() { } void EffectSystem::circleTransition(int length, double amount) { - circleEffectStart_ = circleEffect_; - circleEffectDest_ = amount; - circleEffectLength_ = length; - circleEffectThus_ = 0; + circleEffect_.start(length, amount); } diff --git a/src/effect_system.h b/src/effect_system.h index 904e6cb..b2b4669 100644 --- a/src/effect_system.h +++ b/src/effect_system.h @@ -1,6 +1,7 @@ #ifndef EFFECT_SYSTEM_H_0B497B39 #define EFFECT_SYSTEM_H_0B497B39 +#include "interpolation.h" #include "system.h" #include "timer.h" #include "vector.h" @@ -41,54 +42,40 @@ public: /* Information */ - bool isScreenFaded() const { return screenFade_ > 0.0; } + bool isScreenFaded() const { return screenFade_.getProgress() > 0.0; } - double getScreenFadeProgress() const { return screenFade_; } + double getScreenFadeProgress() const { return screenFade_.getProgress(); } - bool isScreenFadeComplete() const { return screenFade_ == screenFadeDest_; } + bool isScreenFadeComplete() const { return screenFade_.isComplete(); } - bool isMapFaded() const { return mapFade_ > 0.0; } + bool isMapFaded() const { return mapFade_.getProgress() > 0.0; } - double getMapFadeProgress() const { return mapFade_; } + double getMapFadeProgress() const { return mapFade_.getProgress(); } - bool isMapFadeComplete() const { return mapFade_ == mapFadeDest_; } + bool isMapFadeComplete() const { return mapFade_.isComplete(); } const vec2i& getCameraShakeOffset() const { return cameraShakeOffset_; } bool isCameraShaking() const { return cameraShaking_; } - bool isCircleTransitionActive() const { return circleEffect_ > 0.0; } + bool isCircleTransitionActive() const { return circleEffect_.getProgress() > 0.0; } - double getCircleTransitionProgress() const { return circleEffect_; } + double getCircleTransitionProgress() const { return circleEffect_.getProgress(); } - bool isCircleTransitionComplete() const { return circleEffect_ == circleEffectDest_; } + bool isCircleTransitionComplete() const { return circleEffect_.isComplete(); } private: Game& game_; - double screenFade_ = 0.0; - double screenFadeDest_ = 0.0; - double screenFadeStart_ = 0.0; - double screenFadeLength_ = 0; - double screenFadeThus_ = 0; - - double mapFade_ = 0.0; - double mapFadeDest_ = 0.0; - double mapFadeStart_ = 0.0; - double mapFadeLength_ = 0.0; - double mapFadeThus_ = 0.0; + Interpolation screenFade_; + Interpolation mapFade_; + Interpolation circleEffect_; bool cameraShaking_ = false; vec2i cameraShakeOffset_ { 0, 0 }; bool cameraShakeOn_ = false; Timer cameraShakeTimer_ { 0 }; - - double circleEffect_ = 0.0; - double circleEffectDest_ = 0.0; - double circleEffectStart_ = 0.0; - double circleEffectLength_ = 0.0; - double circleEffectThus_ = 0.0; }; #endif /* end of include guard: EFFECT_SYSTEM_H_0B497B39 */ diff --git a/src/interpolation.cpp b/src/interpolation.cpp new file mode 100644 index 0000000..3c9bcf8 --- /dev/null +++ b/src/interpolation.cpp @@ -0,0 +1,19 @@ +#include "interpolation.h" + +void Interpolation::start(int length, double amount) { + start_ = progress_; + dest_ = amount; + length_ = length; + thus_ = 0; +} + +void Interpolation::tick(double dt) { + if (progress_ != dest_) { + thus_ += dt; + if (thus_ >= length_) { + thus_ = length_; + } + + progress_ = (dest_ - start_) / length_ * thus_ + start_; + } +} diff --git a/src/interpolation.h b/src/interpolation.h new file mode 100644 index 0000000..d89602d --- /dev/null +++ b/src/interpolation.h @@ -0,0 +1,26 @@ +#ifndef INTERPOLATION_H_861230A8 +#define INTERPOLATION_H_861230A8 + +class Interpolation { +public: + + void start(int length, double amount); + + void tick(double dt); + + // Info + + double getProgress() const { return progress_; } + + bool isComplete() const { return progress_ == dest_; } + +private: + + double progress_ = 0.0; + double dest_ = 0.0; + double start_ = 0.0; + double length_ = 0.0; + double thus_ = 0.0; +}; + +#endif /* end of include guard: INTERPOLATION_H_861230A8 */ -- cgit 1.4.1