Skip to content
Snippets Groups Projects
Verified Commit 28269b4b authored by Louis's avatar Louis :fire:
Browse files

Add basic collision checking system

parent c74a4bbc
No related branches found
No related tags found
No related merge requests found
use crate::deref_as; use crate::deref_as;
use bevy::prelude::{Color, Component, Rect, Vec2}; use bevy::math::Vec3Swizzles;
use bevy::prelude::{Changed, Color, Component, Entity, Query, Rect, Transform, Vec2};
#[derive(Clone, Copy, Debug, Component)] #[derive(Clone, Copy, Debug, Component)]
pub struct BoxSize(Vec2); pub struct BoxSize(Vec2);
...@@ -37,4 +38,45 @@ impl CollisionGroup { ...@@ -37,4 +38,45 @@ impl CollisionGroup {
Enemy => Color::rgb_u8(133, 20, 75), Enemy => Color::rgb_u8(133, 20, 75),
} }
} }
pub fn interacts_with(&self, other: CollisionGroup) -> bool {
use CollisionGroup::*;
matches!(
(*self, other),
(FriendlyProjectile, Enemy)
| (Enemy, FriendlyProjectile)
| (Player, Pickup)
| (Pickup, Player)
| (HostileProjectile, Player)
| (Player, HostileProjectile)
)
}
}
pub fn check_entity_collisions(
moved_colliders: Query<(Entity, &Transform, &BoxSize, &CollisionGroup), Changed<Transform>>,
all_colliders: Query<(Entity, &Transform, &BoxSize, &CollisionGroup)>,
) {
for (moved_entity, moved_transform, moved_size, moved_group) in &moved_colliders {
for (target_entity, target_transform, target_size, target_group) in &all_colliders {
if moved_entity == target_entity {
continue;
}
let moved_box = Rect::from_center_size(moved_transform.translation.xy(), **moved_size);
let target_box =
Rect::from_center_size(target_transform.translation.xy(), **target_size);
if moved_group.interacts_with(*target_group)
&& check_box_collisions(moved_box, target_box)
{
log::info!(
"Two colliding entities! {:?} and {:?}",
target_entity,
moved_entity
);
}
}
}
} }
...@@ -19,6 +19,7 @@ mod _plugin { ...@@ -19,6 +19,7 @@ mod _plugin {
super::player::process_player_input, super::player::process_player_input,
super::motion::apply_velocity, super::motion::apply_velocity,
super::spawning::apply_despawn_boundaries, super::spawning::apply_despawn_boundaries,
super::collision::check_entity_collisions,
)); ));
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment