Galactica/crates/playeragent/src/playeragent.rs

83 lines
2.0 KiB
Rust
Raw Normal View History

2024-01-12 14:34:31 -08:00
use galactica_content::{Content, SystemHandle, SystemObjectHandle};
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};
2024-01-12 14:34:31 -08:00
/// What the player has selected
#[derive(Debug, Clone, Copy)]
pub enum PlayerSelection {
/// We have nothing selected
None,
/// We have a system body selected
OrbitingBody(SystemObjectHandle),
/// We have a ship selected
Ship(ColliderHandle),
}
impl PlayerSelection {
pub fn get_planet(&self) -> Option<SystemObjectHandle> {
match self {
Self::OrbitingBody(h) => Some(*h),
_ => None,
}
}
}
2024-01-11 11:28:51 -08:00
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
2024-01-12 14:34:31 -08:00
/// What the player has selected
pub selection: PlayerSelection,
2024-01-11 11:28:51 -08:00
/// This player's camera
pub camera: Camera,
/// What buttons this player is pressing
pub input: InputStatus,
}
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),
2024-01-12 14:34:31 -08:00
selection: PlayerSelection::OrbitingBody(SystemObjectHandle {
system_handle: SystemHandle { index: 0 },
body_index: 1,
}),
2024-01-11 11:28:51 -08:00
}
}
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) {
2024-01-12 14:34:31 -08:00
if self.input.get_v_scroll() != 0.0 {
self.camera.zoom = (self.camera.zoom + self.input.get_v_scroll())
2024-01-11 11:28:51 -08:00
.clamp(ct.get_config().zoom_min, ct.get_config().zoom_max);
}
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
}
}