diff options
author | Tom Barrett <tom@tombarrett.xyz> | 2021-03-20 14:00:10 +0100 |
---|---|---|
committer | Tom Barrett <tom@tombarrett.xyz> | 2021-03-20 14:00:10 +0100 |
commit | baad8b54bdff624b648af7bcf977f14711ff59bc (patch) | |
tree | a90d7f04039212a3841e5d1bf1ac5b7e1f3f0b36 /src/main.rs | |
parent | ec95776771d061c21af17686491181c78b7955c9 (diff) |
populate top row with new gems
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 72 |
1 files changed, 59 insertions, 13 deletions
diff --git a/src/main.rs b/src/main.rs index acecaad..f3d41b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,44 @@ use bevy::math::Vec3; use bevy::prelude::*; -use gems::constants; +use rand::{ + distributions::{Distribution, Standard}, + Rng, +}; +#[derive(Debug, PartialEq)] enum Occupant { None, Green, + Yellow, + Red, + Diamond, } +impl Occupant { + pub fn to_index(&self) -> u32 { + match self { + Occupant::Green => 0, + Occupant::Yellow => 1, + Occupant::Red => 2, + Occupant::Diamond => 3, + Occupant::None => 13, + } + } +} + +impl Distribution<Occupant> for Standard { + fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Occupant { + match rng.gen_range(0..=3) { + 0 => Occupant::Green, + 1 => Occupant::Yellow, + 2 => Occupant::Diamond, + 3 => Occupant::Red, + _ => Occupant::None, + } + } +} + +#[derive(Debug)] struct Cell { x: u8, y: u8, @@ -23,6 +55,17 @@ impl Cell { } } +fn cell_falling_system(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite)>) { + for (mut cell, mut sprite) in cell_query.iter_mut() { + if cell.occupant == Occupant::None { + if cell.y == 7 { + cell.occupant = rand::random(); + sprite.index = cell.occupant.to_index(); + } + } + } +} + pub fn setup( commands: &mut Commands, asset_server: Res<AssetServer>, @@ -81,20 +124,22 @@ pub fn setup( for i in 0..8 { for j in 0..8 { - commands.spawn(SpriteSheetBundle { - sprite: TextureAtlasSprite::new(1), - texture_atlas: atlas_handle.clone(), - transform: Transform { - translation: Vec3 { - x: ((i as f32) * 16.0 * 3.5) - 320.0, - y: ((j as f32) * 16.0 * 3.5) - 170.0, - z: 0.0, + commands + .spawn(SpriteSheetBundle { + texture_atlas: atlas_handle.clone(), + sprite: TextureAtlasSprite::new(11), + transform: Transform { + 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, + }, + scale: Vec3::splat(3.5), + ..Default::default() }, - scale: Vec3::splat(3.5), ..Default::default() - }, - ..Default::default() - }); + }) + .with(Cell::new(i, j)); } } } @@ -103,6 +148,7 @@ pub struct GemsPlugin; impl Plugin for GemsPlugin { fn build(&self, app: &mut AppBuilder) { app.add_startup_system(setup.system()); + app.add_system(cell_falling_system.system()); } } |