Skip to content

Commit 90526ac

Browse files
committed
add starry background
closes #17
1 parent c06251e commit 90526ac

File tree

5 files changed

+55
-0
lines changed

5 files changed

+55
-0
lines changed
384 Bytes
Binary file not shown.

assets/sprites/stars/star1.png

95 Bytes
Loading
377 Bytes
Binary file not shown.

assets/sprites/stars/star2.png

96 Bytes
Loading

src/screens/game.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub(super) fn plugin(app: &mut App) {
4040
spawn_asteroids,
4141
update_asteroids,
4242
collide_with_asteroid_check,
43+
update_stars,
4344
)
4445
.in_set(GameSystems::Environment),
4546
);
@@ -74,9 +75,47 @@ fn spawn_game_screen(
7475
PIXEL_PERFECT_LAYERS,
7576
));
7677

78+
spawn_default_stars(&mut commands, &asset_server);
79+
7780
time.unpause();
7881
}
7982

83+
fn spawn_default_stars(commands: &mut Commands, asset_server: &Res<AssetServer>) {
84+
let star_count = 100;
85+
for _ in 0..star_count {
86+
let x = random_range(-400.0..400.0);
87+
let y = random_range(-220.0..220.0);
88+
let variant = random_range(1..=2);
89+
90+
let brightness: f32 = random_range(0.6..1.0);
91+
let brightness = brightness * brightness;
92+
93+
let color = if random_range(0.0..1.0) < 0.5 {
94+
Color::linear_rgb(brightness, brightness * 0.9, brightness * 0.7) // warmer star
95+
} else {
96+
Color::linear_rgb(brightness * 0.8, brightness * 0.9, brightness) // cooler star
97+
};
98+
99+
commands.spawn((
100+
Name::new("Star"),
101+
Star {
102+
speed: random_range(0.5..=2.0),
103+
},
104+
Sprite {
105+
image: asset_server.load(format!("sprites/stars/star{variant}.png")),
106+
color,
107+
..default()
108+
},
109+
Transform {
110+
translation: Vec3::new(x, y, -10.0),
111+
..default()
112+
},
113+
DespawnOnExit(Screen::Game),
114+
PIXEL_PERFECT_LAYERS,
115+
));
116+
}
117+
}
118+
80119
fn despawn_game_screen(
81120
mut commands: Commands,
82121
player: Query<Entity, With<Player>>,
@@ -111,6 +150,11 @@ struct Player {
111150
#[derive(Component)]
112151
struct Projectile;
113152

153+
#[derive(Component)]
154+
struct Star {
155+
pub speed: f32,
156+
}
157+
114158
#[derive(Component)]
115159
struct Asteroid {
116160
pub speed: f32,
@@ -260,6 +304,17 @@ fn update_asteroids(
260304
}
261305
}
262306

307+
fn update_stars(mut stars: Query<(&mut Transform, &mut Star)>) {
308+
for (mut transform, mut star) in stars.iter_mut() {
309+
transform.translation -= Vec3::Y * star.speed;
310+
if transform.translation.y < -240.0 {
311+
transform.translation.y = 240.0 + random_range(0.0..20.0);
312+
transform.translation.x = random_range(-400.0..400.0);
313+
star.speed = random_range(0.5..=1.5);
314+
}
315+
}
316+
}
317+
263318
fn spawn_asteroid(
264319
commands: &mut Commands,
265320
asset_server: &Res<AssetServer>,

0 commit comments

Comments
 (0)