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)] #[reflect(Resource)]
struct UiActiveHeads { struct UiActiveHeads {
heads: [Option<UiHeadState>; 5], heads: [Option<UiHeadState>; 5],
current_slot: usize, selected_slot: usize,
} }
pub fn plugin(app: &mut App) { pub fn plugin(app: &mut App) {
@@ -170,7 +170,7 @@ fn update(
} }
} }
for (HeadSelector(head), mut vis) in head_selector.iter_mut() { for (HeadSelector(head), mut vis) in head_selector.iter_mut() {
*vis = if *head == res.current_slot { *vis = if *head == res.selected_slot {
Visibility::Visible Visibility::Visible
} else { } else {
Visibility::Hidden Visibility::Hidden
@@ -224,7 +224,8 @@ fn sync(
}; };
if active_heads.is_changed() || active_heads.reloading() { 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 { for i in 0..HEAD_SLOTS {
state.heads[i] = active_heads state.heads[i] = active_heads
.head(i) .head(i)

View File

@@ -54,6 +54,7 @@ impl HeadState {
pub struct ActiveHeads { pub struct ActiveHeads {
heads: [Option<HeadState>; 5], heads: [Option<HeadState>; 5],
current_slot: usize, current_slot: usize,
selected_slot: usize,
} }
impl ActiveHeads { impl ActiveHeads {
@@ -61,6 +62,7 @@ impl ActiveHeads {
Self { Self {
heads, heads,
current_slot: 0, 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> { pub fn head(&self, slot: usize) -> Option<HeadState> {
self.heads[slot] self.heads[slot]
} }
@@ -221,20 +219,25 @@ fn on_select_active_head(
match trigger.event() { match trigger.event() {
SelectActiveHead::Right => { 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 => { 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(PlaySound::Selection);
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( commands.trigger(HeadChanged(
active_heads.heads[active_heads.current_slot].unwrap().head, active_heads.heads[active_heads.current_slot].unwrap().head,
)); ));
} }
}
fn on_swap_backpack( fn on_swap_backpack(
trigger: Trigger<BackbackSwapEvent>, trigger: Trigger<BackbackSwapEvent>,
@@ -250,12 +253,12 @@ fn on_swap_backpack(
return; 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]; let selected_head = active_heads.heads[selected_slot];
active_heads.heads[current_active_slot] = Some(*head); 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; backpack.heads[backpack_slot] = old_active;
} else { } else {
backpack.heads.remove(backpack_slot); backpack.heads.remove(backpack_slot);
@@ -264,6 +267,6 @@ fn on_swap_backpack(
hp.set_health(active_heads.current().unwrap().health); hp.set_health(active_heads.current().unwrap().health);
commands.trigger(HeadChanged( 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)), .run_if(in_state(GameState::Playing)),
); );
global_observer!(app, on_update_head); global_observer!(app, on_update_head_mesh);
} }
fn spawn( fn spawn(
@@ -78,7 +78,6 @@ fn spawn(
CameraTarget, CameraTarget,
transform, transform,
Visibility::default(), Visibility::default(),
// LockedAxes::ROTATION_LOCKED, todo
CollisionLayers::new( CollisionLayers::new(
GameLayer::Player, GameLayer::Player,
LayerMask::ALL & !GameLayer::CollectiblePhysics.to_bits(), 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>, trigger: Trigger<HeadChanged>,
mut commands: Commands, mut commands: Commands,
body_mesh: Single<Entity, With<PlayerBodyMesh>>, body_mesh: Single<Entity, With<PlayerBodyMesh>>,