summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main.rs57
1 files changed, 53 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs
index bb65f81..d9a468f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -52,6 +52,8 @@ struct Cell {
x: usize,
y: usize,
occupant: Occupant,
+ selected: bool,
+ hovered: bool,
}
impl Cell {
@@ -60,6 +62,8 @@ impl Cell {
x,
y,
occupant: Occupant::None,
+ selected: false,
+ hovered: false,
}
}
}
@@ -283,7 +287,8 @@ pub fn setup(
fn mouse_system(
windows: Res<Windows>,
- mut cell_query: Query<(&Cell, &mut Transform, &mut TextureAtlasSprite)>,
+ mut cell_query: Query<(&mut Cell, &mut Transform, &mut TextureAtlasSprite)>,
+ mouse_button_input: Res<Input<MouseButton>>,
) {
if let Some(mut cursor_position) = windows
.get_primary()
@@ -292,17 +297,61 @@ fn mouse_system(
cursor_position.x -= 400.0 - 16.0 * 3.5 * 0.5;
cursor_position.y -= 300.0 - 16.0 * 3.5 * 0.5;
- for (_, _, mut sprite) in cell_query.iter_mut() {
- sprite.color.set_a(1.0);
+ for (cell, _, mut sprite) in cell_query.iter_mut() {
+ if cell.selected {
+ sprite.color.set_a(0.2);
+ } else if cell.hovered {
+ sprite.color.set_a(0.5);
+ } else {
+ sprite.color.set_a(1.0);
+ }
}
- for (_, transform, mut sprite) in cell_query.iter_mut() {
+ for (mut cell, transform, mut sprite) in cell_query.iter_mut() {
if transform.translation.x < cursor_position.x
&& transform.translation.x + 16.0 * 3.5 > cursor_position.x
&& transform.translation.y < cursor_position.y
&& transform.translation.y + 16.0 * 3.5 > cursor_position.y
{
+ cell.hovered = true;
sprite.color.set_a(0.5);
+ if mouse_button_input.just_pressed(MouseButton::Left) {
+ cell.selected = true;
+ }
+ } else {
+ cell.hovered = false;
+ }
+ }
+
+ if mouse_button_input.just_released(MouseButton::Left) {
+ let mut cells: Vec<Cell> = Vec::new();
+ for (cell, _, _) in cell_query.iter_mut() {
+ cells.push(*cell);
+ }
+ if let Some(mut selected) = cells.clone().iter_mut().find(|c| c.selected) {
+ if let Some(mut hovered) = cells.iter_mut().find(|c| c.hovered) {
+ if (selected.x == hovered.x + 1 && selected.y == hovered.y)
+ || (selected.x == hovered.x - 1 && selected.y == hovered.y)
+ || (selected.y == hovered.y + 1 && selected.x == hovered.x)
+ || (selected.y == hovered.y - 1 && selected.x == hovered.x)
+ {
+ let tmp = selected.occupant;
+ selected.occupant = hovered.occupant;
+ hovered.occupant = tmp;
+ for (mut cell, _, mut sprite) in cell_query.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 cell_query.iter_mut() {
+ cell.selected = false;
}
}
}