support empty head slots

This commit is contained in:
2025-06-20 17:19:28 +02:00
parent 66cbff654f
commit a8f2116804
3 changed files with 25 additions and 22 deletions

View File

@@ -21,7 +21,7 @@ struct HeadDamage(pub usize);
#[reflect(Resource)]
struct UiActiveHeads {
heads: [Option<UiHeadState>; 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)

View File

@@ -54,6 +54,7 @@ impl HeadState {
pub struct ActiveHeads {
heads: [Option<HeadState>; 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<HeadState> {
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,
));
}

View File

@@ -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<HeadChanged>,
mut commands: Commands,
body_mesh: Single<Entity, With<PlayerBodyMesh>>,