From e17c712ec673a797c7ca98044e5116d202fb1f56 Mon Sep 17 00:00:00 2001 From: Rendo Date: Sun, 25 Jan 2026 15:08:16 +0500 Subject: [PATCH] simple transform animation --- src/animation/mod.rs | 10 ++++++++++ src/animation/transform.rs | 20 ++++++++++++++++++++ src/card/mod.rs | 4 +++- src/main.rs | 2 ++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 src/animation/mod.rs create mode 100644 src/animation/transform.rs diff --git a/src/animation/mod.rs b/src/animation/mod.rs new file mode 100644 index 0000000..1d20c7f --- /dev/null +++ b/src/animation/mod.rs @@ -0,0 +1,10 @@ +use bevy::{app::Update, prelude::Plugin}; +pub mod transform; + +pub struct AnimationPlugin; + +impl Plugin for AnimationPlugin { + fn build(&self, app: &mut bevy::app::App) { + app.add_systems(Update, transform::transform_animation); + } +} diff --git a/src/animation/transform.rs b/src/animation/transform.rs new file mode 100644 index 0000000..dda954f --- /dev/null +++ b/src/animation/transform.rs @@ -0,0 +1,20 @@ +use bevy::prelude::*; + +#[derive(Component)] +pub struct AnimatedTransform { + pub translation: Vec3, +} + +impl AnimatedTransform { + pub fn from_xyz(x: f32, y: f32, z: f32) -> Self { + AnimatedTransform { + translation: Vec3 { x, y, z }, + } + } +} + +pub fn transform_animation(query: Query<(&mut Transform, &AnimatedTransform)>) { + for (mut transform, anim_transform) in query { + transform.translation = transform.translation.lerp(anim_transform.translation, 0.5); + } +} diff --git a/src/card/mod.rs b/src/card/mod.rs index 8825c50..1ea841e 100644 --- a/src/card/mod.rs +++ b/src/card/mod.rs @@ -1,3 +1,4 @@ +use crate::animation::transform::AnimatedTransform; use bevy::prelude::*; pub mod drag; @@ -41,7 +42,7 @@ fn hand_setup(mut commands: Commands, asset_server: Res) { } fn card_arrange( - query: Query<(Entity, &mut Transform, &Sprite, &ChildOf), With>, + query: Query<(Entity, &mut AnimatedTransform, &Sprite, &ChildOf), With>, hand: Single<(Entity, &Children), With>, assets: Res>, ) { @@ -67,6 +68,7 @@ fn card_add( commands.spawn(( Sprite::from_image(card_image.0.clone()), Transform::from_xyz(0., 0., 0.), + AnimatedTransform::from_xyz(0., 0., 0.), Card, ChildOf(hand.entity()), )); diff --git a/src/main.rs b/src/main.rs index 3a3eea7..a8fd8f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use bevy::{audio::Volume, prelude::*}; +mod animation; mod buttons; mod card; mod dev_tools; @@ -13,6 +14,7 @@ fn main() { card::CardPlugin, dev_tools::DevToolsPlugin, buttons::ButtonPlugin, + animation::AnimationPlugin, )) .add_systems(Startup, setup) .run();