outfly/src/common.rs

100 lines
3.5 KiB
Rust
Raw Normal View History

2024-05-12 22:44:03 +00:00
// ▄████████▄ + ███ + ▄█████████ ███ +
// ███▀ ▀███ + + ███ ███▀ + ███ + +
// ███ + ███ ███ ███ █████████ ███ ███ ███ ███
// ███ +███ ███ ███ ███ ███▐██████ ███ ███ ███
// ███ + ███ ███+ ███ +███ ███ + ███ ███ + ███
// ███▄ ▄███ ███▄ ███ ███ + ███ + ███ ███▄ ███
// ▀████████▀ + ▀███████ ███▄ ███▄ ▀████ ▀███████
// + + + ███
// + ▀████████████████████████████████████████████████████▀
//
2024-05-12 22:48:41 +00:00
// Various common functions and constants
2024-05-12 22:44:03 +00:00
use bevy::prelude::*;
2024-05-13 02:47:47 +00:00
use crate::prelude::*;
2024-05-12 22:44:03 +00:00
pub use bevy::math::{DVec3, DQuat};
2024-05-12 23:00:09 +00:00
pub use std::f32::consts::PI as PI32;
pub use std::f64::consts::PI;
2024-05-14 20:41:02 +00:00
pub const GAME_NAME: &str = "OutFly";
2024-05-12 23:42:14 +00:00
pub const CONF_FILE: &str = "outfly.toml";
pub const FONT: &str = "fonts/Yupiter-Regular.ttf";
2024-05-12 22:48:41 +00:00
pub const EPSILON32: f32 = 1e-9;
pub const EPSILON: f64 = 1e-9;
2024-05-12 22:44:03 +00:00
#[inline]
2024-05-12 23:00:09 +00:00
pub fn bool2vis(boolean: bool) -> Visibility {
2024-05-12 22:44:03 +00:00
if boolean {
2024-05-12 23:00:09 +00:00
Visibility::Inherited
2024-05-12 22:44:03 +00:00
} else {
2024-05-12 23:00:09 +00:00
Visibility::Hidden
2024-05-12 22:44:03 +00:00
}
}
#[inline]
pub fn style_fullscreen() -> Style {
Style {
width: Val::Vw(100.0),
height: Val::Vh(100.0),
position_type: PositionType::Absolute,
top: Val::Px(0.0),
left: Val::Px(0.0),
..default()
}
}
#[inline]
pub fn style_centered() -> Style {
Style {
width: Val::Percent(100.0),
height: Val::Percent(100.0),
align_items: AlignItems::Center,
justify_content: JustifyContent::SpaceAround,
..default()
}
}
2024-05-13 02:47:47 +00:00
pub fn alive(settings: Res<Settings>) -> bool {
return settings.alive;
}
2024-05-13 18:21:56 +00:00
pub fn in_control(settings: Res<Settings>) -> bool {
return settings.in_control();
}
2024-05-14 02:35:45 +00:00
pub fn in_shadow(
light_source_pos: DVec3,
light_source_r: f64,
shadow_caster_pos: DVec3,
shadow_caster_r: f64,
camera_pos: DVec3
) -> bool {
if light_source_r < shadow_caster_r {
error!("common::in_shadow only works if light_source_r > shadow_caster_r");
return false;
}
let direction = (shadow_caster_pos - light_source_pos).normalize();
let shadow_caster_to_player = camera_pos - shadow_caster_pos;
// Calculate the distance to the shadow caster if the player was projected
// onto the line of the shadow, with positive numbers = in the shadow,
// negative numbers = in between light source and shadow caster.
let projection_length = shadow_caster_to_player.dot(direction);
if projection_length < 0.0 {
return false;
}
let projection = projection_length * direction;
// Calculate the vector to the closest point along the shadow line
let closest_vector = shadow_caster_to_player - projection;
let distance_between_light_and_caster = (shadow_caster_pos - light_source_pos).length();
let max_distance = shadow_caster_r + ((shadow_caster_r - light_source_r) / distance_between_light_and_caster) * projection_length;
return closest_vector.length() < max_distance;
}