This commit is contained in:
WanderingPenwing 2024-10-05 16:57:26 +02:00
parent e4059a1366
commit c8d22fbc38
3 changed files with 61 additions and 17 deletions

26
Cargo.lock generated
View file

@ -310,6 +310,7 @@ name = "bevy-test"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bevy", "bevy",
"rand",
"serde", "serde",
"serde_json", "serde_json",
] ]
@ -2983,6 +2984,15 @@ dependencies = [
"unicode-xid", "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]] [[package]]
name = "presser" name = "presser"
version = "0.3.1" version = "0.3.1"
@ -3034,6 +3044,18 @@ version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
dependencies = [ 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", "rand_core",
] ]
@ -3042,6 +3064,9 @@ name = "rand_core"
version = "0.6.4" version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
dependencies = [
"getrandom",
]
[[package]] [[package]]
name = "range-alloc" name = "range-alloc"
@ -4292,6 +4317,7 @@ version = "0.7.35"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0"
dependencies = [ dependencies = [
"byteorder",
"zerocopy-derive", "zerocopy-derive",
] ]

View file

@ -5,6 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
bevy = { version = "0.14.2", features = [ "dynamic_linking" ] } bevy = { version = "0.14.2", features = [ "dynamic_linking" ] }
rand = "0.8.5"
serde = {version = "1.0.210", features = ["derive"] } serde = {version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128" serde_json = "1.0.128"

View file

@ -7,6 +7,7 @@ use std::fs::File;
use std::io::Read; use std::io::Read;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::f64::consts::PI; use std::f64::consts::PI;
use rand::seq::SliceRandom;
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
struct StarData { struct StarData {
@ -198,23 +199,39 @@ fn celestial_to_cartesian(rah: f64, ded: f64) -> Vec3 {
fn player_rotate( fn player_rotate(
keys: Res<ButtonInput<KeyCode>>, keys: Res<ButtonInput<KeyCode>>,
mut query: Query<(&Player, &Transform)>, mut query: Query<(&mut Player, &mut Transform)>, // Query to get Player and Transform
sky: Res<Sky>, // Res to access the Sky resource
mut commands: Commands, mut commands: Commands,
) { ) {
for (mut player, mut transform) in query.iter_mut() { for (mut player, mut transform) in query.iter_mut() {
if keys.just_pressed(KeyCode::Space) { // If the space key was just pressed
info!("space"); if keys.just_pressed(KeyCode::Space) {
info!("space pressed");
let target_rotation = Quat::from_euler(EulerRot::YXZ, 0.1, 0.5, 0.2); // 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 a resource // Store the target rotation in the player component
//player.target_rotation = Some(target_rotation); player.target_rotation = Some(target_rotation);
} }
}
// if let Some(target_rotation) = target_rotation { // If there is a target rotation, smoothly rotate the player towards it
// // let current_rotation = transform.rotation; if let Some(target_rotation) = player.target_rotation {
// // transform.rotation = current_rotation.slerp(target_rotation.0, 0.1); // Get the current rotation of the player
// info!("rotate"); 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
}
}
}
} }