Target UI multiplayer (#95)
This commit is contained in:
8
crates/hedz_reloaded/src/client/aim.rs
Normal file
8
crates/hedz_reloaded/src/client/aim.rs
Normal file
@@ -0,0 +1,8 @@
|
||||
use bevy::prelude::*;
|
||||
|
||||
pub mod marker;
|
||||
pub mod target_ui;
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.add_plugins((marker::plugin, target_ui::plugin));
|
||||
}
|
||||
58
crates/hedz_reloaded/src/client/aim/marker.rs
Normal file
58
crates/hedz_reloaded/src/client/aim/marker.rs
Normal file
@@ -0,0 +1,58 @@
|
||||
use crate::{
|
||||
GameState, aim::MarkerEvent, global_observer, loading_assets::UIAssets,
|
||||
utils::billboards::Billboard,
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
use bevy_sprite3d::Sprite3d;
|
||||
use ops::sin;
|
||||
|
||||
#[derive(Component, Reflect)]
|
||||
#[reflect(Component)]
|
||||
struct TargetMarker;
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.add_systems(Update, move_marker.run_if(in_state(GameState::Playing)));
|
||||
global_observer!(app, marker_event);
|
||||
}
|
||||
|
||||
fn move_marker(mut query: Query<&mut Transform, With<TargetMarker>>, time: Res<Time>) {
|
||||
for mut transform in query.iter_mut() {
|
||||
transform.translation = Vec3::new(0., 3. + (sin(time.elapsed_secs() * 6.) * 0.2), 0.);
|
||||
}
|
||||
}
|
||||
|
||||
fn marker_event(
|
||||
trigger: On<MarkerEvent>,
|
||||
mut commands: Commands,
|
||||
assets: Res<UIAssets>,
|
||||
marker: Query<Entity, With<TargetMarker>>,
|
||||
) {
|
||||
for m in marker.iter() {
|
||||
commands.entity(m).despawn();
|
||||
}
|
||||
|
||||
let MarkerEvent::Spawn(target) = trigger.event() else {
|
||||
return;
|
||||
};
|
||||
|
||||
let id = commands
|
||||
.spawn((
|
||||
Name::new("aim-marker"),
|
||||
Billboard::All,
|
||||
TargetMarker,
|
||||
Transform::default(),
|
||||
Sprite3d {
|
||||
pixels_per_metre: 30.,
|
||||
alpha_mode: AlphaMode::Blend,
|
||||
unlit: true,
|
||||
..default()
|
||||
},
|
||||
Sprite {
|
||||
image: assets.head_selector.clone(),
|
||||
..default()
|
||||
},
|
||||
))
|
||||
.id();
|
||||
|
||||
commands.entity(*target).add_child(id);
|
||||
}
|
||||
156
crates/hedz_reloaded/src/client/aim/target_ui.rs
Normal file
156
crates/hedz_reloaded/src/client/aim/target_ui.rs
Normal file
@@ -0,0 +1,156 @@
|
||||
use crate::{
|
||||
GameState,
|
||||
aim::AimTarget,
|
||||
backpack::UiHeadState,
|
||||
heads::{ActiveHeads, HeadsImages},
|
||||
hitpoints::Hitpoints,
|
||||
loading_assets::UIAssets,
|
||||
npc::Npc,
|
||||
player::LocalPlayer,
|
||||
};
|
||||
use bevy::prelude::*;
|
||||
|
||||
#[derive(Component, Reflect, Default)]
|
||||
#[reflect(Component)]
|
||||
struct HeadImage;
|
||||
|
||||
#[derive(Component, Reflect, Default)]
|
||||
#[reflect(Component)]
|
||||
struct HeadDamage;
|
||||
|
||||
#[derive(Component, Default, PartialEq)]
|
||||
pub struct TargetUi {
|
||||
head: Option<UiHeadState>,
|
||||
}
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.register_required_components::<LocalPlayer, TargetUi>();
|
||||
|
||||
app.add_systems(OnEnter(GameState::Playing), setup);
|
||||
app.add_systems(Update, (sync, update).run_if(in_state(GameState::Playing)));
|
||||
}
|
||||
|
||||
fn setup(mut commands: Commands, assets: Res<UIAssets>) {
|
||||
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()
|
||||
},
|
||||
children![spawn_head_ui(
|
||||
assets.head_bg.clone(),
|
||||
assets.head_regular.clone(),
|
||||
assets.head_damage.clone(),
|
||||
)],
|
||||
));
|
||||
}
|
||||
|
||||
fn spawn_head_ui(bg: Handle<Image>, regular: Handle<Image>, damage: Handle<Image>) -> impl Bundle {
|
||||
const SIZE: f32 = 90.0;
|
||||
const DAMAGE_SIZE: f32 = 74.0;
|
||||
|
||||
(
|
||||
Node {
|
||||
position_type: PositionType::Relative,
|
||||
justify_content: JustifyContent::Center,
|
||||
align_items: AlignItems::Center,
|
||||
width: Val::Px(SIZE),
|
||||
..default()
|
||||
},
|
||||
children![
|
||||
(
|
||||
Node {
|
||||
position_type: PositionType::Absolute,
|
||||
..default()
|
||||
},
|
||||
ImageNode::new(bg),
|
||||
),
|
||||
(
|
||||
Node {
|
||||
position_type: PositionType::Absolute,
|
||||
..default()
|
||||
},
|
||||
ImageNode::default(),
|
||||
Visibility::Hidden,
|
||||
HeadImage,
|
||||
),
|
||||
(
|
||||
Node {
|
||||
position_type: PositionType::Absolute,
|
||||
..default()
|
||||
},
|
||||
ImageNode::new(regular),
|
||||
),
|
||||
(
|
||||
Node {
|
||||
height: Val::Px(DAMAGE_SIZE),
|
||||
width: Val::Px(DAMAGE_SIZE),
|
||||
..default()
|
||||
},
|
||||
children![(
|
||||
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()
|
||||
},
|
||||
children![ImageNode::new(damage)]
|
||||
)]
|
||||
)
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
fn update(
|
||||
target: Single<&TargetUi, (Changed<TargetUi>, With<LocalPlayer>)>,
|
||||
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 let Ok((mut vis, mut image)) = head_image.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.single_mut() {
|
||||
node.height = Val::Percent(target.head.map(|head| head.damage()).unwrap_or(0.) * 100.);
|
||||
}
|
||||
}
|
||||
|
||||
fn sync(
|
||||
mut target: Single<&mut TargetUi, With<LocalPlayer>>,
|
||||
player_target: Single<&AimTarget, With<LocalPlayer>>,
|
||||
target_data: Query<(&Hitpoints, &ActiveHeads), With<Npc>>,
|
||||
) {
|
||||
let mut new_state = None;
|
||||
if let Some(target) = player_target.0
|
||||
&& let Ok((hp, heads)) = target_data.get(target)
|
||||
{
|
||||
let head = heads.current().expect("target must have a head on");
|
||||
new_state = Some(UiHeadState {
|
||||
head: head.head,
|
||||
health: hp.health(),
|
||||
ammo: 1.,
|
||||
reloading: None,
|
||||
});
|
||||
}
|
||||
|
||||
if new_state != target.head {
|
||||
target.head = new_state;
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,7 @@ use bevy_replicon_renet::{
|
||||
use bevy_steamworks::Client;
|
||||
use bevy_trenchbroom::geometry::Brushes;
|
||||
|
||||
pub mod aim;
|
||||
pub mod audio;
|
||||
pub mod backpack;
|
||||
pub mod control;
|
||||
@@ -36,6 +37,8 @@ pub mod ui;
|
||||
|
||||
pub fn plugin(app: &mut App) {
|
||||
app.add_plugins((
|
||||
aim::plugin,
|
||||
audio::plugin,
|
||||
backpack::plugin,
|
||||
control::plugin,
|
||||
debug::plugin,
|
||||
@@ -43,7 +46,6 @@ pub fn plugin(app: &mut App) {
|
||||
heal_effect::plugin,
|
||||
player::plugin,
|
||||
setup::plugin,
|
||||
audio::plugin,
|
||||
steam::plugin,
|
||||
ui::plugin,
|
||||
settings::plugin,
|
||||
|
||||
Reference in New Issue
Block a user