From 9a1b13b6f9f96474805660dcd9dbf1eafefe7ca2 Mon Sep 17 00:00:00 2001 From: WanderingPenwing Date: Mon, 7 Oct 2024 22:41:58 +0200 Subject: [PATCH] added constellation names --- src/explo_state.rs | 84 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 43 +++--------------------- 2 files changed, 89 insertions(+), 38 deletions(-) diff --git a/src/explo_state.rs b/src/explo_state.rs index eaea51d..f9668c1 100644 --- a/src/explo_state.rs +++ b/src/explo_state.rs @@ -1,10 +1,20 @@ use bevy::prelude::*; +use std::f32::consts::E; use crate::Player; use crate::GameState; +use crate::ConstellationModel; use crate::Sky; +use crate::MainGame; + + use crate::spawn_cons_lines; +use crate::CONS_VIEW_RADIUS; + +#[derive(Component)] +pub struct InfoLabel; + pub fn setup ( sky : Res, mut commands: Commands, @@ -14,6 +24,33 @@ pub fn setup ( for constellation in sky.content.iter() { spawn_cons_lines(&mut commands, &mut meshes, &mut materials, constellation.clone()); } + + let centered_container_node = NodeBundle { + style: Style { + position_type: PositionType::Absolute, + width: Val::Percent(100.0), + top: Val::Px(20.0), + justify_content: JustifyContent::Center, + align_items: AlignItems::Center, + ..default() + }, + ..default() + }; + + let info_label_node = TextBundle::from_section( + "info", + TextStyle { + // font: asset_server.load("fonts/FiraSans-Bold.ttf"), + font_size: 20.0, + color: Color::srgb(0.7, 0.7, 0.7), + ..default() + }, + ); + + let centered_container = commands.spawn(centered_container_node).id(); + let info_label = commands.spawn((info_label_node, MainGame, InfoLabel)).id(); + + commands.entity(centered_container).push_children(&[info_label]); } pub fn player_mouse_move ( @@ -120,3 +157,50 @@ pub fn player_interact( game_state.set(GameState::Start); } } + +pub fn constellation_opacity( + mut materials: ResMut>, + player_query: Query<(&Player, &Camera, &GlobalTransform)>, + constellation_query: Query<(&Handle, &ConstellationModel)>, // Query all constellation lines + window_query: Query<&Window, With>, + mut info_label_query: Query<&mut Text, With>, +) { + let (_player, camera, global_transform) = player_query.single(); + let window = window_query.single(); + let Some(cursor_position) = window.cursor_position() else { + return; + }; + + let Some(mouse_ray) = camera.viewport_to_world(&global_transform, cursor_position) else { + return; + }; + + let cursor_global_pos = mouse_ray.get_point(1.0); + + let mut closest_const_name: String = "".into(); + let mut closest_const_pos: Vec3 = Vec3::ZERO; + + + for (material_handle, constellation_model) in constellation_query.iter() { + let Some(material) = materials.get_mut(material_handle) else { + continue; + }; + + let distance = constellation_model.center.distance(cursor_global_pos); + let exponent = -(2.0 * distance / CONS_VIEW_RADIUS).powi(2); + let opa = E.powf(exponent); + + material.base_color = Color::srgba(opa, opa, opa, opa); // Set the alpha channel to adjust transparency + + if distance < closest_const_pos.distance(cursor_global_pos) { + closest_const_name = constellation_model.name.clone(); + closest_const_pos = constellation_model.center; + } + } + + let Ok(mut info_label) = info_label_query.get_single_mut() else { + return; + }; + + info_label.sections[0].value = closest_const_name; +} diff --git a/src/main.rs b/src/main.rs index 4705393..f9c00f1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,6 @@ use bevy::render::render_resource::PrimitiveTopology; use bevy::render::render_asset::RenderAssetUsages; use serde::{Deserialize, Serialize}; use std::f64::consts::PI; -use std::f32::consts::E; mod end_state; mod start_state; @@ -21,7 +20,7 @@ const EASYNESS: f32 = 1.5; const MAX_STAR_SIZE: f32 = 0.63; const STAR_SCALE: f32 = 0.02; const SKY_RADIUS: f32 = 4.0; -const CONS_VIEW_RADIUS: f32 = 1.0; +const CONS_VIEW_RADIUS: f32 = 0.8; #[derive(Serialize, Deserialize, Debug, Clone)] struct StarData { @@ -56,16 +55,16 @@ impl Sky { for cons in self.content.clone() { cons_names.push(cons.name.clone()); } - return cons_names; + cons_names } fn get_constellation(&self, name: &str) -> Constellation { for cons in self.content.clone() { - if &cons.name == name { + if cons.name == name { return cons; } } - return self.content[0].clone(); + self.content[0].clone() } } @@ -142,7 +141,7 @@ fn main() { .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, game_state::ui_buttons.run_if(in_state(GameState::Game))) - .add_systems(Update, constellation_opacity.run_if(in_state(GameState::Game).or_else(in_state(GameState::Explo)))) + .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(OnExit(GameState::Game), despawn_screen::) .add_systems(OnEnter(GameState::End), end_state::setup) @@ -154,38 +153,6 @@ fn main() { .run(); } -fn constellation_opacity( - mut materials: ResMut>, - player_query: Query<(&Player, &Camera, &GlobalTransform)>, - constellation_query: Query<(&Handle, &ConstellationModel)>, // Query all constellation lines - window_query: Query<&Window, With>, -) { - let (_player, camera, global_transform) = player_query.single(); - let window = window_query.single(); - let Some(cursor_position) = window.cursor_position() else { - return; - }; - - let Some(mouse_ray) = camera.viewport_to_world(&global_transform, cursor_position) else { - return; - }; - - let cursor_global_pos = mouse_ray.get_point(1.0); - - - for (material_handle, constellation_model) in constellation_query.iter() { - let Some(material) = materials.get_mut(material_handle) else { - continue; - }; - - let distance = constellation_model.center.distance(cursor_global_pos); - let exponent = -(2.0 * distance / CONS_VIEW_RADIUS).powi(2); - let opa = E.powf(exponent); - - material.base_color = Color::srgba(opa, opa, opa, opa); // Set the alpha channel to adjust transparency - } -} - fn spawn_cons_lines( commands: &mut Commands, meshes: &mut ResMut>,