From 55319d687232f1bbf5a192e3e645b831431c7c14 Mon Sep 17 00:00:00 2001 From: Mark Date: Wed, 7 Feb 2024 15:42:11 -0800 Subject: [PATCH] Reworked player agent for directives --- crates/playeragent/src/camera.rs | 24 ---- crates/playeragent/src/inputstatus.rs | 151 -------------------------- crates/playeragent/src/lib.rs | 4 - crates/playeragent/src/playeragent.rs | 28 +---- 4 files changed, 1 insertion(+), 206 deletions(-) delete mode 100644 crates/playeragent/src/camera.rs delete mode 100644 crates/playeragent/src/inputstatus.rs diff --git a/crates/playeragent/src/camera.rs b/crates/playeragent/src/camera.rs deleted file mode 100644 index 1545792..0000000 --- a/crates/playeragent/src/camera.rs +++ /dev/null @@ -1,24 +0,0 @@ -use nalgebra::Vector2; - -#[derive(Debug, Clone, Copy)] -pub struct Camera { - /// Camera center - pub pos: Vector2, - - /// Camera zoom - /// (How many game units tall is the viewport?) - pub zoom: f32, - - /// Aspect ratio of viewport (width / height) - pub aspect: f32, -} - -impl Camera { - pub fn new() -> Self { - Self { - pos: Vector2::new(0.0, 0.0), - zoom: 500.0, - aspect: 1.0, - } - } -} diff --git a/crates/playeragent/src/inputstatus.rs b/crates/playeragent/src/inputstatus.rs deleted file mode 100644 index d3b8945..0000000 --- a/crates/playeragent/src/inputstatus.rs +++ /dev/null @@ -1,151 +0,0 @@ -use winit::{ - dpi::PhysicalPosition, - event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode}, -}; - -#[derive(Debug)] -pub struct InputStatus { - // Parameters - scroll_speed: f32, - - mouse_position: PhysicalPosition, - - // Continuous keys - key_left: bool, - key_right: bool, - key_thrust: bool, - key_guns: bool, - key_leftclick: bool, - - // One-shot keys (automatically released at the end of each frame) - key_land: bool, - v_scroll: f32, -} - -impl InputStatus { - pub fn new() -> Self { - InputStatus { - key_left: false, - key_right: false, - key_thrust: false, - key_guns: false, - key_land: false, - key_leftclick: false, - mouse_position: PhysicalPosition { x: 0.0, y: 0.0 }, - v_scroll: 0.0, - scroll_speed: 10.0, - } - } - - pub fn release_all(&mut self) { - self.key_left = false; - self.key_right = false; - self.key_thrust = false; - self.key_guns = false; - self.key_land = false; - } - - /// Called at the end of every frame, - /// resets one-shot keys. - pub fn clear_inputs(&mut self) { - self.key_land = false; - self.v_scroll = 0.0; - } - - pub fn process_key(&mut self, state: &ElementState, key: &VirtualKeyCode) { - let down = state == &ElementState::Pressed; - match key { - VirtualKeyCode::Left => { - self.key_left = down; - if down { - self.key_right = false; - } - } - VirtualKeyCode::Right => { - self.key_right = down; - if down { - self.key_left = false; - } - } - VirtualKeyCode::Up => self.key_thrust = down, - VirtualKeyCode::Space => self.key_guns = down, - VirtualKeyCode::L => self.key_land = down, - _ => {} - } - } - - pub fn process_mouse(&mut self, position: &PhysicalPosition) { - self.mouse_position = PhysicalPosition { - x: position.x as f32, - y: position.y as f32, - }; - } - - pub fn process_click(&mut self, state: &ElementState, key: &MouseButton) { - let down = state == &ElementState::Pressed; - match key { - MouseButton::Left => self.key_leftclick = down, - _ => {} - } - } - - pub fn process_scroll(&mut self, delta: &MouseScrollDelta, _phase: &TouchPhase) { - match delta { - MouseScrollDelta::LineDelta(_h, v) => { - self.v_scroll -= self.scroll_speed * v; - } - // TODO: handle this better - MouseScrollDelta::PixelDelta(v) => { - self.v_scroll -= v.x as f32; - } - } - } -} - -// Public get-state methods -impl InputStatus { - /// Has the player applied vertical scroll? - /// This is measured in lines, scaled by scroll_speed - /// - /// A positive value means scroll up, a negative value means scroll down. - /// This is reset to zero at the end of each frame. - pub fn get_v_scroll(&self) -> f32 { - self.v_scroll - } - - /// Get the current mouse position - pub fn get_mouse_pos(&self) -> PhysicalPosition { - self.mouse_position - } - - /// Is the player pressing the "turn left" key? - pub fn pressed_left(&self) -> bool { - self.key_left - } - - /// Is the player pressing the "turn right" key? - pub fn pressed_right(&self) -> bool { - self.key_right - } - - /// Is the player pressing the "fowards" key? - pub fn pressed_thrust(&self) -> bool { - self.key_thrust - } - - /// Is the player pressing the "fire guns" key? - pub fn pressed_guns(&self) -> bool { - self.key_guns - } - - /// Is the player pressing the left mouse button? - pub fn pressed_leftclick(&self) -> bool { - self.key_leftclick - } - - /// Has the player pressed the "land" key? - /// (One-shot, reset to false at the start of each frame) - pub fn pressed_land(&self) -> bool { - self.key_land - } -} diff --git a/crates/playeragent/src/lib.rs b/crates/playeragent/src/lib.rs index c46d594..e20147b 100644 --- a/crates/playeragent/src/lib.rs +++ b/crates/playeragent/src/lib.rs @@ -1,9 +1,5 @@ -mod camera; -mod inputstatus; mod playeragent; mod playerstatus; -pub use camera::Camera; -pub use inputstatus::InputStatus; pub use playeragent::PlayerAgent; pub use playerstatus::PlayerStatus; diff --git a/crates/playeragent/src/playeragent.rs b/crates/playeragent/src/playeragent.rs index 6494ec2..a4436a9 100644 --- a/crates/playeragent/src/playeragent.rs +++ b/crates/playeragent/src/playeragent.rs @@ -1,9 +1,6 @@ -use std::sync::Arc; - use galactica_content::{Content, ContentIndex, SystemObject}; use rapier2d::geometry::ColliderHandle; - -use crate::{camera::Camera, inputstatus::InputStatus, PlayerStatus}; +use std::sync::Arc; /// What the player has selected #[derive(Debug, Clone)] @@ -34,19 +31,11 @@ pub struct PlayerAgent { /// What the player has selected pub selection: PlayerSelection, - - /// This player's camera - pub camera: Camera, - - /// What buttons this player is pressing - pub input: InputStatus, } impl PlayerAgent { pub fn new(ct: &Content, ship: ColliderHandle) -> Self { Self { - input: InputStatus::new(), - camera: Camera::new(), ship: Some(ship), selection: PlayerSelection::OrbitingBody( ct.systems @@ -60,19 +49,4 @@ impl PlayerAgent { ), } } - - pub fn set_camera_aspect(&mut self, v: f32) { - self.camera.aspect = v - } - - pub fn step(&mut self, ct: &Content, status: PlayerStatus) { - if self.input.get_v_scroll() != 0.0 { - self.camera.zoom = (self.camera.zoom + self.input.get_v_scroll()) - .clamp(ct.config.zoom_min, ct.config.zoom_max); - } - - if status.pos.is_some() { - self.camera.pos = status.pos.unwrap(); - } - } }