Simple piece with rotation/orientation

main
Elijah Voigt 3 weeks ago
parent 173a03a7dd
commit a9b50512e6

@ -9,16 +9,26 @@ fn main() {
..default() ..default()
}) })
.add_systems(Startup, init_pieces) .add_systems(Startup, init_pieces)
.add_systems(Update, (kb_movement.run_if(on_event::<KeyboardInput>), update_position)) .add_systems(
Update,
(
kb_movement.run_if(on_event::<KeyboardInput>),
update_position,
update_orientation,
),
)
.add_systems(Update, draw_grid) .add_systems(Update, draw_grid)
.add_systems(Update, zoom_camera)
.run(); .run();
} }
const SCALE: f32 = 30.0; const SCALE: f32 = 30.0;
#[derive(Component, Default, Debug, Clone, Copy)] #[derive(Component, Default, Debug, Clone, Copy)]
struct GridPosition { x: isize, y: isize } #[require(Transform, Visibility)]
struct GridPosition {
x: isize,
y: isize,
}
impl From<&GridPosition> for Vec3 { impl From<&GridPosition> for Vec3 {
fn from(GridPosition { x, y }: &GridPosition) -> Vec3 { fn from(GridPosition { x, y }: &GridPosition) -> Vec3 {
@ -36,7 +46,51 @@ impl std::ops::Add for GridPosition {
type Output = Self; type Output = Self;
fn add(self, GridPosition { x: x2, y: y2 }: Self) -> Self { fn add(self, GridPosition { x: x2, y: y2 }: Self) -> Self {
GridPosition { x: self.x + x2, y: self.y + y2 } GridPosition {
x: self.x + x2,
y: self.y + y2,
}
}
}
#[derive(Component, Default, Debug)]
enum Orientation {
#[default]
Up,
Left,
Down,
Right,
}
impl Orientation {
fn next(&self) -> Self {
match self {
Self::Up => Self::Left,
Self::Left => Self::Down,
Self::Down => Self::Right,
Self::Right => Self::Up,
}
}
fn prev(&self) -> Self {
match self {
Self::Up => Self::Right,
Self::Right => Self::Down,
Self::Down => Self::Left,
Self::Left => Self::Up,
}
}
}
impl From<&Orientation> for Quat {
fn from(other: &Orientation) -> Quat {
let z = match other {
Orientation::Up => 0.0,
Orientation::Left => -PI * 0.5,
Orientation::Down => -PI,
Orientation::Right => -PI * 1.5,
};
Quat::from_rotation_z(z)
} }
} }
@ -45,74 +99,83 @@ fn init_pieces(
mut meshes: ResMut<Assets<Mesh>>, mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<ColorMaterial>>, mut materials: ResMut<Assets<ColorMaterial>>,
) { ) {
commands.spawn(( commands
Mesh2d(meshes.add(Rectangle::new(SCALE, SCALE))), .spawn((Orientation::default(), GridPosition::default()))
MeshMaterial2d(materials.add(ColorMaterial { .with_children(|parent| {
color: WHITE.into(), let mat = materials.add(ColorMaterial {
..default() color: WHITE.into(),
})), ..default()
Transform::default(), });
GridPosition::default(), parent.spawn((
)); Mesh2d(meshes.add(Rectangle::new(SCALE, SCALE))),
MeshMaterial2d(mat.clone()),
Transform::from_xyz(0.0, 0.0, 0.0),
));
parent.spawn((
Mesh2d(meshes.add(Rectangle::new(SCALE, SCALE))),
MeshMaterial2d(mat.clone()),
Transform::from_xyz(SCALE, 0.0, 0.0),
));
parent.spawn((
Mesh2d(meshes.add(Rectangle::new(SCALE, SCALE))),
MeshMaterial2d(mat.clone()),
Transform::from_xyz(0.0, SCALE, 0.0),
));
});
} }
fn update_position( fn update_position(mut query: Query<(&GridPosition, &mut Transform), Changed<GridPosition>>) {
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(); let tmp: Vec3 = gp.into();
info!("Updating position {:?}", tmp); debug!("Updating position {:?}", tmp);
t.translation = gp.into(); t.translation = gp.into();
}); });
} }
fn kb_movement(mut events: EventReader<KeyboardInput>, mut query: Query<&mut GridPosition>) { fn update_orientation(mut query: Query<(&Orientation, &mut Transform), Changed<Orientation>>) {
events.read().for_each(|KeyboardInput { key_code, state, .. }| { query.iter_mut().for_each(|(o, mut t)| {
if let ButtonState::Pressed = state { t.rotation = o.into();
let diff: GridPosition = match key_code { debug!("Setting orientation to {:?}", o);
KeyCode::ArrowUp => {
(0, 1)
},
KeyCode::ArrowDown => {
(0, -1)
},
KeyCode::ArrowLeft => {
(-1, 0)
},
KeyCode::ArrowRight => {
(1, 0)
},
_ => (0, 0)
}.into();
query.iter_mut().for_each(|mut gp| {
info!("Moving by {:?}", diff);
*gp = *gp + diff;
});
}
}); });
} }
fn draw_grid( fn kb_movement(mut events: EventReader<KeyboardInput>, mut query: Query<(&mut GridPosition, &mut Orientation)>) {
mut gizmos: Gizmos, events.read().for_each(
) { |KeyboardInput {
gizmos.grid_2d( key_code, state, ..
Isometry2d::IDENTITY, }| {
UVec2::new(10, 20), if let ButtonState::Pressed = state {
Vec2::new(SCALE, SCALE), let diff: GridPosition = match key_code {
GREEN KeyCode::ArrowUp => (0, 1),
).outer_edges(); KeyCode::ArrowDown => (0, -1),
KeyCode::ArrowLeft => (-1, 0),
KeyCode::ArrowRight => (1, 0),
_ => (0, 0),
}
.into();
query.iter_mut().for_each(|(mut gp, _)| {
debug!("Moving by {:?}", diff);
*gp = *gp + diff;
});
if let KeyCode::Enter = key_code {
query.iter_mut().for_each(|(_, mut o)| {
*o = o.next();
});
}
}
},
);
} }
fn zoom_camera( fn draw_grid(mut gizmos: Gizmos) {
mut events: EventReader<MouseWheel>, gizmos
mut query: Single<&mut Transform, With<Camera>>, .grid_2d(
) { Isometry2d::IDENTITY,
events.read().for_each(|MouseWheel { unit, y, .. }| { UVec2::new(10, 20),
let movement = match unit { Vec2::new(SCALE, SCALE),
MouseScrollUnit::Line => y * 1.0, GREEN,
MouseScrollUnit::Pixel => y * 1.0, )
}; .outer_edges();
query.translation.z += movement;
});
} }

Loading…
Cancel
Save