Skip to content

Commit

Permalink
rf(damage_receiver,impact_effect_removal,player_pickup): mv out of ma…
Browse files Browse the repository at this point in the history
…in.rs
  • Loading branch information
jtara1 committed Apr 24, 2021
1 parent b7635b0 commit 75edc55
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 92 deletions.
101 changes: 10 additions & 91 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub const PLAYER_CLAMP: PlayerPositionClamp = PlayerPositionClamp {

fn main() {
std::env::set_current_dir(std::env::current_exe().unwrap().parent().unwrap());

App::build()
.add_plugins(DefaultPlugins)
// bg color
Expand All @@ -37,18 +38,15 @@ fn main() {
// ship
.insert_resource(ImpactTimer::default())
.add_event::<DamageEvent>()
// player and input
// with fixed timestamp
.add_system_set(
SystemSet::new()
.with_run_criteria(FixedTimestep::step(TIME_STEP as f64))
.with_system(player_movement.system())
//.with_system(bullet_spawning.system())
// .with_system(bullet_movement.system())
.with_system(bullet_collision.system())
.with_system(player_pickup.system())
.with_system(damage_receiver.system())
.with_system(impact_effect_removal.system())
//.with_system(player_cloning.system())
)
// enemy
.insert_resource(IntervalTimer1::default())
Expand All @@ -75,99 +73,20 @@ pub struct PlayerPositionClamp {
y: f32,
}

struct ImpactTimer(Timer);
/**
* Resources
*/
pub struct ImpactTimer(Timer);

impl Default for ImpactTimer {
fn default() -> Self {
ImpactTimer(Timer::from_seconds(0.3, true))
}
}

/**
* Events
*/
pub struct DamageEvent {
entity: Entity
}

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);
}
}
}
}

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();
}
}
}

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");
}
}
}
49 changes: 49 additions & 0 deletions src/systems/damage_receiver.rs
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");
}
}
}
18 changes: 18 additions & 0 deletions src/systems/impact_effect_removal.rs
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();
}
}
}
5 changes: 4 additions & 1 deletion src/systems/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ mod setup;
mod drone;
mod player_shooter;
mod bullet_collision;
mod player_pickup;
mod impact_effect_removal;
mod damage_receiver;


pub use self::{ai_movement::*, enemy::*, label::*, shooter::*, player_movement::*, setup::*, drone::*, player_shooter::*, bullet_collision::*};
pub use self::{ai_movement::*, enemy::*, label::*, shooter::*, player_movement::*, setup::*, drone::*, player_shooter::*, bullet_collision::*, player_pickup::*, impact_effect_removal::*, damage_receiver::*};
33 changes: 33 additions & 0 deletions src/systems/player_pickup.rs
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);
}
}
}
}

0 comments on commit 75edc55

Please sign in to comment.