Collision
This commit is contained in:
parent
36161eb083
commit
7099b8629d
3 changed files with 47 additions and 7 deletions
37
src/collision.rs
Normal file
37
src/collision.rs
Normal file
|
|
@ -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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue