Skip is still buggy but just... less so.

main
Elijah Voigt 12 hours ago
parent a73f5ddb05
commit 66c6019ea4

@ -487,8 +487,32 @@ impl Shape {
output
}
fn height(&self) -> usize {
let mut x = 0;
match self {
Self::M4(this) => {
for i in 0..4 {
if this.col(i).to_array().contains(&1.0) {
x += 1
}
}
}
Self::M3(this) => {
for i in 0..3 {
if this.col(i).to_array().contains(&1.0) {
x += 1
}
}
}
};
x
}
}
// TODO: move to trigger
fn update_position(
mut changed: Query<
(Entity, &GridPosition, &mut Transform),
@ -506,9 +530,9 @@ fn update_position(
});
}
// TODO: Inline this to when movement occurs
// TODO: Move to trigger
fn update_shape_blocks(
query: Query<(Entity, &Shape, &GridPosition), Or<(Added<Shape>, Changed<Shape>)>>,
query: Query<(Entity, &Shape, &GridPosition), Or<(Added<Shape>, Changed<Shape>, Added<GridPosition>, Changed<GridPosition>)>>,
mut blocks: Query<&mut GridPosition, (With<ShapeBlock>, Without<Shape>)>,
mut commands: Commands,
visuals: Res<Visuals>,
@ -620,10 +644,11 @@ fn clock_cycle(n: f32) -> impl FnMut(Res<Time>, Local<f32>) -> bool {
}
fn add_piece(mut commands: Commands, mut shapes: ResMut<ShapesBuffer>) {
let this_shape = shapes.next.pop_front().unwrap();
commands
.spawn((
GridPosition::default(),
shapes.next.pop_front().unwrap(),
GridPosition { y: Y_MAX - this_shape.height(), ..default() },
this_shape,
))
.observe(movement);
}
@ -728,7 +753,7 @@ fn movement(
Movement::Left => vec![center.with_offset(-1, 0)],
Movement::Right => vec![center.with_offset(1, 0)],
Movement::Rotate => vec![Ok(*center)],
Movement::Skip => (1..20).map(|i| center.with_offset(0, -i)).collect(),
Movement::Skip => (1..=center.y).map(|i| center.with_offset(0, -(i as isize))).collect(),
};
let new_shape = match trigger.event() {
Movement::Down | Movement::Left | Movement::Right | Movement::Skip => *this_shape,
@ -774,11 +799,12 @@ fn movement(
}
}
}
info!("Checks passed for {position:?}, committing change");
debug!("Checks passed for {position:?}, committing change");
// Update center
let mut gp = grid_positions.get_mut(trigger.target()).unwrap();
*gp = new_center;
// Update shape/rotation
let mut s = shape.get_mut(trigger.target()).unwrap();
*s = new_shape;
@ -809,13 +835,15 @@ fn deactivate_shape(
events.read().for_each(|target| {
parent.iter_descendants(target).for_each(|block| {
let GridPosition { y, .. } = grid_positions.get(block).unwrap();
let parent_line = lines
if let Some(parent_line) = lines
.iter()
.find_map(|(e, Line(i))| (*y == *i).then_some(e))
.unwrap(); // TODO: This crashed once kinda late in a game... why?
commands
.entity(parent_line)
.add_one_related::<LineBlock>(block);
.find_map(|(e, Line(i))| (*y == *i).then_some(e)) {
commands
.entity(parent_line)
.add_one_related::<LineBlock>(block);
} else {
error!("wtf?");
}
});
commands.entity(target).despawn();
});

@ -83,3 +83,10 @@ fn test_coordinates() {
assert_eq!(actual, expected);
}
#[test]
fn test_height() {
assert_eq!(Shape::new_t().height(), 2);
assert_eq!(Shape::new_i().height(), 4);
assert_eq!(Shape::new_l().height(), 3);
}

Loading…
Cancel
Save