Compare commits

...

2 commits

Author SHA1 Message Date
WanderingPenwing 3430f37783 zoom 2024-10-08 08:34:02 +02:00
WanderingPenwing d07780dcdb tweaked constants 2024-10-07 22:48:07 +02:00
2 changed files with 36 additions and 22 deletions

View file

@ -1,5 +1,8 @@
use bevy::prelude::*; use bevy::prelude::*;
use std::f32::consts::E; use bevy::input::mouse::MouseScrollUnit;
use bevy::input::mouse::MouseWheel;
use std::f32::consts::{E, PI};
use crate::Player; use crate::Player;
use crate::GameState; use crate::GameState;
@ -7,10 +10,10 @@ use crate::ConstellationModel;
use crate::Sky; use crate::Sky;
use crate::MainGame; use crate::MainGame;
use crate::spawn_cons_lines; use crate::spawn_cons_lines;
use crate::CONS_VIEW_RADIUS; use crate::CONS_VIEW_RADIUS;
use crate::MOUSE_SPEED;
#[derive(Component)] #[derive(Component)]
pub struct InfoLabel; pub struct InfoLabel;
@ -105,6 +108,30 @@ pub fn player_mouse_move (
player.dragging_pos = Some(new_cursor); player.dragging_pos = Some(new_cursor);
} }
pub fn zoom(
mut evr_scroll: EventReader<MouseWheel>,
mut projection_query: Query<&mut Projection, With<Player>>,
) {
let Ok(mut projection) = projection_query.get_single_mut() else {
return;
};
let Projection::Perspective(ref mut perspective) = *projection else {
return;
};
for ev in evr_scroll.read() {
match ev.unit {
MouseScrollUnit::Line => {
perspective.fov = (0.6*PI).min((0.02*PI).max(perspective.fov * 0.9_f32.powf(ev.y)));
}
MouseScrollUnit::Pixel => {
println!("Scroll (pixel units): vertical: {}, horizontal: {}", ev.y, ev.x);
}
}
}
}
fn rotate_to_align(ray_1: Ray3d, ray_2: Ray3d) -> Quat { fn rotate_to_align(ray_1: Ray3d, ray_2: Ray3d) -> Quat {
let pos_1 = ray_1.get_point(1.0); let pos_1 = ray_1.get_point(1.0);
let pos_2 = ray_2.get_point(1.0); let pos_2 = ray_2.get_point(1.0);
@ -119,7 +146,7 @@ fn rotate_to_align(ray_1: Ray3d, ray_2: Ray3d) -> Quat {
} }
let dot_product = dir_1.dot(dir_2).clamp(-1.0, 1.0); let dot_product = dir_1.dot(dir_2).clamp(-1.0, 1.0);
let angle_of_rotation = dot_product.acos() * 6.0; let angle_of_rotation = dot_product.acos() * MOUSE_SPEED;
if angle_of_rotation.is_nan() || angle_of_rotation.is_infinite() { if angle_of_rotation.is_nan() || angle_of_rotation.is_infinite() {
return Quat::IDENTITY; return Quat::IDENTITY;

View file

@ -21,6 +21,7 @@ const MAX_STAR_SIZE: f32 = 0.63;
const STAR_SCALE: f32 = 0.02; const STAR_SCALE: f32 = 0.02;
const SKY_RADIUS: f32 = 4.0; const SKY_RADIUS: f32 = 4.0;
const CONS_VIEW_RADIUS: f32 = 0.8; const CONS_VIEW_RADIUS: f32 = 0.8;
const MOUSE_SPEED: f32 = 12.0;
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
struct StarData { struct StarData {
@ -140,8 +141,8 @@ fn main() {
.add_systems(Update, game_state::player_interact.run_if(in_state(GameState::Game))) .add_systems(Update, game_state::player_interact.run_if(in_state(GameState::Game)))
.add_systems(Update, explo_state::player_mouse_move.run_if(in_state(GameState::Game).or_else(in_state(GameState::Explo)))) .add_systems(Update, explo_state::player_mouse_move.run_if(in_state(GameState::Game).or_else(in_state(GameState::Explo))))
.add_systems(Update, explo_state::rotate_camera.run_if(in_state(GameState::Game).or_else(in_state(GameState::Explo)))) .add_systems(Update, explo_state::rotate_camera.run_if(in_state(GameState::Game).or_else(in_state(GameState::Explo))))
.add_systems(Update, explo_state::zoom.run_if(in_state(GameState::Game).or_else(in_state(GameState::Explo))))
.add_systems(Update, game_state::ui_buttons.run_if(in_state(GameState::Game))) .add_systems(Update, game_state::ui_buttons.run_if(in_state(GameState::Game)))
.add_systems(Update, explo_state::constellation_opacity.run_if(in_state(GameState::Game).or_else(in_state(GameState::Explo))))
.add_systems(Update, game_state::ui_labels.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>) .add_systems(OnExit(GameState::Game), despawn_screen::<MainGame>)
.add_systems(OnEnter(GameState::End), end_state::setup) .add_systems(OnEnter(GameState::End), end_state::setup)
@ -149,6 +150,7 @@ fn main() {
.add_systems(OnExit(GameState::End), despawn_screen::<GameOver>) .add_systems(OnExit(GameState::End), despawn_screen::<GameOver>)
.add_systems(OnEnter(GameState::Explo), explo_state::setup) .add_systems(OnEnter(GameState::Explo), explo_state::setup)
.add_systems(Update, explo_state::player_interact.run_if(in_state(GameState::Explo))) .add_systems(Update, explo_state::player_interact.run_if(in_state(GameState::Explo)))
.add_systems(Update, explo_state::constellation_opacity.run_if(in_state(GameState::Explo)))
.add_systems(OnExit(GameState::Explo), despawn_screen::<MainGame>) .add_systems(OnExit(GameState::Explo), despawn_screen::<MainGame>)
.run(); .run();
} }
@ -206,7 +208,8 @@ fn star_setup(
) { ) {
commands.insert_resource(ClearColor(Color::BLACK)); commands.insert_resource(ClearColor(Color::BLACK));
let stars = get_stars().unwrap(); let stars: Vec<StarData> = serde_json::from_str(include_str!("../data/stars.json")).expect("no star json provided");
let star_mesh = meshes.add(Sphere::new(1.0).mesh().ico(3).unwrap()); let star_mesh = meshes.add(Sphere::new(1.0).mesh().ico(3).unwrap());
@ -246,14 +249,6 @@ fn star_setup(
)); ));
} }
fn get_stars() -> std::io::Result<Vec<StarData>> {
let data = include_str!("../data/stars.json");
let stars: Vec<StarData> = serde_json::from_str(&data).unwrap();
Ok(stars)
}
fn star_position(star_data: StarData) -> Vec3 { fn star_position(star_data: StarData) -> Vec3 {
// Convert declination to decimal degrees // Convert declination to decimal degrees
let text_ra = star_data.ra; let text_ra = star_data.ra;
@ -289,15 +284,7 @@ fn celestial_to_cartesian(rah: f64, ded: f64) -> Vec3 {
} }
fn cons_setup(mut sky: ResMut<Sky>) { fn cons_setup(mut sky: ResMut<Sky>) {
sky.content = get_cons().unwrap(); sky.content = serde_json::from_str(include_str!("../data/constellations.json")).expect("no constellation json provided");
}
fn get_cons() -> std::io::Result<Vec<Constellation>> {
let data = include_str!("../data/constellations.json");
let sky_data: Vec<Constellation> = serde_json::from_str(&data).unwrap();
Ok(sky_data)
} }
// Generic system that takes a component as a parameter, and will despawn all entities with that component // Generic system that takes a component as a parameter, and will despawn all entities with that component