From ea5332e4a1a035e888709bee278fcafb182f7922 Mon Sep 17 00:00:00 2001 From: LLLL Colonq Date: Fri, 31 Jul 2026 05:00:49 -0400 Subject: Update --- .../asrael_io/source/src/starfield.rs | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 2026/games_submissions/asrael_io/source/src/starfield.rs (limited to '2026/games_submissions/asrael_io/source/src/starfield.rs') 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, +} + +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); + } + } +} -- cgit v1.3.1