Add Peer UI display
This commit is contained in:
parent
e883f201c0
commit
c29344ba6d
3 changed files with 80 additions and 7 deletions
|
@ -13,7 +13,7 @@ use super::{
|
||||||
check_for_seed, setup_balls, setup_camera, setup_from_seed, setup_player, setup_walls,
|
check_for_seed, setup_balls, setup_camera, setup_from_seed, setup_player, setup_walls,
|
||||||
},
|
},
|
||||||
state::AppState,
|
state::AppState,
|
||||||
ui::{setup_seed_display, update_seed_display},
|
ui::{setup_peer_ui, setup_seed_ui, update_peer_ui, update_peer_ui_timings, update_seed_ui},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
@ -59,7 +59,7 @@ impl Plugin for GamePlugin {
|
||||||
PhysicsPlugins::default().with_length_unit(50.0),
|
PhysicsPlugins::default().with_length_unit(50.0),
|
||||||
))
|
))
|
||||||
.init_state::<AppState>()
|
.init_state::<AppState>()
|
||||||
.add_systems(Startup, (setup_camera, setup_seed_display))
|
.add_systems(Startup, (setup_camera, setup_seed_ui, setup_peer_ui))
|
||||||
.add_systems(
|
.add_systems(
|
||||||
OnEnter(AppState::InGame),
|
OnEnter(AppState::InGame),
|
||||||
(
|
(
|
||||||
|
@ -84,7 +84,9 @@ impl Plugin for GamePlugin {
|
||||||
(
|
(
|
||||||
((move_player, move_camera).chain(), zoom_camera)
|
((move_player, move_camera).chain(), zoom_camera)
|
||||||
.run_if(in_state(AppState::InGame)),
|
.run_if(in_state(AppState::InGame)),
|
||||||
update_seed_display,
|
update_seed_ui,
|
||||||
|
(update_peer_ui, update_peer_ui_timings)
|
||||||
|
.run_if(in_state(NetworkState::MultiPlayer)),
|
||||||
quit.run_if(input_pressed(KeyCode::KeyQ)),
|
quit.run_if(input_pressed(KeyCode::KeyQ)),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
use bevy::prelude::*;
|
use bevy::prelude::*;
|
||||||
|
|
||||||
|
use crate::net::prelude::{Peer, PeerReceiveTiming, PeerSendTiming};
|
||||||
|
|
||||||
use super::seed::Seed;
|
use super::seed::Seed;
|
||||||
|
|
||||||
#[derive(Component, Debug)]
|
#[derive(Component, Debug)]
|
||||||
pub struct SeedUI;
|
pub struct SeedUI;
|
||||||
|
|
||||||
pub fn setup_seed_display(mut commands: Commands) {
|
pub fn setup_seed_ui(mut commands: Commands) {
|
||||||
commands
|
commands
|
||||||
.spawn((
|
.spawn((
|
||||||
Text::new("Seed: "),
|
Text::new("Seed: "),
|
||||||
|
@ -13,13 +15,13 @@ pub fn setup_seed_display(mut commands: Commands) {
|
||||||
position_type: PositionType::Absolute,
|
position_type: PositionType::Absolute,
|
||||||
bottom: Val::Px(5.0),
|
bottom: Val::Px(5.0),
|
||||||
left: Val::Px(5.0),
|
left: Val::Px(5.0),
|
||||||
..Default::default()
|
..default()
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
.with_child((TextSpan::new("<N/A>"), SeedUI));
|
.with_child((TextSpan::new("<N/A>"), SeedUI));
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_seed_display(seed: Option<Res<Seed>>, text: Query<&mut TextSpan, With<SeedUI>>) {
|
pub fn update_seed_ui(seed: Option<Res<Seed>>, text: Query<&mut TextSpan, With<SeedUI>>) {
|
||||||
if let Some(value) = seed {
|
if let Some(value) = seed {
|
||||||
if value.is_changed() {
|
if value.is_changed() {
|
||||||
for mut span in text {
|
for mut span in text {
|
||||||
|
@ -29,3 +31,72 @@ pub fn update_seed_display(seed: Option<Res<Seed>>, text: Query<&mut TextSpan, W
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Component, Debug)]
|
||||||
|
pub struct PeerUI;
|
||||||
|
|
||||||
|
pub fn setup_peer_ui(mut commands: Commands) {
|
||||||
|
commands
|
||||||
|
.spawn((
|
||||||
|
Node {
|
||||||
|
display: Display::Flex,
|
||||||
|
position_type: PositionType::Absolute,
|
||||||
|
top: Val::Px(5.0),
|
||||||
|
right: Val::Px(5.0),
|
||||||
|
row_gap: Val::Px(5.0),
|
||||||
|
flex_direction: FlexDirection::Column,
|
||||||
|
align_items: AlignItems::FlexEnd,
|
||||||
|
justify_content: JustifyContent::FlexStart,
|
||||||
|
..default()
|
||||||
|
},
|
||||||
|
PeerUI,
|
||||||
|
))
|
||||||
|
.with_children(|builder| {
|
||||||
|
builder.spawn(Text::new("Peer Recv Send"));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Component)]
|
||||||
|
pub struct PeerID(Entity);
|
||||||
|
|
||||||
|
pub fn update_peer_ui(
|
||||||
|
ui: Query<(Entity, &Children), With<PeerUI>>,
|
||||||
|
rows: Query<&PeerID>,
|
||||||
|
added: Query<Entity, Added<Peer>>,
|
||||||
|
mut removed: RemovedComponents<Peer>,
|
||||||
|
mut commands: Commands,
|
||||||
|
) {
|
||||||
|
for (table, children) in ui {
|
||||||
|
for addition in added {
|
||||||
|
commands
|
||||||
|
.entity(table)
|
||||||
|
.with_child((Text::new("---- ---- ----"), PeerID(addition)));
|
||||||
|
}
|
||||||
|
for removal in removed.read() {
|
||||||
|
for child in children {
|
||||||
|
if let Ok(id) = rows.get(*child) {
|
||||||
|
if id.0 == removal {
|
||||||
|
commands.entity(*child).despawn();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update_peer_ui_timings(
|
||||||
|
rows: Query<(&mut Text, &PeerID)>,
|
||||||
|
peers: Query<(&Peer, &PeerReceiveTiming, &PeerSendTiming)>,
|
||||||
|
time: Res<Time>,
|
||||||
|
) {
|
||||||
|
for (mut row, id) in rows {
|
||||||
|
if let Ok((peer, recv, send)) = peers.get(id.0) {
|
||||||
|
**row = format!(
|
||||||
|
"{} {:.2} {:.2}",
|
||||||
|
peer.uuid,
|
||||||
|
(time.elapsed() - recv.time().unwrap_or(default())).as_secs_f64(),
|
||||||
|
(time.elapsed() - send.time()).as_secs_f64()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -9,6 +9,6 @@ mod thread;
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
pub use super::packet::{InboundPacket, OutboundPacket, Packet};
|
pub use super::packet::{InboundPacket, OutboundPacket, Packet};
|
||||||
pub use super::peer::Peer;
|
pub use super::peer::{Peer, PeerReceiveTiming, PeerSendTiming};
|
||||||
pub use super::plugin::{NetIOPlugin, NetworkState};
|
pub use super::plugin::{NetIOPlugin, NetworkState};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue