Skip to content

Commit

Permalink
add damage dealt system in health plugin for handling damage to healt…
Browse files Browse the repository at this point in the history
…h and spawning damage numbers
  • Loading branch information
cdsupina committed Aug 12, 2023
1 parent d798daa commit 018ee1a
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 89 deletions.
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ fn main() {
.add_plugins(animation::AnimationPlugin)
.add_plugins(states::StatesPlugin)
.add_plugins(game::counters::plugin::CountingMetricsPlugin)
.add_plugins(misc::HealthPlugin)
.insert_resource(ClearColor(Color::BLACK))
.insert_resource(AmbientLight {
color: Color::WHITE,
Expand Down
61 changes: 60 additions & 1 deletion src/misc/health.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,64 @@
use bevy::prelude::warn;
use bevy::prelude::*;
use serde::Deserialize;
use thetawave_interface::spawnable::{EffectType, TextEffectType};

use crate::spawnable::{MobComponent, MobSegmentComponent, SpawnEffectEvent};

#[derive(Event)]
pub struct DamageDealtEvent {
pub damage: f32,
pub target: Entity,
}

pub fn damage_system(
mut damge_dealt_event: EventReader<DamageDealtEvent>,
mut mob_query: Query<(Entity, &Transform, &mut MobComponent)>,
mut mob_seg_query: Query<(Entity, &Transform, &mut MobSegmentComponent)>,
mut spawn_effect_event_writer: EventWriter<SpawnEffectEvent>,
) {
'events: for event in damge_dealt_event.iter() {
for (mob_entity, mob_transform, mut mob_component) in mob_query.iter_mut() {
if event.target == mob_entity {
// take damage from health
mob_component.health.take_damage(event.damage);

// spawn damage dealt text effect
spawn_effect_event_writer.send(SpawnEffectEvent {
effect_type: EffectType::Text(TextEffectType::DamageDealt),
transform: Transform {
translation: mob_transform.translation,
scale: mob_transform.scale,
..Default::default()
},
text: Some(event.damage.to_string()),
..default()
});

continue 'events;
}
}

for (mob_seg_entity, mob_seg_transform, mut mob_seg_component) in mob_seg_query.iter_mut() {
if event.target == mob_seg_entity {
mob_seg_component.health.take_damage(event.damage);

// spawn damage dealt text effect
spawn_effect_event_writer.send(SpawnEffectEvent {
effect_type: EffectType::Text(TextEffectType::DamageDealt),
transform: Transform {
translation: mob_seg_transform.translation,
scale: mob_seg_transform.scale,
..Default::default()
},
text: Some(event.damage.to_string()),
..default()
});

continue 'events;
}
}
}
}

#[derive(Deserialize, Debug, Clone)]
pub struct Health {
Expand Down
12 changes: 11 additions & 1 deletion src/misc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
use bevy::prelude::*;

mod health;
pub use self::health::{DamageDealtEvent, Health};

pub struct HealthPlugin;

pub use self::health::Health;
impl Plugin for HealthPlugin {
fn build(&self, app: &mut App) {
app.add_event::<DamageDealtEvent>()
.add_systems(Update, health::damage_system);
}
}
55 changes: 13 additions & 42 deletions src/spawnable/mob/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::{
collision::SortedCollisionEvent,
game::GameParametersResource,
loot::LootDropsResource,
misc::DamageDealtEvent,
spawnable::{
InitialMotion, PlayerComponent, SpawnConsumableEvent, SpawnEffectEvent,
SpawnProjectileEvent,
Expand Down Expand Up @@ -75,6 +76,7 @@ pub fn mob_execute_behavior_system(
mut spawn_projectile_event_writer: EventWriter<SpawnProjectileEvent>,
mut spawn_mob_event_writer: EventWriter<SpawnMobEvent>,
mut mob_destroyed_event_writer: EventWriter<MobDestroyedEvent>,
mut damage_dealt_event_writer: EventWriter<DamageDealtEvent>,
loot_drops_resource: Res<LootDropsResource>,
audio_channel: Res<AudioChannel<audio::SoundEffectsAudioChannel>>,
audio_assets: Res<GameAudioAssets>,
Expand Down Expand Up @@ -197,10 +199,8 @@ pub fn mob_execute_behavior_system(
receive_damage_on_impact(
entity,
&collision_events_vec,
&mut mob_component,
*mob_transform,
&mut spawn_effect_event_writer,
&mut player_query,
&mut damage_dealt_event_writer,
);
}
MobBehavior::DieAtZeroHealth => {
Expand Down Expand Up @@ -254,10 +254,8 @@ pub struct MobDestroyedEvent {
fn receive_damage_on_impact(
entity: Entity,
collision_events: &[&SortedCollisionEvent],
mob_component: &mut super::MobComponent,
mob_transform: Transform,
spawn_effect_event_writer: &mut EventWriter<SpawnEffectEvent>,
player_query: &mut Query<(Entity, &mut PlayerComponent)>,
damage_dealt_event_writer: &mut EventWriter<DamageDealtEvent>,
) {
for collision_event in collision_events.iter() {
match collision_event {
Expand All @@ -271,18 +269,9 @@ fn receive_damage_on_impact(
if entity == *mob_entity {
for (player_entity_q, mut _player_component) in player_query.iter_mut() {
if player_entity_q == *player_entity && *player_damage > 0.0 {
mob_component.health.take_damage(*player_damage);

// spawn damage dealt text effect
spawn_effect_event_writer.send(SpawnEffectEvent {
effect_type: EffectType::Text(TextEffectType::DamageDealt),
transform: Transform {
translation: mob_transform.translation,
scale: mob_transform.scale,
..Default::default()
},
text: Some(player_damage.to_string()),
..default()
damage_dealt_event_writer.send(DamageDealtEvent {
damage: *player_damage,
target: *mob_entity,
});
}
}
Expand All @@ -297,18 +286,9 @@ fn receive_damage_on_impact(
mob_damage_2,
} => {
if entity == *mob_entity_1 && *mob_damage_2 > 0.0 {
mob_component.health.take_damage(*mob_damage_2);

// spawn damage dealt text effect
spawn_effect_event_writer.send(SpawnEffectEvent {
effect_type: EffectType::Text(TextEffectType::DamageDealt),
transform: Transform {
translation: mob_transform.translation,
scale: mob_transform.scale,
..Default::default()
},
text: Some(mob_damage_2.to_string()),
..default()
damage_dealt_event_writer.send(DamageDealtEvent {
damage: *mob_damage_2,
target: *mob_entity_1,
});
}
}
Expand All @@ -321,18 +301,9 @@ fn receive_damage_on_impact(
mob_segment_damage,
} => {
if entity == *mob_entity && *mob_segment_damage > 0.0 {
mob_component.health.take_damage(*mob_segment_damage);

// spawn damage dealt text effect
spawn_effect_event_writer.send(SpawnEffectEvent {
effect_type: EffectType::Text(TextEffectType::DamageDealt),
transform: Transform {
translation: mob_transform.translation,
scale: mob_transform.scale,
..Default::default()
},
text: Some(mob_segment_damage.to_string()),
..default()
damage_dealt_event_writer.send(DamageDealtEvent {
damage: *mob_segment_damage,
target: *mob_entity,
});
}
}
Expand Down
59 changes: 14 additions & 45 deletions src/spawnable/mob/mob_segment/behavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ use crate::{
collision::SortedCollisionEvent,
game::GameParametersResource,
loot::LootDropsResource,
misc::DamageDealtEvent,
player::PlayerComponent,
spawnable::{
behavior_sequence::EntityPair, InitialMotion, MobDestroyedEvent, SpawnConsumableEvent,
behavior_sequence::EntityPair, mob, InitialMotion, MobDestroyedEvent, SpawnConsumableEvent,
SpawnEffectEvent, SpawnMobEvent, SpawnPosition,
},
};
Expand Down Expand Up @@ -76,6 +77,7 @@ pub fn mob_segment_execute_behavior_system(
mut spawn_mob_event_writer: EventWriter<SpawnMobEvent>,
mut mob_segment_destroyed_event_writer: EventWriter<MobSegmentDestroyedEvent>,
game_parameters: Res<GameParametersResource>,
mut damage_dealt_event_writer: EventWriter<DamageDealtEvent>,
) {
let mut collision_events_vec = vec![];
for collision_event in collision_events.iter() {
Expand All @@ -99,10 +101,8 @@ pub fn mob_segment_execute_behavior_system(
receive_damage_on_impact(
entity,
&collision_events_vec,
&mut mob_segment_component,
*mob_segment_transform,
&mut spawn_effect_event_writer,
&mut player_query,
&mut damage_dealt_event_writer,
);
}
MobSegmentBehavior::DieAtZeroHealth => {
Expand Down Expand Up @@ -313,10 +313,8 @@ fn deal_damage_to_player_on_impact(
fn receive_damage_on_impact(
entity: Entity,
collision_events: &[&SortedCollisionEvent],
mob_segment_component: &mut super::MobSegmentComponent,
mob_segment_transform: Transform,
spawn_effect_event_writer: &mut EventWriter<SpawnEffectEvent>,
player_query: &mut Query<(Entity, &mut PlayerComponent)>,
damage_dealt_event_writer: &mut EventWriter<DamageDealtEvent>,
) {
for collision_event in collision_events.iter() {
match collision_event {
Expand All @@ -330,18 +328,9 @@ fn receive_damage_on_impact(
if entity == *mob_segment_entity {
for (player_entity_q, mut _player_component) in player_query.iter_mut() {
if player_entity_q == *player_entity && *player_damage > 0.0 {
mob_segment_component.health.take_damage(*player_damage);

// spawn damage dealt text effect
spawn_effect_event_writer.send(SpawnEffectEvent {
effect_type: EffectType::Text(TextEffectType::DamageDealt),
transform: Transform {
translation: mob_segment_transform.translation,
scale: mob_segment_transform.scale,
..Default::default()
},
text: Some(player_damage.to_string()),
..default()
damage_dealt_event_writer.send(DamageDealtEvent {
damage: *player_damage,
target: *mob_segment_entity,
});
}
}
Expand All @@ -356,18 +345,9 @@ fn receive_damage_on_impact(
mob_damage,
} => {
if entity == *mob_segment_entity && *mob_damage > 0.0 {
mob_segment_component.health.take_damage(*mob_damage);

// spawn damage dealt text effect
spawn_effect_event_writer.send(SpawnEffectEvent {
effect_type: EffectType::Text(TextEffectType::DamageDealt),
transform: Transform {
translation: mob_segment_transform.translation,
scale: mob_segment_transform.scale,
..Default::default()
},
text: Some(mob_damage.to_string()),
..default()
damage_dealt_event_writer.send(DamageDealtEvent {
damage: *mob_damage,
target: *mob_segment_entity,
});
}
}
Expand All @@ -380,20 +360,9 @@ fn receive_damage_on_impact(
mob_segment_damage_2,
} => {
if entity == *mob_segment_entity_1 && *mob_segment_damage_2 > 0.0 {
mob_segment_component
.health
.take_damage(*mob_segment_damage_2);

// spawn damage dealt text effect
spawn_effect_event_writer.send(SpawnEffectEvent {
effect_type: EffectType::Text(TextEffectType::DamageDealt),
transform: Transform {
translation: mob_segment_transform.translation,
scale: mob_segment_transform.scale,
..Default::default()
},
text: Some(mob_segment_damage_2.to_string()),
..default()
damage_dealt_event_writer.send(DamageDealtEvent {
damage: *mob_segment_damage_2,
target: *mob_segment_entity_1,
});
}
}
Expand Down

0 comments on commit 018ee1a

Please sign in to comment.