Skip to content

Commit d050f0c

Browse files
committed
Fix #7. Queen and bishop now move like they should.
Problem was that the diagonal move making did not work like it should at all. To fix this the struct RadialDiagonal was created
1 parent 83069a5 commit d050f0c

File tree

4 files changed

+65
-14
lines changed

4 files changed

+65
-14
lines changed

src/cell.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::ops::{Index, IndexMut};
44
use bevy::prelude::*;
55

66
use crate::gamemanager::Palette;
7-
use crate::utils::{self, CartesianDirection, RadialDirection};
7+
use crate::utils::{self, CartesianDirection, RadialDiagonal, RadialDirection};
88

99
#[derive(Clone, Debug)]
1010
pub(crate) struct Cell {
@@ -85,7 +85,7 @@ impl CellCoordinates {
8585
/// than the first
8686
pub(crate) fn get_cell_in_direction(
8787
&self,
88-
direction: utils::CartesianDirection,
88+
direction: CartesianDirection,
8989
cube_side_length: u32,
9090
) -> Option<(CellCoordinates, bool)> {
9191
let normal = self.normal_direction();
@@ -140,6 +140,8 @@ impl CellCoordinates {
140140
Some((adjacent, folded_to_other_face))
141141
}
142142

143+
/// Returns a tuple where the second element denotes if the new cell is on a different side
144+
/// than the first
143145
pub(crate) fn get_cell_in_radial_direction(
144146
&self,
145147
radial_direction: RadialDirection,
@@ -159,8 +161,7 @@ impl CellCoordinates {
159161
}
160162

161163
/// Gets the diagonal that can be reached by walking in the cartesian directions consecutively,
162-
/// does not return true neighbors. The second element of the second element denotes if the new
163-
/// cell is on a different side than the first
164+
/// does not return true neighbors. The second element of the returned tuple denotes if the move crosses an edge
164165
pub(crate) fn get_diagonal(
165166
&self,
166167
diagonal: (CartesianDirection, CartesianDirection),
@@ -179,6 +180,36 @@ impl CellCoordinates {
179180
Some((cell2.0, cell1.1 || cell2.1))
180181
}
181182

183+
/// Gets the diagonal that can be reached by walking in the radial directions consecutively,
184+
/// does not return true neighbors. The second element of the returned tuple denotes if the move crosses an edge
185+
pub(crate) fn get_diagonal_radial(
186+
&self,
187+
diagonal: RadialDiagonal,
188+
cube_side_length: u32,
189+
) -> Option<(CellCoordinates, bool)> {
190+
let mut directions = Vec::new();
191+
if self.x != 0 {
192+
directions.push(CartesianDirection::from_axis_num(0, diagonal.0));
193+
}
194+
if self.y != 0 {
195+
directions.push(CartesianDirection::from_axis_num(1, diagonal.1));
196+
}
197+
if self.z != 0 {
198+
directions.push(CartesianDirection::from_axis_num(2, diagonal.2));
199+
}
200+
201+
let Some(dir1) = directions.get(0) else {
202+
error!("Directions vector is not large enough in get_diagonal_radial. This should not happen.");
203+
return None;
204+
};
205+
let Some(dir2) = directions.get(1) else {
206+
error!("Directions vector is not large enough in get_diagonal_radial. This should not happen.");
207+
return None;
208+
};
209+
let directions = (*dir1, *dir2);
210+
211+
self.get_diagonal(directions, cube_side_length)
212+
}
182213
pub(crate) fn normal_direction(&self) -> CartesianDirection {
183214
if self.z == 0 {
184215
if self.normal_is_positive {

src/cube_rotation.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ pub(crate) fn iterate(
121121

122122
input_handling(input, rotation_data);
123123

124-
dbg!(&rotation_data);
125-
126124
// Apply the rotation
127125
for mut camera in &mut query {
128126
let mut transform = camera.0;

src/movement.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ mod parts {
161161

162162
use crate::cell::{Board, CellCoordinates};
163163
use crate::units::Units;
164-
use crate::utils::{CartesianDirection, RadialDirection};
164+
use crate::utils::{RadialDiagonal, RadialDirection};
165165

166166
pub(crate) fn get_straight(
167167
coords: CellCoordinates,
@@ -231,35 +231,38 @@ mod parts {
231231
units: &Units,
232232
) -> Vec<CellCoordinates> {
233233
let mut output = Vec::new();
234-
for diagonal in CartesianDirection::diagonals() {
234+
for diagonal in RadialDiagonal::diagonals() {
235235
let mut latest_cell = coords;
236236
let mut dist = 0;
237237
let mut edge_crossings = 0;
238238
loop {
239-
let Some(next_cell) = latest_cell.get_diagonal(diagonal, cube_side_length) else {
239+
let Some((next_cell, move_crosses_edge)) =
240+
latest_cell.get_diagonal_radial(diagonal, cube_side_length)
241+
else {
240242
break;
241243
};
242244

243-
if output.iter().any(|cell| *cell == next_cell.0) {
245+
// Check for loops
246+
if output.iter().any(|cell| *cell == next_cell) {
244247
break;
245248
}
246249

247250
dist += 1;
248-
if next_cell.1 {
251+
if move_crosses_edge {
249252
edge_crossings += 1;
250253
}
251254

252255
if dist > max_dist || edge_crossings > max_edge_crossings {
253256
break;
254257
}
255258

256-
output.push(next_cell.0);
259+
output.push(next_cell);
257260

258-
if units.is_unit_at(next_cell.0) {
261+
if units.is_unit_at(next_cell) {
259262
break;
260263
}
261264

262-
latest_cell = next_cell.0;
265+
latest_cell = next_cell;
263266
}
264267
}
265268
output

src/utils.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ pub(crate) fn first_nonzero_component(v: Vec3) -> Option<u32> {
2828
None
2929
}
3030

31+
/// Returns the indices of the components that are non-zero
3132
pub(crate) fn nonzero_components(v: Vec3) -> Vec<u32> {
3233
let mut output = Vec::new();
3334
for i in 0..3 {
@@ -38,6 +39,24 @@ pub(crate) fn nonzero_components(v: Vec3) -> Vec<u32> {
3839
output
3940
}
4041

42+
/// Used to represent a diagonal in the cube. The three booleans represent the diagonal axis
43+
#[derive(Clone, Copy, Debug)]
44+
pub(crate) struct RadialDiagonal(pub(crate) bool, pub(crate) bool, pub(crate) bool);
45+
46+
impl RadialDiagonal {
47+
pub(crate) fn diagonals() -> Vec<RadialDiagonal> {
48+
let mut out = Vec::new();
49+
for i in [false, true] {
50+
for j in [false, true] {
51+
for k in [false, true] {
52+
out.push(Self(i, j, k));
53+
}
54+
}
55+
}
56+
out
57+
}
58+
}
59+
4160
#[derive(Debug, Clone, Copy, PartialEq)]
4261
pub(crate) enum RadialDirection {
4362
ClockwiseX,

0 commit comments

Comments
 (0)