player can take damage
This commit is contained in:
@@ -4,7 +4,8 @@ use crate::{
|
||||
GameState,
|
||||
abilities::HeadAbility,
|
||||
backpack::{BackbackSwapEvent, Backpack},
|
||||
player::head_id_to_str,
|
||||
hitpoints::Hitpoints,
|
||||
player::{Player, head_id_to_str},
|
||||
sounds::PlaySound,
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
@@ -92,6 +93,15 @@ impl ActiveHeads {
|
||||
|
||||
false
|
||||
}
|
||||
|
||||
pub fn set_hitpoint(&mut self, hp: &Hitpoints) {
|
||||
let Some(head) = &mut self.heads[self.current_slot] else {
|
||||
error!("cannot use ammo of empty head");
|
||||
return;
|
||||
};
|
||||
|
||||
(head.health, head.health_max) = hp.get()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Event, Reflect)]
|
||||
@@ -118,7 +128,10 @@ pub fn plugin(app: &mut App) {
|
||||
});
|
||||
|
||||
app.add_systems(OnEnter(GameState::Playing), setup);
|
||||
app.add_systems(Update, reload.run_if(in_state(GameState::Playing)));
|
||||
app.add_systems(
|
||||
Update,
|
||||
(reload, sync_hp).run_if(in_state(GameState::Playing)),
|
||||
);
|
||||
|
||||
app.add_observer(on_select_active_head);
|
||||
app.add_observer(on_swap_backpack);
|
||||
@@ -133,6 +146,14 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
commands.insert_resource(HeadsImages { heads });
|
||||
}
|
||||
|
||||
fn sync_hp(mut active: ResMut<ActiveHeads>, query: Query<&Hitpoints, With<Player>>) {
|
||||
let Ok(hp) = query.get_single() else {
|
||||
return;
|
||||
};
|
||||
|
||||
active.set_hitpoint(hp);
|
||||
}
|
||||
|
||||
fn reload(mut commands: Commands, mut active: ResMut<ActiveHeads>, time: Res<Time>) {
|
||||
if !active.reloading() {
|
||||
return;
|
||||
@@ -154,6 +175,7 @@ fn on_select_active_head(
|
||||
trigger: Trigger<SelectActiveHead>,
|
||||
mut commands: Commands,
|
||||
mut res: ResMut<ActiveHeads>,
|
||||
mut query: Query<&mut Hitpoints, With<Player>>,
|
||||
) {
|
||||
match trigger.event() {
|
||||
SelectActiveHead::Right => {
|
||||
@@ -164,6 +186,12 @@ fn on_select_active_head(
|
||||
}
|
||||
}
|
||||
|
||||
// sync health back into Hitpoints
|
||||
let Ok(mut hp) = query.get_single_mut() else {
|
||||
return;
|
||||
};
|
||||
hp.set_health(res.current().unwrap().health);
|
||||
|
||||
commands.trigger(PlaySound::Selection);
|
||||
commands.trigger(HeadChanged(res.heads[res.current_slot].unwrap().head));
|
||||
}
|
||||
@@ -173,6 +201,7 @@ fn on_swap_backpack(
|
||||
mut commands: Commands,
|
||||
mut res: ResMut<ActiveHeads>,
|
||||
mut backpack: ResMut<Backpack>,
|
||||
mut query: Query<&mut Hitpoints, With<Player>>,
|
||||
) {
|
||||
let backpack_slot = trigger.event().0;
|
||||
|
||||
@@ -189,5 +218,11 @@ fn on_swap_backpack(
|
||||
backpack.heads.remove(backpack_slot);
|
||||
}
|
||||
|
||||
// sync health back into Hitpoints
|
||||
let Ok(mut hp) = query.get_single_mut() else {
|
||||
return;
|
||||
};
|
||||
hp.set_health(res.current().unwrap().health);
|
||||
|
||||
commands.trigger(HeadChanged(res.heads[res.current_slot].unwrap().head));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user