diff options
author | Tom Barrett <tom@tombarrett.xyz> | 2021-03-20 16:11:32 +0100 |
---|---|---|
committer | Tom Barrett <tom@tombarrett.xyz> | 2021-03-20 16:11:32 +0100 |
commit | c657649cdc1a4159f2c76852b32fc563d1279509 (patch) | |
tree | 30844352efb07cdcfd28811ace8ee644818ebb9c /src/main.rs | |
parent | 4fe231fcc252f19e3fa5e50d369571f1caf6ad33 (diff) |
added check system
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 106 |
1 files changed, 92 insertions, 14 deletions
diff --git a/src/main.rs b/src/main.rs index 56f2ea9..c81203f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,25 @@ use bevy::math::Vec3; use bevy::prelude::*; +use gems::constants; use rand::{ distributions::{Distribution, Standard}, Rng, }; -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Clone, Copy)] enum Occupant { None, Green, Yellow, Red, Diamond, + Explosion, +} + +impl Default for Occupant { + fn default() -> Occupant { + Occupant::None + } } impl Occupant { @@ -21,6 +29,7 @@ impl Occupant { Occupant::Yellow => 1, Occupant::Red => 2, Occupant::Diamond => 3, + Occupant::Explosion => 4, Occupant::None => 13, } } @@ -38,15 +47,15 @@ impl Distribution<Occupant> for Standard { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default, Copy)] struct Cell { - x: u8, - y: u8, + x: usize, + y: usize, occupant: Occupant, } impl Cell { - pub fn new(x: u8, y: u8) -> Cell { + pub fn new(x: usize, y: usize) -> Cell { Cell { x, y, @@ -57,10 +66,78 @@ impl Cell { fn cell_insert_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(); + if cell.occupant == Occupant::None && cell.y == constants::ROWS - 1 { + cell.occupant = rand::random(); + sprite.index = cell.occupant.to_index(); + } + } +} + +fn cell_check_system(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite)>) { + let mut cells = [[Cell::default(); constants::COLUMNS]; constants::ROWS]; + for (cell, _) in cell_query.iter_mut() { + cells[cell.x][cell.y] = *cell; + } + + let mut last = Occupant::None; + let mut connected = Vec::new(); + + for (i, _) in cells.iter().enumerate() { + let mut c = Vec::new(); + for j in 0..constants::ROWS { + if cells[i][j].occupant == last && last != Occupant::None { + c.push((i, j)); + c.push((i, j - 1)); + } else { + connected.push(c.clone()); + c.clear(); + } + last = cells[i][j].occupant; + } + connected.push(c); + last = Occupant::None; + } + + for c in connected.iter() { + if c.len() > 3 { + for (i, j) in c.iter() { + for (mut cell, mut sprite) in cell_query.iter_mut() { + if &cell.x == i && &cell.y == j { + cell.occupant = Occupant::Explosion; + sprite.index = cell.occupant.to_index(); + } + } + } + } + } + + connected.clear(); + + for (i, row) in cells.iter().enumerate() { + let mut c = Vec::new(); + for (j, _) in row.iter().enumerate() { + if cells[j][i].occupant == last && last != Occupant::None { + c.push((j, i)); + c.push((j - 1, i)); + } else { + connected.push(c.clone()); + c.clear(); + } + last = cells[j][i].occupant; + } + connected.push(c); + last = Occupant::None; + } + + for c in connected.iter() { + if c.len() > 3 { + for (i, j) in c.iter() { + for (mut cell, mut sprite) in cell_query.iter_mut() { + if &cell.x == i && &cell.y == j { + cell.occupant = Occupant::Explosion; + sprite.index = cell.occupant.to_index(); + } + } } } } @@ -70,7 +147,7 @@ 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() { if cell.occupant != Occupant::None { - have_gems.push(cell.clone()); + have_gems.push(*cell); } } @@ -81,9 +158,9 @@ fn cell_falling_system(mut cell_query: Query<(&mut Cell, &mut TextureAtlasSprite .iter() .find(|&c| (c.x, c.y) == (cell.x, cell.y + 1)) { - cell.occupant = c.occupant.clone(); + cell.occupant = c.occupant; sprite.index = cell.occupant.to_index(); - moved_gems.push(c.clone()); + moved_gems.push(c); } } } @@ -152,8 +229,8 @@ pub fn setup( ..Default::default() }); - for i in 0..8 { - for j in 0..8 { + for i in 0..constants::COLUMNS { + for j in 0..constants::ROWS { commands .spawn(SpriteSheetBundle { texture_atlas: atlas_handle.clone(), @@ -180,6 +257,7 @@ impl Plugin for GemsPlugin { app.add_startup_system(setup.system()); app.add_system(cell_insert_system.system()); app.add_system(cell_falling_system.system()); + app.add_system(cell_check_system.system()); } } |