//
// Created by sebastian on 04.08.26.
//

#include "AnimationSystem.h"

#include "components/TransformAnimationComponent.h"
#include "ecs/standardComponents/TransformComponent.h"

namespace engine::animation {
    void animation::AnimationSystem::update(float deltaTime) {
        for (auto& [entity, animation] : m_registry.getPool<TransformAnimationComponent>()) {
            animation.time += deltaTime;

            auto& transform = m_registry.getComponent<TransformComponent>(entity);

            transform.m_position = evaluate(animation.position, animation.time);
            transform.m_rotation = evaluate(animation.rotation, animation.time);
            transform.m_scale = evaluate(animation.scale, animation.time);

            if (animation.time >= animation.duration) {
                if (animation.loop) {
                    animation.time = 0.f;
                } else {
                    if (animation.onFinished) {
                        animation.onFinished();
                    }
                    animation.finished = true;
                }
            }
        }
    }
} // engine