r/o
1
use bevy::prelude::*;
2
use bevy_rapier2d::{prelude::*, rapier::prelude::CollisionEventFlags};
3
4
use crate::{
5
Enemy, Health,
6
player::{Player, PlayerBullet},
7
sound::{SoundEvent, SoundKind},
8
};
9
10
pub(crate) fn setup(rapier_config: Single<&mut RapierConfiguration>) {
11
rapier_config.into_inner().gravity = Vec2::ZERO;
12
}
13
14
pub(crate) fn check_bullet_collisions(
15
mut commands: Commands,
16
player_bullet_query: Query<(Entity, &PlayerBullet)>,
17
mut player_query: Query<(Entity, &mut Health), With<Player>>,
18
enemy_query: Query<(Entity, &Transform), With<Enemy>>,
19
mut collision_events: EventReader<CollisionEvent>,
20
mut sound_events: EventWriter<SoundEvent>,
21
) {
22
// for (player_bullet, player_bullet_entity, player_bullet_transform) in &player_bullet_query {
23
// for (enemy_entity, enemy_transform) in &enemy_query {
24
// // TODO: rapier
25
// if player_bullet_transform
26
// .translation
27
// .distance(enemy_transform.translation)
28
// < 50.
29
// {
30
// commands.entity(player_bullet_entity).despawn();
31
// commands.entity(enemy_entity).despawn();
32
// sound_events.write(SoundEvent(SoundKind::Explosion));
33
// }
34
// }
35
// }
36
37
for collision_event in collision_events.read() {
38
match collision_event {
39
CollisionEvent::Started(a, b, CollisionEventFlags::SENSOR) => {
40
match (
41
player_bullet_query.get(a.clone()),
42
player_bullet_query.get(b.clone()),
43
) {
44
(Ok((player_bullet_entity, player_bullet)), Err(_))
45
| (Err(_), Ok((player_bullet_entity, player_bullet))) => {
46
for (other_player_entity, mut other_player_health) in &mut player_query {
47
if a == &other_player_entity || b == &other_player_entity {
48
commands.entity(player_bullet_entity).despawn();
49
other_player_health.current -= player_bullet.damage;
50
if other_player_health.current <= 0. {
51
commands.entity(other_player_entity).try_despawn();
52
sound_events.write(SoundEvent(SoundKind::Explosion));
53
} else {
54
sound_events.write(SoundEvent(SoundKind::ShipDamage));
55
}
56
break;
57
}
58
}
59
}
60
_ => {}
61
}
62
}
63
_ => {}
64
}
65
}
66
}
67