-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rf(damage_receiver,impact_effect_removal,player_pickup): mv out of ma…
…in.rs
- Loading branch information
Showing
5 changed files
with
114 additions
and
92 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
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,49 @@ | ||
use bevy::prelude::*; | ||
use rand::Rng; | ||
|
||
use crate::components::{Score, Pickup, Health}; | ||
use crate::{DamageEvent}; | ||
|
||
pub fn damage_receiver( | ||
mut commands: Commands, | ||
mut damage_readers: EventReader<DamageEvent>, | ||
mut health_query: Query<(&mut Health, &Transform)>, | ||
asset_server: Res<AssetServer>, | ||
audio: Res<Audio>, | ||
mut materials: ResMut<Assets<ColorMaterial>>, | ||
mut score: ResMut<Score> | ||
) { | ||
for event in damage_readers.iter() { | ||
if let Ok((mut health, transform)) = health_query.get_mut(event.entity) { | ||
health.add(-1); | ||
if *health.current() <= 0 { | ||
// spawning a pickup | ||
let rand = rand::thread_rng().gen_range(0..10); | ||
if rand <= 2 { | ||
let material = materials | ||
.add(asset_server.get_handle("sprites/shield_bronze.png").into()); | ||
|
||
commands | ||
.spawn_bundle(SpriteBundle { | ||
transform: *transform, | ||
material: material.clone(), | ||
..Default::default() | ||
}) | ||
.insert(Pickup); | ||
} | ||
|
||
// remove this entity | ||
commands.entity(event.entity).despawn(); | ||
|
||
// play sound | ||
let sfx = asset_server.load("sounds/Explosion.mp3"); | ||
audio.play(sfx); | ||
|
||
// incr score | ||
score.0 = score.0 + 1; | ||
} | ||
} else { | ||
println!("Query for Health, Transform failed"); | ||
} | ||
} | ||
} |
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,18 @@ | ||
use bevy::prelude::*; | ||
|
||
use crate::{ImpactTimer}; | ||
use crate::components::{ImpactEffect}; | ||
|
||
|
||
pub fn impact_effect_removal( | ||
time: Res<Time>, | ||
mut timer: ResMut<ImpactTimer>, | ||
mut commands: Commands, | ||
impact_effect_query: Query<(Entity, &ImpactEffect)> | ||
) { | ||
for (entity, _impact_effect) in impact_effect_query.iter() { | ||
if timer.0.tick(time.delta()).just_finished() { | ||
commands.entity(entity).despawn(); | ||
} | ||
} | ||
} |
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,33 @@ | ||
use bevy::prelude::*; | ||
use bevy::sprite::collide_aabb::{Collision, collide}; | ||
|
||
use crate::components::{Pickup, Player, Health}; | ||
|
||
pub(crate) fn player_pickup( | ||
mut commands: Commands, | ||
mut pickup_query: Query<(Entity, &Pickup, &Transform, &Sprite)>, | ||
mut player_query: Query<(&Player, &Transform, &Sprite, &mut Health)>, | ||
asset_server: Res<AssetServer>, | ||
audio: Res<Audio>, | ||
) { | ||
for (entity, _pickup, pickup_transform, pickup_sprite) in pickup_query.iter_mut() { | ||
let pickup_size = pickup_sprite.size; | ||
for (_player, player_transform, player_sprite, mut health) in player_query.iter_mut() { | ||
let mut player_size = player_sprite.size; | ||
|
||
let collision: Option<Collision> = collide( | ||
pickup_transform.translation, | ||
pickup_size, | ||
player_transform.translation, | ||
player_size, | ||
); | ||
|
||
if let Some(_) = collision { | ||
commands.entity(entity).despawn(); | ||
let sfx = asset_server.load("sounds/sfx_shieldUp.mp3"); | ||
audio.play(sfx); | ||
health.add(5); | ||
} | ||
} | ||
} | ||
} |