moltrun 1.7.2

High-performance game engine library with AI capabilities, built on wgpu for modern 3D graphics and physics simulation
Documentation
use crate::math::Vec2;
use crate::core::InputState;
use winit::keyboard::KeyCode;

/// 2D 카메라 - 월드 공간에서 뷰 공간으로의 변환을 담당
pub struct Camera {
    /// 카메라의 월드 좌표 위치
    pub position: Vec2,
    /// 카메라 이동 속도 (pixels per second)
    pub speed: f32,
}

impl Camera {
    /// 기본 카메라 생성 (원점, 200px/s 속도)
    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,
        }
    }
    
    /// 카메라 업데이트 - WASD 입력 처리
    pub fn update(&mut self, delta_time: f32, input: &InputState) {
        let speed = self.speed * delta_time;
        
        // WASD 키로 카메라 이동
        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()
    }
}