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 +++++++++++++++++++++++++++++------------------ src/constants.rs | 12 ++++++------ src/main.rs | 45 ++++++++++++++++++++++++--------------------- 3 files changed, 59 insertions(+), 45 deletions(-) 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); } } } diff --git a/src/constants.rs b/src/constants.rs index 7eed139..86bbe5e 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -19,17 +19,17 @@ pub const TILESHEET_PURPLE: u32 = 9; pub const TILESHEET_STAR1: u32 = 10; pub const TILESHEET_STAR2: u32 = 11; pub const TILESHEET_STAR3: u32 = 12; -pub const TILESHEET_NONE1: u32 = 13; -pub const TILESHEET_NONE2: u32 = 14; +//pub const TILESHEET_X: u32 = 13; +//pub const TILESHEET_X: u32 = 14; pub const TILESHEET_COSMONAUT1: u32 = 15; pub const TILESHEET_VISOR1: u32 = 16; pub const TILESHEET_VISOR2: u32 = 17; -pub const TILESHEET_NONE3: u32 = 18; -pub const TILESHEET_NONE4: u32 = 19; +//pub const TILESHEET_X: u32 = 18; +//pub const TILESHEET_X: u32 = 19; pub const TILESHEET_COSMONAUT2: u32 = 20; pub const TILESHEET_VISOR3: u32 = 21; pub const TILESHEET_VISOR4: u32 = 22; -pub const TILESHEET_NONE6: u32 = 23; -pub const TILESHEET_NONE7: u32 = 24; +//pub const TILESHEET_X: u32 = 23; +//pub const TILESHEET_X: u32 = 24; diff --git a/src/main.rs b/src/main.rs index 8a59000..c69d791 100644 --- a/src/main.rs +++ b/src/main.rs @@ -100,7 +100,10 @@ pub fn setup( commands .spawn_bundle(SpriteSheetBundle { texture_atlas: atlas_handle.clone(), - sprite: TextureAtlasSprite::new(constants::TILESHEET_NONE1), + visible: Visible { + is_visible: false, + is_transparent: true, + }, transform: Transform { translation: Vec3::new( ((i as f32) * constants::TILE_SIZE * constants::TILE_SCALE) - 330.0, @@ -125,35 +128,35 @@ fn animation( Option<&mut Cell>, &mut Timer, &mut TextureAtlasSprite, + &mut Visible, )>, ) { - for (entity, cell, mut timer, mut sprite) in q.iter_mut() { + for (entity, cell, mut timer, mut sprite, mut visible) in q.iter_mut() { timer.tick(time.delta()); if timer.finished() { let index = match sprite.index { - constants::TILESHEET_EXPLOSION1 => constants::TILESHEET_EXPLOSION2, - constants::TILESHEET_EXPLOSION2 => constants::TILESHEET_EXPLOSION3, - constants::TILESHEET_EXPLOSION3 => constants::TILESHEET_EXPLOSION4, + constants::TILESHEET_EXPLOSION1 => Some(constants::TILESHEET_EXPLOSION2), + constants::TILESHEET_EXPLOSION2 => Some(constants::TILESHEET_EXPLOSION3), + constants::TILESHEET_EXPLOSION3 => Some(constants::TILESHEET_EXPLOSION4), + constants::TILESHEET_EXPLOSION4 => Some(constants::TILESHEET_EXPLOSION5), - constants::TILESHEET_VISOR1 => constants::TILESHEET_VISOR2, - constants::TILESHEET_VISOR2 => constants::TILESHEET_VISOR3, - constants::TILESHEET_VISOR3 => constants::TILESHEET_VISOR4, + constants::TILESHEET_VISOR1 => Some(constants::TILESHEET_VISOR2), + constants::TILESHEET_VISOR2 => Some(constants::TILESHEET_VISOR3), + constants::TILESHEET_VISOR3 => Some(constants::TILESHEET_VISOR4), - constants::TILESHEET_STAR1 => constants::TILESHEET_STAR2, - constants::TILESHEET_STAR2 => constants::TILESHEET_STAR3, + constants::TILESHEET_STAR1 => Some(constants::TILESHEET_STAR2), + constants::TILESHEET_STAR2 => Some(constants::TILESHEET_STAR3), - constants::TILESHEET_NONE1 => constants::TILESHEET_NONE2, - _ => constants::TILESHEET_NONE1, + _ => None, }; - sprite.index = index; - if index == constants::TILESHEET_NONE2 { - if let Some(mut cell) = cell { - cell.occupant = Occupant::None; - sprite.index = cell.occupant.to_index(); - commands.entity(entity).remove::(); - } else { - commands.entity(entity).despawn(); - } + + if let Some(index) = index { + sprite.index = index; + } else if let Some(mut cell) = cell { + cell.set_occupant(Occupant::None, &mut sprite, &mut visible); + commands.entity(entity).remove::(); + } else { + commands.entity(entity).despawn(); } } } -- cgit v1.2.3