Galactica/crates/playeragent/src/playeragent.rs

59 lines
1.5 KiB
Rust
Raw Normal View History

2024-01-11 11:28:51 -08:00
use galactica_content::Content;
2024-01-11 22:10:36 -08:00
use rapier2d::geometry::ColliderHandle;
2024-01-11 11:28:51 -08:00
use winit::event::{ElementState, MouseButton, MouseScrollDelta, TouchPhase, VirtualKeyCode};
use crate::{camera::Camera, inputstatus::InputStatus, PlayerStatus};
pub struct PlayerAgent {
/// Which ship this player is controlling
2024-01-11 22:10:36 -08:00
pub ship: Option<ColliderHandle>,
2024-01-11 11:28:51 -08:00
/// This player's camera
pub camera: Camera,
/// What buttons this player is pressing
pub input: InputStatus,
/// What the player currently has selected
pub selection: Option<()>,
}
impl PlayerAgent {
2024-01-11 22:10:36 -08:00
pub fn new(ship: ColliderHandle) -> Self {
2024-01-11 11:28:51 -08:00
Self {
input: InputStatus::new(),
camera: Camera::new(),
ship: Some(ship),
selection: None,
}
}
pub fn set_camera_aspect(&mut self, v: f32) {
self.camera.aspect = v
}
pub fn process_key(&mut self, state: &ElementState, key: &VirtualKeyCode) {
self.input.process_key(state, key)
}
pub fn process_click(&mut self, state: &ElementState, key: &MouseButton) {
self.input.process_click(state, key)
}
pub fn process_scroll(&mut self, delta: &MouseScrollDelta, phase: &TouchPhase) {
self.input.process_scroll(delta, phase)
}
pub fn step(&mut self, ct: &Content, status: PlayerStatus) {
if self.input.v_scroll != 0.0 {
self.camera.zoom = (self.camera.zoom + self.input.v_scroll)
.clamp(ct.get_config().zoom_min, ct.get_config().zoom_max);
self.input.v_scroll = 0.0;
}
2024-01-11 20:21:07 -08:00
if status.pos.is_some() {
self.camera.pos = status.pos.unwrap();
}
2024-01-11 11:28:51 -08:00
}
}