target ui indicating health too (#17)
This commit is contained in:
160
src/aim/target_ui.rs
Normal file
160
src/aim/target_ui.rs
Normal file
@@ -0,0 +1,160 @@
|
||||
use super::AimState;
|
||||
use crate::{
|
||||
backpack::BackpackHead,
|
||||
heads_ui::HeadsImages,
|
||||
npc::{Hitpoints, NpcHead},
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component, Reflect, Default)]
|
||||
#[reflect(Component)]
|
||||
struct HeadImage;
|
||||
|
||||
#[derive(Component, Reflect, Default)]
|
||||
#[reflect(Component)]
|
||||
struct HeadDamage;
|
||||
|
||||
#[derive(Resource, Default, PartialEq)]
|
||||
struct TargetUi {
|
||||
head: Option<BackpackHead>,
|
||||
}
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.add_systems(Startup, setup);
|
||||
app.add_systems(Update, (sync, update));
|
||||
}
|
||||
|
||||
fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
|
||||
let bg = asset_server.load("ui/head_bg.png");
|
||||
let regular = asset_server.load("ui/head_regular.png");
|
||||
let damage = asset_server.load("ui/head_damage.png");
|
||||
|
||||
commands
|
||||
.spawn((
|
||||
Name::new("target-ui"),
|
||||
Node {
|
||||
position_type: PositionType::Absolute,
|
||||
top: Val::Px(150.0),
|
||||
left: Val::Px(20.0),
|
||||
height: Val::Px(74.0),
|
||||
..default()
|
||||
},
|
||||
))
|
||||
.with_children(|parent| {
|
||||
spawn_head_ui(parent, bg.clone(), regular.clone(), damage.clone());
|
||||
});
|
||||
|
||||
commands.insert_resource(TargetUi::default());
|
||||
}
|
||||
|
||||
fn spawn_head_ui(
|
||||
parent: &mut ChildBuilder,
|
||||
bg: Handle<Image>,
|
||||
regular: Handle<Image>,
|
||||
damage: Handle<Image>,
|
||||
) {
|
||||
const SIZE: f32 = 90.0;
|
||||
const DAMAGE_SIZE: f32 = 74.0;
|
||||
|
||||
parent
|
||||
.spawn((Node {
|
||||
position_type: PositionType::Relative,
|
||||
justify_content: JustifyContent::Center,
|
||||
align_items: AlignItems::Center,
|
||||
width: Val::Px(SIZE),
|
||||
..default()
|
||||
},))
|
||||
.with_children(|parent| {
|
||||
parent.spawn((
|
||||
Node {
|
||||
position_type: PositionType::Absolute,
|
||||
..default()
|
||||
},
|
||||
ImageNode::new(bg),
|
||||
));
|
||||
parent.spawn((
|
||||
Node {
|
||||
position_type: PositionType::Absolute,
|
||||
..default()
|
||||
},
|
||||
ImageNode::default(),
|
||||
Visibility::Hidden,
|
||||
HeadImage,
|
||||
));
|
||||
parent.spawn((
|
||||
Node {
|
||||
position_type: PositionType::Absolute,
|
||||
..default()
|
||||
},
|
||||
ImageNode::new(regular),
|
||||
));
|
||||
parent
|
||||
.spawn((Node {
|
||||
height: Val::Px(DAMAGE_SIZE),
|
||||
width: Val::Px(DAMAGE_SIZE),
|
||||
..default()
|
||||
},))
|
||||
.with_children(|parent| {
|
||||
parent
|
||||
.spawn((
|
||||
HeadDamage,
|
||||
Node {
|
||||
position_type: PositionType::Absolute,
|
||||
display: Display::Block,
|
||||
overflow: Overflow::clip(),
|
||||
top: Val::Px(0.),
|
||||
left: Val::Px(0.),
|
||||
right: Val::Px(0.),
|
||||
height: Val::Percent(25.),
|
||||
..default()
|
||||
},
|
||||
))
|
||||
.with_child(ImageNode::new(damage));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn update(
|
||||
target: Res<TargetUi>,
|
||||
heads_images: Res<HeadsImages>,
|
||||
mut head_image: Query<
|
||||
(&mut Visibility, &mut ImageNode),
|
||||
(Without<HeadDamage>, With<HeadImage>),
|
||||
>,
|
||||
mut head_damage: Query<&mut Node, (With<HeadDamage>, Without<HeadImage>)>,
|
||||
) {
|
||||
if target.is_changed() {
|
||||
if let Ok((mut vis, mut image)) = head_image.get_single_mut() {
|
||||
if let Some(head) = target.head {
|
||||
*vis = Visibility::Visible;
|
||||
image.image = heads_images.heads[head.head].clone();
|
||||
} else {
|
||||
*vis = Visibility::Hidden;
|
||||
}
|
||||
}
|
||||
|
||||
if let Ok(mut node) = head_damage.get_single_mut() {
|
||||
node.height = Val::Percent(target.head.map(|head| head.damage()).unwrap_or(0.) * 100.);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn sync(
|
||||
mut target: ResMut<TargetUi>,
|
||||
aim: Res<AimState>,
|
||||
target_data: Query<(&Hitpoints, &NpcHead)>,
|
||||
) {
|
||||
let mut new_state = None;
|
||||
if let Some(e) = aim.target {
|
||||
if let Ok((hp, head)) = target_data.get(e) {
|
||||
new_state = Some(BackpackHead {
|
||||
head: head.0,
|
||||
health: hp.health(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if new_state != target.head {
|
||||
target.head = new_state;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user