Constant speed even for small/large terminal sizes

This commit is contained in:
Kevin Alberts 2025-08-15 11:44:12 +02:00
parent 33a6c5e33b
commit 90e701daa3

View file

@ -11,6 +11,8 @@ struct MatrixColumn {
length: u16,
speed: u16,
chars: Vec<char>,
last_update: std::time::Instant,
update_interval: std::time::Duration,
}
impl MatrixColumn {
@ -22,12 +24,17 @@ impl MatrixColumn {
.map(|_| Self::random_matrix_char(&mut rng))
.collect();
let now = std::time::Instant::now();
let update_interval = std::time::Duration::from_millis((100 / speed as u64).max(10));
Self {
x,
y: 0,
length,
speed,
chars,
last_update: now,
update_interval,
}
}
@ -47,15 +54,20 @@ impl MatrixColumn {
*matrix_chars.choose(rng).unwrap()
}
fn update(&mut self, height: u16, frame_counter: usize, speed_multiplier: usize) {
// Calculate effective speed with multiplier (higher multiplier = faster)
let effective_speed = if speed_multiplier == 0 {
self.speed as usize * 8 // Much slower when multiplier is 0
fn update(&mut self, height: u16, _frame_counter: usize, speed_multiplier: usize) {
let now = std::time::Instant::now();
// Calculate effective update interval with multiplier (higher multiplier = faster)
let base_interval = std::time::Duration::from_millis((200 / self.speed as u64).max(20));
let effective_interval = if speed_multiplier == 0 {
base_interval * 4 // Much slower when multiplier is 0
} else {
(self.speed as usize * 4).saturating_sub(speed_multiplier.saturating_sub(1)).max(1)
let divisor = (speed_multiplier + 1) as u32;
base_interval / divisor.max(1)
};
if frame_counter % effective_speed == 0 {
if now.duration_since(self.last_update) >= effective_interval {
self.last_update = now;
self.y = self.y.wrapping_add(1);
// Reset column when it goes off screen
@ -64,6 +76,7 @@ impl MatrixColumn {
let mut rng = rng();
self.length = rng.random_range(5..15);
self.speed = rng.random_range(1..4);
self.update_interval = std::time::Duration::from_millis((100 / self.speed as u64).max(10));
self.chars = (0..self.length)
.map(|_| Self::random_matrix_char(&mut rng))
.collect();