|
1 | 1 | use bevy_ecs::prelude::*; |
2 | | -use ferrumc_entities::components::{EntityId, Health, Velocity}; |
| 2 | +use ferrumc_core::identity::player_identity::PlayerIdentity; |
| 3 | +use ferrumc_entities::components::{EntityId, EntityType, Health, Velocity}; |
3 | 4 | use ferrumc_entities::DamageEvent; |
| 5 | +use ferrumc_net::connection::StreamWriter; |
| 6 | +use ferrumc_net::packets::outgoing::entity_sound_effect::EntitySoundEffectPacket; |
4 | 7 | use ferrumc_state::GlobalStateResource; |
5 | 8 | use tracing::{debug, info}; |
6 | 9 |
|
7 | 10 | /// System that processes damage events and applies damage + knockback to entities |
8 | 11 | pub fn entity_damage_system( |
9 | 12 | mut damage_events: EventReader<DamageEvent>, |
10 | | - mut entity_query: Query<(&EntityId, &mut Health, &mut Velocity)>, |
11 | | - _state: Res<GlobalStateResource>, |
| 13 | + mut entity_query: Query<(&EntityId, &EntityType, &mut Health, &mut Velocity)>, |
| 14 | + player_query: Query<(Entity, &StreamWriter), With<PlayerIdentity>>, |
| 15 | + state: Res<GlobalStateResource>, |
12 | 16 | ) { |
13 | 17 | for event in damage_events.read() { |
14 | 18 | // Get the target entity's components |
15 | | - let Ok((entity_id, mut health, mut velocity)) = entity_query.get_mut(event.target) else { |
| 19 | + let Ok((entity_id, entity_type, mut health, mut velocity)) = |
| 20 | + entity_query.get_mut(event.target) |
| 21 | + else { |
16 | 22 | debug!( |
17 | 23 | "Damage event target entity {:?} not found or missing components", |
18 | 24 | event.target |
@@ -47,8 +53,33 @@ pub fn entity_damage_system( |
47 | 53 | ); |
48 | 54 | } |
49 | 55 |
|
| 56 | + // Send hurt sound effect to all connected players |
| 57 | + let sound_id = match entity_type { |
| 58 | + EntityType::Pig => 1114, // entity.pig.hurt |
| 59 | + // TODO: Add more entity types and their hurt sounds |
| 60 | + _ => { |
| 61 | + debug!("No hurt sound defined for {:?}", entity_type); |
| 62 | + continue; // Skip sound if not defined |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + let sound_packet = EntitySoundEffectPacket::hurt(sound_id, entity_id.to_network_id()); |
| 67 | + |
| 68 | + for (player_entity, stream_writer) in player_query.iter() { |
| 69 | + if state.0.players.is_connected(player_entity) { |
| 70 | + if let Err(e) = stream_writer.send_packet_ref(&sound_packet) { |
| 71 | + debug!("Failed to send hurt sound to player: {}", e); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + debug!( |
| 77 | + "Sent hurt sound {} for entity {}", |
| 78 | + sound_id, |
| 79 | + entity_id.to_network_id() |
| 80 | + ); |
| 81 | + |
50 | 82 | // TODO: Send damage animation packet to nearby players |
51 | | - // TODO: Send hurt sound effect |
52 | 83 | // TODO: Apply invulnerability ticks (prevent damage spam) |
53 | 84 | } |
54 | 85 | } |
0 commit comments