diff options
author | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-30 21:02:27 +0100 |
---|---|---|
committer | heavydemon21 <nielsstunnebrink1@gmail.com> | 2024-11-30 21:02:27 +0100 |
commit | 6ae0b9038e432869b506cbdfe2779e97d3732d87 (patch) | |
tree | 7fadff8dd2125c48422ab10c6faa532650f8327a /src/crepe/system | |
parent | 1520cb55ad714583c3903f2988b0fdc38ede9c18 (diff) |
improved animator
Diffstat (limited to 'src/crepe/system')
-rw-r--r-- | src/crepe/system/AnimatorSystem.cpp | 26 | ||||
-rw-r--r-- | src/crepe/system/AnimatorSystem.h | 7 |
2 files changed, 23 insertions, 10 deletions
diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp index 4c40940..6a84150 100644 --- a/src/crepe/system/AnimatorSystem.cpp +++ b/src/crepe/system/AnimatorSystem.cpp @@ -1,7 +1,6 @@ -#include <cstdint> + #include "api/Animator.h" -#include "facade/SDLContext.h" #include "AnimatorSystem.h" #include "ComponentManager.h" @@ -13,12 +12,25 @@ void AnimatorSystem::update() { RefVector<Animator> animations = mgr.get_components_by_type<Animator>(); - uint64_t tick = SDLContext::get_instance().get_ticks(); + double elapsed_time = this->timer.get_current_time(); + for (Animator & a : animations) { if (!a.active) continue; - // (10 frames per second) - a.curr_row = (tick / 100) % a.row; - a.spritesheet.mask.x = (a.curr_row * a.spritesheet.mask.w) + a.curr_col; - a.spritesheet.mask = a.spritesheet.mask; + + double frame_duration = 1.0f / a.fps; + + int cycle_end = (a.cycle_end == -1) ? a.row : cycle_end; + int total_frames = cycle_end - a.cycle_start; + + + int curr_frame = static_cast<int>(elapsed_time / frame_duration) % total_frames; + + a.curr_row = a.cycle_start + curr_frame; + a.spritesheet.mask.x = std::clamp((a.curr_row * a.spritesheet.mask.w - a.offset_x), 0, a.spritesheet.mask.w); + a.spritesheet.mask.y = (a.curr_col * a.spritesheet.mask.h); + + if (!a.looping && curr_frame == total_frames) { + a.active = false; + } } } diff --git a/src/crepe/system/AnimatorSystem.h b/src/crepe/system/AnimatorSystem.h index f8179a9..e8a5158 100644 --- a/src/crepe/system/AnimatorSystem.h +++ b/src/crepe/system/AnimatorSystem.h @@ -1,9 +1,7 @@ #pragma once #include "System.h" - -//TODO: -// control if flip works with animation system +#include "api/LoopTimer.h" namespace crepe { @@ -26,6 +24,9 @@ public: * looping). */ void update() override; + +private: + LoopTimer & timer = LoopTimer::get_instance(); }; } // namespace crepe |