use crate::math::Vec2;
use crate::core::InputState;
use winit::keyboard::KeyCode;
pub struct Camera {
pub position: Vec2,
pub speed: f32,
}
impl Camera {
pub fn new() -> Self {
Self {
position: Vec2::zero(),
speed: 200.0,
}
}
pub fn at_position(position: Vec2) -> Self {
Self {
position,
speed: 200.0,
}
}
pub fn with_speed(position: Vec2, speed: f32) -> Self {
Self {
position,
speed,
}
}
pub fn update(&mut self, delta_time: f32, input: &InputState) {
let speed = self.speed * delta_time;
if input.is_key_down(KeyCode::KeyW) {
self.position.y -= speed; }
if input.is_key_down(KeyCode::KeyS) {
self.position.y += speed; }
if input.is_key_down(KeyCode::KeyA) {
self.position.x -= speed; }
if input.is_key_down(KeyCode::KeyD) {
self.position.x += speed; }
}
pub fn world_to_view(&self, world_pos: Vec2) -> Vec2 {
world_pos - self.position
}
pub fn set_position(&mut self, position: Vec2) {
self.position = position;
}
pub fn translate(&mut self, delta: Vec2) {
self.position = self.position + delta;
}
}
impl Default for Camera {
fn default() -> Self {
Self::new()
}
}