Skip to content

Commit

Permalink
Implement animation clips for rifle; add a new event distinguishing b…
Browse files Browse the repository at this point in the history
…etween hitting something and dealing damage to it

Closes #227
  • Loading branch information
jwright159 committed Jan 7, 2025
1 parent 797c57e commit d0d9e0f
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 107 deletions.
25 changes: 14 additions & 11 deletions sbepis/src/player_controller/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub use self::camera_controls::{interact_with, MouseSensitivity, PlayerBody};
use self::movement::*;
use self::movement::{axes_to_ground_velocity, jump};
use self::weapons::hammer::*;
//use self::weapons::rifle::*;
use self::weapons::rifle::*;
use self::weapons::sword::*;
use self::weapons::*;

Expand All @@ -39,6 +39,7 @@ impl Plugin for PlayerControllerPlugin {
acceleration: 8.0,
air_acceleration: 6.0,
})
.add_event::<EntityHit>()
.add_event::<EntityDamaged>()
.add_plugins(InputManagerMenuPlugin::<PlayerAction>::default())
.add_systems(Startup, setup)
Expand All @@ -52,9 +53,9 @@ impl Plugin for PlayerControllerPlugin {
switch_weapon_next.run_if(button_just_pressed(PlayerAction::NextWeapon)),
switch_weapon_prev.run_if(button_just_pressed(PlayerAction::PrevWeapon)),
initialize_weapon_sets,
// animate_rifle,
// charge_rifle,
charge_rifle,
sweep_dealers,
hit_to_damage,
deal_all_damage,
update_damage_numbers,
update_is_grounded,
Expand Down Expand Up @@ -152,17 +153,19 @@ fn setup(
body,
);

// let (rifle_pivot, _rifle_barrel) = spawn_rifle(
// &mut commands,
// &asset_server,
// &mut materials,
// &mut meshes,
// body,
// );
let (rifle_pivot, _rifle_barrel) = spawn_rifle(
&mut commands,
&asset_server,
&mut materials,
&mut meshes,
&mut animations,
&mut graphs,
body,
);

commands.entity(body).insert((
WeaponSet {
weapons: vec![hammer_pivot, sword_pivot],
weapons: vec![hammer_pivot, sword_pivot, rifle_pivot],
active_weapon: 0,
},
UninitializedWeaponSet,
Expand Down
46 changes: 34 additions & 12 deletions sbepis/src/player_controller/weapons/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ use crate::fray::FrayMusic;
use crate::util::QuaternionEx;

pub mod hammer;
//pub mod rifle;
pub mod rifle;
pub mod sword;

#[derive(Event)]
pub struct EntityHit {
pub victim: Entity,
pub allies: EntityHashSet,
pub damage: f32,
pub fray_modifier: f32,
}

#[derive(Event)]
pub struct EntityDamaged {
pub victim: Entity,
Expand Down Expand Up @@ -109,9 +117,7 @@ pub fn sweep_dealers(
pivots: Query<(&SweepPivot, &GlobalTransform), Without<DamageSweep>>,
rapier_context: Query<&RapierContext>,
debug_collider_visualizers: Query<Entity, With<DebugColliderVisualizer>>,
parents: Query<&Parent>,
healths: Query<&GelViscosity>,
mut ev_hit: EventWriter<EntityDamaged>,
mut ev_hit: EventWriter<EntityHit>,
) {
let debug_collider_visualizer = debug_collider_visualizers.single();
let rapier_context = rapier_context.single();
Expand Down Expand Up @@ -140,13 +146,7 @@ pub fn sweep_dealers(
&collider,
QueryFilter::new(),
|hit_entity| {
let hit_entity = std::iter::once(hit_entity)
.chain(parents.iter_ancestors(hit_entity))
.find(|entity| healths.get(*entity).is_ok())
.unwrap_or(hit_entity);
if !dealer.allies.contains(&hit_entity) {
dealer.hit_entities.insert(hit_entity);
}
dealer.hit_entities.insert(hit_entity);
true
},
);
Expand All @@ -159,8 +159,9 @@ pub fn sweep_dealers(

if let Some(end) = end {
for entity in dealer.hit_entities.iter() {
ev_hit.send(EntityDamaged {
ev_hit.send(EntityHit {
victim: *entity,
allies: dealer.allies.clone(),
damage: end.damage,
fray_modifier: end.fray_modifier,
});
Expand All @@ -174,6 +175,27 @@ pub fn sweep_dealers(
}
}

pub fn hit_to_damage(
parents: Query<&Parent>,
healths: Query<&GelViscosity>,
mut ev_hit: EventReader<EntityHit>,
mut ev_damage: EventWriter<EntityDamaged>,
) {
for event in ev_hit.read() {
let victim = std::iter::once(event.victim)
.chain(parents.iter_ancestors(event.victim))
.find(|entity| healths.get(*entity).is_ok())
.unwrap_or(event.victim);
if !event.allies.contains(&victim) {
ev_damage.send(EntityDamaged {
victim,
damage: event.damage,
fray_modifier: event.fray_modifier,
});
}
}
}

pub fn deal_all_damage(
mut ev_hit: EventReader<EntityDamaged>,
mut ev_kill: EventWriter<EntityKilled>,
Expand Down
Loading

0 comments on commit d0d9e0f

Please sign in to comment.