diff --git a/Cargo.lock b/Cargo.lock index 8f3ce64..5f11b37 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -314,13 +314,6 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" -[[package]] -name = "bevi-practice" -version = "0.1.0" -dependencies = [ - "bevy", -] - [[package]] name = "bevy" version = "0.17.2" @@ -4094,6 +4087,13 @@ dependencies = [ "serde", ] +[[package]] +name = "spacorium" +version = "0.1.0" +dependencies = [ + "bevy", +] + [[package]] name = "spin" version = "0.10.0" diff --git a/src/collision.rs b/src/collision.rs new file mode 100644 index 0000000..77c1862 --- /dev/null +++ b/src/collision.rs @@ -0,0 +1,37 @@ +use bevy::prelude::*; + +pub struct CollisionPlugin; + +impl Plugin for CollisionPlugin { + fn build(&self, app: &mut App) { + app.add_systems(FixedUpdate, calculate_collisions); + } +} + +#[derive(Component)] +pub struct Collider(f32); + +#[derive(EntityEvent)] +pub struct Collided { + entity: Entity, + with: Entity, +} + +fn calculate_collisions(mut commands: Commands, query: Query<(Entity, &Transform, &Collider)>) { + for (first_entity, first_transform, first_collider) in query { + for (second_entity, second_transform, second_collider) in query { + if first_entity == second_entity { + continue; + } + + if (second_transform.translation - first_transform.translation).length_squared() + <= (first_collider.0 + second_collider.0) * (first_collider.0 + second_collider.0) + { + commands.trigger(Collided { + entity: first_entity, + with: second_entity, + }); + } + } + } +} diff --git a/src/main.rs b/src/main.rs index 2f50c2f..34dd68b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,12 +1,15 @@ use bevy::prelude::*; +use crate::collision::CollisionPlugin; use crate::damagable::DamagablePlugin; +mod collision; mod damagable; fn main() { App::new() .add_plugins(DefaultPlugins) .add_plugins(DamagablePlugin) + .add_plugins(CollisionPlugin) .run(); }