From d4f443070017c5c2e6a938bf4d0a86b70ae6beaa Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Mon, 6 Jan 2025 10:26:03 +0100 Subject: revert Animator --- src/crepe/system/AnimatorSystem.cpp | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) (limited to 'src/crepe/system') 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 animations = mgr.get_components_by_type(); - duration_t elapsed = timer.get_delta_time(); + float elapsed_time = duration_cast>(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(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; + } } } -- cgit v1.2.3