Compare commits
4 commits
e58629c2f1
...
9ab8db41de
Author | SHA1 | Date | |
---|---|---|---|
9ab8db41de | |||
c29344ba6d | |||
e883f201c0 | |||
58795eb87e |
11 changed files with 123 additions and 13 deletions
4
.vscode/launch.json
vendored
4
.vscode/launch.json
vendored
|
@ -11,7 +11,7 @@
|
|||
"cargo": {
|
||||
"args": ["build"]
|
||||
},
|
||||
"args": ["--seed=gargamel"],
|
||||
"args": ["--seed=gargamel", "--port=25565"],
|
||||
"cwd": "${workspaceFolder}",
|
||||
"env": {
|
||||
"CARGO_MANIFEST_DIR": "${workspaceFolder}",
|
||||
|
@ -30,7 +30,7 @@
|
|||
"cargo": {
|
||||
"args": ["build"]
|
||||
},
|
||||
"args": ["--port=25566", "--connect=[::1]:25565"],
|
||||
"args": ["--connect=[::1]:25565"],
|
||||
"cwd": "${workspaceFolder}",
|
||||
"env": {
|
||||
"CARGO_MANIFEST_DIR": "${workspaceFolder}",
|
||||
|
|
|
@ -6,6 +6,7 @@ mod runtime;
|
|||
mod seed;
|
||||
mod setup;
|
||||
pub mod state;
|
||||
mod ui;
|
||||
|
||||
#[allow(unused_imports)]
|
||||
pub mod prelude {
|
||||
|
|
|
@ -11,7 +11,6 @@ pub fn handle_new_peer(
|
|||
) {
|
||||
if let Some(seed) = seed {
|
||||
for peer in new_peers {
|
||||
warn!("Sending seed to peer: {}", peer.uuid);
|
||||
outbound.write(OutboundPacket(Packet::new((*seed).into(), peer.uuid)));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -9,8 +9,11 @@ use super::{
|
|||
net::{handle_deleted_peer, handle_incoming_packets, handle_new_peer},
|
||||
runtime::{move_camera, move_player, quit, zoom_camera},
|
||||
seed::Seed,
|
||||
setup::{check_for_seed, setup_balls, setup_from_seed, setup_player, setup_ui, setup_walls},
|
||||
setup::{
|
||||
check_for_seed, setup_balls, setup_camera, setup_from_seed, setup_player, setup_walls,
|
||||
},
|
||||
state::AppState,
|
||||
ui::{setup_peer_ui, setup_seed_ui, update_peer_ui, update_peer_ui_timings, update_seed_ui},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
|
@ -56,7 +59,7 @@ impl Plugin for GamePlugin {
|
|||
PhysicsPlugins::default().with_length_unit(50.0),
|
||||
))
|
||||
.init_state::<AppState>()
|
||||
.add_systems(Startup, setup_ui)
|
||||
.add_systems(Startup, (setup_camera, setup_seed_ui, setup_peer_ui))
|
||||
.add_systems(
|
||||
OnEnter(AppState::InGame),
|
||||
(
|
||||
|
@ -81,6 +84,9 @@ impl Plugin for GamePlugin {
|
|||
(
|
||||
((move_player, move_camera).chain(), zoom_camera)
|
||||
.run_if(in_state(AppState::InGame)),
|
||||
update_seed_ui,
|
||||
(update_peer_ui, update_peer_ui_timings)
|
||||
.run_if(in_state(NetworkState::MultiPlayer)),
|
||||
quit.run_if(input_pressed(KeyCode::KeyQ)),
|
||||
),
|
||||
);
|
||||
|
|
|
@ -43,7 +43,7 @@ pub fn setup_from_seed(mut commands: Commands, seed: Res<Seed>) {
|
|||
}
|
||||
|
||||
/// I mean, a camera is technically a user interface, I guess
|
||||
pub fn setup_ui(mut commands: Commands) {
|
||||
pub fn setup_camera(mut commands: Commands) {
|
||||
commands.spawn((Name::new("Camera"), Camera2d, IsDefaultUiCamera));
|
||||
}
|
||||
|
||||
|
|
102
src/game/ui.rs
Normal file
102
src/game/ui.rs
Normal file
|
@ -0,0 +1,102 @@
|
|||
use bevy::prelude::*;
|
||||
|
||||
use crate::net::prelude::{Peer, PeerReceiveTiming, PeerSendTiming};
|
||||
|
||||
use super::seed::Seed;
|
||||
|
||||
#[derive(Component, Debug)]
|
||||
pub struct SeedUI;
|
||||
|
||||
pub fn setup_seed_ui(mut commands: Commands) {
|
||||
commands
|
||||
.spawn((
|
||||
Text::new("Seed: "),
|
||||
Node {
|
||||
position_type: PositionType::Absolute,
|
||||
bottom: Val::Px(5.0),
|
||||
left: Val::Px(5.0),
|
||||
..default()
|
||||
},
|
||||
))
|
||||
.with_child((TextSpan::new("<N/A>"), SeedUI));
|
||||
}
|
||||
|
||||
pub fn update_seed_ui(seed: Option<Res<Seed>>, text: Query<&mut TextSpan, With<SeedUI>>) {
|
||||
if let Some(value) = seed {
|
||||
if value.is_changed() {
|
||||
for mut span in text {
|
||||
let number: u64 = (*value).into();
|
||||
**span = format!("{}", number);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[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()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -20,7 +20,7 @@ pub struct AppSettings {
|
|||
source: Source,
|
||||
|
||||
/// The port the app should listen for connections on
|
||||
#[arg(short, long, default_value = "25565")]
|
||||
#[arg(short, long, default_value = "0")]
|
||||
port: u16,
|
||||
}
|
||||
|
||||
|
|
|
@ -26,7 +26,9 @@ pub fn handle_network_input(
|
|||
.into());
|
||||
}
|
||||
let uuid = Uuid::from_slice(message.split_off(message.len() - 16).as_slice())?;
|
||||
if !message.is_empty() {
|
||||
to_app.write(InboundPacket(Packet::new(message, uuid)));
|
||||
}
|
||||
if let Some(peer_id) = peer_map.get(&uuid) {
|
||||
let (peer, mut last) = peers.get_mut(*peer_id)?;
|
||||
last.update(&time);
|
||||
|
@ -74,7 +76,7 @@ pub fn heartbeat(
|
|||
}
|
||||
}
|
||||
|
||||
pub fn timeouts(
|
||||
pub fn timeout(
|
||||
peers: Query<(&Peer, &PeerReceiveTiming)>,
|
||||
time: Res<Time>,
|
||||
mut delete: EventWriter<PeerChangeEvent>,
|
||||
|
|
|
@ -9,6 +9,6 @@ mod thread;
|
|||
#[allow(unused_imports)]
|
||||
pub mod prelude {
|
||||
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};
|
||||
}
|
||||
|
|
|
@ -107,7 +107,7 @@ pub fn handle_peer_change(
|
|||
peer_map.remove(&change.peer);
|
||||
}
|
||||
} else if let Some(addr) = change.addr {
|
||||
info!("Adding peer {} ({:?})", change.peer, change.addr);
|
||||
info!("Adding peer {} ({})", change.peer, addr);
|
||||
peer_map.insert(
|
||||
change.peer,
|
||||
commands
|
||||
|
|
|
@ -4,7 +4,7 @@ use bevy::prelude::*;
|
|||
use uuid::Uuid;
|
||||
|
||||
use super::{
|
||||
io::{Config, handle_network_input, handle_network_output, heartbeat, timeouts},
|
||||
io::{Config, handle_network_input, handle_network_output, heartbeat, timeout},
|
||||
packet::{InboundPacket, OutboundPacket},
|
||||
peer::{Peer, PeerChangeEvent, PeerMap, handle_new_peer, handle_peer_change},
|
||||
queues::{NetworkReceive, NetworkSend},
|
||||
|
@ -40,7 +40,7 @@ impl Plugin for NetIOPlugin {
|
|||
)
|
||||
.add_systems(
|
||||
FixedUpdate,
|
||||
(heartbeat, timeouts, handle_new_peer).run_if(in_state(NetworkState::MultiPlayer)),
|
||||
(heartbeat, timeout, handle_new_peer).run_if(in_state(NetworkState::MultiPlayer)),
|
||||
)
|
||||
.add_systems(
|
||||
FixedPostUpdate,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue