This commit is contained in:
Magnus von Wachenfeldt 2022-08-11 13:30:28 +02:00
parent b0eadd5420
commit b46a22539e
Signed by: magnus
GPG Key ID: A469F7D71D09F795
2 changed files with 34 additions and 13 deletions

View File

@ -83,6 +83,7 @@ fn main() {
app.add_system(update_visulizer_system);
app.add_startup_system(setup_level);
app.add_startup_system(setup_camera);
app.add_system(panic_on_error_system);
app.run();
@ -159,8 +160,10 @@ fn client_sync_players(
mut most_recent_tick: ResMut<MostRecentTick>,
) {
let client_id = client.client_id();
while let Some(message) = client.receive_message(ServerChannel::ServerMessages.id()) {
let server_message = bincode::deserialize(&message).unwrap();
match server_message {
ServerMessages::PlayerCreate {
id,
@ -168,10 +171,13 @@ fn client_sync_players(
entity,
} => {
println!("Player {} connected.", id);
let mut client_entity = commands.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Capsule::default())),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_xyz(translation[0], translation[1], translation[2]),
let mut client_entity = commands.spawn_bundle(SpriteBundle {
sprite: Sprite {
color: Color::rgb(0., 0.47, 1.),
custom_size: Some(Vec2::new(1., 1.)),
..Default::default()
},
..Default::default()
});
@ -183,11 +189,14 @@ fn client_sync_players(
server_entity: entity,
client_entity: client_entity.id(),
};
lobby.players.insert(id, player_info);
network_mapping.0.insert(entity, client_entity.id());
}
ServerMessages::PlayerRemove { id } => {
println!("Player {} disconnected.", id);
if let Some(PlayerInfo {
server_entity,
client_entity,
@ -223,6 +232,7 @@ fn client_sync_players(
while let Some(message) = client.receive_message(ServerChannel::NetworkFrame.id()) {
let frame: NetworkFrame = bincode::deserialize(&message).unwrap();
match most_recent_tick.0 {
None => most_recent_tick.0 = Some(frame.tick),
Some(tick) if tick < frame.tick => most_recent_tick.0 = Some(frame.tick),
@ -236,11 +246,16 @@ fn client_sync_players(
translation,
..Default::default()
};
commands.entity(*entity).insert(transform);
}
}
}
}
fn setup_camera(mut commands: Commands) {
commands.spawn_bundle(Camera2dBundle::default());
}
#[derive(Component)]
struct Target;

View File

@ -92,6 +92,7 @@ fn server_update_system(
match event {
ServerEvent::ClientConnected(id, _) => {
println!("Player {} connected.", id);
visualizer.add_client(*id);
// Initialize other players for this new client
@ -109,10 +110,12 @@ fn server_update_system(
// Spawn new player
let transform = Transform::from_xyz(0., 0.51, 0.);
let player_entity = commands
.spawn_bundle(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Capsule::default())),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform,
.spawn_bundle(SpriteBundle {
sprite: Sprite {
color: Color::rgb(0., 0.47, 1.),
custom_size: Some(Vec2::new(1., 1.)),
..Default::default()
},
..Default::default()
})
.insert(RigidBody::Dynamic)
@ -140,14 +143,17 @@ fn server_update_system(
}
ServerEvent::ClientDisconnected(id) => {
println!("Player {} disconnected.", id);
visualizer.remove_client(*id);
client_ticks.0.remove(id);
if let Some(player_entity) = lobby.players.remove(id) {
commands.entity(player_entity).despawn();
}
let message =
bincode::serialize(&ServerMessages::PlayerRemove { id: *id }).unwrap();
server.broadcast_message(ServerChannel::ServerMessages.id(), message);
}
}
@ -156,6 +162,7 @@ fn server_update_system(
for client_id in server.clients_id().into_iter() {
while let Some(message) = server.receive_message(client_id, ClientChannel::Command.id()) {
let command: PlayerCommand = bincode::deserialize(&message).unwrap();
match command {
PlayerCommand::BasicAttack { mut cast_at } => {
println!(
@ -177,6 +184,7 @@ fn server_update_system(
};
let message = bincode::serialize(&message).unwrap();
server.broadcast_message(ServerChannel::ServerMessages.id(), message);
}
}
@ -186,6 +194,7 @@ fn server_update_system(
while let Some(message) = server.receive_message(client_id, ClientChannel::Input.id()) {
let input: PlayerInput = bincode::deserialize(&message).unwrap();
client_ticks.0.insert(client_id, input.most_recent_tick);
if let Some(player_entity) = lobby.players.get(&client_id) {
@ -247,17 +256,14 @@ fn move_players_system(mut query: Query<(&mut Velocity, &PlayerInput)>) {
let x = (input.right as i8 - input.left as i8) as f32;
let y = (input.down as i8 - input.up as i8) as f32;
let direction = Vec2::new(x, y).normalize_or_zero();
velocity.linvel.x = direction.x * PLAYER_MOVE_SPEED;
velocity.linvel.y = direction.y * PLAYER_MOVE_SPEED;
}
}
pub fn setup_simple_camera(mut commands: Commands) {
// camera
commands.spawn_bundle(Camera3dBundle {
transform: Transform::from_xyz(-5.5, 5.0, 5.5).looking_at(Vec3::ZERO, Vec3::Y),
..Default::default()
});
commands.spawn_bundle(Camera2dBundle::default());
}
fn despawn_projectile_system(