summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorTom Barrett <tom@tombarrett.xyz>2023-03-06 21:51:36 +0100
committerTom Barrett <tom@tombarrett.xyz>2023-03-06 21:51:36 +0100
commita0207d6b7a53fd942f9010287d27336692893da4 (patch)
tree310452333dd87bfa9b670ffbdc956a83802a2b0b /src/main.rs
parentff2f897520704adacc8287b3499167bbcf56deb8 (diff)
bevy 10
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs77
1 files changed, 36 insertions, 41 deletions
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<Windows>,
mut q: Query<(&Transform, &TextureAtlasSprite, &Handle<TextureAtlas>)>,
+ mut windows: Query<&Window, With<PrimaryWindow>>,
) {
- 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<Windows>,
mut q: Query<(&mut Cell, &mut Transform, &mut TextureAtlasSprite)>,
+ mut windows: Query<&Window, With<PrimaryWindow>>,
mouse_button_input: Res<Input<MouseButton>>,
) {
- 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<Cell> = 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<Cell> = 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()
}),
)