From ed9a2d65814cb8e21fea5fbc93fd8bc01a4ccacc Mon Sep 17 00:00:00 2001 From: Elijah Voigt Date: Sun, 19 Nov 2023 21:02:42 -0800 Subject: [PATCH] Camera animations! And Turns! --- src/display3d.rs | 54 +++++++++++++++++++++- src/game.rs | 30 ++++++++++-- target/x86_64-unknown-linux-gnu/Dockerfile | 14 ------ 3 files changed, 79 insertions(+), 19 deletions(-) delete mode 100644 target/x86_64-unknown-linux-gnu/Dockerfile diff --git a/src/display3d.rs b/src/display3d.rs index f1f060c..9b50102 100644 --- a/src/display3d.rs +++ b/src/display3d.rs @@ -42,6 +42,9 @@ impl Plugin for Display3dPlugin { .run_if(on_event::()), pick_up.run_if(any_component_added::), put_down.run_if(any_component_removed::()), + switch_sides + .run_if(in_state(GameState::Play)) + .run_if(state_changed::()), ), ) .add_systems( @@ -59,7 +62,11 @@ impl Plugin for Display3dPlugin { ) .add_systems( OnEnter(DisplayState::Display3d), - (activate::, set_piece_texture), + ( + activate::, + set_piece_texture, + opening_animation.run_if(run_once()), + ), ) .add_systems(OnExit(DisplayState::Display3d), deactivate::) .add_systems( @@ -160,6 +167,10 @@ fn initialize(mut commands: Commands, board: Res, assets: Res>, assets: Res, + assets_map: Res, + gltfs: Res>, + state: Res>, + mut players: Query<&mut AnimationPlayer>, mut commands: Commands, ) { events @@ -167,6 +178,7 @@ fn hydrate_camera( .filter(|(name, _)| name.as_str() == "GameCam") .for_each(|(_, entity)| { info!("Initialize 3d camera"); + // Populate the components for the camera commands.entity(entity).insert(( Display3d, DisplayState::Display3d, @@ -193,6 +205,20 @@ fn hydrate_camera( }, Name::new("3D Camera"), )); + + let gltf = gltfs.get(&assets_map.models).expect("Load GLTF content"); + // Set it to the default position by starting the initial animation + if let Ok(mut player) = players.get_mut(entity) { + info!("Animations: {:?}", gltf.named_animations.keys()); + // GameCamIntro1, GameCamIntro2, GameCamSide1>2, GameCamSide2>1 + let animation = match state.get() { + game::TurnState::SideA => gltf.named_animations.get("GameCamIntro1"), + game::TurnState::SideB => gltf.named_animations.get("GameCamIntro2"), + }; + player + .play(animation.expect("Camera Startup").clone()) + .pause(); + } }); } @@ -587,3 +613,29 @@ fn set_tile_hitbox( *transform = Transform::from_translation(board_translation(index)); }); } + +fn opening_animation(mut players: Query<&mut AnimationPlayer, (With, With)>) { + players.iter_mut().for_each(|mut player| { + info!("Playing intro camera animation"); + player.resume() + }); +} + +fn switch_sides( + mut players: Query<&mut AnimationPlayer, (With, With)>, + assets_map: Res, + gltfs: Res>, + state: Res>, +) { + let gltf = gltfs.get(&assets_map.models).expect("Load GLTF content"); + players.iter_mut().for_each(|mut player| { + let animation = match state.get() { + game::TurnState::SideA => gltf.named_animations.get("GameCamSide2>1"), + game::TurnState::SideB => gltf.named_animations.get("GameCamSide1>2"), + }; + player.start_with_transition( + animation.expect("Camera Transition Animation").clone(), + Duration::from_secs_f32(1.00), + ); + }); +} diff --git a/src/game.rs b/src/game.rs index 6d1a453..8baff09 100644 --- a/src/game.rs +++ b/src/game.rs @@ -9,6 +9,7 @@ impl Plugin for GamePlugin { fn build(&self, app: &mut App) { app.add_event::() .add_event::() + .add_state::() .add_systems(Startup, setup_board) .add_systems( Update, @@ -23,6 +24,9 @@ impl Plugin for GamePlugin { }), handle_selection.run_if(on_event::()), capture_piece.run_if(any_component_added::), + switch_sides.run_if(|input: Res>| -> bool { + input.just_pressed(KeyCode::N) + }), ), ) .add_systems( @@ -30,7 +34,8 @@ impl Plugin for GamePlugin { ( asserts::.run_if(in_state(DisplayState::Display2d)), asserts::.run_if(in_state(DisplayState::Display3d)), - ).run_if(in_state(GameState::Play)), + ) + .run_if(in_state(GameState::Play)), ) .add_systems( PostUpdate, @@ -39,6 +44,13 @@ impl Plugin for GamePlugin { } } +#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, Default, States)] +pub(crate) enum TurnState { + #[default] + SideA, + SideB, +} + #[derive(Debug, Component, Clone, PartialEq, Copy)] pub(crate) enum Piece { Pawn, @@ -455,7 +467,7 @@ fn handle_selection( audio_event.send(AudioEvent::Invalid); *done = true; } - }, + } } }); } @@ -507,6 +519,16 @@ fn asserts( ); } if cameras.iter().len() != 1 { - panic!("There should be 1 cameras in this state, but there is only {}", cameras.iter().len()); + panic!( + "There should be 1 cameras in this state, but there is only {}", + cameras.iter().len() + ); } -} \ No newline at end of file +} + +fn switch_sides(state: Res>, mut next: ResMut>) { + match state.get() { + TurnState::SideA => next.set(TurnState::SideB), + TurnState::SideB => next.set(TurnState::SideA), + } +} diff --git a/target/x86_64-unknown-linux-gnu/Dockerfile b/target/x86_64-unknown-linux-gnu/Dockerfile deleted file mode 100644 index b886343..0000000 --- a/target/x86_64-unknown-linux-gnu/Dockerfile +++ /dev/null @@ -1,14 +0,0 @@ - -FROM ghcr.io/cross-rs/x86_64-unknown-linux-gnu:0.2.5 - -RUN apt-get update && \ - apt-get install --assume-yes \ - g++ \ - libasound2-dev \ - libudev-dev \ - libwayland-dev \ - libx11-dev \ - libxkbcommon-dev \ - mesa-vulkan-drivers \ - pkg-config ; -