Skip to content

Commit

Permalink
feat: Animate combined aspect on combiner socket
Browse files Browse the repository at this point in the history
This was actually a bit more tricky then I expected. I originally wanted
to tween the sprites alpha channel but that didn't work because you
apparently can't have mutliple different tween generics in a Track. So
instead I used the scale.
  • Loading branch information
PraxTube committed Jul 29, 2024
1 parent 4d6d772 commit f70ba6c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 21 deletions.
83 changes: 67 additions & 16 deletions src/aspect/combiner.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
use std::time::Duration;

use bevy::prelude::*;
use bevy_tweening::{
lens::{TransformPositionLens, TransformScaleLens},
Animator, EaseFunction, Tracks, Tween,
};

use crate::{audio::PlaySound, player::input::PlayerInput, GameAssets, GameState};
use crate::{
aspect::icon::{DEFAULT_ICON_POSITION, HIGHLIGHTED_ICON_POSITION},
audio::PlaySound,
player::input::PlayerInput,
GameAssets, GameState,
};

use super::{
icon::icon_texture,
Expand Down Expand Up @@ -114,28 +125,68 @@ fn select_aspects(
}

fn show_combiner_icon(
mut commands: Commands,
assets: Res<GameAssets>,
mut combiner: ResMut<Combiner>,
mut q_combiner_icon: Query<(&mut Handle<Image>, &mut Visibility), With<CombinerIcon>>,
mut q_combiner_icon: Query<(Entity, &mut Handle<Image>), With<CombinerIcon>>,
mut visible: Local<bool>,
) {
let (mut texture, mut visibility) = match q_combiner_icon.get_single_mut() {
let (entity, mut texture) = match q_combiner_icon.get_single_mut() {
Ok(r) => r,
Err(_) => return,
};

let (left_aspect, right_aspect) =
if let (Some(l_aspect), Some(r_aspect)) = (combiner.left_aspect, combiner.right_aspect) {
(l_aspect, r_aspect)
} else {
*visibility = Visibility::Hidden;
return;
};

*visibility = Visibility::Inherited;

let combined_aspect = aspect_combinations(&left_aspect, &right_aspect);
combiner.current_combination = Some(combined_aspect);
*texture = icon_texture(&assets, &combined_aspect);
if let (Some(left_aspect), Some(right_aspect)) = (combiner.left_aspect, combiner.right_aspect) {
let combined_aspect = aspect_combinations(&left_aspect, &right_aspect);
combiner.current_combination = Some(combined_aspect);
*texture = icon_texture(&assets, &combined_aspect);

if !*visible {
*visible = true;

let seq = Tracks::new([
Tween::new(
EaseFunction::QuarticOut,
Duration::from_secs_f32(0.2),
TransformPositionLens {
start: DEFAULT_ICON_POSITION.extend(0.0),
end: HIGHLIGHTED_ICON_POSITION.extend(0.0),
},
),
Tween::new(
EaseFunction::QuarticOut,
Duration::from_secs_f32(0.2),
TransformScaleLens {
start: Vec3::splat(0.0),
end: Vec3::splat(1.0),
},
),
]);
commands.entity(entity).insert(Animator::new(seq));
}
} else if *visible {
*visible = false;

let seq = Tracks::new([
Tween::new(
EaseFunction::QuarticOut,
Duration::from_secs_f32(0.2),
TransformPositionLens {
start: HIGHLIGHTED_ICON_POSITION.extend(0.0),
end: DEFAULT_ICON_POSITION.extend(0.0),
},
),
Tween::new(
EaseFunction::QuarticOut,
Duration::from_secs_f32(0.2),
TransformScaleLens {
start: Vec3::splat(1.0),
end: Vec3::splat(0.0),
},
),
]);
commands.entity(entity).insert(Animator::new(seq));
}
}

fn select_combined_aspect(
Expand Down
2 changes: 1 addition & 1 deletion src/aspect/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::{
};

pub const DEFAULT_ICON_POSITION: Vec2 = Vec2::new(0.0, 16.0);
const HIGHLIGHTED_ICON_POSITION: Vec2 = Vec2::new(0.0, 24.0);
pub const HIGHLIGHTED_ICON_POSITION: Vec2 = Vec2::new(0.0, 24.0);
const DEHIGHLIGHTED_ICON_POSITION: Vec2 = Vec2::new(0.0, 8.0);
const REPOSITION_TIME: f32 = 0.2;

Expand Down
8 changes: 4 additions & 4 deletions src/aspect/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{

use super::{
combiner::{aspect_combinations, is_socket_combination_possible, CombinedAspect, Combiner},
icon::{icon_texture, DEFAULT_ICON_POSITION},
icon::{icon_texture, DEFAULT_ICON_POSITION, HIGHLIGHTED_ICON_POSITION},
name_text::AspectNameText,
Aspect, AspectCombiner, AspectCombinerInitiater, AspectSocketInitiater,
};
Expand Down Expand Up @@ -173,11 +173,11 @@ fn spawn_combiner_socket(
let icon = commands
.spawn((
CombinerIcon,
YSortChild(17.0),
YSortChild(HIGHLIGHTED_ICON_POSITION.y + 1.0),
SpriteBundle {
texture: icon_texture(&assets, &Aspect::NotImplemented),
transform: Transform::from_translation(DEFAULT_ICON_POSITION.extend(0.0)),
visibility: Visibility::Hidden,
transform: Transform::from_translation(DEFAULT_ICON_POSITION.extend(0.0))
.with_scale(Vec3::splat(0.0)),
..default()
},
))
Expand Down

0 comments on commit f70ba6c

Please sign in to comment.