-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extracts collision and spatial plugins into its own modules.
- Loading branch information
Showing
3 changed files
with
116 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::marker::PhantomData; | ||
|
||
use bevy::prelude::*; | ||
|
||
use crate::spatial::Spatial; | ||
|
||
pub struct CollisionPlugin<Hittable, Hurtable> { | ||
_phantom: PhantomData<(Hittable, Hurtable)>, | ||
} | ||
|
||
impl<Hittable: Component, Hurtable: Component> CollisionPlugin<Hittable, Hurtable> { | ||
pub fn new() -> Self { | ||
Self { | ||
_phantom: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<Hittable: Component, Hurtable: Component> Plugin for CollisionPlugin<Hittable, Hurtable> { | ||
fn build(&self, app: &mut App) { | ||
app.add_event::<HitEvent<Hittable, Hurtable>>() | ||
.add_system(collision_system::<Hittable, Hurtable>.label(CollisionSystemLabel)); | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
#[derive(SystemLabel)] | ||
pub struct CollisionSystemLabel; | ||
|
||
#[derive(Debug)] | ||
pub struct HitEvent<A, B> { | ||
entities: (Entity, Entity), | ||
_phantom: PhantomData<(A, B)>, | ||
} | ||
|
||
impl<A, B> HitEvent<A, B> { | ||
pub fn hittable(&self) -> Entity { | ||
self.entities.0 | ||
} | ||
|
||
pub fn hurtable(&self) -> Entity { | ||
self.entities.1 | ||
} | ||
} | ||
|
||
#[derive(Debug, Component)] | ||
pub struct Collidable; | ||
|
||
fn collision_system<A: Component, B: Component>( | ||
mut hits: EventWriter<HitEvent<A, B>>, | ||
hittables: Query<(Entity, &Spatial), (With<Collidable>, With<A>)>, | ||
hurtables: Query<(Entity, &Spatial), (With<Collidable>, With<B>)>, | ||
) { | ||
for (hittable_entity, hittable) in hittables.iter() { | ||
for (hurtable_entity, hurtable) in hurtables.iter() { | ||
if hittable.intersects(hurtable) { | ||
hits.send(HitEvent { | ||
entities: (hittable_entity, hurtable_entity), | ||
_phantom: PhantomData, | ||
}); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use bevy::prelude::*; | ||
|
||
pub struct SpatialPlugin; | ||
|
||
impl Plugin for SpatialPlugin { | ||
fn build(&self, app: &mut App) { | ||
app.add_system(spatial_system.label(SpatialSystemLabel)); | ||
} | ||
} | ||
|
||
#[derive(Debug, Component, Default, Clone)] | ||
pub struct Spatial { | ||
pub position: Vec2, | ||
pub rotation: f32, | ||
pub radius: f32, | ||
} | ||
|
||
impl Spatial { | ||
pub fn intersects(&self, other: &Spatial) -> bool { | ||
let distance = (self.position - other.position).length(); | ||
distance < self.radius + other.radius | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, SystemLabel)] | ||
pub struct SpatialSystemLabel; | ||
|
||
fn spatial_system(mut query: Query<(&mut Transform, &Spatial)>) { | ||
for (mut transform, spatial) in query.iter_mut() { | ||
transform.translation.x = spatial.position.x; | ||
transform.translation.y = spatial.position.y; | ||
transform.rotation = Quat::from_rotation_z(spatial.rotation); | ||
} | ||
} |