diff options
author | Tom Barrett <tom@tombarrett.xyz> | 2021-03-21 17:16:19 +0100 |
---|---|---|
committer | Tom Barrett <tom@tombarrett.xyz> | 2021-03-21 17:16:23 +0100 |
commit | 42c0f017348ef2033b6889b272f31f0c5eb5d41d (patch) | |
tree | 2c06119504aa843382b7696eec910debd3f4688b /src | |
parent | 5198891f3683c656f8e50a99a3a271b4e0734293 (diff) |
added twinkling stars
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 44 |
1 files changed, 37 insertions, 7 deletions
diff --git a/src/main.rs b/src/main.rs index 5577a11..bab21a0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,7 @@ use bevy::prelude::*; use gems::constants; use rand::{ distributions::{Distribution, Standard}, - Rng, + thread_rng, Rng, }; #[derive(Debug, PartialEq, Clone, Copy)] @@ -177,6 +177,33 @@ fn cell_check_system(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite)> } } +fn star_spawning_system(commands: &mut Commands, time: Res<Time>, mut q: Query<&mut Timer>) { + for mut timer in q.iter_mut() { + if !timer.repeating() { + timer.tick(time.delta_seconds()); + if timer.just_finished() { + timer.set_duration(thread_rng().gen_range(2..5) as f32); + timer.reset(); + commands + .spawn(SpriteSheetBundle { + sprite: TextureAtlasSprite::new(16), + transform: Transform { + translation: Vec3 { + x: thread_rng().gen_range(-300..300) as f32, + y: thread_rng().gen_range(-200..300) as f32, + z: 0.0, + }, + scale: Vec3::splat(thread_rng().gen_range(1..3) as f32), + ..Default::default() + }, + ..Default::default() + }) + .with(Timer::from_seconds(0.25, true)); + } + } + } +} + fn cell_falling_system(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite)>) { let mut have_gems = Vec::new(); for (cell, _sprite) in cell_query.iter_mut() { @@ -216,7 +243,7 @@ pub fn setup( let background = asset_server.load("background.png"); let tileset = asset_server.load("tileset.png"); - let atlas = TextureAtlas::from_grid(tileset, Vec2::new(16.0, 16.0), 4, 4); + let atlas = TextureAtlas::from_grid(tileset, Vec2::new(16.0, 16.0), 4, 5); let atlas_handle = texture_atlases.add(atlas); commands @@ -248,6 +275,7 @@ pub fn setup( }, ..Default::default() }) + .spawn((Timer::from_seconds(1.0, false),)) .spawn(SpriteSheetBundle { sprite: TextureAtlasSprite::new(12), texture_atlas: atlas_handle.clone(), @@ -273,7 +301,7 @@ pub fn setup( translation: Vec3 { x: ((i as f32) * 16.0 * 3.5) - 320.0, y: ((j as f32) * 16.0 * 3.5) - 160.0, - z: 0.0, + z: 1.0, }, scale: Vec3::splat(3.5), ..Default::default() @@ -285,7 +313,7 @@ pub fn setup( } } -fn cosmonaut_animation_system( +fn animation_system( commands: &mut Commands, time: Res<Time>, mut cell_query: Query<(Entity, &mut Timer, &mut TextureAtlasSprite), Without<Cell>>, @@ -298,12 +326,13 @@ fn cosmonaut_animation_system( 10 => 13, 13 => 14, 11 => 12, + 16 => 17, + 17 => 18, _ => 11, }; sprite.index = index; if index == 12 { - commands.remove_one::<Timer>(entity); - commands.remove_one::<TextureAtlasSprite>(entity); + commands.despawn(entity); } } } @@ -421,7 +450,8 @@ impl Plugin for GemsPlugin { app.add_system(explosion_animation_system.system()); app.add_system(mouse_system.system()); app.add_system(cosmonaut_detect_system.system()); - app.add_system(cosmonaut_animation_system.system()); + app.add_system(animation_system.system()); + app.add_system(star_spawning_system.system()); } } |