From 893805baa14972dc6c1c28170856097bb54bf280 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Sat, 10 Apr 2021 13:06:25 +0200 Subject: redid how visibility works, moved setting occupants to a fucntion, setup animation function better --- src/cell.rs | 47 +++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 18 deletions(-) (limited to 'src/cell.rs') diff --git a/src/cell.rs b/src/cell.rs index 10c11f0..e4be4ac 100644 --- a/src/cell.rs +++ b/src/cell.rs @@ -31,7 +31,7 @@ impl Occupant { Occupant::Blue => constants::TILESHEET_BLUE, Occupant::Purple => constants::TILESHEET_PURPLE, Occupant::Explosion => constants::TILESHEET_EXPLOSION1, - Occupant::None => constants::TILESHEET_NONE1, + Occupant::None => 0, } } } @@ -68,13 +68,27 @@ impl Cell { hovered: false, } } + + pub fn set_occupant( + &mut self, + occupant: Occupant, + sprite: &mut TextureAtlasSprite, + visible: &mut Visible, + ) { + self.occupant = occupant; + sprite.index = self.occupant.to_index(); + if self.occupant == Occupant::None { + visible.is_visible = false; + } else { + visible.is_visible = true; + } + } } -pub fn insert(mut q: Query<(&mut Cell, &mut TextureAtlasSprite)>) { - for (mut cell, mut sprite) in q.iter_mut() { +pub fn insert(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visible)>) { + for (mut cell, mut sprite, mut visible) in q.iter_mut() { if cell.occupant == Occupant::None && cell.y == constants::GRID_SIZE - 1 { - cell.occupant = rand::random(); - sprite.index = cell.occupant.to_index(); + cell.set_occupant(rand::random(), &mut sprite, &mut visible); } } } @@ -89,9 +103,9 @@ pub fn start_explosion(mut commands: Commands, mut q: Query<(Entity, &Cell), Wit } } -pub fn check(mut q: Query<(&mut Cell, &mut TextureAtlasSprite)>) { +pub fn check(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visible)>) { let mut cells = [[Cell::default(); constants::GRID_SIZE]; constants::GRID_SIZE]; - for (cell, _) in q.iter_mut() { + for (cell, _, _) in q.iter_mut() { cells[cell.x][cell.y] = *cell; } @@ -136,42 +150,39 @@ pub fn check(mut q: Query<(&mut Cell, &mut TextureAtlasSprite)>) { for c in connected.iter() { for (i, j) in c.iter() { - for (mut cell, mut sprite) in q.iter_mut() { + for (mut cell, mut sprite, mut visible) in q.iter_mut() { if &cell.x == i && &cell.y == j && cell.occupant != Occupant::Explosion { - cell.occupant = Occupant::Explosion; - sprite.index = cell.occupant.to_index(); + cell.set_occupant(Occupant::Explosion, &mut sprite, &mut visible); } } } } } -pub fn falling(mut q: Query<(&mut Cell, &mut TextureAtlasSprite)>) { +pub fn falling(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visible)>) { let mut have_gems = Vec::new(); - for (cell, _sprite) in q.iter_mut() { + for (cell, _sprite, _visible) in q.iter_mut() { if cell.occupant != Occupant::None { have_gems.push(*cell); } } let mut moved_gems = Vec::new(); - for (mut cell, mut sprite) in q.iter_mut() { + for (mut cell, mut sprite, mut visible) in q.iter_mut() { if cell.occupant == Occupant::None { if let Some(c) = have_gems .iter() .find(|&c| (c.x, c.y) == (cell.x, cell.y + 1)) { - cell.occupant = c.occupant; - sprite.index = cell.occupant.to_index(); + cell.set_occupant(c.occupant, &mut sprite, &mut visible); moved_gems.push(c); } } } - for (mut cell, mut sprite) in q.iter_mut() { + for (mut cell, mut sprite, mut visible) in q.iter_mut() { if moved_gems.iter().any(|c| (c.x, c.y) == (cell.x, cell.y)) { - cell.occupant = Occupant::None; - sprite.index = cell.occupant.to_index(); + cell.set_occupant(Occupant::None, &mut sprite, &mut visible); } } } -- cgit v1.2.3