cleaned up
This commit is contained in:
parent
f4440dd06e
commit
ab804fb9b5
83
src/explo_state.rs
Normal file
83
src/explo_state.rs
Normal file
|
@ -0,0 +1,83 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
use crate::Player;
|
||||
|
||||
pub fn player_mouse_move (
|
||||
buttons: Res<ButtonInput<MouseButton>>,
|
||||
mut player_query: Query<(&mut Player, &Camera, &mut GlobalTransform)>,
|
||||
window_query: Query<&Window, With<bevy::window::PrimaryWindow>>,
|
||||
// debug_line_query: Query<&mut Transform, (With<DebugLine>, Without<Player>)>,
|
||||
) {
|
||||
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)
|
||||
}
|
|
@ -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<Assets<Mesh>>,
|
||||
mut materials: ResMut<Assets<StandardMaterial>>,
|
||||
) {
|
||||
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<ButtonInput<MouseButton>>,
|
||||
mut player_query: Query<(&mut Player, &Camera, &mut GlobalTransform)>,
|
||||
window_query: Query<&Window, With<bevy::window::PrimaryWindow>>,
|
||||
// debug_line_query: Query<&mut Transform, (With<DebugLine>, Without<Player>)>,
|
||||
) {
|
||||
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<DebugLine>, Without<Player>)>,
|
||||
// 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<HealthLabel>>,
|
||||
|
|
|
@ -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::<StartMenu>)
|
||||
.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::<MainGame>)
|
||||
|
|
Loading…
Reference in a new issue