Hit detection not working, but not horribly broken either... somewhere in between.

main
Elijah Voigt 2 weeks ago
parent 2cab218e5c
commit f6aae11f8c

@ -92,6 +92,10 @@ struct BlockOf {
parent: Entity, parent: Entity,
} }
// Just marks a block either of a shape or line
#[derive(Component, Debug)]
struct Block;
#[derive(Component, Debug)] #[derive(Component, Debug)]
#[require(GridPosition)] #[require(GridPosition)]
struct RelativePosition { struct RelativePosition {
@ -454,7 +458,7 @@ fn set_piece(
.entity(e) .entity(e)
.with_related_entities::<BlockOf>(|parent| { .with_related_entities::<BlockOf>(|parent| {
positions.into_iter().for_each(|rp| { positions.into_iter().for_each(|rp| {
parent.spawn((Mesh2d(mesh.clone()), MeshMaterial2d(mat.clone()), rp)); parent.spawn((Mesh2d(mesh.clone()), MeshMaterial2d(mat.clone()), rp, Block));
}); });
}); });
} else { } else {
@ -569,9 +573,9 @@ fn clock_cycle(n: f32) -> impl FnMut(Res<Time>, Local<f32>) -> bool {
/// Check if the active piece is about to collide with the ground /// Check if the active piece is about to collide with the ground
fn check_collision( fn check_collision(
active: Query<(&GridPosition, Entity), With<RelativePosition>>, active: Query<(&GridPosition, Entity), (With<Block>, With<RelativePosition>)>,
shape: Single<Entity, With<Shape>>, shape: Single<(Entity, &GridPosition), With<ShapeBlocks>>,
inactive: Query<&GridPosition, Without<RelativePosition>>, inactive: Query<&GridPosition, (With<Block>, Without<RelativePosition>)>,
mut commands: Commands, mut commands: Commands,
) { ) {
// TODO: Easy optimizations // TODO: Easy optimizations
@ -582,13 +586,17 @@ fn check_collision(
// Check if active peice is near other blocks // Check if active peice is near other blocks
let hit_block = inactive.iter().any(|b| { let hit_block = inactive.iter().any(|b| {
debug!("Checking against: {:?}", b); debug!("Checking against: {:?}", b);
a.is_colliding_with(b) if a.is_colliding_with(b) {
info!("{:?} is colliding with {:?}", a, b);
true
} else { false }
}); });
hit_floor || hit_block hit_floor || hit_block
}); });
if false { if hit {
info!("hit detected!"); info!("hit detected!");
let blocks: Vec<Entity> = active let blocks: Vec<Entity> = active
@ -599,8 +607,9 @@ fn check_collision(
debug_assert_eq!(blocks.len(), 4, "Shapes should have 4 blocks"); debug_assert_eq!(blocks.len(), 4, "Shapes should have 4 blocks");
info!("De-relating blocks {:?} and shape {:?}", blocks, *shape); info!("De-relating blocks {:?} and shape {:?}", blocks, *shape);
let (shape_e, _shape_pos) = *shape;
commands commands
.entity(*shape) .entity(shape_e)
.remove_related::<BlockOf>(blocks.as_slice()) .remove_related::<BlockOf>(blocks.as_slice())
.despawn(); .despawn();
} }

Loading…
Cancel
Save