added constellation names

This commit is contained in:
WanderingPenwing 2024-10-07 22:41:58 +02:00
parent b20579f22b
commit 9a1b13b6f9
2 changed files with 89 additions and 38 deletions

View file

@ -1,10 +1,20 @@
use bevy::prelude::*; use bevy::prelude::*;
use std::f32::consts::E;
use crate::Player; use crate::Player;
use crate::GameState; use crate::GameState;
use crate::ConstellationModel;
use crate::Sky; use crate::Sky;
use crate::MainGame;
use crate::spawn_cons_lines; use crate::spawn_cons_lines;
use crate::CONS_VIEW_RADIUS;
#[derive(Component)]
pub struct InfoLabel;
pub fn setup ( pub fn setup (
sky : Res<Sky>, sky : Res<Sky>,
mut commands: Commands, mut commands: Commands,
@ -14,6 +24,33 @@ pub fn setup (
for constellation in sky.content.iter() { for constellation in sky.content.iter() {
spawn_cons_lines(&mut commands, &mut meshes, &mut materials, constellation.clone()); 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 ( pub fn player_mouse_move (
@ -120,3 +157,50 @@ pub fn player_interact(
game_state.set(GameState::Start); game_state.set(GameState::Start);
} }
} }
pub fn constellation_opacity(
mut materials: ResMut<Assets<StandardMaterial>>,
player_query: Query<(&Player, &Camera, &GlobalTransform)>,
constellation_query: Query<(&Handle<StandardMaterial>, &ConstellationModel)>, // Query all constellation lines
window_query: Query<&Window, With<bevy::window::PrimaryWindow>>,
mut info_label_query: Query<&mut Text, With<InfoLabel>>,
) {
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;
}

View file

@ -4,7 +4,6 @@ use bevy::render::render_resource::PrimitiveTopology;
use bevy::render::render_asset::RenderAssetUsages; use bevy::render::render_asset::RenderAssetUsages;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::f64::consts::PI; use std::f64::consts::PI;
use std::f32::consts::E;
mod end_state; mod end_state;
mod start_state; mod start_state;
@ -21,7 +20,7 @@ const EASYNESS: f32 = 1.5;
const MAX_STAR_SIZE: f32 = 0.63; 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 = 1.0; const CONS_VIEW_RADIUS: f32 = 0.8;
#[derive(Serialize, Deserialize, Debug, Clone)] #[derive(Serialize, Deserialize, Debug, Clone)]
struct StarData { struct StarData {
@ -56,16 +55,16 @@ impl Sky {
for cons in self.content.clone() { for cons in self.content.clone() {
cons_names.push(cons.name.clone()); cons_names.push(cons.name.clone());
} }
return cons_names; cons_names
} }
fn get_constellation(&self, name: &str) -> Constellation { fn get_constellation(&self, name: &str) -> Constellation {
for cons in self.content.clone() { for cons in self.content.clone() {
if &cons.name == name { if cons.name == name {
return cons; 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::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, 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, 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(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)
@ -154,38 +153,6 @@ fn main() {
.run(); .run();
} }
fn constellation_opacity(
mut materials: ResMut<Assets<StandardMaterial>>,
player_query: Query<(&Player, &Camera, &GlobalTransform)>,
constellation_query: Query<(&Handle<StandardMaterial>, &ConstellationModel)>, // Query all constellation lines
window_query: Query<&Window, With<bevy::window::PrimaryWindow>>,
) {
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( fn spawn_cons_lines(
commands: &mut Commands, commands: &mut Commands,
meshes: &mut ResMut<Assets<Mesh>>, meshes: &mut ResMut<Assets<Mesh>>,