use nightshade::prelude::*;
#[inline]
pub fn key_down(world: &World, key: KeyCode) -> bool {
world.resources.input.keyboard.is_key_pressed(key)
}
#[inline]
pub fn key_pressed(world: &World, key: KeyCode) -> bool {
world.resources.input.keyboard.just_pressed(key)
}
#[inline]
pub fn mouse_down(world: &World, button: MouseButton) -> bool {
let state = world.resources.input.mouse.state;
match button {
MouseButton::Left => state.contains(MouseState::LEFT_CLICKED),
MouseButton::Right => state.contains(MouseState::RIGHT_CLICKED),
MouseButton::Middle => state.contains(MouseState::MIDDLE_CLICKED),
_ => false,
}
}
#[inline]
pub fn mouse_clicked(world: &World, button: MouseButton) -> bool {
let state = world.resources.input.mouse.state;
match button {
MouseButton::Left => state.contains(MouseState::LEFT_JUST_PRESSED),
MouseButton::Right => state.contains(MouseState::RIGHT_JUST_PRESSED),
MouseButton::Middle => state.contains(MouseState::MIDDLE_JUST_PRESSED),
_ => false,
}
}
#[inline]
pub fn mouse_position(world: &World) -> Vec2 {
world.resources.input.mouse.position
}
#[inline]
pub fn mouse_delta(world: &World) -> Vec2 {
world.resources.input.mouse.position_delta
}
#[inline]
pub fn axis(world: &World, negative: KeyCode, positive: KeyCode) -> f32 {
let mut value = 0.0;
if key_down(world, negative) {
value -= 1.0;
}
if key_down(world, positive) {
value += 1.0;
}
value
}
pub fn wasd(world: &World) -> Vec3 {
let forward_input = axis(world, KeyCode::KeyS, KeyCode::KeyW);
let strafe_input = axis(world, KeyCode::KeyA, KeyCode::KeyD);
if forward_input == 0.0 && strafe_input == 0.0 {
return Vec3::zeros();
}
let camera_forward = crate::camera::camera_forward(world);
let mut planar_forward = Vec3::new(camera_forward.x, 0.0, camera_forward.z);
if planar_forward.magnitude_squared() < f32::EPSILON {
planar_forward = Vec3::new(0.0, 0.0, -1.0);
}
let planar_forward = planar_forward.normalize();
let right = nalgebra_glm::cross(&planar_forward, &Vec3::y());
(planar_forward * forward_input + right * strafe_input).normalize()
}
#[inline]
pub fn delta_time(world: &World) -> f32 {
world.resources.window.timing.delta_time
}
#[inline]
pub fn elapsed_seconds(world: &World) -> f32 {
world.resources.window.timing.uptime_milliseconds as f32 / 1000.0
}