diff --git a/CMakeLists.txt b/CMakeLists.txt index 1a4e171..0c45d12 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -200,6 +200,9 @@ add_library(Engine STATIC engine/src/core/audio/SoundSource.h engine/src/loader/sounds/SoundUploader.cpp engine/src/loader/sounds/SoundUploader.h + engine/src/core/animation/components/TransformAnimationComponent.h + engine/src/core/animation/AnimationSystem.cpp + engine/src/core/animation/AnimationSystem.h ) target_include_directories(Engine PUBLIC engine/src ${dr_libs_SOURCE_DIR}) target_link_libraries(Engine PUBLIC OpenGL::GL glfw glad glm::glm imgui spdlog::spdlog tinyobjloader stb_image OpenAL::OpenAL) diff --git a/assets/sounds/407484__ruthdt98__boardgame.wav b/assets/sounds/407484__ruthdt98__boardgame.wav new file mode 100644 index 0000000..976c102 Binary files /dev/null and b/assets/sounds/407484__ruthdt98__boardgame.wav differ diff --git a/assets/sounds/pawn_step.wav b/assets/sounds/pawn_step.wav new file mode 100644 index 0000000..3fb890a Binary files /dev/null and b/assets/sounds/pawn_step.wav differ diff --git a/engine/src/core/animation/AnimationSystem.cpp b/engine/src/core/animation/AnimationSystem.cpp new file mode 100644 index 0000000..2469783 --- /dev/null +++ b/engine/src/core/animation/AnimationSystem.cpp @@ -0,0 +1,53 @@ +// +// 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()) { + animation.time += deltaTime; + + auto& transform = m_registry.getComponent(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; + } + } + } + } + + glm::vec3 AnimationSystem::evaluate(const AnimationTrack &track, float time) { + Keyframe a; + Keyframe b; + findSurroundingKeys(track, time, a, b); + + float localT = (time - a.time) / (b.time - a.time); + float easedT = a.easingFunction(localT); + return glm::mix(a.value, b.value, easedT); + } + + void AnimationSystem::findSurroundingKeys(const AnimationTrack &track, float time, Keyframe &a, Keyframe &b) { + for (int i = 1; i < track.keys.size(); i++) { + if (track.keys[i-1].time < 0.f && track.keys[i].time > 0.f) { + a = track.keys[i-1]; + b = track.keys[i]; + return; + } + } + } +} // engine \ No newline at end of file diff --git a/engine/src/core/animation/AnimationSystem.h b/engine/src/core/animation/AnimationSystem.h new file mode 100644 index 0000000..fa2f5b0 --- /dev/null +++ b/engine/src/core/animation/AnimationSystem.h @@ -0,0 +1,23 @@ +// +// Created by sebastian on 04.08.26. +// + +#ifndef COLORRACE_ANIMATIONSYSTEM_H +#define COLORRACE_ANIMATIONSYSTEM_H +#include "components/TransformAnimationComponent.h" +#include "ecs/EntityRegistry.h" +#include "glm/glm.hpp" +namespace engine::animation { + class AnimationSystem { + public: + explicit AnimationSystem(EntityRegistry& registry) : m_registry(registry) {} + void update(float deltaTime); + private: + EntityRegistry& m_registry; + glm::vec3 evaluate(const AnimationTrack& track, float time); + void findSurroundingKeys(const AnimationTrack& track, float time, Keyframe& a, Keyframe& b); + + }; +} // engine + +#endif //COLORRACE_ANIMATIONSYSTEM_H diff --git a/engine/src/core/animation/components/TransformAnimationComponent.h b/engine/src/core/animation/components/TransformAnimationComponent.h new file mode 100644 index 0000000..17b4384 --- /dev/null +++ b/engine/src/core/animation/components/TransformAnimationComponent.h @@ -0,0 +1,40 @@ +// +// Created by sebastian on 04.08.26. +// + +#ifndef COLORRACE_TRANSFORMANIMATIONCOMPONENT_H +#define COLORRACE_TRANSFORMANIMATIONCOMPONENT_H + +#include + +#include "glm/glm.hpp" +namespace engine::animation { + using EasingFunction = std::function; + + struct Keyframe { + float time; + glm::vec3 value; + + EasingFunction easingFunction = [](float t) { + return t; + }; + }; + + struct AnimationTrack { + std::vector keys; + }; + + struct TransformAnimationComponent { + float time = 0.0f; + float duration = 0.0f; + + AnimationTrack position; + AnimationTrack rotation; + AnimationTrack scale; + + std::function onFinished; + bool loop = false; + bool finished = false; + }; +} +#endif //COLORRACE_TRANSFORMANIMATIONCOMPONENT_H diff --git a/game/src/GameScene.cpp b/game/src/GameScene.cpp index 02b1aee..f3b1d19 100644 --- a/game/src/GameScene.cpp +++ b/game/src/GameScene.cpp @@ -57,5 +57,6 @@ std::vector GameScene::getRequiredAssets() const { requests.emplace_back(engine::ModelRequest{"pawn_yellow", "assets/models/chess_yellow.obj", engine::ModelSplitPolicy::Combined}); requests.emplace_back(engine::ModelRequest{ "gameboard", "assets/models/GameBoard.obj", engine::ModelSplitPolicy::Combined}); requests.emplace_back(engine::SoundRequest{ "rolling_dice", "assets/sounds/rolling_dice.wav"}); + requests.emplace_back(engine::SoundRequest{ "pawn_step", "assets/sounds/pawn_step.wav"}); return requests; } \ No newline at end of file