From c8d22fbc38f306f1707951970d9cba49ea9a170d Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Sat, 5 Oct 2024 16:57:26 +0200 Subject: [PATCH] movement --- Cargo.lock | 26 ++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 51 ++++++++++++++++++++++++++++++++++----------------- 3 files changed, 61 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 42bc8e4..5a40516 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -310,6 +310,7 @@ name = "bevy-test" version = "0.1.0" dependencies = [ "bevy", + "rand", "serde", "serde_json", ] @@ -2983,6 +2984,15 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + [[package]] name = "presser" version = "0.3.1" @@ -3034,6 +3044,18 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", "rand_core", ] @@ -3042,6 +3064,9 @@ name = "rand_core" version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] [[package]] name = "range-alloc" @@ -4292,6 +4317,7 @@ version = "0.7.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" dependencies = [ + "byteorder", "zerocopy-derive", ] diff --git a/Cargo.toml b/Cargo.toml index 67cc6b2..0a26a64 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2021" [dependencies] bevy = { version = "0.14.2", features = [ "dynamic_linking" ] } +rand = "0.8.5" serde = {version = "1.0.210", features = ["derive"] } serde_json = "1.0.128" diff --git a/src/main.rs b/src/main.rs index f4dedaa..1752751 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,6 +7,7 @@ use std::fs::File; use std::io::Read; use serde::{Deserialize, Serialize}; use std::f64::consts::PI; +use rand::seq::SliceRandom; #[derive(Serialize, Deserialize, Debug, Clone)] struct StarData { @@ -198,23 +199,39 @@ fn celestial_to_cartesian(rah: f64, ded: f64) -> Vec3 { fn player_rotate( keys: Res>, - mut query: Query<(&Player, &Transform)>, + mut query: Query<(&mut Player, &mut Transform)>, // Query to get Player and Transform + sky: Res, // Res to access the Sky resource mut commands: Commands, ) { - for (mut player, mut transform) in query.iter_mut() { - if keys.just_pressed(KeyCode::Space) { - info!("space"); - - let target_rotation = Quat::from_euler(EulerRot::YXZ, 0.1, 0.5, 0.2); - - // Store the target rotation in a resource - //player.target_rotation = Some(target_rotation); - } - - // if let Some(target_rotation) = target_rotation { - // // let current_rotation = transform.rotation; - // // transform.rotation = current_rotation.slerp(target_rotation.0, 0.1); - // info!("rotate"); - // } - } + for (mut player, mut transform) in query.iter_mut() { + // If the space key was just pressed + if keys.just_pressed(KeyCode::Space) { + info!("space pressed"); + + // Select a random constellation from the Sky's content + if let Some(constellation) = sky.content.choose(&mut rand::thread_rng()) { + // Create a target rotation quaternion from the constellation's direction + let target_rotation = Quat::from_rotation_arc(Vec3::Z, celestial_to_cartesian(constellation.rah, constellation.dec)); + + // Store the target rotation in the player component + player.target_rotation = Some(target_rotation); + } + } + + // If there is a target rotation, smoothly rotate the player towards it + if let Some(target_rotation) = player.target_rotation { + // Get the current rotation of the player + let current_rotation = transform.rotation; + + // Slerp between the current rotation and the target rotation + transform.rotation = current_rotation.slerp(target_rotation, 0.1); // 0.1 is the interpolation factor + + info!("rotating player"); + + // Optionally, you could clear the target rotation when close enough + if transform.rotation.angle_between(target_rotation) < 0.01 { + player.target_rotation = None; // Clear once the rotation is close enough + } + } + } }