dynamic crosshair stuff

This commit is contained in:
griffi-gh 2024-05-02 00:43:24 +02:00
parent 66d3ea656b
commit 610d309ead
2 changed files with 62 additions and 55 deletions

View file

@ -19,6 +19,7 @@ pub struct GameSettings {
pub render_distance: u8,
pub mouse_sensitivity: f32,
pub debug_draw_current_chunk_border: bool,
pub dynamic_crosshair: bool,
}
impl Default for GameSettings {
fn default() -> Self {
@ -34,6 +35,7 @@ impl Default for GameSettings {
},
mouse_sensitivity: 1.,
debug_draw_current_chunk_border: false, //cfg!(not(target_os = "android")) && cfg!(debug_assertions),
dynamic_crosshair: true,
}
}
}

View file

@ -1,6 +1,6 @@
use std::f32::consts::PI;
use glam::uvec2;
use glam::{uvec2, Vec2};
use hui::{
draw::{ImageHandle, TextureFormat},
element::{container::Container, image::Image, transformer::ElementTransformExt, UiElementExt},
@ -8,7 +8,7 @@ use hui::{
size
};
use shipyard::{AllStoragesViewMut, IntoIter, NonSendSync, Unique, UniqueView, UniqueViewMut, View};
use crate::{hui_integration::UiState, player::MainPlayer, rendering::WindowSize, world::raycast::LookingAtBlock};
use crate::{hui_integration::UiState, player::MainPlayer, rendering::WindowSize, settings::GameSettings, world::raycast::LookingAtBlock};
const CROSSHAIR_SIZE: usize = 9;
const CROSSHAIR: &[u8] = &[
@ -38,10 +38,15 @@ pub fn draw_crosshair(
size: UniqueView<WindowSize>,
player: View<MainPlayer>,
raycast: View<LookingAtBlock>,
settings: UniqueView<GameSettings>,
) {
let mut cursor_active = false;
let mut active = false;
if settings.dynamic_crosshair {
if let Some((_, raycast)) = (&player, &raycast).iter().next() {
cursor_active = raycast.0.is_some();
active = raycast.0.is_some();
}
} else {
active = true;
}
Container::default()
@ -49,11 +54,11 @@ pub fn draw_crosshair(
.with_align(Alignment::Center)
.with_children(|ui| {
Image::new(crosshair.0)
.with_color((1., 1., 1., 0.5))
.with_color((1., 1., 1., if active { 0.5 } else { 0.3 }))
.with_size(size!(18, 18))
.transform()
.scale(glam::Vec2::splat(if cursor_active { 1. } else { 0.66 }))
.rotate(if cursor_active { 0. } else { PI / 4. })
.scale(Vec2::splat(if active { 1. } else { 0.66 }))
.rotate(if active { 0. } else { PI / 4. })
.add_child(ui);
})
.add_root(&mut ui.hui, uvec2(size.0.x & !1, size.0.y & !1).as_vec2());