From ab804fb9b54d274678013338c3918db07bc3a4ec Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Mon, 7 Oct 2024 17:05:22 +0200 Subject: [PATCH] cleaned up --- src/explo_state.rs | 83 ++++++++++++++++++++++++++++ src/game_state.rs | 135 --------------------------------------------- src/main.rs | 3 +- 3 files changed, 85 insertions(+), 136 deletions(-) create mode 100644 src/explo_state.rs diff --git a/src/explo_state.rs b/src/explo_state.rs new file mode 100644 index 0000000..4f59904 --- /dev/null +++ b/src/explo_state.rs @@ -0,0 +1,83 @@ +use bevy::prelude::*; + +use crate::Player; + +pub fn player_mouse_move ( + buttons: Res>, + mut player_query: Query<(&mut Player, &Camera, &mut GlobalTransform)>, + window_query: Query<&Window, With>, + // debug_line_query: Query<&mut Transform, (With, Without)>, +) { + let Ok((mut player, camera, global_transform)) = player_query.get_single_mut() else { + return; + }; + let local_transform = &global_transform.compute_transform(); + + // Check if left mouse button is pressed + if !buttons.pressed(MouseButton::Left) { + player.dragging_pos = None; + return; + } + + let window = window_query.single(); + + let Some(new_cursor) = window.cursor_position() else { + return; + }; + + let Some(old_cursor) = player.dragging_pos else { + player.dragging_pos = Some(new_cursor); + return; + }; + + // Check if cursor has moved significantly + if old_cursor.distance(new_cursor) < 3.0 { + return; + } + + // Raycasting from the camera based on the cursor positions + let Some(old_ray) = camera.viewport_to_world(&global_transform, old_cursor) else { + return; + }; + + let Some(new_ray) = camera.viewport_to_world(&global_transform, new_cursor) else { + return; + }; + + let delta_rotation = rotate_to_align(new_ray, old_ray); // oposite direction and never stop + + //debug_vector(debug_line_query, new_ray.get_point(SKY_RADIUS), axis*10.0); + + player.target_rotation = Some(delta_rotation * local_transform.rotation ); + player.dragging_pos = Some(new_cursor); +} + +fn rotate_to_align(ray_1: Ray3d, ray_2: Ray3d) -> Quat { + // Step 1: Get the direction vectors from the rays + let pos_1 = ray_1.get_point(1.0); + let pos_2 = ray_2.get_point(1.0); + + // Compute direction vectors + let dir_1 = (pos_1 - Vec3::ZERO).normalize(); // Change Vec3::ZERO to the origin or a relevant point + let dir_2 = (pos_2 - Vec3::ZERO).normalize(); + + // Step 2: Compute the axis of rotation (cross product) + let axis_of_rotation = dir_1.cross(dir_2).normalize(); + + // Check if vectors are parallel + if axis_of_rotation.length_squared() < f32::EPSILON { + return Quat::IDENTITY; + } + + // Step 3: Compute the angle of rotation (dot product and arccosine) + let dot_product = dir_1.dot(dir_2).clamp(-1.0, 1.0); // Clamp the value to prevent NaN from acos + let angle_of_rotation = dot_product.acos() * 6.0; // Ensure the angle is in radians + + // Handle any potential invalid angle values + if angle_of_rotation.is_nan() || angle_of_rotation.is_infinite() { + return Quat::IDENTITY; + } + + // Step 4: Create a quaternion representing the rotation + Quat::from_axis_angle(axis_of_rotation, angle_of_rotation) +} diff --git a/src/game_state.rs b/src/game_state.rs index 8dcf745..6bc5fe0 100644 --- a/src/game_state.rs +++ b/src/game_state.rs @@ -1,6 +1,4 @@ use bevy::prelude::*; -use bevy::render::render_resource::PrimitiveTopology; -use bevy::render::render_asset::RenderAssetUsages; use std::f64::consts::PI; use rand::seq::SliceRandom; use rand::RngCore; @@ -32,13 +30,8 @@ pub struct ScoreLabel; #[derive(Component)] pub struct HintLabel; -#[derive(Component)] -pub struct DebugLine; - pub fn setup( mut commands: Commands, - mut meshes: ResMut>, - mut materials: ResMut>, ) { let container_node = NodeBundle { style: Style { @@ -168,31 +161,6 @@ pub fn setup( let hint_label = commands.spawn((hint_label_node, MainGame, HintLabel)).id(); commands.entity(centered_container).push_children(&[hint_label]); - - - let line_material = materials.add(StandardMaterial { - emissive: LinearRgba::rgb(2.0, 0.5, 0.5), - ..default() - }); - - let vertices = vec![ - Vec3::new(0.0, 0.0, 0.0), - Vec3::new(1.0, 0.0, 0.0) - ]; - - let mut mesh = Mesh::new(PrimitiveTopology::LineList, RenderAssetUsages::RENDER_WORLD); - mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, vertices); - - commands.spawn(( - PbrBundle { - mesh: meshes.add(mesh), - material: line_material.clone(), - transform: Transform::default(), - ..default() - }, - DebugLine, - MainGame - )); } @@ -275,109 +243,6 @@ pub fn player_interact( } } -pub fn player_mouse_move ( - buttons: Res>, - mut player_query: Query<(&mut Player, &Camera, &mut GlobalTransform)>, - window_query: Query<&Window, With>, - // debug_line_query: Query<&mut Transform, (With, Without)>, -) { - let Ok((mut player, camera, global_transform)) = player_query.get_single_mut() else { - return; - }; - let local_transform = &global_transform.compute_transform(); - - // Check if left mouse button is pressed - if !buttons.pressed(MouseButton::Left) { - player.dragging_pos = None; - return; - } - - let window = window_query.single(); - - let Some(new_cursor) = window.cursor_position() else { - return; - }; - - let Some(old_cursor) = player.dragging_pos else { - player.dragging_pos = Some(new_cursor); - return; - }; - - // Check if cursor has moved significantly - if old_cursor.distance(new_cursor) < 3.0 { - return; - } - - // Raycasting from the camera based on the cursor positions - let Some(old_ray) = camera.viewport_to_world(&global_transform, old_cursor) else { - return; - }; - - let Some(new_ray) = camera.viewport_to_world(&global_transform, new_cursor) else { - return; - }; - - let delta_rotation = rotate_to_align(new_ray, old_ray); // oposite direction and never stop - - //debug_vector(debug_line_query, new_ray.get_point(SKY_RADIUS), axis*10.0); - - player.target_rotation = Some(delta_rotation * local_transform.rotation ); - player.dragging_pos = Some(new_cursor); -} - - -fn rotate_to_align(ray_1: Ray3d, ray_2: Ray3d) -> Quat { - // Step 1: Get the direction vectors from the rays - let pos_1 = ray_1.get_point(1.0); - let pos_2 = ray_2.get_point(1.0); - - // Compute direction vectors - let dir_1 = (pos_1 - Vec3::ZERO).normalize(); // Change Vec3::ZERO to the origin or a relevant point - let dir_2 = (pos_2 - Vec3::ZERO).normalize(); - - // Step 2: Compute the axis of rotation (cross product) - let axis_of_rotation = dir_1.cross(dir_2).normalize(); - - // Check if vectors are parallel - if axis_of_rotation.length_squared() < f32::EPSILON { - return Quat::IDENTITY; - } - - // Step 3: Compute the angle of rotation (dot product and arccosine) - let dot_product = dir_1.dot(dir_2).clamp(-1.0, 1.0); // Clamp the value to prevent NaN from acos - let angle_of_rotation = dot_product.acos() * 6.0; // Ensure the angle is in radians - - // Handle any potential invalid angle values - if angle_of_rotation.is_nan() || angle_of_rotation.is_infinite() { - return Quat::IDENTITY; - } - - // Step 4: Create a quaternion representing the rotation - Quat::from_axis_angle(axis_of_rotation, angle_of_rotation) -} - -// fn debug_vector( -// mut debug_line_query: Query<&mut Transform, (With, Without)>, -// pos: Vec3, vec: Vec3 -// ) { -// let Ok(mut debug_transform) = debug_line_query.get_single_mut() else { -// info!("no debug line"); -// return; -// }; -// -// let vec_norm = vec.length(); -// -// debug_transform.scale = Vec3::new(vec_norm, 1.0, 1.0); -// -// debug_transform.translation = pos; -// -// if vec_norm > f32::EPSILON { -// let rotation = Quat::from_rotation_arc(Vec3::X, vec.normalize()); -// debug_transform.rotation = rotation; -// } -// } - - pub fn ui_labels( mut param_set: ParamSet<( Query<&mut Text, With>, diff --git a/src/main.rs b/src/main.rs index c79e408..f2fa8f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use std::f64::consts::PI; mod end_state; mod start_state; mod game_state; +mod explo_state; const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15); const WRONG_BUTTON: Color = Color::srgb(0.50, 0.15, 0.15); @@ -134,7 +135,7 @@ fn main() { .add_systems(OnExit(GameState::Start), despawn_screen::) .add_systems(OnEnter(GameState::Game), game_state::setup) .add_systems(Update, game_state::player_interact.run_if(in_state(GameState::Game))) - .add_systems(Update, game_state::player_mouse_move.run_if(in_state(GameState::Game))) + .add_systems(Update, explo_state::player_mouse_move.run_if(in_state(GameState::Game))) .add_systems(Update, game_state::ui_buttons.run_if(in_state(GameState::Game))) .add_systems(Update, game_state::ui_labels.run_if(in_state(GameState::Game))) .add_systems(OnExit(GameState::Game), despawn_screen::)