diff options
Diffstat (limited to 'src/solar_system.rs')
| -rw-r--r-- | src/solar_system.rs | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/src/solar_system.rs b/src/solar_system.rs index c98a0e8..cf6a36a 100644 --- a/src/solar_system.rs +++ b/src/solar_system.rs @@ -1,6 +1,6 @@ use serde::{Deserialize}; use crate::{GameState, known_stars::{KNOWN_STARS, StarNotFoundError}, timeman::{Second, TimeMan}}; -use std::{cell::RefCell, error::Error}; +use std::{cell::RefCell, error::Error, f64::consts::PI}; const GRAVITATIONAL_CONSTANT: f64 = 6.67408e-20; @@ -33,11 +33,13 @@ pub struct SerialOrbitalBody pub struct OrbitalBody { body: SerialOrbitalBody, + id: BodyId, position: Option<cgmath::Vector3<Kilometers>> } pub struct SolarSystem { + id: SystemId, name: String, bodies: Vec<OrbitalBody>, time: Option<Second> @@ -46,6 +48,7 @@ pub struct SolarSystem impl SolarSystem { pub fn new_from_csv( + id: SystemId, data: &'static str) -> Result<Self, Box<dyn Error>> { let data_reader = stringreader::StringReader::new(data); @@ -68,11 +71,13 @@ impl SolarSystem bodies.push(OrbitalBody { body: record, + id: bodies.len(), position: None }); } Ok(Self { + id: id, name: bodies[0].name().clone(), bodies: bodies, time: None @@ -80,15 +85,18 @@ impl SolarSystem } pub fn new_from_known_star( + id: SystemId, star: &'static str) -> Result<Self, Box<dyn Error>> { let star_csv = match KNOWN_STARS.get(star).copied() { Some(csv) => csv, None => return Err(Box::new(StarNotFoundError { star: star })) }; - SolarSystem::new_from_csv(star_csv) + SolarSystem::new_from_csv(id, star_csv) } + pub fn id(&self) -> SystemId { self.id } + pub fn name(&self) -> &String { &self.name } pub fn bodies(&self) @@ -140,18 +148,24 @@ impl SolarSystem impl OrbitalBody { + pub fn id(&self) -> BodyId { self.id } pub fn name(&self) -> &String { &self.body.name } pub fn radius(&self) -> f32 { self.body.radius as f32 } pub fn position(&self) -> cgmath::Vector3<f64> { self.position.unwrap() } + pub fn does_orbit(&self) -> bool { self.body.orbits.is_some() } + pub fn get_orbits(&self) -> Option<BodyId> { self.body.orbits } - fn calculate_orbit_at( + pub fn orbital_period(&self) -> Second + { ((self.body.semi_major_axis.powf(3.0) / self.body.sgp).sqrt() * std::f64::consts::TAU) as u64 } + + pub fn calculate_orbit_at( &self, time: Second) -> cgmath::Vector3<f64> { if self.body.orbits.is_none() { return cgmath::Vector3::<f64>::new(0.0, 0.0, 0.0); - } +} let eccentricity = self.body.eccentricity; |
