summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJon Santmyer <jon@jonsantmyer.com>2026-05-03 18:37:35 -0400
committerJon Santmyer <jon@jonsantmyer.com>2026-05-03 18:37:35 -0400
commitdd5de0107163bc3ea3898c07089d00f82feeec5e (patch)
treed8feef7d8a89c01ef183c6b032b29cd6d451aaec /src
parent759d5d27c7773c7fe8b165ce08b57204db990b74 (diff)
downloadsystemic4x-dd5de0107163bc3ea3898c07089d00f82feeec5e.tar.gz
systemic4x-dd5de0107163bc3ea3898c07089d00f82feeec5e.tar.bz2
systemic4x-dd5de0107163bc3ea3898c07089d00f82feeec5e.zip
add tick and subtick system for solar system orbit updates
Diffstat (limited to 'src')
-rw-r--r--src/main.rs12
-rw-r--r--src/solar_system.rs34
-rw-r--r--src/timeman.rs26
-rw-r--r--src/ui/camera_info.rs10
-rw-r--r--src/ui/topbar.rs9
5 files changed, 53 insertions, 38 deletions
diff --git a/src/main.rs b/src/main.rs
index 7a7e3a1..72cd3dd 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -22,7 +22,7 @@ use winit::application::{ApplicationHandler};
use winit::keyboard::PhysicalKey;
use winit::window::{Window, WindowId, WindowAttributes};
-use solar_system::SolarSystem;
+use solar_system::*;
use crate::timeman::TimeMan;
use crate::window::GameWindow;
@@ -64,12 +64,14 @@ impl GameState
pub fn new()
-> Self {
let timeman = TimeMan::new(0);
- let sol_system = match SolarSystem::new_from_known_star(0, "sol")
+ let mut sol_system = match SolarSystem::new_from_known_star(0, "sol")
{
Ok(system) => system,
Err(e) => panic!("Unable to create sol system : {}", e)
};
+ sol_system.update(timeman.seconds());
+
Self {
timeman: timeman,
solar_systems: vec![ sol_system ]
@@ -89,9 +91,9 @@ impl GameState
&mut self,
dt: Duration)
{
- self.timeman.update();
- self.solar_systems.iter_mut().for_each(|solar_system| {
- solar_system.update(self.timeman.seconds());
+ let ticks = self.timeman.update();
+ ticks.iter().for_each(|time| {
+ solar_system::update_systems(self.solar_systems.iter_mut(), *time);
});
}
}
diff --git a/src/solar_system.rs b/src/solar_system.rs
index df1d246..f4ca9ae 100644
--- a/src/solar_system.rs
+++ b/src/solar_system.rs
@@ -41,7 +41,15 @@ pub struct SolarSystem
id: SystemId,
name: String,
bodies: Vec<OrbitalBody>,
- time: Option<Second>
+}
+
+pub fn update_systems(
+ solar_systems: std::slice::IterMut<'_, SolarSystem>,
+ time: Second)
+{
+ for system in solar_systems {
+ system.update(time);
+ }
}
impl SolarSystem
@@ -66,6 +74,7 @@ impl SolarSystem
}
None => {}
}
+ if record.radius == 0.0 { continue; }
println!("New body: {:?}", record);
bodies.push(OrbitalBody {
@@ -78,7 +87,6 @@ impl SolarSystem
id: id,
name: bodies[0].name().clone(),
bodies: bodies,
- time: None
})
}
@@ -103,16 +111,6 @@ impl SolarSystem
self.bodies.as_slice()
}
- fn update_bodies(
- &mut self,
- time: Second)
- {
- self.bodies.iter_mut().for_each(|body| {
- body.position = Some(body.calculate_orbit_at(time));
- });
- self.time = Some(time);
- }
-
pub fn body_position(
&self,
body: &OrbitalBody)
@@ -132,15 +130,9 @@ impl SolarSystem
&mut self,
time: Second)
{
- match self.time {
- Some(cache_time) => {
- if cache_time == time { return; }
- self.update_bodies(time);
- }
- None => {
- self.update_bodies(time);
- }
- }
+ self.bodies.iter_mut().for_each(|body| {
+ body.position = Some(body.calculate_orbit_at(time));
+ });
}
}
diff --git a/src/timeman.rs b/src/timeman.rs
index 32a6f45..64250ee 100644
--- a/src/timeman.rs
+++ b/src/timeman.rs
@@ -12,9 +12,12 @@ pub const HOUR: Second = MINUTE * 60;
pub const DAY: Second = HOUR * 24;
pub const YEAR: Second = DAY * 365;
+pub const SYSTEM_TICK_INTERVAL: Second = 1 * DAY;
+
pub struct TimeMan
{
time: Second,
+ last_time: Second,
pub auto_tick: Option<Second>
}
@@ -24,6 +27,7 @@ impl TimeMan
-> Self {
Self {
time,
+ last_time: time,
auto_tick: None
}
}
@@ -42,11 +46,33 @@ impl TimeMan
pub fn update(
&mut self)
+ -> Vec<Second>
{
match self.auto_tick {
Some(advance) => { self.time += advance; },
None => {}
}
+
+ let time_diff = self.time - self.last_time;
+
+ let tick_diff = self.time / SYSTEM_TICK_INTERVAL -
+ self.last_time / SYSTEM_TICK_INTERVAL;
+
+ if time_diff == 0 || tick_diff == 0 {
+ self.last_time = self.time;
+ return vec![];
+ }
+
+ let time_start = self.last_time - (self.last_time % SYSTEM_TICK_INTERVAL);
+ let time_end = self.time - (self.time % SYSTEM_TICK_INTERVAL);
+
+ let tick_start = time_start / SYSTEM_TICK_INTERVAL;
+ let tick_end = time_end / SYSTEM_TICK_INTERVAL;
+
+ self.last_time = self.time;
+ (tick_start..tick_end).map(|v| {
+ v * SYSTEM_TICK_INTERVAL
+ }).collect::<Vec<_>>()
}
pub fn format_duration(time: Second) -> String
diff --git a/src/ui/camera_info.rs b/src/ui/camera_info.rs
index a7bc80b..fa46f50 100644
--- a/src/ui/camera_info.rs
+++ b/src/ui/camera_info.rs
@@ -53,16 +53,6 @@ impl CameraWindowState
});
ui.label(format!("Scale: {}", camera_state.camera_scale));
- match camera_state.camera_rot {
- Some(rot) => {
- ui.label(format!("pitch:{:^10} yaw:{:^10}",
- rot.y.0,
- rot.x.0,
- ));
- },
- None => {}
- }
-
});
});
}
diff --git a/src/ui/topbar.rs b/src/ui/topbar.rs
index 07c20a1..faa809f 100644
--- a/src/ui/topbar.rs
+++ b/src/ui/topbar.rs
@@ -43,6 +43,8 @@ impl TopBarState
}
});
+ ui.separator();
+
let button_seconds = [
1,
5,
@@ -58,6 +60,11 @@ impl TopBarState
let selected_button = state.auto_tick;
let timeman = game_state.timeman_mut();
+ ui.vertical(|ui| {
+ ui.label("Manual");
+ ui.checkbox(&mut state.do_auto_tick, "Auto");
+ });
+
button_seconds.iter().for_each(|&seconds| {
ui.vertical(|ui| {
let auto_selected = match selected_button {
@@ -75,8 +82,6 @@ impl TopBarState
}
});
});
-
- ui.checkbox(&mut state.do_auto_tick, "Auto");
});
ui.vertical_centered_justified(|ui| {
let timeman = game_state.timeman_mut();