Stephan/hez 2 npc should loose head if 0 health (#34)
This commit is contained in:
@@ -181,7 +181,7 @@ fn spawn_head_ui(
|
||||
top: Val::Px(0.),
|
||||
left: Val::Px(0.),
|
||||
right: Val::Px(0.),
|
||||
height: Val::Percent(25.),
|
||||
height: Val::Percent(0.),
|
||||
..default()
|
||||
},
|
||||
))
|
||||
|
||||
@@ -2,8 +2,7 @@ mod backpack_ui;
|
||||
mod ui_head_state;
|
||||
|
||||
use crate::{
|
||||
GameState,
|
||||
heads::{HEAD_COUNT, HeadState},
|
||||
cash::CashCollectEvent, global_observer, head_drop::HeadCollected, heads::HeadState,
|
||||
heads_database::HeadsDatabase,
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
@@ -26,21 +25,38 @@ impl Backpack {
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
pub fn contains(&self, head_id: usize) -> bool {
|
||||
self.heads.iter().any(|head| head.head == head_id)
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, head_id: usize, heads_db: &HeadsDatabase) {
|
||||
self.heads.push(HeadState::new(head_id, heads_db));
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Event)]
|
||||
pub struct BackbackSwapEvent(pub usize);
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.init_resource::<Backpack>();
|
||||
|
||||
app.add_plugins(backpack_ui::plugin);
|
||||
|
||||
app.add_systems(OnEnter(GameState::Playing), setup);
|
||||
global_observer!(app, on_head_collect);
|
||||
}
|
||||
|
||||
fn setup(mut commands: Commands, heads: Res<HeadsDatabase>) {
|
||||
commands.insert_resource(Backpack {
|
||||
heads: (0usize..HEAD_COUNT)
|
||||
.map(|i| HeadState::new(i, heads.as_ref()))
|
||||
.collect(),
|
||||
});
|
||||
fn on_head_collect(
|
||||
trigger: Trigger<HeadCollected>,
|
||||
mut cmds: Commands,
|
||||
mut backpack: ResMut<Backpack>,
|
||||
heads_db: Res<HeadsDatabase>,
|
||||
) {
|
||||
let HeadCollected(head) = *trigger.event();
|
||||
|
||||
if backpack.contains(head) {
|
||||
cmds.trigger(CashCollectEvent);
|
||||
} else {
|
||||
backpack.insert(head, heads_db.as_ref());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user