From 6aba81051776a0c17ff09088be9a5125d3db1a89 Mon Sep 17 00:00:00 2001 From: Imbris Date: Sun, 26 Apr 2020 23:40:56 -0400 Subject: [PATCH] Add key to select entities --- assets/voxygen/i18n/en.ron | 3 ++- client/src/lib.rs | 1 - voxygen/src/hud/mod.rs | 7 +++++-- voxygen/src/session.rs | 24 ++++++++++++++++++++---- voxygen/src/settings.rs | 2 ++ voxygen/src/window.rs | 2 ++ 6 files changed, 31 insertions(+), 8 deletions(-) diff --git a/assets/voxygen/i18n/en.ron b/assets/voxygen/i18n/en.ron index 0d4c6c2f9d..8052ecf4b7 100644 --- a/assets/voxygen/i18n/en.ron +++ b/assets/voxygen/i18n/en.ron @@ -377,7 +377,8 @@ magically infused items?"#, "gameinput.freelook": "Free Look", "gameinput.autowalk": "Auto Walk", "gameinput.dance": "Dance", - + "gameinput.select": "Select Entity", + /// End GameInput section diff --git a/client/src/lib.rs b/client/src/lib.rs index 298c9bef15..08b10a84e6 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -996,7 +996,6 @@ impl Client { // the view distance match change_notification { Added(uid) => { - warn!("message to add: {}", uid); if !self.group_members.insert(uid) { warn!( "Received msg to add uid {} to the group members but they \ diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index 39edeff12e..cd23a0223f 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -243,6 +243,7 @@ pub struct HudInfo { pub is_aiming: bool, pub is_first_person: bool, pub target_entity: Option, + pub selected_entity: Option, } pub enum Event { @@ -1047,8 +1048,10 @@ impl Hud { ) .join() .filter(|(entity, _, _, stats, _, _, _, _, _, _)| *entity != me && !stats.is_dead - && (stats.health.current() != stats.health.maximum() || info.target_entity.map_or(false, |e| e == *entity)) - ) + && (stats.health.current() != stats.health.maximum() + || info.target_entity.map_or(false, |e| e == *entity) + || info.selected_entity.map_or(false, |e| e == *entity) + )) // Don't show outside a certain range .filter(|(_, pos, _, _, _, _, _, _, hpfl, _)| { pos.0.distance_squared(player_pos) diff --git a/voxygen/src/session.rs b/voxygen/src/session.rs index 578dc9b0fa..a6f5e608b1 100644 --- a/voxygen/src/session.rs +++ b/voxygen/src/session.rs @@ -52,6 +52,8 @@ pub struct SessionState { free_look: bool, auto_walk: bool, is_aiming: bool, + target_entity: Option, + selected_entity: Option, } /// Represents an active game session (i.e., the one being played). @@ -86,6 +88,8 @@ impl SessionState { free_look: false, auto_walk: false, is_aiming: false, + target_entity: None, + selected_entity: None, } } @@ -234,7 +238,7 @@ impl PlayState for SessionState { let (build_pos, select_pos, target_entity) = under_cursor(&self.client.borrow(), cam_pos, cam_dir); // Throw out distance info, it will be useful in the future - let target_entity = target_entity.map(|x| x.0); + self.target_entity = target_entity.map(|x| x.0); let can_build = self .client @@ -527,6 +531,11 @@ impl PlayState for SessionState { let camera = self.scene.camera_mut(); camera.next_mode(self.client.borrow().is_admin()); }, + Event::InputUpdate(GameInput::Select, state) => { + if !state { + self.selected_entity = self.target_entity; + } + }, Event::AnalogGameInput(input) => match input { AnalogGameInput::MovementX(v) => { self.key_state.analog_matrix.x = v; @@ -676,7 +685,8 @@ impl PlayState for SessionState { self.scene.camera().get_mode(), camera::CameraMode::FirstPerson ), - target_entity, + target_entity: self.target_entity, + selected_entity: self.selected_entity, }, ); @@ -948,7 +958,7 @@ impl PlayState for SessionState { let scene_data = SceneData { state: client.state(), player_entity: client.entity(), - target_entity, + target_entity: self.target_entity, loaded_distance: client.loaded_distance(), view_distance: client.view_distance().unwrap_or(1), tick: client.get_tick(), @@ -1003,6 +1013,7 @@ impl PlayState for SessionState { let scene_data = SceneData { state: client.state(), player_entity: client.entity(), + target_entity: self.target_entity, loaded_distance: client.loaded_distance(), view_distance: client.view_distance().unwrap_or(1), tick: client.get_tick(), @@ -1097,14 +1108,18 @@ fn under_cursor( let radius = s.map_or(1.0, |s| s.0) * b.radius() * RADIUS_SCALE; // Move position up from the feet let pos = Vec3::new(p.0.x, p.0.y, p.0.z + radius); + // Distance squared from camera to the entity let dist_sqr = pos.distance_squared(cam_pos); (e, pos, radius, dist_sqr) }) + // Roughly filter out entities farther than ray distance + .filter(|(_, _, r, d_sqr)| *d_sqr <= cast_dist.powi(2) + 2.0 * cast_dist * r + r.powi(2)) // Ignore entities intersecting the camera .filter(|(_, _, r, d_sqr)| *d_sqr > r.powi(2)) - .filter(|(_, _, r, d_sqr)| *d_sqr <= cast_dist.powi(2) + 2.0 * cast_dist * r + r.powi(2)) + // Substract sphere radius from distance to the camera .map(|(e, p, r, d_sqr)| (e, p, r, d_sqr.sqrt() - r)) .collect::>(); + // Sort by distance nearby.sort_unstable_by(|a, b| a.3.partial_cmp(&b.3).unwrap()); let seg_ray = LineSegment3 { @@ -1115,6 +1130,7 @@ fn under_cursor( let target_entity = nearby .iter() .map(|(e, p, r, _)| (e, *p, r)) + // Find first one that intersects the ray segment .find(|(_, p, r)| seg_ray.projected_point(*p).distance_squared(*p) < r.powi(2)) .and_then(|(e, p, r)| { let dist_to_player = p.distance(player_pos); diff --git a/voxygen/src/settings.rs b/voxygen/src/settings.rs index 1767ee8764..d89833dd23 100644 --- a/voxygen/src/settings.rs +++ b/voxygen/src/settings.rs @@ -169,6 +169,7 @@ impl ControlSettings { GameInput::Slot9 => KeyMouse::Key(VirtualKeyCode::Key9), GameInput::Slot10 => KeyMouse::Key(VirtualKeyCode::Q), GameInput::SwapLoadout => KeyMouse::Key(VirtualKeyCode::LAlt), + GameInput::Select => KeyMouse::Key(VirtualKeyCode::I), } } } @@ -234,6 +235,7 @@ impl Default for ControlSettings { GameInput::Slot9, GameInput::Slot10, GameInput::SwapLoadout, + GameInput::Select, ]; for game_input in game_inputs { new_settings.insert_binding(game_input, ControlSettings::default_binding(game_input)); diff --git a/voxygen/src/window.rs b/voxygen/src/window.rs index c634a332f5..d338d2ca10 100644 --- a/voxygen/src/window.rs +++ b/voxygen/src/window.rs @@ -67,6 +67,7 @@ pub enum GameInput { FreeLook, AutoWalk, CycleCamera, + Select, } impl GameInput { @@ -123,6 +124,7 @@ impl GameInput { GameInput::Slot9 => "gameinput.slot9", GameInput::Slot10 => "gameinput.slot10", GameInput::SwapLoadout => "gameinput.swaploadout", + GameInput::Select => "gameinput.select", } }