From 7b4682bc21ac5c51c9f72c4fd0217915866b6657 Mon Sep 17 00:00:00 2001 From: "Elijah C. Voigt" Date: Mon, 18 Mar 2024 22:55:48 -0700 Subject: [PATCH] mid refactor: dissolve::{in,out} tagging for animation So far things are borked, but it _feels_ like we're going in the right direction --- assets/martian.tweak.toml | 1 + src/display3d.rs | 325 ++++++++++++++++++++++---------------- src/intro.rs | 2 +- src/main.rs | 1 + src/menu.rs | 3 + 5 files changed, 193 insertions(+), 139 deletions(-) diff --git a/assets/martian.tweak.toml b/assets/martian.tweak.toml index b1f821e..d0f71f7 100644 --- a/assets/martian.tweak.toml +++ b/assets/martian.tweak.toml @@ -191,6 +191,7 @@ drone_blue = "DroneBlue" pawn_blue = "PawnBlue" board = "Gameboard" valid_move = "Valid Move Spot" +title = "Title" [display3d.models.animations] intro_a = "GameCamIntro1" diff --git a/src/display3d.rs b/src/display3d.rs index a0f4277..0173cec 100644 --- a/src/display3d.rs +++ b/src/display3d.rs @@ -41,6 +41,7 @@ impl Plugin for Display3dPlugin { ), ), set_board_model.run_if(any_component_added::()), + set_title_model.run_if(any_component_added::()), set_valid_move_model.run_if(any_component_added::()), set_tile_hitbox.run_if(any_component_added::()), select @@ -49,7 +50,10 @@ impl Plugin for Display3dPlugin { .run_if(just_pressed(MouseButton::Left)), pick_up.run_if(any_component_added::()), put_down.run_if(any_component_removed::()), - setup_capture_piece.run_if(any_component_added::>().or_else(any_component_changed::>())), + setup_dissolve_materials.run_if( + any_component_added::>() + .or_else(any_component_changed::>()), + ), capture_piece.run_if(any_with_component::), skip_animation .run_if(just_pressed(KeyCode::Enter).or_else(just_pressed(MouseButton::Left))) @@ -87,13 +91,37 @@ impl Plugin for Display3dPlugin { .run_if(run_once()) .run_if(in_state(DisplayState::Display3d)), ), - ); + ) + .add_systems( + Update, + continue_title.run_if( + in_state(GameState::Title).and_then( + just_pressed(KeyCode::Enter).or_else(just_pressed(MouseButton::Left)), + ), + ), + ) + .add_systems(Update, dissolve_animation.run_if(any_with_component::)); } } #[derive(Debug, Component, PartialEq)] pub(crate) struct Display3d; +#[derive(Debug, Component)] +pub(crate) struct TitleText; + +#[derive(Debug, Component)] +pub(crate) struct Dissolvable { + start: f32, + duration: f32, +} + +#[derive(Debug, Component)] +pub(crate) enum Dissolving { + In(f32), + Out(f32), +} + #[derive(Debug, Resource, Clone)] struct AssetsMap { hitbox_shape: Handle, @@ -122,7 +150,7 @@ fn load_assets( }); } -/// Initialize the 3d board +/// Initialize the board and pieces fn initialize(mut commands: Commands, board: Res, assets: Res) { info!("Initializing root"); commands @@ -194,8 +222,19 @@ fn initialize(mut commands: Commands, board: Res, assets: Res("display3d_models_skybox_file") .unwrap(); let skybox_brightness = tweak.get::("display3d_skybox_brightness").unwrap(); - let environment_map_intensity = tweak.get::("display3d_environment_map_light_intensity").unwrap(); - let fog_settings = tweak.get::("display3d_fog").unwrap().into(); + let environment_map_intensity = tweak + .get::("display3d_environment_map_light_intensity") + .unwrap(); + let fog_settings = tweak + .get::("display3d_fog") + .unwrap() + .into(); info!("Hydrating camera {:?}", entity); // Populate the components for the camera commands.entity(entity).insert(( @@ -304,7 +348,15 @@ fn update_tweaks( if let Some(tweak) = tweaks.get(tweaks_file.handle.clone()) { warn!("Updating tweaks!"); camera_settings.iter_mut().for_each( - |(entity, mut fog, mut color_grading, mut tonemapping, mut bloom, mut skybox, mut environment_map_light)| { + |( + entity, + mut fog, + mut color_grading, + mut tonemapping, + mut bloom, + mut skybox, + mut environment_map_light, + )| { *fog = tweak .get::("display3d_fog") .unwrap() @@ -321,9 +373,7 @@ fn update_tweaks( .get::("display3d_bloom") .unwrap() .into(); - skybox.brightness = tweak - .get::("display3d_skybox_brightness") - .unwrap(); + skybox.brightness = tweak.get::("display3d_skybox_brightness").unwrap(); environment_map_light.intensity = tweak .get::("display3d_environment_map_light_intensity") .unwrap(); @@ -410,6 +460,37 @@ fn set_board_model( }); } +fn set_title_model( + mut titles: Query<(&mut Handle, &mut Transform), (Added, With)>, + gltfs: Res>, + tweaks: Res>, + tweaks_file: Res, +) { + let tweak = tweaks + .get(tweaks_file.handle.clone()) + .expect("Load tweakfile"); + titles.iter_mut().for_each(|(mut handle, mut transform)| { + info!("Setting title model"); + let assets_handle = tweak + .get_handle::("display3d_models_assets_file") + .unwrap(); + let gltf = gltfs.get(assets_handle).expect("Load GLTF content"); + *handle = gltf + .named_scenes + .get( + tweak + .get::("display3d_models_scenes_title") + .unwrap() + .as_str(), + ) + .expect("Game title model") + .clone(); + transform.translation -= Vec3::Y * 0.5; + transform.rotate_local_z(std::f32::consts::PI); + transform.rotate_local_y(std::f32::consts::PI / 2.0); + }); +} + /// Given a board index returns the Vec3 location in space fn board_translation(&BoardIndex { x, y }: &BoardIndex) -> Vec3 { // Scale x down by 4 to account for -4..4 scaling @@ -1097,26 +1178,19 @@ impl MaterialExtension for DissolveExtension { } } -// Component for 'backing up' components which are temporarily not used -#[derive(Debug, Component, Clone)] -struct Backup(T); - /// Sets up all pieces to have an associated "dissolve" material ready for capture -fn setup_capture_piece( +fn setup_dissolve_materials( // All entities with materials are candidates for this procedure - events: Query< - (Entity, &Handle), - Added>, - >, + events: Query<(Entity, &Handle), Added>>, // Only process newly created pieces (we do not delete pieces at runtime) - query: Query, Added)>, + query: Query, Added)>, // Children of pieces are the actual meshes that need materials parents: Query<&Parent>, - // Used to create DissovleMaterial + // Used to create DissolveMaterial standard_materials: Res>, // Used to create Handle mut dissolve_materials: ResMut>, - // Used to insert Backup(Handle); + // Used to insert Handle; mut commands: Commands, // Cache dissolve textures that have already been created mut cache: Local, Handle>>, @@ -1152,7 +1226,9 @@ fn setup_capture_piece( cache.insert(std_handle.clone(), dis_handle.clone()); // Add the dissolve handle as a Backup(T) - commands.entity(child).insert(Backup(dis_handle.clone())); + commands.entity(child) + .insert(dis_handle.clone()) + .remove::>(); }); } @@ -1167,50 +1243,17 @@ fn capture_piece( (&mut Visibility, &mut Transform, &Side), (With, With), >, + dissolving: Query>, mut state: Local>, - mut kids: Local>, - mut prog: Local, - mut dissolve_materials: ResMut>, - object_standard_materials: Query<( - Entity, - &Handle, - &Backup>, - )>, - object_dissolve_materials: Query<( - Entity, - &Handle, - &Backup>, - )>, - children: Query<&Children>, mut commands: Commands, - time: Res