diff options
author | Alexander Foremny <aforemny@posteo.de> | 2024-03-24 16:02:10 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2024-03-24 16:02:10 +0100 |
commit | 05c01e7aa13d8a6fe7302462fc32a5c559fade56 (patch) | |
tree | faf2985df6f85e87b7acad74f17be91439474bd7 | |
parent | b09b4a6c8854d8c253e2129b57a9637f4c362fbe (diff) |
fix: attack closest (instead of hovered) target
-rw-r--r-- | src/client.rs | 39 |
1 files changed, 28 insertions, 11 deletions
diff --git a/src/client.rs b/src/client.rs index bff66cd..e2f52cb 100644 --- a/src/client.rs +++ b/src/client.rs @@ -337,9 +337,13 @@ fn buffer_input( attack.0 = None; } Ability::Targeted(_) => { - let Some((target_player, _, _)) = - hovered_other_player(&cameras, &client_id, &players, &windows) - else { + let Some((target_player, _, _)) = hovered_other_player_within( + HOVER_RANGE_ATTACK, + &cameras, + &client_id, + &players, + &windows, + ) else { return; }; client.add_input(Inputs::Imperative(Imperative::AttackTarget( @@ -350,9 +354,13 @@ fn buffer_input( } }, None => { - if let Some((target_player, _, _)) = - hovered_other_player(&cameras, &client_id, &players, &windows) - { + if let Some((target_player, _, _)) = hovered_other_player_within( + HOVER_RANGE, + &cameras, + &client_id, + &players, + &windows, + ) { client.add_input(Inputs::Imperative(Imperative::AttackTarget( AbilitySlot::A, target_player, @@ -426,10 +434,15 @@ fn gizmos_hover_indicator( hoverables: Query<(&PlayerId, &PlayerPosition, &Shape)>, mut gizmos: Gizmos, windows: Query<&Window>, + attack: Res<Attack>, ) { - let Some((_, position, shape)) = - hovered_other_player(&cameras, &client_id, &hoverables, &windows) - else { + let Some((_, position, shape)) = hovered_other_player_within( + attack.0.map(|_| HOVER_RANGE_ATTACK).unwrap_or(HOVER_RANGE), + &cameras, + &client_id, + &hoverables, + &windows, + ) else { return; }; gizmos.circle_2d(position.0, shape.radius + 1., Color::GREEN); @@ -452,7 +465,11 @@ fn world_to_viewport(cameras: &Query<(&Camera, &GlobalTransform)>, position: Vec camera.world_to_viewport(camera_transform, Vec3::new(position.x, position.y, 0.)) } -fn hovered_other_player( +const HOVER_RANGE: f32 = 10.; +const HOVER_RANGE_ATTACK: f32 = 100.; + +fn hovered_other_player_within( + range: f32, cameras: &Query<(&Camera, &GlobalTransform)>, client_id: &Res<ClientId>, hoverables: &Query<(&PlayerId, &PlayerPosition, &Shape)>, @@ -468,7 +485,7 @@ fn hovered_other_player( continue; } let distance = position.0.distance(world_position); - if distance < shape.radius + 5. { + if distance < shape.radius + range { if hovered_distance.map_or(true, |hovered_distance| distance < hovered_distance) { hovered_player = Some((*id, *position, *shape)); hovered_distance = Some(distance); |