Compare commits

..

No commits in common. '66c6019ea4f1b422c39843b9018c4984e8ba4f07' and '21ad8763e4ed58b0c868da44a456f8ccfb102fe0' have entirely different histories.

@ -42,13 +42,13 @@ fn main() {
add_piece add_piece
.run_if(not(any_with_component::<Shape>)) .run_if(not(any_with_component::<Shape>))
.after(update_next_shapes), .after(update_next_shapes),
update_shape_blocks
.run_if(any_component_added::<Shape>.or(any_component_changed::<Shape>)),
falling falling
.run_if(in_state(GameState::Falling)) .run_if(in_state(GameState::Falling))
.run_if(clock_cycle(1.0)), .run_if(clock_cycle(1.0)),
update_position.run_if(any_component_changed::<GridPosition>), update_position.run_if(any_component_changed::<GridPosition>),
update_shape_blocks deactivate_shape.run_if(any_component_removed::<Shape>),
.run_if(any_component_added::<Shape>.or(any_component_changed::<Shape>)).after(update_position),
deactivate_shape.run_if(any_component_removed::<Shape>).after(update_shape_blocks),
check_line_removal, check_line_removal,
// Clearing lines systems // Clearing lines systems
clear_line.run_if(any_component_changed::<LineBlocks>), clear_line.run_if(any_component_changed::<LineBlocks>),
@ -487,32 +487,8 @@ impl Shape {
output 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( fn update_position(
mut changed: Query< mut changed: Query<
(Entity, &GridPosition, &mut Transform), (Entity, &GridPosition, &mut Transform),
@ -530,9 +506,9 @@ fn update_position(
}); });
} }
// TODO: Move to trigger // TODO: Inline this to when movement occurs
fn update_shape_blocks( fn update_shape_blocks(
query: Query<(Entity, &Shape, &GridPosition), Or<(Added<Shape>, Changed<Shape>, Added<GridPosition>, Changed<GridPosition>)>>, query: Query<(Entity, &Shape, &GridPosition), Or<(Added<Shape>, Changed<Shape>)>>,
mut blocks: Query<&mut GridPosition, (With<ShapeBlock>, Without<Shape>)>, mut blocks: Query<&mut GridPosition, (With<ShapeBlock>, Without<Shape>)>,
mut commands: Commands, mut commands: Commands,
visuals: Res<Visuals>, visuals: Res<Visuals>,
@ -589,9 +565,6 @@ fn kb_input(
KeyCode::ArrowRight => { KeyCode::ArrowRight => {
commands.entity(e).trigger(Movement::Right); commands.entity(e).trigger(Movement::Right);
} }
KeyCode::Space => {
commands.entity(e).trigger(Movement::Skip);
}
KeyCode::Escape => next.set(match curr.get() { KeyCode::Escape => next.set(match curr.get() {
GameState::Falling => GameState::Pause, GameState::Falling => GameState::Pause,
GameState::Pause => GameState::Falling, GameState::Pause => GameState::Falling,
@ -644,11 +617,10 @@ fn clock_cycle(n: f32) -> impl FnMut(Res<Time>, Local<f32>) -> bool {
} }
fn add_piece(mut commands: Commands, mut shapes: ResMut<ShapesBuffer>) { fn add_piece(mut commands: Commands, mut shapes: ResMut<ShapesBuffer>) {
let this_shape = shapes.next.pop_front().unwrap();
commands commands
.spawn(( .spawn((
GridPosition { y: Y_MAX - this_shape.height(), ..default() }, GridPosition::default(),
this_shape, shapes.next.pop_front().unwrap(),
)) ))
.observe(movement); .observe(movement);
} }
@ -733,7 +705,6 @@ enum Movement {
Left, Left,
Right, Right,
Rotate, Rotate,
Skip,
} }
// TODO: When out of bounds left/right, try to move piece away from wall // TODO: When out of bounds left/right, try to move piece away from wall
@ -748,67 +719,57 @@ fn movement(
shape.get_mut(trigger.target()), shape.get_mut(trigger.target()),
grid_positions.get(trigger.target()), grid_positions.get(trigger.target()),
) { ) {
let new_positions = match trigger.event() { let (new_center, new_shape) = match trigger.event() {
Movement::Down => vec![center.with_offset(0, -1)], Movement::Down => (center.with_offset(0, -1), *this_shape),
Movement::Left => vec![center.with_offset(-1, 0)], Movement::Left => (center.with_offset(-1, 0), *this_shape),
Movement::Right => vec![center.with_offset(1, 0)], Movement::Right => (center.with_offset(1, 0), *this_shape),
Movement::Rotate => vec![Ok(*center)], Movement::Rotate => (Ok(*center), this_shape.rotated()),
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,
Movement::Rotate => this_shape.rotated(),
}; };
debug!( debug!(
"Proposed change: {:?}\n{}", "Proposed change: {:?}\n{}",
new_positions, new_center,
new_shape.as_ascii() new_shape.as_ascii()
); );
for position in new_positions { match new_center {
match position { Err(GameError::OutOfBoundsLeft) | Err(GameError::OutOfBoundsRight) => (), // Do nothing
Err(GameError::OutOfBoundsLeft) | Err(GameError::OutOfBoundsRight) => (), // Do nothing Err(GameError::OutOfBoundsDown) => {
Err(GameError::OutOfBoundsDown) => { commands.entity(trigger.target()).remove::<Shape>();
commands.entity(trigger.target()).remove::<Shape>(); }
} Err(GameError::Collision) => panic!("This shouldn't happen!"),
Err(GameError::Collision) => panic!("This shouldn't happen!"), Ok(new_center) => {
Ok(new_center) => { let new_blocks = new_shape.coordinates(&new_center);
let new_blocks = new_shape.coordinates(&new_center); for block_gp in new_blocks {
for block_gp in new_blocks { match block_gp {
match block_gp { Err(GameError::OutOfBoundsLeft) | Err(GameError::OutOfBoundsRight) => {
Err(GameError::OutOfBoundsLeft) | Err(GameError::OutOfBoundsRight) => { return;
return; } // Do nothing
} // Do nothing Err(GameError::OutOfBoundsDown) => {
Err(GameError::OutOfBoundsDown) => { commands.entity(trigger.target()).remove::<Shape>();
commands.entity(trigger.target()).remove::<Shape>(); return;
return; }
} Err(GameError::Collision) => panic!("This shouldn't happen!"),
Err(GameError::Collision) => panic!("This shouldn't happen!"), Ok(gp) => {
Ok(gp) => { for other_gp in inactive.iter() {
for other_gp in inactive.iter() { // If there would be a collision between blocks
// If there would be a collision between blocks if gp == *other_gp {
if gp == *other_gp { // And we are moving down
// And we are moving down if *trigger.event() == Movement::Down {
if *trigger.event() == Movement::Down { // De-activate this piece
// De-activate this piece commands.entity(trigger.target()).remove::<Shape>();
commands.entity(trigger.target()).remove::<Shape>();
}
// Regardless, cancel the move
return;
} }
// Regardless, cancel the move
return;
} }
} }
} }
} }
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;
} }
// 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;
} }
} }
} else { } else {
@ -835,15 +796,13 @@ fn deactivate_shape(
events.read().for_each(|target| { events.read().for_each(|target| {
parent.iter_descendants(target).for_each(|block| { parent.iter_descendants(target).for_each(|block| {
let GridPosition { y, .. } = grid_positions.get(block).unwrap(); let GridPosition { y, .. } = grid_positions.get(block).unwrap();
if let Some(parent_line) = lines let parent_line = lines
.iter() .iter()
.find_map(|(e, Line(i))| (*y == *i).then_some(e)) { .find_map(|(e, Line(i))| (*y == *i).then_some(e))
commands .unwrap(); // TODO: This crashed once kinda late in a game... why?
.entity(parent_line) commands
.add_one_related::<LineBlock>(block); .entity(parent_line)
} else { .add_one_related::<LineBlock>(block);
error!("wtf?");
}
}); });
commands.entity(target).despawn(); commands.entity(target).despawn();
}); });

@ -83,10 +83,3 @@ fn test_coordinates() {
assert_eq!(actual, expected); 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