From 447dfc9f2267d5c0adc06ab8164f6349df901e8a Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Mon, 7 Oct 2024 17:57:44 +0200 Subject: [PATCH] biiig cleanup --- src/end_state.rs | 77 +++++++------ src/explo_state.rs | 23 +--- src/game_state.rs | 272 +++++++++++++++++++++++---------------------- src/main.rs | 52 +++++---- src/start_state.rs | 28 ++--- 5 files changed, 221 insertions(+), 231 deletions(-) diff --git a/src/end_state.rs b/src/end_state.rs index aa505f0..eb4ca92 100644 --- a/src/end_state.rs +++ b/src/end_state.rs @@ -1,57 +1,56 @@ use bevy::prelude::*; -use crate::Player; + use crate::GameState; use crate::GameOver; +use crate::GameData; pub fn setup( mut commands: Commands, _asset_server: Res, - mut player_query: Query<&mut Player> + game_data: Res, ) { - if let Ok(player) = player_query.get_single_mut() { - let container_node = NodeBundle { - style: Style { - width: Val::Percent(100.0), - height: Val::Percent(100.0), - flex_direction: FlexDirection::Column, - justify_content: JustifyContent::Center, - align_items: AlignItems::Center, - ..default() - }, - ..default() - }; + let container_node = NodeBundle { + style: Style { + width: Val::Percent(100.0), + height: Val::Percent(100.0), + flex_direction: FlexDirection::Column, + justify_content: JustifyContent::Center, + align_items: AlignItems::Center, + ..default() + }, + ..default() + }; - let container = commands.spawn(container_node).id(); + let container = commands.spawn(container_node).id(); - let top_text_style = TextStyle { - font_size: 50.0, - color: Color::WHITE, - // font: asset_server.load("fonts/FiraSans-Bold.ttf"), - ..default() - }; + let top_text_style = TextStyle { + font_size: 50.0, + color: Color::WHITE, + // font: asset_server.load("fonts/FiraSans-Bold.ttf"), + ..default() + }; - let bottom_text_style = TextStyle { - font_size: 30.0, - color: Color::WHITE, - // font: asset_server.load("fonts/FiraSans-Regular.ttf"), - ..default() - }; + let bottom_text_style = TextStyle { + font_size: 30.0, + color: Color::WHITE, + // font: asset_server.load("fonts/FiraSans-Regular.ttf"), + ..default() + }; - let top_text_node = TextBundle::from_section( - "Game Over", - top_text_style, - ); + let top_text_node = TextBundle::from_section( + "Game Over", + top_text_style, + ); - let bottom_text_node = TextBundle::from_section( - format!("final score : {}", player.score), - bottom_text_style, - ); + let bottom_text_node = TextBundle::from_section( + format!("final score : {}", game_data.score), + bottom_text_style, + ); - let top_text = commands.spawn((top_text_node, GameOver)).id(); - let bottom_text = commands.spawn((bottom_text_node, GameOver)).id(); + let top_text = commands.spawn((top_text_node, GameOver)).id(); + let bottom_text = commands.spawn((bottom_text_node, GameOver)).id(); - commands.entity(container).push_children(&[top_text, bottom_text]); - } + commands.entity(container).push_children(&[top_text, bottom_text]); } pub fn player_interact( diff --git a/src/explo_state.rs b/src/explo_state.rs index 4f59904..e994226 100644 --- a/src/explo_state.rs +++ b/src/explo_state.rs @@ -6,14 +6,12 @@ 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; @@ -30,12 +28,10 @@ pub fn player_mouse_move ( 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; }; @@ -44,40 +40,31 @@ pub fn player_mouse_move ( 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); + let delta_rotation = rotate_to_align(new_ray, old_ray); 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(); + let dir_1 = pos_1.normalize(); + let dir_2 = pos_2.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 + let dot_product = dir_1.dot(dir_2).clamp(-1.0, 1.0); + let angle_of_rotation = dot_product.acos() * 6.0; - // 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 6bc5fe0..647fb7d 100644 --- a/src/game_state.rs +++ b/src/game_state.rs @@ -10,6 +10,7 @@ use crate::Sky; use crate::Constellation; use crate::ConstellationLine; use crate::PlayerState; +use crate::GameData; use crate::celestial_to_cartesian; use crate::spawn_cons_lines; @@ -167,6 +168,7 @@ pub fn setup( pub fn player_interact( keys: Res>, mut player_query: Query<(&mut Player, &mut Transform)>, + mut game_data: ResMut, sky: Res, text_query: Query<&mut Text, With>, button_query: Query<(&mut BackgroundColor, &mut BorderColor), With