2024-10-05 08:41:37 +02:00
|
|
|
|
use bevy::prelude::*;
|
2024-10-05 09:37:47 +02:00
|
|
|
|
use bevy::math::*;
|
2024-10-06 11:18:45 +02:00
|
|
|
|
use bevy::render::render_resource::PrimitiveTopology;
|
|
|
|
|
use bevy::render::render_asset::RenderAssetUsages;
|
2024-10-05 12:17:55 +02:00
|
|
|
|
use std::fs::File;
|
|
|
|
|
use std::io::Read;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
2024-10-05 12:55:18 +02:00
|
|
|
|
use std::f64::consts::PI;
|
2024-10-05 16:57:26 +02:00
|
|
|
|
use rand::seq::SliceRandom;
|
2024-10-06 09:46:05 +02:00
|
|
|
|
use rand::RngCore;
|
2024-10-05 12:17:55 +02:00
|
|
|
|
|
2024-10-06 10:38:42 +02:00
|
|
|
|
const NORMAL_BUTTON: Color = Color::srgb(0.15, 0.15, 0.15);
|
|
|
|
|
const WRONG_BUTTON: Color = Color::srgb(0.50, 0.15, 0.15);
|
|
|
|
|
const RIGHT_BUTTON: Color = Color::srgb(0.15, 0.50, 0.15);
|
|
|
|
|
|
2024-10-06 12:54:51 +02:00
|
|
|
|
const EASYNESS: f32 = 1.5;
|
|
|
|
|
const MAX_STAR_SIZE: f32 = 0.63;
|
|
|
|
|
const STAR_SCALE: f32 = 0.02;
|
|
|
|
|
const SKY_RADIUS: f32 = 4.0;
|
|
|
|
|
|
2024-10-05 14:12:31 +02:00
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
2024-10-05 12:17:55 +02:00
|
|
|
|
struct StarData {
|
|
|
|
|
#[serde(rename = "Dec")]
|
|
|
|
|
dec: String,
|
|
|
|
|
#[serde(rename = "HR")]
|
|
|
|
|
hr: String,
|
|
|
|
|
#[serde(rename = "K")]
|
|
|
|
|
k: Option<String>,
|
|
|
|
|
#[serde(rename = "RA")]
|
|
|
|
|
ra: String,
|
|
|
|
|
#[serde(rename = "V")]
|
|
|
|
|
v: String,
|
|
|
|
|
#[serde(rename = "C")]
|
2024-10-06 10:38:42 +02:00
|
|
|
|
constellation: Option<String>,
|
2024-10-05 12:17:55 +02:00
|
|
|
|
#[serde(rename = "F")]
|
2024-10-06 10:38:42 +02:00
|
|
|
|
f: Option<String>,
|
2024-10-05 12:17:55 +02:00
|
|
|
|
#[serde(rename = "B")]
|
2024-10-06 10:38:42 +02:00
|
|
|
|
bayer_designation: Option<String>,
|
2024-10-05 12:17:55 +02:00
|
|
|
|
#[serde(rename = "N")]
|
2024-10-06 10:38:42 +02:00
|
|
|
|
name: Option<String>,
|
2024-10-05 12:17:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-05 16:21:57 +02:00
|
|
|
|
#[derive(Resource, Default)]
|
|
|
|
|
struct Sky {
|
2024-10-06 10:38:42 +02:00
|
|
|
|
content: Vec<Constellation>,
|
2024-10-05 16:21:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
|
|
|
struct Constellation {
|
|
|
|
|
#[serde(rename = "Name")]
|
2024-10-06 10:38:42 +02:00
|
|
|
|
name: String,
|
2024-10-05 16:21:57 +02:00
|
|
|
|
#[serde(rename = "RAh")]
|
2024-10-06 10:38:42 +02:00
|
|
|
|
rah: f64,
|
2024-10-05 16:21:57 +02:00
|
|
|
|
#[serde(rename = "DEd")]
|
2024-10-06 10:38:42 +02:00
|
|
|
|
dec: f64,
|
|
|
|
|
stars: Vec<StarPos>,
|
|
|
|
|
lines: Vec<[u32; 2]>,
|
2024-10-05 16:21:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, Clone)]
|
|
|
|
|
struct StarPos {
|
|
|
|
|
id: usize,
|
|
|
|
|
#[serde(rename = "bfID")]
|
|
|
|
|
bfid: String,
|
|
|
|
|
#[serde(rename = "RAh")]
|
|
|
|
|
rah: f64,
|
|
|
|
|
#[serde(rename = "DEd")]
|
|
|
|
|
dec: f64,
|
|
|
|
|
}
|
2024-10-05 08:41:37 +02:00
|
|
|
|
|
2024-10-05 09:46:57 +02:00
|
|
|
|
#[derive(Component)]
|
|
|
|
|
struct Star;
|
|
|
|
|
|
2024-10-06 09:28:13 +02:00
|
|
|
|
#[derive(Component)]
|
|
|
|
|
struct AnswerButton;
|
|
|
|
|
|
2024-10-06 15:13:59 +02:00
|
|
|
|
#[derive(Component)]
|
|
|
|
|
struct HealthLabel;
|
|
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
|
struct ScoreLabel;
|
|
|
|
|
|
2024-10-06 11:18:45 +02:00
|
|
|
|
#[derive(Component)]
|
|
|
|
|
struct ConstellationLine;
|
|
|
|
|
|
2024-10-06 13:27:43 +02:00
|
|
|
|
#[derive(Component)]
|
|
|
|
|
struct StartMenu;
|
|
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
|
struct MainGame;
|
|
|
|
|
|
|
|
|
|
#[derive(Component)]
|
|
|
|
|
struct GameOver;
|
|
|
|
|
|
2024-10-05 16:21:57 +02:00
|
|
|
|
#[derive(Component)]
|
|
|
|
|
struct Player {
|
|
|
|
|
target_rotation: Option<Quat>,
|
2024-10-06 09:28:13 +02:00
|
|
|
|
target_cons_name: Option<String>,
|
2024-10-06 14:19:20 +02:00
|
|
|
|
score: usize,
|
|
|
|
|
health: usize,
|
2024-10-06 15:23:17 +02:00
|
|
|
|
thinking: bool,
|
2024-10-05 16:21:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-06 13:27:43 +02:00
|
|
|
|
#[derive(Clone, Copy, Default, Eq, PartialEq, Debug, Hash, States)]
|
|
|
|
|
enum GameState {
|
|
|
|
|
#[default]
|
|
|
|
|
Start,
|
|
|
|
|
Game,
|
|
|
|
|
End,
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-06 10:38:42 +02:00
|
|
|
|
fn main() {
|
|
|
|
|
App::new()
|
|
|
|
|
.add_plugins(DefaultPlugins)
|
|
|
|
|
.insert_resource(Sky::default())
|
2024-10-06 13:27:43 +02:00
|
|
|
|
.init_state::<GameState>()
|
2024-10-06 10:38:42 +02:00
|
|
|
|
.add_systems(Startup, star_setup)
|
|
|
|
|
.add_systems(Startup, cons_setup)
|
2024-10-06 14:19:20 +02:00
|
|
|
|
.add_systems(OnEnter(GameState::Start), start_ui_setup)
|
2024-10-06 13:27:43 +02:00
|
|
|
|
.add_systems(Update, start_menu_system.run_if(in_state(GameState::Start)))
|
|
|
|
|
.add_systems(OnExit(GameState::Start), despawn_screen::<StartMenu>)
|
|
|
|
|
.add_systems(OnEnter(GameState::Game), game_ui_setup)
|
|
|
|
|
.add_systems(Update, player_input.run_if(in_state(GameState::Game)))
|
|
|
|
|
.add_systems(Update, game_buttons.run_if(in_state(GameState::Game)))
|
2024-10-06 15:13:59 +02:00
|
|
|
|
.add_systems(Update, label_update.run_if(in_state(GameState::Game)))
|
2024-10-06 14:19:20 +02:00
|
|
|
|
.add_systems(OnExit(GameState::Game), despawn_screen::<MainGame>)
|
|
|
|
|
.add_systems(OnEnter(GameState::End), end_setup)
|
|
|
|
|
.add_systems(Update, end_buttons.run_if(in_state(GameState::End)))
|
|
|
|
|
.add_systems(OnExit(GameState::End), despawn_screen::<GameOver>)
|
2024-10-06 10:38:42 +02:00
|
|
|
|
.run();
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-06 14:19:20 +02:00
|
|
|
|
fn end_setup() {
|
|
|
|
|
info!("ending")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn end_buttons(
|
|
|
|
|
keys: Res<ButtonInput<KeyCode>>,
|
|
|
|
|
mut game_state: ResMut<NextState<GameState>>
|
|
|
|
|
) {
|
|
|
|
|
if keys.just_pressed(KeyCode::Space) {
|
|
|
|
|
info!("start space");
|
|
|
|
|
game_state.set(GameState::Start);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-06 14:00:53 +02:00
|
|
|
|
fn start_ui_setup(mut commands: Commands, _asset_server: Res<AssetServer>) {
|
|
|
|
|
// Create a container node that places its children (text areas) in a vertical column and centers them
|
|
|
|
|
let container_node = NodeBundle {
|
|
|
|
|
style: Style {
|
|
|
|
|
width: Val::Percent(100.0), // Full width of the screen
|
|
|
|
|
height: Val::Percent(100.0), // Full height of the screen
|
|
|
|
|
flex_direction: FlexDirection::Column, // Arrange children in a column (vertical)
|
|
|
|
|
justify_content: JustifyContent::Center, // Center vertically
|
|
|
|
|
align_items: AlignItems::Center, // Center horizontally
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
..default()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Create the container for the text areas
|
|
|
|
|
let container = commands.spawn(container_node).id();
|
|
|
|
|
|
|
|
|
|
// TextStyle for the top text (larger font)
|
|
|
|
|
let top_text_style = TextStyle {
|
|
|
|
|
font_size: 50.0, // Larger font size
|
|
|
|
|
color: Color::WHITE,
|
|
|
|
|
// font: asset_server.load("fonts/FiraSans-Bold.ttf"), // Load font if needed
|
|
|
|
|
..default()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// TextStyle for the bottom text (smaller font)
|
|
|
|
|
let bottom_text_style = TextStyle {
|
|
|
|
|
font_size: 30.0, // Smaller font size
|
|
|
|
|
color: Color::WHITE,
|
|
|
|
|
// font: asset_server.load("fonts/FiraSans-Regular.ttf"), // Load font if needed
|
|
|
|
|
..default()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// TextBundle for the top text
|
|
|
|
|
let top_text_node = TextBundle::from_section(
|
|
|
|
|
"Astraea", // Text for the top section
|
|
|
|
|
top_text_style,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// TextBundle for the bottom text
|
|
|
|
|
let bottom_text_node = TextBundle::from_section(
|
|
|
|
|
"Press Space to Begin", // Text for the bottom section
|
|
|
|
|
bottom_text_style,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Spawn the text nodes and add them as children to the container
|
|
|
|
|
let top_text = commands.spawn((top_text_node, StartMenu)).id();
|
|
|
|
|
let bottom_text = commands.spawn((bottom_text_node, StartMenu)).id();
|
|
|
|
|
|
|
|
|
|
commands.entity(container).push_children(&[top_text, bottom_text]);
|
2024-10-06 14:19:20 +02:00
|
|
|
|
|
|
|
|
|
commands.spawn((
|
|
|
|
|
Camera3dBundle {
|
|
|
|
|
transform: Transform::from_xyz(0.0, 0.0, 0.0),
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
Player {
|
|
|
|
|
target_rotation: None,
|
|
|
|
|
target_cons_name: None,
|
|
|
|
|
score: 0,
|
|
|
|
|
health: 3,
|
2024-10-06 15:23:17 +02:00
|
|
|
|
thinking: true,
|
2024-10-06 14:19:20 +02:00
|
|
|
|
},
|
|
|
|
|
GameOver,
|
|
|
|
|
));
|
2024-10-06 14:00:53 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-10-06 11:42:32 +02:00
|
|
|
|
fn spawn_cons_lines(
|
2024-10-06 11:18:45 +02:00
|
|
|
|
mut commands: Commands,
|
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
2024-10-06 11:42:32 +02:00
|
|
|
|
sky: Res<Sky>,
|
2024-10-06 11:48:13 +02:00
|
|
|
|
target_constellation_name: String,
|
2024-10-06 11:18:45 +02:00
|
|
|
|
) {
|
|
|
|
|
// Create a material for the line
|
|
|
|
|
let line_material = materials.add(StandardMaterial {
|
2024-10-06 14:00:53 +02:00
|
|
|
|
emissive: LinearRgba::rgb(0.5, 0.5, 1.0), // Red color for the line
|
2024-10-06 11:18:45 +02:00
|
|
|
|
..default()
|
|
|
|
|
});
|
|
|
|
|
|
2024-10-06 11:48:13 +02:00
|
|
|
|
let mut target_constellation = sky.content[0].clone();
|
|
|
|
|
for constellation in sky.content.clone() {
|
|
|
|
|
if constellation.name == target_constellation_name {
|
|
|
|
|
target_constellation = constellation;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-06 12:14:20 +02:00
|
|
|
|
let mut vertices : Vec<Vec3> = vec![];
|
|
|
|
|
|
|
|
|
|
for line in target_constellation.lines {
|
|
|
|
|
for star_index in line {
|
|
|
|
|
let star = target_constellation.stars[star_index as usize].clone();
|
|
|
|
|
vertices.push(celestial_to_cartesian(star.rah, star.dec));
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-06 11:18:45 +02:00
|
|
|
|
|
|
|
|
|
// Create the mesh and add the vertices
|
2024-10-06 12:14:20 +02:00
|
|
|
|
let mut mesh = Mesh::new(PrimitiveTopology::LineList, RenderAssetUsages::RENDER_WORLD);
|
2024-10-06 11:18:45 +02:00
|
|
|
|
mesh.insert_attribute(Mesh::ATTRIBUTE_POSITION, vertices);
|
2024-10-06 12:54:51 +02:00
|
|
|
|
|
2024-10-06 11:18:45 +02:00
|
|
|
|
commands.spawn((
|
|
|
|
|
PbrBundle {
|
|
|
|
|
mesh: meshes.add(mesh),
|
|
|
|
|
material: line_material.clone(),
|
|
|
|
|
transform: Transform::default(), // Position and scale for the line
|
|
|
|
|
..default()
|
|
|
|
|
},
|
2024-10-06 14:19:20 +02:00
|
|
|
|
ConstellationLine,
|
|
|
|
|
MainGame
|
2024-10-06 11:18:45 +02:00
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-05 16:21:57 +02:00
|
|
|
|
fn star_setup(
|
2024-10-05 09:37:47 +02:00
|
|
|
|
mut commands: Commands,
|
|
|
|
|
mut meshes: ResMut<Assets<Mesh>>,
|
|
|
|
|
mut materials: ResMut<Assets<StandardMaterial>>,
|
2024-10-05 08:41:37 +02:00
|
|
|
|
) {
|
2024-10-05 09:46:57 +02:00
|
|
|
|
// plane
|
2024-10-05 14:15:28 +02:00
|
|
|
|
commands.insert_resource(ClearColor(Color::BLACK));
|
2024-10-05 12:17:55 +02:00
|
|
|
|
|
|
|
|
|
let stars = get_stars().unwrap();
|
|
|
|
|
|
2024-10-05 14:12:31 +02:00
|
|
|
|
//let mesh = meshes.add(Cuboid::new(star_size, star_size, star_size));
|
|
|
|
|
let star_mesh = meshes.add(Sphere::new(1.0).mesh().ico(3).unwrap());
|
|
|
|
|
//let material = materials.add(Color::srgb(1.0, 1.0, 1.0));
|
|
|
|
|
let star_material = materials.add(StandardMaterial {
|
|
|
|
|
emissive: LinearRgba::rgb(1.0, 1.0, 1.0),
|
|
|
|
|
..default()
|
|
|
|
|
});
|
2024-10-05 13:11:11 +02:00
|
|
|
|
|
2024-10-05 12:17:55 +02:00
|
|
|
|
for star in stars {
|
2024-10-06 12:54:51 +02:00
|
|
|
|
let star_pos = star_position(star.clone()) * SKY_RADIUS;
|
2024-10-05 14:12:31 +02:00
|
|
|
|
let star_mag = star.v.parse::<f32>().unwrap();
|
2024-10-06 12:54:51 +02:00
|
|
|
|
let mut star_size = STAR_SCALE * 2.512f32.powf(-star_mag*0.5);
|
2024-10-05 12:55:18 +02:00
|
|
|
|
|
2024-10-05 14:12:31 +02:00
|
|
|
|
if star.constellation.is_some() {
|
2024-10-06 12:54:51 +02:00
|
|
|
|
star_size *= EASYNESS;
|
2024-10-05 14:12:31 +02:00
|
|
|
|
}
|
2024-10-06 12:54:51 +02:00
|
|
|
|
star_size = star_size.min(MAX_STAR_SIZE*STAR_SCALE);
|
2024-10-05 14:12:31 +02:00
|
|
|
|
|
2024-10-05 13:11:11 +02:00
|
|
|
|
commands.spawn((
|
|
|
|
|
PbrBundle {
|
2024-10-05 14:12:31 +02:00
|
|
|
|
mesh: star_mesh.clone(),
|
|
|
|
|
material: star_material.clone(),
|
|
|
|
|
transform: Transform::from_xyz(star_pos.x, star_pos.y, star_pos.z)
|
|
|
|
|
.with_scale(Vec3::splat(star_size)),
|
2024-10-05 13:11:11 +02:00
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
Star,
|
|
|
|
|
));
|
2024-10-05 12:17:55 +02:00
|
|
|
|
}
|
2024-10-05 16:21:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-05 12:17:55 +02:00
|
|
|
|
fn get_stars() -> std::io::Result<Vec<StarData>> {
|
|
|
|
|
let mut file = File::open("data/stars.json")?;
|
|
|
|
|
let mut data = String::new();
|
|
|
|
|
file.read_to_string(&mut data)?;
|
|
|
|
|
|
|
|
|
|
let stars: Vec<StarData> = serde_json::from_str(&data).unwrap();
|
|
|
|
|
|
|
|
|
|
Ok(stars)
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-05 12:55:18 +02:00
|
|
|
|
fn star_position(star_data: StarData) -> Vec3 {
|
|
|
|
|
// Convert declination to decimal degrees
|
|
|
|
|
let text_ra = star_data.ra;
|
|
|
|
|
let text_dec = star_data.dec;
|
|
|
|
|
|
|
|
|
|
let ra_seconds: f64 = 3600.0 * text_ra[0..2].parse::<f64>().unwrap()
|
|
|
|
|
+ 60.0 * text_ra[4..6].parse::<f64>().unwrap()
|
|
|
|
|
+ text_ra[8..12].parse::<f64>().unwrap();
|
2024-10-05 14:12:31 +02:00
|
|
|
|
|
2024-10-05 12:55:18 +02:00
|
|
|
|
// Parse Dec
|
|
|
|
|
let formated_dec = text_dec
|
|
|
|
|
.replace("°", " ")
|
|
|
|
|
.replace("′", " ")
|
|
|
|
|
.replace("″", " ");
|
|
|
|
|
let dec_parts: Vec<&str> = formated_dec.split_whitespace().collect();
|
|
|
|
|
|
|
|
|
|
let dec_deg: f64 = dec_parts[0].parse::<f64>().unwrap()
|
|
|
|
|
+ dec_parts[1].parse::<f64>().unwrap() / 60.0
|
|
|
|
|
+ dec_parts[2].parse::<f64>().unwrap() / 3600.0;
|
|
|
|
|
|
2024-10-05 14:15:28 +02:00
|
|
|
|
celestial_to_cartesian(ra_seconds/3600.0, dec_deg)
|
2024-10-05 12:55:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn celestial_to_cartesian(rah: f64, ded: f64) -> Vec3 {
|
|
|
|
|
let y_rot = 2.0 * PI * rah / 24.0;
|
|
|
|
|
let x_rot = 2.0 * PI * ded / 360.0;
|
|
|
|
|
|
|
|
|
|
let x : f32 = (y_rot.sin() * x_rot.cos()) as f32;
|
|
|
|
|
let y : f32 = x_rot.sin() as f32;
|
|
|
|
|
let z : f32 = (y_rot.cos() * x_rot.cos()) as f32;
|
|
|
|
|
|
|
|
|
|
Vec3::new(x, y, z)
|
|
|
|
|
}
|
2024-10-05 12:17:55 +02:00
|
|
|
|
|
2024-10-06 10:38:42 +02:00
|
|
|
|
fn cons_setup(mut sky: ResMut<Sky>) {
|
|
|
|
|
sky.content = get_cons().unwrap();
|
|
|
|
|
}
|
2024-10-05 16:57:26 +02:00
|
|
|
|
|
2024-10-06 10:38:42 +02:00
|
|
|
|
fn get_cons() -> std::io::Result<Vec<Constellation>> {
|
|
|
|
|
let mut file = File::open("data/constellations.json")?;
|
|
|
|
|
let mut data = String::new();
|
|
|
|
|
file.read_to_string(&mut data)?;
|
2024-10-05 16:57:26 +02:00
|
|
|
|
|
2024-10-06 10:38:42 +02:00
|
|
|
|
let sky_data: Vec<Constellation> = serde_json::from_str(&data).unwrap();
|
2024-10-05 16:57:26 +02:00
|
|
|
|
|
2024-10-06 10:38:42 +02:00
|
|
|
|
Ok(sky_data)
|
2024-10-05 16:21:57 +02:00
|
|
|
|
}
|
2024-10-06 08:39:03 +02:00
|
|
|
|
|
2024-10-06 13:27:43 +02:00
|
|
|
|
fn game_ui_setup(mut commands: Commands, _asset_server: Res<AssetServer>) {
|
2024-10-06 08:58:38 +02:00
|
|
|
|
// Create a container node that places its children (buttons) at the bottom of the screen
|
2024-10-06 08:51:49 +02:00
|
|
|
|
let container_node = NodeBundle {
|
|
|
|
|
style: Style {
|
2024-10-06 08:58:38 +02:00
|
|
|
|
width: Val::Percent(100.0), // Full width of the screen
|
|
|
|
|
height: Val::Percent(100.0), // Full height of the screen
|
|
|
|
|
flex_direction: FlexDirection::Row, // Arrange children in a row (horizontal)
|
|
|
|
|
justify_content: JustifyContent::Center, // Center horizontally
|
|
|
|
|
align_items: AlignItems::FlexEnd, // Place at the bottom of the screen
|
|
|
|
|
padding: UiRect::all(Val::Px(10.0)), // Optional padding
|
2024-10-06 08:51:49 +02:00
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
..default()
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-06 08:58:38 +02:00
|
|
|
|
// Button style (same for all buttons)
|
|
|
|
|
let button_style = Style {
|
|
|
|
|
width: Val::Px(150.0),
|
|
|
|
|
height: Val::Px(65.0),
|
|
|
|
|
margin: UiRect::all(Val::Px(10.0)), // Add margin between buttons
|
|
|
|
|
justify_content: JustifyContent::Center, // Center text horizontally
|
|
|
|
|
align_items: AlignItems::Center, // Center text vertically
|
|
|
|
|
border: UiRect::all(Val::Px(5.0)),
|
2024-10-06 08:51:49 +02:00
|
|
|
|
..default()
|
|
|
|
|
};
|
|
|
|
|
|
2024-10-06 08:58:38 +02:00
|
|
|
|
// Create the container for the buttons
|
2024-10-06 08:51:49 +02:00
|
|
|
|
let container = commands.spawn(container_node).id();
|
|
|
|
|
|
2024-10-06 08:58:38 +02:00
|
|
|
|
// Function to create buttons with different text
|
2024-10-06 10:50:21 +02:00
|
|
|
|
for _i in 1..=4 {
|
2024-10-06 08:58:38 +02:00
|
|
|
|
let button_node = ButtonBundle {
|
|
|
|
|
style: button_style.clone(),
|
|
|
|
|
border_color: BorderColor(Color::BLACK),
|
|
|
|
|
background_color: NORMAL_BUTTON.into(),
|
|
|
|
|
..default()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let button_text_node = TextBundle::from_section(
|
2024-10-06 10:50:21 +02:00
|
|
|
|
"".to_string(),
|
2024-10-06 08:58:38 +02:00
|
|
|
|
TextStyle {
|
2024-10-06 09:05:54 +02:00
|
|
|
|
//font: asset_server.load("fonts/FiraSans-Bold.ttf"), // Load font
|
|
|
|
|
font_size: 15.0,
|
2024-10-06 08:58:38 +02:00
|
|
|
|
color: Color::srgb(0.9, 0.9, 0.9),
|
2024-10-06 09:05:54 +02:00
|
|
|
|
..default()
|
2024-10-06 08:58:38 +02:00
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Spawn the button and its text as children of the container
|
2024-10-06 14:19:20 +02:00
|
|
|
|
let button = commands.spawn((button_node, MainGame)).id();
|
|
|
|
|
let button_text = commands.spawn((button_text_node, AnswerButton, MainGame)).id();
|
2024-10-06 08:58:38 +02:00
|
|
|
|
|
|
|
|
|
commands.entity(button).push_children(&[button_text]);
|
|
|
|
|
commands.entity(container).push_children(&[button]);
|
|
|
|
|
}
|
2024-10-06 15:13:59 +02:00
|
|
|
|
|
|
|
|
|
// Label style for top corners
|
|
|
|
|
let label_style = Style {
|
|
|
|
|
position_type: PositionType::Absolute, // Absolute positioning
|
|
|
|
|
width: Val::Auto, // Auto width to fit text
|
|
|
|
|
height: Val::Auto, // Auto height to fit text
|
|
|
|
|
margin: UiRect::all(Val::Px(10.0)), // Margin around the text
|
|
|
|
|
..default()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Top left label
|
|
|
|
|
let top_left_label_node = TextBundle {
|
|
|
|
|
style: Style {
|
|
|
|
|
position_type: PositionType::Absolute,
|
|
|
|
|
left: Val::Px(10.0),
|
|
|
|
|
top: Val::Px(10.0),
|
|
|
|
|
..label_style.clone()
|
|
|
|
|
},
|
|
|
|
|
text: Text::from_section(
|
|
|
|
|
"* * *", // Text content
|
|
|
|
|
TextStyle {
|
|
|
|
|
// font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
|
|
|
font_size: 30.0,
|
|
|
|
|
color: Color::WHITE,
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
..default()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Top right label
|
|
|
|
|
let top_right_label_node = TextBundle {
|
|
|
|
|
style: Style {
|
|
|
|
|
position_type: PositionType::Absolute,
|
|
|
|
|
right: Val::Px(10.0),
|
|
|
|
|
top: Val::Px(10.0),
|
|
|
|
|
..label_style.clone()
|
|
|
|
|
},
|
|
|
|
|
text: Text::from_section(
|
|
|
|
|
"0000", // Text content
|
|
|
|
|
TextStyle {
|
|
|
|
|
// font: asset_server.load("fonts/FiraSans-Bold.ttf"),
|
|
|
|
|
font_size: 30.0,
|
|
|
|
|
color: Color::WHITE,
|
|
|
|
|
..default()
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
..default()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Spawn the top left and top right labels
|
|
|
|
|
commands.spawn((top_left_label_node, MainGame, HealthLabel));
|
|
|
|
|
commands.spawn((top_right_label_node, MainGame, ScoreLabel));
|
2024-10-06 08:39:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-06 15:13:59 +02:00
|
|
|
|
|
2024-10-06 14:00:53 +02:00
|
|
|
|
fn choose_constellation(
|
|
|
|
|
player: &mut Player,
|
|
|
|
|
sky: Res<Sky>, // Res to access the Sky resource
|
|
|
|
|
mut text_query: Query<&mut Text, With<AnswerButton>>,
|
|
|
|
|
mut button_query: Query<(&mut BackgroundColor, &mut BorderColor), With<Button>>, // Query to reset button colors
|
|
|
|
|
constellation_line_query : Query<(Entity, &ConstellationLine)>,
|
|
|
|
|
mut commands: Commands,
|
2024-10-06 15:23:17 +02:00
|
|
|
|
mut game_state: ResMut<NextState<GameState>>
|
2024-10-06 14:00:53 +02:00
|
|
|
|
) {
|
2024-10-06 15:23:17 +02:00
|
|
|
|
if player.health == 0 {
|
|
|
|
|
info!("dead");
|
|
|
|
|
game_state.set(GameState::End);
|
|
|
|
|
}
|
2024-10-06 14:00:53 +02:00
|
|
|
|
if sky.content.len() >= 4 {
|
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
|
let mut selected_constellations = sky.content.clone();
|
|
|
|
|
selected_constellations.shuffle(&mut rng);
|
|
|
|
|
let constellations = &selected_constellations[0..4];
|
|
|
|
|
|
|
|
|
|
let target_index = rng.next_u32().rem_euclid(4) as usize;
|
|
|
|
|
let target_constellation = &constellations[target_index];
|
|
|
|
|
let target_rotation = Quat::from_rotation_arc(
|
|
|
|
|
Vec3::Z,
|
|
|
|
|
-celestial_to_cartesian(target_constellation.rah, target_constellation.dec),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
player.target_rotation = Some(target_rotation);
|
|
|
|
|
player.target_cons_name = Some(target_constellation.name.clone());
|
|
|
|
|
|
|
|
|
|
info!("Target constellation: {}", target_constellation.name);
|
|
|
|
|
|
|
|
|
|
for (i, mut text) in text_query.iter_mut().enumerate() {
|
|
|
|
|
text.sections[0].value = constellations[i].name.clone();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (mut bg_color, mut border_color) in &mut button_query {
|
|
|
|
|
*bg_color = NORMAL_BUTTON.into();
|
|
|
|
|
*border_color = Color::BLACK.into();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (entity, _line) in constellation_line_query.iter() {
|
|
|
|
|
commands.entity(entity).despawn();
|
|
|
|
|
}
|
2024-10-06 15:23:17 +02:00
|
|
|
|
|
|
|
|
|
player.thinking = true;
|
2024-10-06 14:00:53 +02:00
|
|
|
|
} else {
|
|
|
|
|
info!("Not enough constellations in the sky (need 4)");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-06 13:27:43 +02:00
|
|
|
|
fn player_input(
|
2024-10-06 10:38:42 +02:00
|
|
|
|
keys: Res<ButtonInput<KeyCode>>,
|
2024-10-06 11:18:45 +02:00
|
|
|
|
mut player_query: Query<(&mut Player, &mut Transform)>, // Query to get Player and Transform
|
2024-10-06 10:38:42 +02:00
|
|
|
|
sky: Res<Sky>, // Res to access the Sky resource
|
2024-10-06 14:00:53 +02:00
|
|
|
|
text_query: Query<&mut Text, With<AnswerButton>>,
|
|
|
|
|
button_query: Query<(&mut BackgroundColor, &mut BorderColor), With<Button>>, // Query to reset button colors
|
2024-10-06 11:18:45 +02:00
|
|
|
|
constellation_line_query : Query<(Entity, &ConstellationLine)>,
|
2024-10-06 14:00:53 +02:00
|
|
|
|
commands: Commands,
|
2024-10-06 15:23:17 +02:00
|
|
|
|
game_state: ResMut<NextState<GameState>>
|
2024-10-06 10:38:42 +02:00
|
|
|
|
) {
|
2024-10-06 14:19:20 +02:00
|
|
|
|
if let Ok((mut player, mut transform)) = player_query.get_single_mut() {
|
2024-10-06 10:38:42 +02:00
|
|
|
|
// If the space key was just pressed
|
2024-10-06 14:00:53 +02:00
|
|
|
|
if keys.just_pressed(KeyCode::Space) || player.target_cons_name.is_none() {
|
2024-10-06 15:23:17 +02:00
|
|
|
|
choose_constellation(&mut player, sky, text_query, button_query, constellation_line_query, commands, game_state);
|
2024-10-06 14:00:53 +02:00
|
|
|
|
return
|
2024-10-06 10:38:42 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-06 12:14:20 +02:00
|
|
|
|
let mut rotation = Quat::IDENTITY;
|
|
|
|
|
|
|
|
|
|
// Rotate left when the A key is pressed
|
|
|
|
|
if keys.pressed(KeyCode::KeyA) {
|
|
|
|
|
rotation *= Quat::from_rotation_y((PI / 60.0) as f32); // Rotate by 3 degrees (PI/60 radians)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Rotate right when the D key is pressed
|
|
|
|
|
if keys.pressed(KeyCode::KeyD) {
|
|
|
|
|
rotation *= Quat::from_rotation_y((-PI / 60.0) as f32); // Rotate by -3 degrees
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply the rotation to the transform
|
|
|
|
|
if rotation != Quat::IDENTITY {
|
|
|
|
|
transform.rotation *= rotation; // Apply the rotation
|
|
|
|
|
player.target_rotation = None;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-06 10:38:42 +02:00
|
|
|
|
if let Some(target_rotation) = player.target_rotation {
|
|
|
|
|
let current_rotation = transform.rotation;
|
|
|
|
|
|
|
|
|
|
transform.rotation = current_rotation.slerp(target_rotation, 0.1);
|
|
|
|
|
|
|
|
|
|
if transform.rotation.angle_between(target_rotation) < 0.01 {
|
|
|
|
|
player.target_rotation = None;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-06 08:51:49 +02:00
|
|
|
|
|
2024-10-06 13:27:43 +02:00
|
|
|
|
fn start_menu_system(
|
|
|
|
|
keys: Res<ButtonInput<KeyCode>>,
|
2024-10-06 14:46:56 +02:00
|
|
|
|
mut game_state: ResMut<NextState<GameState>>,
|
|
|
|
|
mut player_query: Query<(&mut Player, &mut Transform)>
|
2024-10-06 13:27:43 +02:00
|
|
|
|
) {
|
|
|
|
|
if keys.just_pressed(KeyCode::Space) {
|
|
|
|
|
info!("start space");
|
2024-10-06 14:00:53 +02:00
|
|
|
|
game_state.set(GameState::Game);
|
2024-10-06 13:27:43 +02:00
|
|
|
|
}
|
2024-10-06 14:46:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if let Ok((_player, mut transform)) = player_query.get_single_mut() {
|
|
|
|
|
let mut rotation = Quat::IDENTITY;
|
|
|
|
|
rotation *= Quat::from_rotation_y((PI / 6000.0) as f32); // Rotate by 3 degrees (PI/60 radians)
|
|
|
|
|
rotation *= Quat::from_rotation_x((-PI / 2000.0) as f32); // Rotate by -3 degrees
|
|
|
|
|
transform.rotation *= rotation; // Apply the rotation
|
|
|
|
|
}
|
2024-10-06 13:27:43 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-10-06 15:13:59 +02:00
|
|
|
|
fn label_update(
|
|
|
|
|
mut param_set: ParamSet<(
|
|
|
|
|
Query<&mut Text, With<HealthLabel>>,
|
|
|
|
|
Query<&mut Text, With<ScoreLabel>>,
|
|
|
|
|
)>,
|
|
|
|
|
mut player_query: Query<(&mut Player, &mut Transform)>,
|
|
|
|
|
) {
|
|
|
|
|
if let Ok((player, _)) = player_query.get_single_mut() {
|
|
|
|
|
// Update the health label
|
|
|
|
|
if let Ok(mut health_text) = param_set.p0().get_single_mut() {
|
|
|
|
|
health_text.sections[0].value = "# ".repeat(player.health);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update the score label
|
|
|
|
|
if let Ok(mut score_text) = param_set.p1().get_single_mut() {
|
|
|
|
|
score_text.sections[0].value = format!("{}", player.score);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-10-06 13:27:43 +02:00
|
|
|
|
fn game_buttons(
|
2024-10-06 08:51:49 +02:00
|
|
|
|
mut interaction_query: Query<
|
|
|
|
|
(
|
|
|
|
|
&Interaction,
|
|
|
|
|
&mut BackgroundColor,
|
|
|
|
|
&mut BorderColor,
|
|
|
|
|
&Children,
|
|
|
|
|
),
|
2024-10-06 10:33:29 +02:00
|
|
|
|
With<Button>,
|
2024-10-06 08:51:49 +02:00
|
|
|
|
>,
|
2024-10-06 10:33:29 +02:00
|
|
|
|
mut text_query: Query<&mut Text, With<AnswerButton>>,
|
|
|
|
|
mut player_query: Query<&mut Player>,
|
2024-10-06 11:48:13 +02:00
|
|
|
|
commands: Commands,
|
|
|
|
|
meshes: ResMut<Assets<Mesh>>,
|
|
|
|
|
materials: ResMut<Assets<StandardMaterial>>,
|
2024-10-06 15:23:17 +02:00
|
|
|
|
sky: Res<Sky>
|
2024-10-06 08:51:49 +02:00
|
|
|
|
) {
|
2024-10-06 15:23:17 +02:00
|
|
|
|
if let Ok(mut player) = player_query.get_single_mut() {
|
|
|
|
|
if !player.thinking {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut pressed_button: Option<String> = None;
|
|
|
|
|
|
|
|
|
|
for (
|
|
|
|
|
interaction,
|
|
|
|
|
_color,
|
|
|
|
|
_border_color,
|
|
|
|
|
children
|
|
|
|
|
) in &mut interaction_query {
|
|
|
|
|
if *interaction == Interaction::Pressed {
|
|
|
|
|
if let Ok(text) = text_query.get_mut(children[0]) {
|
|
|
|
|
pressed_button = Some(text.sections[0].value.clone());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-06 10:33:29 +02:00
|
|
|
|
|
2024-10-06 15:23:17 +02:00
|
|
|
|
if let Some(selected_cons) = pressed_button {
|
2024-10-06 14:19:20 +02:00
|
|
|
|
if let Some(target_cons) = player.target_cons_name.clone() {
|
|
|
|
|
spawn_cons_lines(commands, meshes, materials, sky, target_cons.clone());
|
|
|
|
|
|
|
|
|
|
if target_cons == selected_cons {
|
|
|
|
|
player.score += 100;
|
|
|
|
|
} else {
|
|
|
|
|
player.health -= 1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-06 15:23:17 +02:00
|
|
|
|
player.thinking = false;
|
|
|
|
|
|
2024-10-06 14:19:20 +02:00
|
|
|
|
for (
|
|
|
|
|
_interaction,
|
|
|
|
|
mut color,
|
|
|
|
|
mut border_color,
|
|
|
|
|
children
|
|
|
|
|
) in &mut interaction_query {
|
|
|
|
|
if let Ok(text) = text_query.get_mut(children[0]) {
|
|
|
|
|
let button_text = text.sections[0].value.clone();
|
|
|
|
|
|
|
|
|
|
*color = if button_text == target_cons {
|
|
|
|
|
RIGHT_BUTTON.into()
|
|
|
|
|
} else {
|
|
|
|
|
WRONG_BUTTON.into()
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
border_color.0 = if button_text == selected_cons {
|
|
|
|
|
Color::WHITE
|
|
|
|
|
} else {
|
|
|
|
|
Color::BLACK
|
|
|
|
|
};
|
|
|
|
|
}
|
2024-10-06 10:33:29 +02:00
|
|
|
|
}
|
2024-10-06 14:19:20 +02:00
|
|
|
|
}
|
2024-10-06 10:33:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-06 08:51:49 +02:00
|
|
|
|
}
|
2024-10-06 08:39:03 +02:00
|
|
|
|
|
2024-10-06 13:27:43 +02:00
|
|
|
|
// Generic system that takes a component as a parameter, and will despawn all entities with that component
|
|
|
|
|
fn despawn_screen<T: Component>(to_despawn: Query<Entity, With<T>>, mut commands: Commands) {
|
|
|
|
|
for entity in &to_despawn {
|
|
|
|
|
commands.entity(entity).despawn_recursive();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-10-06 10:33:29 +02:00
|
|
|
|
|