From a8f211680413dd41dd9eac679e52f25f28b1b4ac Mon Sep 17 00:00:00 2001 From: extrawurst Date: Fri, 20 Jun 2025 17:19:28 +0200 Subject: [PATCH] support empty head slots --- src/heads/heads_ui.rs | 7 ++++--- src/heads/mod.rs | 35 +++++++++++++++++++---------------- src/player.rs | 5 ++--- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/heads/heads_ui.rs b/src/heads/heads_ui.rs index 0da64ff..065d1c4 100644 --- a/src/heads/heads_ui.rs +++ b/src/heads/heads_ui.rs @@ -21,7 +21,7 @@ struct HeadDamage(pub usize); #[reflect(Resource)] struct UiActiveHeads { heads: [Option; 5], - current_slot: usize, + selected_slot: usize, } pub fn plugin(app: &mut App) { @@ -170,7 +170,7 @@ fn update( } } for (HeadSelector(head), mut vis) in head_selector.iter_mut() { - *vis = if *head == res.current_slot { + *vis = if *head == res.selected_slot { Visibility::Visible } else { Visibility::Hidden @@ -224,7 +224,8 @@ fn sync( }; if active_heads.is_changed() || active_heads.reloading() { - state.current_slot = active_heads.slot(); + state.selected_slot = active_heads.selected_slot; + for i in 0..HEAD_SLOTS { state.heads[i] = active_heads .head(i) diff --git a/src/heads/mod.rs b/src/heads/mod.rs index 54d9c7c..9b770a5 100644 --- a/src/heads/mod.rs +++ b/src/heads/mod.rs @@ -54,6 +54,7 @@ impl HeadState { pub struct ActiveHeads { heads: [Option; 5], current_slot: usize, + selected_slot: usize, } impl ActiveHeads { @@ -61,6 +62,7 @@ impl ActiveHeads { Self { heads, current_slot: 0, + selected_slot: 0, } } @@ -111,10 +113,6 @@ impl ActiveHeads { } } - pub fn slot(&self) -> usize { - self.current_slot - } - pub fn head(&self, slot: usize) -> Option { self.heads[slot] } @@ -221,19 +219,24 @@ fn on_select_active_head( match trigger.event() { SelectActiveHead::Right => { - active_heads.current_slot = (active_heads.current_slot + 1) % HEAD_SLOTS; + active_heads.selected_slot = (active_heads.selected_slot + 1) % HEAD_SLOTS; } SelectActiveHead::Left => { - active_heads.current_slot = (active_heads.current_slot + (HEAD_SLOTS - 1)) % HEAD_SLOTS; + active_heads.selected_slot = + (active_heads.selected_slot + (HEAD_SLOTS - 1)) % HEAD_SLOTS; } } - hp.set_health(active_heads.current().unwrap().health); - commands.trigger(PlaySound::Selection); - commands.trigger(HeadChanged( - active_heads.heads[active_heads.current_slot].unwrap().head, - )); + + if active_heads.head(active_heads.selected_slot).is_some() { + active_heads.current_slot = active_heads.selected_slot; + hp.set_health(active_heads.current().unwrap().health); + + commands.trigger(HeadChanged( + active_heads.heads[active_heads.current_slot].unwrap().head, + )); + } } fn on_swap_backpack( @@ -250,12 +253,12 @@ fn on_swap_backpack( return; }; - let current_active_slot = active_heads.current_slot; + let selected_slot = active_heads.selected_slot; - let current_active_head = active_heads.heads[current_active_slot]; - active_heads.heads[current_active_slot] = Some(*head); + let selected_head = active_heads.heads[selected_slot]; + active_heads.heads[selected_slot] = Some(*head); - if let Some(old_active) = current_active_head { + if let Some(old_active) = selected_head { backpack.heads[backpack_slot] = old_active; } else { backpack.heads.remove(backpack_slot); @@ -264,6 +267,6 @@ fn on_swap_backpack( hp.set_health(active_heads.current().unwrap().health); commands.trigger(HeadChanged( - active_heads.heads[active_heads.current_slot].unwrap().head, + active_heads.heads[active_heads.selected_slot].unwrap().head, )); } diff --git a/src/player.rs b/src/player.rs index 4c1453d..e60cd6d 100644 --- a/src/player.rs +++ b/src/player.rs @@ -46,7 +46,7 @@ pub fn plugin(app: &mut App) { .run_if(in_state(GameState::Playing)), ); - global_observer!(app, on_update_head); + global_observer!(app, on_update_head_mesh); } fn spawn( @@ -78,7 +78,6 @@ fn spawn( CameraTarget, transform, Visibility::default(), - // LockedAxes::ROTATION_LOCKED, todo CollisionLayers::new( GameLayer::Player, LayerMask::ALL & !GameLayer::CollectiblePhysics.to_bits(), @@ -200,7 +199,7 @@ fn toggle_animation( } } -fn on_update_head( +fn on_update_head_mesh( trigger: Trigger, mut commands: Commands, body_mesh: Single>,