From a0207d6b7a53fd942f9010287d27336692893da4 Mon Sep 17 00:00:00 2001 From: Tom Barrett Date: Mon, 6 Mar 2023 21:51:36 +0100 Subject: bevy 10 --- src/cell.rs | 8 ++----- src/main.rs | 77 +++++++++++++++++++++++++++++-------------------------------- 2 files changed, 38 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/cell.rs b/src/cell.rs index 7c2385d..0d9b381 100644 --- a/src/cell.rs +++ b/src/cell.rs @@ -115,11 +115,7 @@ impl Cell { ) { self.occupant = occupant; sprite.index = self.occupant.to_index(); - if self.occupant == Occupant::None { - visibility.is_visible = false; - } else { - visibility.is_visible = true; - } + *visibility = Visibility::Inherited; } } @@ -187,7 +183,7 @@ pub fn check(mut q: Query<(&mut Cell, &mut TextureAtlasSprite, &mut Visibility)> last = Occupant::None; } - connected = connected.into_iter().filter(|c| c.len() > 4).collect(); + connected.retain(|c| c.len() > 4); for c in connected.iter() { for (i, j) in c.iter() { diff --git a/src/main.rs b/src/main.rs index e958c16..627fec0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use bevy::math::Vec3; use bevy::prelude::*; +use bevy::window::{PrimaryWindow, WindowResolution}; use gems::cell::{self, AnimationTimer, Cell, Occupant}; use gems::constants; use rand::{thread_rng, Rng}; @@ -109,7 +110,7 @@ pub fn setup( commands .spawn(SpriteSheetBundle { texture_atlas: atlas_handle.clone(), - visibility: Visibility { is_visible: false }, + visibility: Visibility::Inherited, transform: Transform { translation: Vec3::new( ((i as f32) * constants::TILE_SIZE * constants::TILE_SCALE) - 330.0, @@ -170,13 +171,11 @@ fn animation( fn cosmonaut_detect( mut commands: Commands, - windows: Res, mut q: Query<(&Transform, &TextureAtlasSprite, &Handle)>, + mut windows: Query<&Window, With>, ) { - if let Some(mut cursor_position) = windows - .get_primary() - .and_then(|window| window.cursor_position()) - { + let window = windows.single_mut(); + if let Some(mut cursor_position) = window.cursor_position() { cursor_position.x -= (constants::WINDOW_WIDTH / 2.0) - constants::TILE_SIZE * constants::TILE_SCALE * 0.5; cursor_position.y -= @@ -204,14 +203,12 @@ fn cosmonaut_detect( } fn mouse( - windows: Res, mut q: Query<(&mut Cell, &mut Transform, &mut TextureAtlasSprite)>, + mut windows: Query<&Window, With>, mouse_button_input: Res>, ) { - if let Some(mut cursor_position) = windows - .get_primary() - .and_then(|window| window.cursor_position()) - { + let window = windows.single_mut(); + if let Some(mut cursor_position) = window.cursor_position() { cursor_position.x -= (constants::WINDOW_WIDTH / 2.0) - constants::TILE_SIZE * constants::TILE_SCALE * 0.5; cursor_position.y -= @@ -243,37 +240,36 @@ fn mouse( cell.hovered = false; } } + } - if mouse_button_input.just_released(MouseButton::Left) { - let mut cells: Vec = Vec::new(); - for (cell, _, _) in q.iter_mut() { - cells.push(*cell); - } - if let Some(selected) = cells.clone().iter_mut().find(|c| c.selected) { - if let Some(hovered) = cells.iter_mut().find(|c| c.hovered) { - if ((selected.x == hovered.x + 1 - || selected.x == hovered.x.overflowing_sub(1).0) - && selected.y == hovered.y) - || ((selected.y == hovered.y + 1 - || selected.y == hovered.y.overflowing_sub(1).0) - && selected.x == hovered.x) - { - std::mem::swap(&mut selected.occupant, &mut hovered.occupant); - for (mut cell, _, mut sprite) in q.iter_mut() { - if cell.x == selected.x && cell.y == selected.y { - cell.occupant = selected.occupant; - sprite.index = cell.occupant.to_index(); - } else if cell.x == hovered.x && cell.y == hovered.y { - cell.occupant = hovered.occupant; - sprite.index = cell.occupant.to_index(); - } + if mouse_button_input.just_released(MouseButton::Left) { + let mut cells: Vec = Vec::new(); + for (cell, _, _) in q.iter_mut() { + cells.push(*cell); + } + if let Some(selected) = cells.clone().iter_mut().find(|c| c.selected) { + if let Some(hovered) = cells.iter_mut().find(|c| c.hovered) { + if ((selected.x == hovered.x + 1 || selected.x == hovered.x.overflowing_sub(1).0) + && selected.y == hovered.y) + || ((selected.y == hovered.y + 1 + || selected.y == hovered.y.overflowing_sub(1).0) + && selected.x == hovered.x) + { + std::mem::swap(&mut selected.occupant, &mut hovered.occupant); + for (mut cell, _, mut sprite) in q.iter_mut() { + if cell.x == selected.x && cell.y == selected.y { + cell.occupant = selected.occupant; + sprite.index = cell.occupant.to_index(); + } else if cell.x == hovered.x && cell.y == hovered.y { + cell.occupant = hovered.occupant; + sprite.index = cell.occupant.to_index(); } } } } - for (mut cell, _, _) in q.iter_mut() { - cell.selected = false; - } + } + for (mut cell, _, _) in q.iter_mut() { + cell.selected = false; } } } @@ -284,13 +280,12 @@ pub fn main() { DefaultPlugins .set(ImagePlugin::default_nearest()) .set(WindowPlugin { - window: WindowDescriptor { + primary_window: Some(Window { title: "gems".to_string(), - width: constants::WINDOW_WIDTH, - height: constants::WINDOW_HEIGHT, + resolution: WindowResolution::new(constants::WINDOW_WIDTH,constants::WINDOW_HEIGHT), resizable: false, ..default() - }, + }), ..default() }), ) -- cgit v1.2.3