diff options
| author | LLLL Colonq <llll@colonq> | 2026-08-26 23:47:59 -0400 |
|---|---|---|
| committer | LLLL Colonq <llll@colonq> | 2026-08-26 23:47:59 -0400 |
| commit | 0d2d8cd897e94bd94e0b3ae7c6ce9aa580a44a0a (patch) | |
| tree | e9039d1c3aedca515d6cb499451d4b73aca4f5fd /2026/games_submissions/asrael_io/source/src/starfield.rs | |
| parent | 242943e5b023ce40eb3be826c418a478d4672f78 (diff) | |
Add a_tension_span
Diffstat (limited to '2026/games_submissions/asrael_io/source/src/starfield.rs')
| -rw-r--r-- | 2026/games_submissions/asrael_io/source/src/starfield.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/2026/games_submissions/asrael_io/source/src/starfield.rs b/2026/games_submissions/asrael_io/source/src/starfield.rs new file mode 100644 index 0000000..ab7a199 --- /dev/null +++ b/2026/games_submissions/asrael_io/source/src/starfield.rs @@ -0,0 +1,59 @@ +use crate::color::Palette; +use crate::color::db32::{BLUE, CYAN, GRAY, LIGHT_RED, LIME, PALE_BLUE, WHITE, YELLOW}; +use crate::rng::Rng; +use crate::{GAME_H, GAME_W}; + +const STAR_COLORS: [u8; 8] = [WHITE, GRAY, PALE_BLUE, CYAN, BLUE, YELLOW, LIGHT_RED, LIME]; + +struct Star { + color: u8, + speed: f32, + x: i32, + y: f32, +} + +#[derive(Default)] +pub struct Starfield { + stars: Vec<Star>, +} + +impl Starfield { + pub fn new(count: usize, rng: &mut Rng) -> Self { + let stars = (0..count) + .map(|i| { + let speed = match i % 3 { + 0 => 0.5, + 1 => 1.0, + _ => 1.5, + }; + + Star { + color: STAR_COLORS[rng.range(STAR_COLORS.len() as u32) as usize], + speed, + x: rng.range(GAME_W) as i32, + y: rng.range(GAME_H) as f32, + } + }) + .collect(); + + Self { stars } + } + + pub fn update(&mut self) { + for star in &mut self.stars { + star.y += star.speed; + if star.y >= GAME_H as f32 { + star.y -= GAME_H as f32; + } + } + } + + pub fn draw(&self, frame: &mut [u32], palette: &Palette, a: f32) { + for star in &self.stars { + let y = star.y - star.speed * (1.0 - a); + let y = (y as i32).rem_euclid(GAME_H as i32); + + frame[(y * GAME_W as i32 + star.x) as usize] = palette.at(star.color); + } + } +} |
