Skip to content

Commit dea665b

Browse files
committed
change input system
1 parent 73fc1a4 commit dea665b

2 files changed

Lines changed: 58 additions & 19 deletions

File tree

solitaire-game/src/board.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::ops::{Add, Mul, Sub};
2+
13
use bevy::{ecs::entity_disabling::Disabled, prelude::*};
24
use bevy_vector_shapes::{prelude::ShapePainter, shapes::DiscPainter};
35
use solitaire_solver::{Board, Idx};
@@ -36,6 +38,36 @@ pub struct BoardPosition {
3638
pub y: Idx,
3739
}
3840

41+
impl Sub for BoardPosition {
42+
type Output = Self;
43+
44+
fn sub(self, rhs: Self) -> Self::Output {
45+
let x = self.x - rhs.x;
46+
let y = self.y - rhs.y;
47+
Self { x, y }
48+
}
49+
}
50+
51+
impl Add for BoardPosition {
52+
type Output = Self;
53+
54+
fn add(self, rhs: Self) -> Self::Output {
55+
let x = self.x + rhs.x;
56+
let y = self.y + rhs.y;
57+
Self { x, y }
58+
}
59+
}
60+
61+
impl Mul<Idx> for BoardPosition {
62+
type Output = Self;
63+
64+
fn mul(self, rhs: Idx) -> Self::Output {
65+
let x = self.x * rhs;
66+
let y = self.y * rhs;
67+
Self { x, y }
68+
}
69+
}
70+
3971
#[derive(Event)]
4072
struct MovePeg {
4173
mov: solitaire_solver::Move,

solitaire-game/src/input.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use bevy::{
33
prelude::*,
44
window::PrimaryWindow,
55
};
6-
use solitaire_solver::Board;
76

87
use crate::{
98
Selected,
@@ -44,10 +43,8 @@ fn grab_peg(
4443
if let Some(cursor_pos) = window.cursor_position() {
4544
if let Some(world_pos_cursor) = viewport_to_world(cursor_pos, camera, camera_transform) {
4645
let board_pos = BoardPosition::from_world_space(world_pos_cursor.xy());
47-
if Board::inbounds(board_pos.into()) {
48-
if let Some(peg) = pegs.iter().find(|(_, p)| **p == board_pos).map(|(p, _)| p) {
49-
commands.entity(peg).insert(Selected);
50-
}
46+
if let Some(peg) = pegs.iter().find(|(_, p)| **p == board_pos).map(|(p, _)| p) {
47+
commands.entity(peg).insert(Selected);
5148
}
5249
};
5350
}
@@ -64,11 +61,7 @@ fn release_peg(
6461
if let Some(world_pos_cursor) = viewport_to_world(cursor_pos, camera, camera_transform) {
6562
let board_pos = BoardPosition::from_world_space(world_pos_cursor.xy());
6663
for (selected_peg, &current_pos) in selected_pegs {
67-
commands.entity(selected_peg).remove::<Selected>();
68-
commands.trigger(RequestPegMove {
69-
src: current_pos,
70-
dst: board_pos,
71-
});
64+
move_peg(&mut commands, selected_peg, current_pos, board_pos);
7265
}
7366
};
7467
}
@@ -85,23 +78,37 @@ fn peg_selection_touch(
8578
for touch in touches.iter_just_pressed() {
8679
if let Some(world_pos) = viewport_to_world(touch.position(), camera, camera_transform) {
8780
let board_pos = BoardPosition::from_world_space(world_pos.xy());
88-
if Board::inbounds(board_pos.into()) {
89-
if let Some(peg) = pegs.iter().find(|(_, p)| **p == board_pos).map(|(p, _)| p) {
90-
commands.entity(peg).insert(Selected);
91-
}
81+
if let Some(peg) = pegs.iter().find(|(_, p)| **p == board_pos).map(|(p, _)| p) {
82+
commands.entity(peg).insert(Selected);
9283
}
9384
}
9485
}
9586
for touch in touches.iter_just_released() {
9687
if let Some(world_pos) = viewport_to_world(touch.position(), camera, camera_transform) {
9788
let board_pos = BoardPosition::from_world_space(world_pos.xy());
9889
for (selected_peg, &current_pos) in selected_pegs {
99-
commands.entity(selected_peg).remove::<Selected>();
100-
commands.trigger(RequestPegMove {
101-
src: current_pos,
102-
dst: board_pos,
103-
});
90+
move_peg(&mut commands, selected_peg, current_pos, board_pos);
10491
}
10592
}
10693
}
10794
}
95+
96+
fn move_peg(commands: &mut Commands, selected: Entity, src: BoardPosition, dst: BoardPosition) {
97+
let diff = dst - src;
98+
let diff = BoardPosition {
99+
x: if diff.x == 0 { 0 } else { diff.x.signum() },
100+
y: if diff.y == 0 { 0 } else { diff.y.signum() },
101+
};
102+
if diff.x > diff.y {
103+
commands.trigger(RequestPegMove {
104+
src,
105+
dst: src + diff * 2,
106+
});
107+
} else if diff.y > diff.x {
108+
commands.trigger(RequestPegMove {
109+
src: src,
110+
dst: src + diff * 2,
111+
});
112+
}
113+
commands.entity(selected).remove::<Selected>();
114+
}

0 commit comments

Comments
 (0)