Files
HEDZReloaded/crates/shared/src/heads/heads_ui.rs
PROMETHIA-27 b83e506a4d Bevy 0.17 Migration Final PR (#76)
* Get bevy 0.17 compiling and running (#72)

* get bevy 0.17 compiling and running

* try to fix CI breaking from const assertion for client/server features

* fix `bin` -> `lib` for `shared` in CI

* typo

* fix some collider issues (#73)

* Physics/controller improvements (#74)

* trying to fix physics prediction

* fixed prediction desync

* substantial controller improvements

* Finish off main bevy 0.17 migration (#75)

* fix lookdir issues
- airplane moving backwards
- player model facing backwards
- camera was technically backwards the whole time, and player models were facing the right way; camera is now facing forwards
- firing without a target now respects lookdir

* fix aim targeting

* migrate to bevy_trenchbroom 0.10 crates release

* fixed colliders not being adjusted out of worldspace

* predict platforms to stop constant rollbacks while riding them

* fix key/head drop visuals not working

* Fix key/head drop random initial force

* fixed static head drops duplicating

* fix platform velocity inheritance

* fix thrown projectiles not autorotating

* fix inconsistent explosion animations

* update avian3d to 0.4.1

* fix controller snapping to fixed angle upon switching heads

* clean up commented code

* fix broken physics positions

* Clean comments, fix warnings (#77)

* clean comments, fix warnings

* fix missing import

* steamworks 162 libs

* fix mouselook

---------

Co-authored-by: extrawurst <mail@rusticorn.com>
2025-11-15 09:16:38 -05:00

243 lines
7.3 KiB
Rust

#[cfg(feature = "server")]
use super::ActiveHeads;
use super::HEAD_SLOTS;
#[cfg(feature = "client")]
use super::HeadsImages;
#[cfg(feature = "server")]
use crate::player::Player;
use crate::{GameState, backpack::UiHeadState, loading_assets::UIAssets};
use bevy::{ecs::spawn::SpawnIter, prelude::*};
use serde::{Deserialize, Serialize};
use std::f32::consts::PI;
#[derive(Component, Reflect, Default)]
#[reflect(Component)]
struct HeadSelector(pub usize);
#[derive(Component, Reflect, Default)]
#[reflect(Component)]
struct HeadImage(pub usize);
#[derive(Component, Reflect, Default)]
#[reflect(Component)]
struct HeadDamage(pub usize);
#[derive(Component, Default, Reflect, Serialize, Deserialize, PartialEq)]
#[reflect(Component)]
pub struct UiActiveHeads {
heads: [Option<UiHeadState>; 5],
selected_slot: usize,
}
pub fn plugin(app: &mut App) {
app.register_type::<HeadDamage>();
app.register_type::<UiActiveHeads>();
app.add_systems(OnEnter(GameState::Playing), setup);
#[cfg(feature = "server")]
app.add_systems(FixedUpdate, sync.run_if(in_state(GameState::Playing)));
#[cfg(feature = "client")]
app.add_systems(
FixedUpdate,
(update, update_ammo, update_health).run_if(in_state(GameState::Playing)),
);
}
fn setup(mut commands: Commands, assets: Res<UIAssets>) {
commands.spawn((
Name::new("heads-ui"),
Node {
position_type: PositionType::Absolute,
bottom: Val::Px(20.0),
right: Val::Px(20.0),
height: Val::Px(74.0),
..default()
},
Children::spawn(SpawnIter((0..HEAD_SLOTS).map({
let bg = assets.head_bg.clone();
let regular = assets.head_regular.clone();
let selector = assets.head_selector.clone();
let damage = assets.head_damage.clone();
move |i| {
spawn_head_ui(
bg.clone(),
regular.clone(),
selector.clone(),
damage.clone(),
i,
)
}
}))),
));
}
fn spawn_head_ui(
bg: Handle<Image>,
regular: Handle<Image>,
selector: Handle<Image>,
damage: Handle<Image>,
head_slot: usize,
) -> impl Bundle {
const SIZE: f32 = 90.0;
const DAMAGE_SIZE: f32 = 74.0;
(
Node {
position_type: PositionType::Relative,
justify_content: JustifyContent::Center,
align_items: AlignItems::Center,
width: Val::Px(SIZE),
..default()
},
children![
(
Node {
position_type: PositionType::Absolute,
top: Val::Px(-30.0),
..default()
},
Visibility::Hidden,
ImageNode::new(selector),
HeadSelector(head_slot),
),
(
Node {
position_type: PositionType::Absolute,
..default()
},
ImageNode::new(bg),
),
(
Name::new("head-icon"),
Node {
position_type: PositionType::Absolute,
..default()
},
BorderRadius::all(Val::Px(9999.)),
BackgroundGradient::from(ConicGradient {
start: 0.,
stops: vec![
AngularColorStop::new(Color::linear_rgba(0., 0., 0., 0.9), 0.),
AngularColorStop::new(Color::linear_rgba(0., 0., 0., 0.9), PI * 1.5),
AngularColorStop::new(Color::linear_rgba(0., 0., 0., 0.0), PI * 1.5),
],
position: UiPosition::CENTER,
color_space: InterpolationColorSpace::Srgba,
}),
ImageNode::default(),
Visibility::Hidden,
HeadImage(head_slot),
),
(
Node {
position_type: PositionType::Absolute,
..default()
},
ImageNode::new(regular),
),
(
Node {
height: Val::Px(DAMAGE_SIZE),
width: Val::Px(DAMAGE_SIZE),
..default()
},
children![(
HeadDamage(head_slot),
Node {
position_type: PositionType::Absolute,
display: Display::Block,
overflow: Overflow::clip(),
top: Val::Px(0.),
left: Val::Px(0.),
right: Val::Px(0.),
height: Val::Percent(25.),
..default()
},
children![ImageNode::new(damage)]
)]
)
],
)
}
#[cfg(feature = "client")]
fn update(
res: Single<&UiActiveHeads, Changed<UiActiveHeads>>,
heads_images: Res<HeadsImages>,
mut head_image: Query<(&HeadImage, &mut Visibility, &mut ImageNode), Without<HeadSelector>>,
mut head_selector: Query<(&HeadSelector, &mut Visibility), Without<HeadImage>>,
) {
for (HeadImage(head), mut vis, mut image) in head_image.iter_mut() {
if let Some(head) = res.heads[*head] {
*vis = Visibility::Visible;
image.image = heads_images.heads[head.head].clone();
} else {
*vis = Visibility::Hidden;
}
}
for (HeadSelector(head), mut vis) in head_selector.iter_mut() {
*vis = if *head == res.selected_slot {
Visibility::Visible
} else {
Visibility::Hidden
};
}
}
#[cfg(feature = "client")]
fn update_ammo(
res: Single<&UiActiveHeads, Changed<UiActiveHeads>>,
mut gradients: Query<(&mut BackgroundGradient, &HeadImage)>,
) {
for (mut gradient, HeadImage(head)) in gradients.iter_mut() {
if let Some(head) = res.heads[*head] {
let Gradient::Conic(gradient) = &mut gradient.0[0] else {
continue;
};
let progress = if let Some(reloading) = head.reloading() {
1. - reloading
} else {
head.ammo_used()
};
let angle = progress * PI * 2.;
gradient.stops[1].angle = Some(angle);
gradient.stops[2].angle = Some(angle);
}
}
}
#[cfg(feature = "client")]
fn update_health(
res: Single<&UiActiveHeads, Changed<UiActiveHeads>>,
mut query: Query<(&mut Node, &HeadDamage)>,
) {
for (mut node, HeadDamage(head)) in query.iter_mut() {
node.height = Val::Percent(res.heads[*head].map(|head| head.damage()).unwrap_or(0.) * 100.);
}
}
#[cfg(feature = "server")]
fn sync(
active_heads: Query<Ref<ActiveHeads>, With<Player>>,
mut state: Single<&mut UiActiveHeads>,
time: Res<Time>,
) {
let Ok(active_heads) = active_heads.single() else {
return;
};
if active_heads.is_changed() || active_heads.reloading() {
state.selected_slot = active_heads.selected_slot;
for i in 0..HEAD_SLOTS {
state.heads[i] = active_heads
.head(i)
.map(|state| UiHeadState::new(state, time.elapsed_secs()));
}
}
}