press R to look to current constellation

This commit is contained in:
WanderingPenwing 2024-10-07 11:03:05 +02:00
parent efdb86c081
commit cb74487f1b
5 changed files with 67 additions and 31 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/target
/out

34
Cargo.lock generated
View file

@ -216,6 +216,7 @@ dependencies = [
"rand",
"serde",
"serde_json",
"wasm-bindgen",
]
[[package]]
@ -2196,9 +2197,9 @@ dependencies = [
[[package]]
name = "js-sys"
version = "0.3.70"
version = "0.3.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1868808506b929d7b0cfa8f75951347aa71bb21144b7791bae35d9bccfcfe37a"
checksum = "29c15563dc2726973df627357ce0c9ddddbea194836909d655df6a75d2cf296d"
dependencies = [
"wasm-bindgen",
]
@ -3623,20 +3624,19 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "wasm-bindgen"
version = "0.2.93"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a82edfc16a6c469f5f44dc7b571814045d60404b55a0ee849f9bcfa2e63dd9b5"
checksum = "4be2531df63900aeb2bca0daaaddec08491ee64ceecbee5076636a3b026795a8"
dependencies = [
"cfg-if",
"once_cell",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.93"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9de396da306523044d3302746f1208fa71d7532227f15e347e2d93e4145dd77b"
checksum = "614d787b966d3989fa7bb98a654e369c762374fd3213d212cfc0251257e747da"
dependencies = [
"bumpalo",
"log",
@ -3649,9 +3649,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-futures"
version = "0.4.43"
version = "0.4.42"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61e9300f63a621e96ed275155c108eb6f843b6a26d053f122ab69724559dc8ed"
checksum = "76bc14366121efc8dbb487ab05bcc9d346b3b5ec0eaa76e46594cabbe51762c0"
dependencies = [
"cfg-if",
"js-sys",
@ -3661,9 +3661,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.93"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "585c4c91a46b072c92e908d99cb1dcdf95c5218eeb6f3bf1efa991ee7a68cccf"
checksum = "a1f8823de937b71b9460c0c34e25f3da88250760bec0ebac694b49997550d726"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
@ -3671,9 +3671,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.93"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836"
checksum = "e94f17b526d0a461a191c78ea52bbce64071ed5c04c9ffe424dcb38f74171bb7"
dependencies = [
"proc-macro2",
"quote",
@ -3684,15 +3684,15 @@ dependencies = [
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.93"
version = "0.2.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484"
checksum = "af190c94f2773fdb3729c55b007a722abb5384da03bc0986df4c289bf5567e96"
[[package]]
name = "web-sys"
version = "0.3.70"
version = "0.3.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26fdeaafd9bd129f65e7c031593c24d62186301e0c72c8978fa1678be7d532c0"
checksum = "77afa9a11836342370f4817622a2f0f418b134426d91a82dfb48f532d2ec13ef"
dependencies = [
"js-sys",
"wasm-bindgen",

View file

@ -8,6 +8,7 @@ bevy = { version = "0.14.2", features = [ "dynamic_linking" ] }
rand = "0.8.5"
serde = {version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
wasm-bindgen = "=0.2.92"
# Enable a small amount of optimization in the dev profile.
[profile.dev]
@ -16,3 +17,6 @@ opt-level = 1
# Enable a large amount of optimization in the dev profile for dependencies.
[profile.dev.package."*"]
opt-level = 3
[target.wasm32-unknown-unknown]
runner = "wasm-server-runner"

View file

@ -7,6 +7,7 @@ use crate::Player;
use crate::GameState;
use crate::MainGame;
use crate::Sky;
use crate::Constellation;
use crate::ConstellationLine;
use crate::PlayerState;
@ -195,6 +196,7 @@ pub fn player_interact(
if let Some(target_cons) = player.target_cons_name.clone() {
player.state = PlayerState::Hinted;
spawn_cons_lines(commands, meshes, materials, sky, target_cons);
return
}
}
@ -203,6 +205,18 @@ pub fn player_interact(
player.target_rotation = None;
}
if keys.pressed(KeyCode::KeyR) {
if let Some(target_constellation_name) = player.target_cons_name.clone() {
let mut target_constellation = sky.content[0].clone();
for constellation in sky.content.clone() {
if constellation.name == target_constellation_name {
target_constellation = constellation
}
}
player.target_rotation = Some(constellation_center(target_constellation));
}
}
if let Some(target_rotation) = player.target_rotation {
let current_rotation = transform.rotation;
@ -348,16 +362,7 @@ fn choose_constellation(
let target_index = rng.next_u32().rem_euclid(4) as usize;
let target_constellation = &constellations[target_index];
let mut mean_pos = Vec3::ZERO;
for star in target_constellation.stars.clone() {
mean_pos += celestial_to_cartesian(star.rah, star.dec)
}
let target_rotation = Quat::from_rotation_arc(
Vec3::Z,
-mean_pos*(1.0/target_constellation.stars.len() as f32),
);
player.target_rotation = Some(target_rotation);
player.target_rotation = Some(constellation_center(target_constellation.clone()));
player.target_cons_name = Some(target_constellation.name.clone());
info!("Target constellation: {}", target_constellation.name);
@ -380,3 +385,16 @@ fn choose_constellation(
info!("Not enough constellations in the sky (need 4)");
}
}
fn constellation_center(target_constellation: Constellation) -> Quat {
let mut mean_pos = Vec3::ZERO;
for star in target_constellation.stars.clone() {
mean_pos += celestial_to_cartesian(star.rah, star.dec)
}
Quat::from_rotation_arc(
Vec3::Z,
-mean_pos*(1.0/target_constellation.stars.len() as f32),
)
}

View file

@ -1,16 +1,20 @@
use bevy::prelude::*;
use std::f64::consts::PI;
//use bevy::input::mouse::MouseMotion;
use crate::Player;
use crate::GameState;
use crate::GameOver;
use crate::StartMenu;
use crate::PlayerState;
#[derive(Component)]
struct AudioPlayer;
pub fn audio_setup(asset_server: Res<AssetServer>, mut commands: Commands) {
commands.spawn(AudioBundle {
commands.spawn((AudioBundle {
source: asset_server.load("Banjo.ogg"),
settings: PlaybackSettings::LOOP,
});
}, AudioPlayer));
info!("audio started");
}
@ -77,15 +81,24 @@ pub fn setup(mut commands: Commands, _asset_server: Res<AssetServer>) {
pub fn player_interact(
keys: Res<ButtonInput<KeyCode>>,
mut game_state: ResMut<NextState<GameState>>,
mut player_query: Query<(&mut Player, &mut Transform)>
mut player_query: Query<(&mut Player, &mut Transform)>,
//commands: Commands,
//mut evr_motion: EventReader<MouseMotion>,
//asset_server: Res<AssetServer>,
//mut audio_query: Query<&mut AudioPlayer>,
) {
if keys.just_pressed(KeyCode::Space) {
info!("start space");
game_state.set(GameState::Game);
}
if let Ok((_player, mut transform)) = player_query.get_single_mut() {
// for _ev in evr_motion.read() {
// if let Err(_) = audio_query.get_single_mut() {
// audio_setup(asset_server, commands);
// }
// }
let mut rotation = Quat::IDENTITY;
rotation *= Quat::from_rotation_y((PI / 6000.0) as f32);
rotation *= Quat::from_rotation_x((-PI / 2000.0) as f32);