Constant speed even for small/large terminal sizes
This commit is contained in:
		
							parent
							
								
									33a6c5e33b
								
							
						
					
					
						commit
						90e701daa3
					
				
					 1 changed files with 19 additions and 6 deletions
				
			
		| 
						 | 
					@ -11,6 +11,8 @@ struct MatrixColumn {
 | 
				
			||||||
    length: u16,
 | 
					    length: u16,
 | 
				
			||||||
    speed: u16,
 | 
					    speed: u16,
 | 
				
			||||||
    chars: Vec<char>,
 | 
					    chars: Vec<char>,
 | 
				
			||||||
 | 
					    last_update: std::time::Instant,
 | 
				
			||||||
 | 
					    update_interval: std::time::Duration,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl MatrixColumn {
 | 
					impl MatrixColumn {
 | 
				
			||||||
| 
						 | 
					@ -22,12 +24,17 @@ impl MatrixColumn {
 | 
				
			||||||
            .map(|_| Self::random_matrix_char(&mut rng))
 | 
					            .map(|_| Self::random_matrix_char(&mut rng))
 | 
				
			||||||
            .collect();
 | 
					            .collect();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let now = std::time::Instant::now();
 | 
				
			||||||
 | 
					        let update_interval = std::time::Duration::from_millis((100 / speed as u64).max(10));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        Self {
 | 
					        Self {
 | 
				
			||||||
            x,
 | 
					            x,
 | 
				
			||||||
            y: 0,
 | 
					            y: 0,
 | 
				
			||||||
            length,
 | 
					            length,
 | 
				
			||||||
            speed,
 | 
					            speed,
 | 
				
			||||||
            chars,
 | 
					            chars,
 | 
				
			||||||
 | 
					            last_update: now,
 | 
				
			||||||
 | 
					            update_interval,
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -47,15 +54,20 @@ impl MatrixColumn {
 | 
				
			||||||
        *matrix_chars.choose(rng).unwrap()
 | 
					        *matrix_chars.choose(rng).unwrap()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn update(&mut self, height: u16, frame_counter: usize, speed_multiplier: usize) {
 | 
					    fn update(&mut self, height: u16, _frame_counter: usize, speed_multiplier: usize) {
 | 
				
			||||||
        // Calculate effective speed with multiplier (higher multiplier = faster)
 | 
					        let now = std::time::Instant::now();
 | 
				
			||||||
        let effective_speed = if speed_multiplier == 0 {
 | 
					
 | 
				
			||||||
            self.speed as usize * 8 // Much slower when multiplier is 0
 | 
					        // 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 {
 | 
					        } 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);
 | 
					            self.y = self.y.wrapping_add(1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // Reset column when it goes off screen
 | 
					            // Reset column when it goes off screen
 | 
				
			||||||
| 
						 | 
					@ -64,6 +76,7 @@ impl MatrixColumn {
 | 
				
			||||||
                let mut rng = rng();
 | 
					                let mut rng = rng();
 | 
				
			||||||
                self.length = rng.random_range(5..15);
 | 
					                self.length = rng.random_range(5..15);
 | 
				
			||||||
                self.speed = rng.random_range(1..4);
 | 
					                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)
 | 
					                self.chars = (0..self.length)
 | 
				
			||||||
                    .map(|_| Self::random_matrix_char(&mut rng))
 | 
					                    .map(|_| Self::random_matrix_char(&mut rng))
 | 
				
			||||||
                    .collect();
 | 
					                    .collect();
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue