Animation speed for dissolve looks sick as hell

main
Elijah C. Voigt 1 year ago
parent c319ecab61
commit 9c4e43a24c

@ -14,6 +14,7 @@ impl Plugin for Display3dPlugin {
)) ))
.init_state::<DissolvingAnimation>() .init_state::<DissolvingAnimation>()
.init_resource::<PiecePointer>() .init_resource::<PiecePointer>()
.insert_resource(AnimationSpeed(1.0))
.insert_resource(Msaa::Off) .insert_resource(Msaa::Off)
.insert_resource(AmbientLight { .insert_resource(AmbientLight {
color: Color::WHITE, color: Color::WHITE,
@ -91,15 +92,19 @@ impl Plugin for Display3dPlugin {
put_down.run_if(any_component_removed::<game::Selected>()), put_down.run_if(any_component_removed::<game::Selected>()),
dissolve_animation.run_if(any_with_component::<Dissolving>), dissolve_animation.run_if(any_with_component::<Dissolving>),
capture_piece.run_if(any_with_component::<game::Captured>), capture_piece.run_if(any_with_component::<game::Captured>),
skip_animation
.run_if(in_state(GameState::Play))
.run_if(pressed(KeyCode::Enter).or_else(pressed(MouseButton::Left))),
un_skip_animation.run_if(in_state(GameState::Play)).run_if(
just_released(KeyCode::Enter).or_else(just_released(MouseButton::Left)),
),
monitor_animations monitor_animations
.run_if(in_state(GameState::Play)) .run_if(in_state(GameState::Play))
.run_if(any_component_changed::<AnimationPlayer>()), .run_if(any_component_changed::<AnimationPlayer>()),
set_animation_player_speed
.run_if(any_component_added::<Animating>()
.or_else(resource_changed::<AnimationSpeed>)),
set_animation_speed
.run_if(
just_pressed(KeyCode::Enter)
.or_else(just_pressed(MouseButton::Left))
.or_else(just_released(KeyCode::Enter))
.or_else(just_released(MouseButton::Left))
)
), ),
) )
.add_systems( .add_systems(
@ -128,25 +133,25 @@ impl Plugin for Display3dPlugin {
( (
// Toggle hidden/visible 3d entities // Toggle hidden/visible 3d entities
manage_state_entities::<DisplayState>(), manage_state_entities::<DisplayState>(),
// fixup_shadows,
color_grading_tweak.run_if(resource_exists::<tweak::GameTweaks>), color_grading_tweak.run_if(resource_exists::<tweak::GameTweaks>),
fog_tweak.run_if(resource_exists::<tweak::GameTweaks>), fog_tweak.run_if(resource_exists::<tweak::GameTweaks>),
bloom_tweak.run_if(resource_exists::<tweak::GameTweaks>), bloom_tweak.run_if(resource_exists::<tweak::GameTweaks>),
update_pieces.run_if(resource_exists::<tweak::GameTweaks>),
), ),
) )
.add_systems( .add_systems(
Update, Update,
(
setup_dissolve_materials setup_dissolve_materials
.run_if(in_state(GameState::Intro))
.run_if( .run_if(
any_component_added::<Handle<StandardMaterial>>() any_component_added::<Handle<StandardMaterial>>()
.or_else(any_component_changed::<Handle<StandardMaterial>>()), .or_else(any_component_changed::<Handle<StandardMaterial>>()),
), ),
) )
)
.add_systems( .add_systems(
OnEnter(GameState::Play), OnEnter(GameState::Play),
( (
update_pieces.run_if(resource_exists::<tweak::GameTweaks>),
opening_animation opening_animation
.run_if(run_once()) .run_if(run_once())
.run_if(in_state(DisplayState::Display3d)), .run_if(in_state(DisplayState::Display3d)),
@ -154,7 +159,10 @@ impl Plugin for Display3dPlugin {
) )
.add_systems( .add_systems(
OnEnter(GameState::Title), OnEnter(GameState::Title),
(fixup_shadows, intro_title_dissolve), (
intro_title_dissolve,
fixup_shadows
)
) )
.add_systems(OnExit(GameState::Title), outro_title_dissolve) .add_systems(OnExit(GameState::Title), outro_title_dissolve)
.add_systems( .add_systems(
@ -193,6 +201,9 @@ enum DissolvingAnimation {
Out, Out,
} }
#[derive(Debug, Resource)]
struct AnimationSpeed(f32);
#[derive(Debug, Resource, Clone)] #[derive(Debug, Resource, Clone)]
struct AssetsMap { struct AssetsMap {
hitbox_shape: Handle<Mesh>, hitbox_shape: Handle<Mesh>,
@ -992,26 +1003,32 @@ fn opening_animation(mut players: Query<&mut AnimationPlayer, (With<Camera>, Wit
}); });
} }
// When called skips any running animations
fn skip_animation( fn set_animation_speed(
mut players: Query<&mut AnimationPlayer, With<Animating>>, mut animation_speed: ResMut<AnimationSpeed>,
tweaks: Res<Assets<Tweaks>>, tweaks: Res<Assets<Tweaks>>,
tweaks_file: Res<tweak::GameTweaks>, tweaks_file: Res<tweak::GameTweaks>,
keys: Res<ButtonInput<KeyCode>>,
mouse: Res<ButtonInput<MouseButton>>,
) { ) {
animation_speed.0 = if keys.pressed(KeyCode::Enter) || mouse.pressed(MouseButton::Left) {
let tweak = tweaks let tweak = tweaks
.get(tweaks_file.handle.clone()) .get(tweaks_file.handle.clone())
.expect("Load tweakfile"); .expect("Load tweakfile");
let speed = tweak.get::<f32>("animation_fast_speed").unwrap(); let speed = tweak.get::<f32>("animation_fast_speed").unwrap();
players.iter_mut().for_each(|mut p| { speed
debug!("Skipping animation"); } else {
p.set_speed(speed); 1.0
}) }
} }
fn un_skip_animation(mut players: Query<&mut AnimationPlayer, With<Animating>>) { // When an animation starts, or the animation speed changes, update player speed
fn set_animation_player_speed(
mut players: Query<&mut AnimationPlayer, With<Animating>>,
animation_speed: Res<AnimationSpeed>,
) {
players.iter_mut().for_each(|mut p| { players.iter_mut().for_each(|mut p| {
debug!("Un-Skipping animation"); p.set_speed(animation_speed.0);
p.set_speed(1.0);
}) })
} }
@ -1093,7 +1110,7 @@ fn setup_dissolve_materials(
.filter(|(child, _, _)| query.iter_many(parents.iter_ancestors(*child)).count() > 0) .filter(|(child, _, _)| query.iter_many(parents.iter_ancestors(*child)).count() > 0)
// Handle this entity (mesh) // Handle this entity (mesh)
.for_each(|(child, std_handle, name)| { .for_each(|(child, std_handle, name)| {
debug!("Setting up dissolve material for {:?} {:?}", name, child); info!("Setting up dissolve material for {:?} {:?}", name, child);
// Get dissolvable data for percentage start // Get dissolvable data for percentage start
let dissolvable = query let dissolvable = query
@ -1139,6 +1156,8 @@ fn capture_piece(
mut state: Local<Option<game::CaptureFlow>>, mut state: Local<Option<game::CaptureFlow>>,
mut commands: Commands, mut commands: Commands,
score: Res<game::Score>, score: Res<game::Score>,
mut timer: Local<f32>,
time: Res<Time>,
) { ) {
match *state { match *state {
// State is None, so we need to initiate the animation // State is None, so we need to initiate the animation
@ -1158,11 +1177,16 @@ fn capture_piece(
game::CaptureFlow::FadeOut(_entity) => { game::CaptureFlow::FadeOut(_entity) => {
// Wait for fade-out animation // Wait for fade-out animation
// If all pieces are done dissolving // If all pieces are done dissolving
if dissolving.is_empty() { if dissolving.is_empty() && *timer >= 3.0 {
debug!("Nothing dissolving, moving on to next step!"); debug!("Nothing dissolving, moving on to next step!");
// reset the timer
*timer = 0.0;
// Move to next state now that animation is done // Move to next state now that animation is done
*state = s.next(); *state = s.next();
// This takes effect after updating all children // This takes effect after updating all children
} else {
*timer += time.delta_seconds();
} }
} }
game::CaptureFlow::Store(entity) => { game::CaptureFlow::Store(entity) => {
@ -1295,45 +1319,34 @@ fn dissolve_animation(
mut dissolve_materials: ResMut<Assets<DissolveMaterial>>, mut dissolve_materials: ResMut<Assets<DissolveMaterial>>,
object_materials: Query<(Entity, &Handle<DissolveMaterial>)>, object_materials: Query<(Entity, &Handle<DissolveMaterial>)>,
mut commands: Commands, mut commands: Commands,
keys: Res<ButtonInput<KeyCode>>,
mouse: Res<ButtonInput<MouseButton>>,
time: Res<Time>, time: Res<Time>,
mut next: ResMut<NextState<DissolvingAnimation>>, mut next: ResMut<NextState<DissolvingAnimation>>,
animation_speed: Res<AnimationSpeed>,
) { ) {
query query
.iter_mut() .iter_mut()
.for_each(|(entity, dissolvable, mut dissolving)| { .for_each(|(entity, dissolvable, mut dissolving)| {
debug!("Entity {:?} has Dissolving {:?}", entity, dissolving); debug!("Entity {:?} has Dissolving {:?}", entity, dissolving);
let skip = keys.just_pressed(KeyCode::Enter) || mouse.just_pressed(MouseButton::Left);
let percentage = match *dissolving { let percentage = match *dissolving {
Dissolving::In(mut sec) => { Dissolving::In(mut sec) => {
if skip {
1.0
} else {
// Check if seconds is below 0.0 // Check if seconds is below 0.0
sec = (sec - time.delta_seconds()).max(0.0); sec = (sec - (time.delta_seconds() * animation_speed.0)).max(0.0);
*dissolving = Dissolving::In(sec); *dissolving = Dissolving::In(sec);
// Calculate the target percentage value // Calculate the target percentage value
1.0 - (sec / dissolvable.duration) 1.0 - (sec / dissolvable.duration)
} }
}
Dissolving::Out(mut sec) => { Dissolving::Out(mut sec) => {
if skip {
0.0
} else {
// Check if seconds is below 0.0 // Check if seconds is below 0.0
sec = (sec - time.delta_seconds()).max(0.0); sec = (sec - (time.delta_seconds() * animation_speed.0)).max(0.0);
*dissolving = Dissolving::Out(sec); *dissolving = Dissolving::Out(sec);
// Calculate the target percentage value // Calculate the target percentage value
sec / dissolvable.duration sec / dissolvable.duration
} }
}
}; };
object_materials object_materials
@ -1364,7 +1377,7 @@ fn fixup_shadows(
mut point_lights: Query<&mut PointLight>, mut point_lights: Query<&mut PointLight>,
mut directional_lights: Query<&mut DirectionalLight>, mut directional_lights: Query<&mut DirectionalLight>,
) { ) {
debug!("Fixing up shadows"); info!("Fixing up shadows");
spot_lights.iter_mut().for_each(|mut l| { spot_lights.iter_mut().for_each(|mut l| {
l.shadows_enabled = true; l.shadows_enabled = true;
}); });

Loading…
Cancel
Save