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 12:17:55 +02:00
|
|
|
|
|
2024-10-06 15:36:10 +02:00
|
|
|
|
mod end_state;
|
2024-10-06 15:46:32 +02:00
|
|
|
|
mod start_state;
|
2024-10-06 16:19:10 +02:00
|
|
|
|
mod game_state;
|
2024-10-06 15:36:10 +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 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 17:16:03 +02:00
|
|
|
|
state: PlayerState,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(PartialEq)]
|
|
|
|
|
enum PlayerState {
|
|
|
|
|
Playing,
|
|
|
|
|
Hinted,
|
|
|
|
|
Answered,
|
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 15:46:32 +02:00
|
|
|
|
.add_systems(OnEnter(GameState::Start), start_state::setup)
|
2024-10-06 17:16:03 +02:00
|
|
|
|
.add_systems(OnEnter(GameState::Start), start_state::audio_setup)
|
2024-10-06 15:46:32 +02:00
|
|
|
|
.add_systems(Update, start_state::player_interact.run_if(in_state(GameState::Start)))
|
2024-10-06 13:27:43 +02:00
|
|
|
|
.add_systems(OnExit(GameState::Start), despawn_screen::<StartMenu>)
|
2024-10-06 16:19:10 +02:00
|
|
|
|
.add_systems(OnEnter(GameState::Game), game_state::setup)
|
|
|
|
|
.add_systems(Update, game_state::player_interact.run_if(in_state(GameState::Game)))
|
|
|
|
|
.add_systems(Update, game_state::ui_buttons.run_if(in_state(GameState::Game)))
|
|
|
|
|
.add_systems(Update, game_state::ui_labels.run_if(in_state(GameState::Game)))
|
2024-10-06 14:19:20 +02:00
|
|
|
|
.add_systems(OnExit(GameState::Game), despawn_screen::<MainGame>)
|
2024-10-06 15:36:10 +02:00
|
|
|
|
.add_systems(OnEnter(GameState::End), end_state::setup)
|
2024-10-06 15:46:32 +02:00
|
|
|
|
.add_systems(Update, end_state::player_interact.run_if(in_state(GameState::End)))
|
2024-10-06 14:19:20 +02:00
|
|
|
|
.add_systems(OnExit(GameState::End), despawn_screen::<GameOver>)
|
2024-10-06 10:38:42 +02:00
|
|
|
|
.run();
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
// 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
|
|
|
|
|