From 876fd9d60de9c09e8657a2cb70fd55fd041d1f97 Mon Sep 17 00:00:00 2001 From: Alexander Foremny Date: Sun, 24 Mar 2024 15:01:04 +0100 Subject: feat: color projectiles --- src/shared/ability.rs | 103 ++++++++++++++++++++++++++--------------------- src/shared/projectile.rs | 8 +++- 2 files changed, 63 insertions(+), 48 deletions(-) (limited to 'src/shared') diff --git a/src/shared/ability.rs b/src/shared/ability.rs index a9387f3..66d236f 100644 --- a/src/shared/ability.rs +++ b/src/shared/ability.rs @@ -253,7 +253,7 @@ fn dash_activation(dash: Dash) -> DirectionalAbilityActivation { move |entity_map: Res, mut imperatives: Query<&mut Imperative>, mut set: ParamSet<( - Query<&mut PlayerPosition>, + Query<(&mut PlayerPosition, &PlayerColor)>, Query<(Entity, &PlayerId, &PlayerPosition, &Shape)>, )>, mut commands: Commands| { @@ -278,9 +278,12 @@ fn dash_activation(dash: Dash) -> DirectionalAbilityActivation { }; let mut positions = set.p0(); - if let Ok(mut position) = positions.get_mut(*source_entity) { - position.0 = dash_end; - } + let Ok((mut position, source_player_color)) = + positions.get_mut(*source_entity) + else { + return; + }; + position.0 = dash_end; if let Some(DashCollision { collision_player_id, @@ -295,13 +298,16 @@ fn dash_activation(dash: Dash) -> DirectionalAbilityActivation { source_player, area_of_effect_type: AreaOfEffectType::Slow, })); - commands.spawn(ProjectileBundle::new(Projectile { - type_: ProjectileType::Instant(InstantProjectile { - target_player: collision_player_id, - }), - source_player, - damage: dash.damage, - })); + commands.spawn(ProjectileBundle::new( + Projectile { + type_: ProjectileType::Instant(InstantProjectile { + target_player: collision_player_id, + }), + source_player, + damage: dash.damage, + }, + ProjectileColor(source_player_color.0), + )); if let Ok(mut imperative) = imperatives.get_mut(*source_entity) { *imperative = Imperative::AttackTarget(AbilitySlot::A, collision_player_id); @@ -375,21 +381,12 @@ fn pull_activation(pull: Pull) -> DirectionalAbilityActivation { move |players: Query<(Entity, &PlayerId)>, mut imperatives: Query<&mut Imperative>, mut set: ParamSet<( - Query<&mut PlayerPosition>, + Query<(&mut PlayerPosition, &PlayerColor)>, Query<(Entity, &PlayerId, &PlayerPosition, &Shape)>, )>, - mut commands: Commands| { - let Some(source_entity) = ({ - let mut source_entity = None; - for (entity, player_id) in players.iter() { - if *player_id != source_player { - continue; - } - source_entity = Some(entity); - break; - } - source_entity - }) else { + mut commands: Commands, + entity_map: Res| { + let Some(source_entity) = entity_map.0.get(&source_player.0) else { return; }; @@ -400,7 +397,7 @@ fn pull_activation(pull: Pull) -> DirectionalAbilityActivation { }) = ({ let pull_targets = set.p1(); pull_collision( - source_entity, + *source_entity, direction.normalize_or_zero(), pull.max_distance, &pull_targets, @@ -425,18 +422,24 @@ fn pull_activation(pull: Pull) -> DirectionalAbilityActivation { }; let mut positions = set.p0(); - if let Ok(mut position) = positions.get_mut(target_entity) { - position.0 = pull_end; - } + let Ok([(_, source_player_color), (mut position, _)]) = + positions.get_many_mut([*source_entity, target_entity]) + else { + return; + }; + position.0 = pull_end; - commands.spawn(ProjectileBundle::new(Projectile { - type_: ProjectileType::Instant(InstantProjectile { - target_player: collision_player_id, - }), - source_player, - damage: pull.damage, - })); - if let Ok(mut imperative) = imperatives.get_mut(source_entity) { + commands.spawn(ProjectileBundle::new( + Projectile { + type_: ProjectileType::Instant(InstantProjectile { + target_player: collision_player_id, + }), + source_player, + damage: pull.damage, + }, + ProjectileColor(source_player_color.0), + )); + if let Ok(mut imperative) = imperatives.get_mut(*source_entity) { *imperative = Imperative::AttackTarget(AbilitySlot::A, collision_player_id); } @@ -571,23 +574,31 @@ fn spear_activation(spear: Spear) -> DirectionalAbilityActivation { move |commands: &mut Commands, source_player: PlayerId, direction: Vec2| { commands.add(move |world: &mut World| { world.run_system_once( - move |mut commands: Commands, players: Query<(&PlayerId, &PlayerPosition)>| { - for (id, position) in players.iter() { - if *id != source_player { - continue; - } - commands.spawn(ProjectileBundle::new(Projectile { + move |mut commands: Commands, + players: Query<(&PlayerId, &PlayerPosition, &PlayerColor)>, + entity_map: Res| { + let Some(source_entity) = entity_map.0.get(&source_player.0) else { + return; + }; + let Ok((_source_player_id, source_player_position, source_player_color)) = + players.get(*source_entity) + else { + return; + }; + commands.spawn(ProjectileBundle::new( + Projectile { type_: ProjectileType::Free(FreeProjectile { - position: position.0, + position: source_player_position.0, direction: direction.normalize_or_zero(), - starting_position: position.0, + starting_position: source_player_position.0, max_distance: spear.max_distance, scale_damage: Some((0.75, 2.5)), }), source_player, damage: spear.damage, - })); - } + }, + ProjectileColor(source_player_color.0), + )); }, ) }); diff --git a/src/shared/projectile.rs b/src/shared/projectile.rs index 5da0526..8fd0070 100644 --- a/src/shared/projectile.rs +++ b/src/shared/projectile.rs @@ -5,13 +5,15 @@ use crate::shared::*; #[derive(Bundle)] pub struct ProjectileBundle { pub projectile: Projectile, + pub projectile_color: ProjectileColor, pub replicate: Replicate, } impl ProjectileBundle { - pub fn new(projectile: Projectile) -> Self { + pub fn new(projectile: Projectile, projectile_color: ProjectileColor) -> Self { ProjectileBundle { projectile, + projectile_color: projectile_color, replicate: Replicate { replication_group: ReplicationGroup::default().set_priority(1.), ..Default::default() @@ -21,13 +23,15 @@ impl ProjectileBundle { } #[derive(Component, Message, Serialize, Deserialize, Clone, Debug, PartialEq)] - pub struct Projectile { pub type_: ProjectileType, pub source_player: PlayerId, pub damage: Damage, } +#[derive(Component, Message, Serialize, Deserialize, Clone, Debug, PartialEq)] +pub struct ProjectileColor(pub Color); + #[derive(Component, Message, Serialize, Deserialize, Clone, Debug, PartialEq)] pub enum ProjectileType { Free(FreeProjectile), -- cgit v1.2.3