movement
This commit is contained in:
parent
e4059a1366
commit
c8d22fbc38
26
Cargo.lock
generated
26
Cargo.lock
generated
|
@ -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",
|
||||
]
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
41
src/main.rs
41
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<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,
|
||||
) {
|
||||
for (mut player, mut transform) in query.iter_mut() {
|
||||
// If the space key was just pressed
|
||||
if keys.just_pressed(KeyCode::Space) {
|
||||
info!("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
|
||||
//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");
|
||||
// }
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue