|
|
|
@ -26,18 +26,49 @@ const SCALE: f32 = 30.0;
|
|
|
|
#[derive(Component, Default, Debug, Clone, Copy)]
|
|
|
|
#[derive(Component, Default, Debug, Clone, Copy)]
|
|
|
|
#[require(Transform, Visibility)]
|
|
|
|
#[require(Transform, Visibility)]
|
|
|
|
struct GridPosition {
|
|
|
|
struct GridPosition {
|
|
|
|
x: isize,
|
|
|
|
x: usize,
|
|
|
|
y: isize,
|
|
|
|
y: usize,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: Restirct movement to within the grid
|
|
|
|
|
|
|
|
impl GridPosition {
|
|
|
|
|
|
|
|
fn move_up(&mut self) {
|
|
|
|
|
|
|
|
if self.y + 1 < 20 {
|
|
|
|
|
|
|
|
self.y = self.y.saturating_add(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn move_down(&mut self) {
|
|
|
|
|
|
|
|
self.y = self.y.saturating_sub(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn move_left(&mut self) {
|
|
|
|
|
|
|
|
self.x = self.x.saturating_sub(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn move_right(&mut self) {
|
|
|
|
|
|
|
|
if self.x + 1 < 10 {
|
|
|
|
|
|
|
|
self.x = self.x.saturating_add(1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<&GridPosition> for Vec3 {
|
|
|
|
impl From<&GridPosition> for Vec3 {
|
|
|
|
fn from(GridPosition { x, y }: &GridPosition) -> Vec3 {
|
|
|
|
fn from(GridPosition { x, y }: &GridPosition) -> Vec3 {
|
|
|
|
Vec3::new((*x as f32) * SCALE, (*y as f32) * SCALE, 0.0)
|
|
|
|
// Grid Positions start in the bottom left of the area
|
|
|
|
|
|
|
|
// So (0, 0) is the bottom left, (0, 9) is the bottom right, etc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let x_0 = -SCALE * 5.0 + (0.5 * SCALE);
|
|
|
|
|
|
|
|
let x = x_0 + ((*x as f32) * SCALE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let y_0 = -SCALE * 10.0 + (0.5 * SCALE);
|
|
|
|
|
|
|
|
let y = y_0 + ((*y as f32) * SCALE);
|
|
|
|
|
|
|
|
Vec3::new(x, y, 0.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
impl From<(isize, isize)> for GridPosition {
|
|
|
|
impl From<(usize, usize)> for GridPosition {
|
|
|
|
fn from((x, y): (isize, isize)) -> GridPosition {
|
|
|
|
fn from((x, y): (usize, usize)) -> GridPosition {
|
|
|
|
GridPosition { x, y }
|
|
|
|
GridPosition { x, y }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -53,6 +84,12 @@ impl std::ops::Add for GridPosition {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl std::ops::AddAssign<&GridPosition> for GridPosition {
|
|
|
|
|
|
|
|
fn add_assign(&mut self, rhs: &GridPosition) {
|
|
|
|
|
|
|
|
*self = *self + *rhs;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Component, Default, Debug)]
|
|
|
|
#[derive(Component, Default, Debug)]
|
|
|
|
enum Orientation {
|
|
|
|
enum Orientation {
|
|
|
|
#[default]
|
|
|
|
#[default]
|
|
|
|
@ -106,30 +143,34 @@ fn init_pieces(
|
|
|
|
color: WHITE.into(),
|
|
|
|
color: WHITE.into(),
|
|
|
|
..default()
|
|
|
|
..default()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
let mesh = meshes.add(Rectangle::new(SCALE, SCALE));
|
|
|
|
parent.spawn((
|
|
|
|
parent.spawn((
|
|
|
|
Mesh2d(meshes.add(Rectangle::new(SCALE, SCALE))),
|
|
|
|
Mesh2d(mesh.clone()),
|
|
|
|
MeshMaterial2d(mat.clone()),
|
|
|
|
MeshMaterial2d(mat.clone()),
|
|
|
|
Transform::from_xyz(0.0, 0.0, 0.0),
|
|
|
|
Transform::from_xyz(0.0, 0.0, 0.0),
|
|
|
|
));
|
|
|
|
));
|
|
|
|
parent.spawn((
|
|
|
|
parent.spawn((
|
|
|
|
Mesh2d(meshes.add(Rectangle::new(SCALE, SCALE))),
|
|
|
|
Mesh2d(mesh.clone()),
|
|
|
|
MeshMaterial2d(mat.clone()),
|
|
|
|
MeshMaterial2d(mat.clone()),
|
|
|
|
Transform::from_xyz(SCALE, 0.0, 0.0),
|
|
|
|
Transform::from_xyz(SCALE, 0.0, 0.0),
|
|
|
|
));
|
|
|
|
));
|
|
|
|
parent.spawn((
|
|
|
|
parent.spawn((
|
|
|
|
Mesh2d(meshes.add(Rectangle::new(SCALE, SCALE))),
|
|
|
|
Mesh2d(mesh.clone()),
|
|
|
|
MeshMaterial2d(mat.clone()),
|
|
|
|
MeshMaterial2d(mat.clone()),
|
|
|
|
Transform::from_xyz(0.0, SCALE, 0.0),
|
|
|
|
Transform::from_xyz(0.0, SCALE, 0.0),
|
|
|
|
));
|
|
|
|
));
|
|
|
|
|
|
|
|
parent.spawn((
|
|
|
|
|
|
|
|
Mesh2d(mesh.clone()),
|
|
|
|
|
|
|
|
MeshMaterial2d(mat.clone()),
|
|
|
|
|
|
|
|
Transform::from_xyz(-SCALE, 0.0, 0.0),
|
|
|
|
|
|
|
|
));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn update_position(mut query: Query<(&GridPosition, &mut Transform), Changed<GridPosition>>) {
|
|
|
|
fn update_position(mut query: Query<(&GridPosition, &mut Transform), Changed<GridPosition>>) {
|
|
|
|
query.iter_mut().for_each(|(gp, mut t)| {
|
|
|
|
query.iter_mut().for_each(|(gp, mut t)| {
|
|
|
|
let tmp: Vec3 = gp.into();
|
|
|
|
|
|
|
|
debug!("Updating position {:?}", tmp);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
t.translation = gp.into();
|
|
|
|
t.translation = gp.into();
|
|
|
|
|
|
|
|
debug!("Updating position {:?}", t.translation);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -146,24 +187,18 @@ fn kb_movement(mut events: EventReader<KeyboardInput>, mut query: Query<(&mut Gr
|
|
|
|
key_code, state, ..
|
|
|
|
key_code, state, ..
|
|
|
|
}| {
|
|
|
|
}| {
|
|
|
|
if let ButtonState::Pressed = state {
|
|
|
|
if let ButtonState::Pressed = state {
|
|
|
|
let diff: GridPosition = match key_code {
|
|
|
|
// TODO: Restict movement based on size/orientation of piece
|
|
|
|
KeyCode::ArrowUp => (0, 1),
|
|
|
|
// Check if children would be outside play area...
|
|
|
|
KeyCode::ArrowDown => (0, -1),
|
|
|
|
query.iter_mut().for_each(|(mut gp, mut o)| {
|
|
|
|
KeyCode::ArrowLeft => (-1, 0),
|
|
|
|
match key_code {
|
|
|
|
KeyCode::ArrowRight => (1, 0),
|
|
|
|
KeyCode::ArrowUp => gp.move_up(),
|
|
|
|
_ => (0, 0),
|
|
|
|
KeyCode::ArrowDown => gp.move_down(),
|
|
|
|
}
|
|
|
|
KeyCode::ArrowLeft => gp.move_left(),
|
|
|
|
.into();
|
|
|
|
KeyCode::ArrowRight => gp.move_right(),
|
|
|
|
query.iter_mut().for_each(|(mut gp, _)| {
|
|
|
|
KeyCode::Enter => *o = o.next(),
|
|
|
|
debug!("Moving by {:?}", diff);
|
|
|
|
_ => ()
|
|
|
|
*gp = *gp + diff;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if let KeyCode::Enter = key_code {
|
|
|
|
|
|
|
|
query.iter_mut().for_each(|(_, mut o)| {
|
|
|
|
|
|
|
|
*o = o.next();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
);
|
|
|
|
);
|
|
|
|
|