aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/system
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2025-01-06 10:26:03 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2025-01-06 10:26:03 +0100
commitd4f443070017c5c2e6a938bf4d0a86b70ae6beaa (patch)
treef3a6f399981d69e66916544e34066af6bce3fd93 /src/crepe/system
parentec69f40540317215d0f30e1f8e43d0e18450f8cc (diff)
revert Animator
Diffstat (limited to 'src/crepe/system')
-rw-r--r--src/crepe/system/AnimatorSystem.cpp34
1 files changed, 16 insertions, 18 deletions
diff --git a/src/crepe/system/AnimatorSystem.cpp b/src/crepe/system/AnimatorSystem.cpp
index 6c93372..e5ab2fa 100644
--- a/src/crepe/system/AnimatorSystem.cpp
+++ b/src/crepe/system/AnimatorSystem.cpp
@@ -13,30 +13,28 @@ void AnimatorSystem::frame_update() {
LoopTimerManager & timer = this->mediator.loop_timer;
RefVector<Animator> animations = mgr.get_components_by_type<Animator>();
- duration_t elapsed = timer.get_delta_time();
+ float elapsed_time = duration_cast<duration<float>>(timer.get_elapsed_time()).count();
- for (Animator & animator : animations) {
- if (!animator.active) continue;
+ for (Animator & a : animations) {
+ if (!a.active) continue;
+ if (a.data.fps == 0) continue;
- Animator::Data & data = animator.data;
- if (animator.data.fps == 0) continue;
+ Animator::Data & ctx = a.data;
+ float frame_duration = 1.0f / ctx.fps;
- if (animator.data.cycle_end == -1)
- animator.data.cycle_end = animator.grid_size.x * animator.grid_size.y;
+ int last_frame = ctx.row;
- animator.elapsed += elapsed;
- duration_t frame_duration = 1000ms / animator.data.fps;
+ int cycle_end = (ctx.cycle_end == -1) ? a.grid_size.x : ctx.cycle_end;
+ int total_frames = cycle_end - ctx.cycle_start;
- if (animator.elapsed > frame_duration) {
- animator.elapsed = 0ms;
- animator.data.frame++;
+ int curr_frame = static_cast<int>(elapsed_time / frame_duration) % total_frames;
- if (animator.data.looping && animator.data.frame >= animator.data.cycle_end)
- animator.data.frame = animator.data.cycle_start;
- }
+ ctx.row = ctx.cycle_start + curr_frame;
+ a.spritesheet.mask.x = ctx.row * a.spritesheet.mask.w;
+ a.spritesheet.mask.y = (ctx.col * a.spritesheet.mask.h);
- Sprite::Mask & mask = animator.spritesheet.mask;
- mask.x = animator.data.frame % animator.grid_size.x * mask.w;
- mask.y = animator.data.frame / animator.grid_size.x * mask.h;
+ if (!ctx.looping && curr_frame == ctx.cycle_start && last_frame == total_frames - 1) {
+ a.active = false;
+ }
}
}