aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system/AnimatorSystem.cpp
diff options
context:
space:
mode:
authorheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-30 21:02:27 +0100
committerheavydemon21 <nielsstunnebrink1@gmail.com>2024-11-30 21:02:27 +0100
commit6ae0b9038e432869b506cbdfe2779e97d3732d87 (patch)
tree7fadff8dd2125c48422ab10c6faa532650f8327a /src/crepe/system/AnimatorSystem.cpp
parent1520cb55ad714583c3903f2988b0fdc38ede9c18 (diff)
improved animator
Diffstat (limited to 'src/crepe/system/AnimatorSystem.cpp')
-rw-r--r--src/crepe/system/AnimatorSystem.cpp26
1 files changed, 19 insertions, 7 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;
+ }
}
}