2d
This commit is contained in:
parent
b0eadd5420
commit
b46a22539e
@ -83,6 +83,7 @@ fn main() {
|
|||||||
app.add_system(update_visulizer_system);
|
app.add_system(update_visulizer_system);
|
||||||
|
|
||||||
app.add_startup_system(setup_level);
|
app.add_startup_system(setup_level);
|
||||||
|
app.add_startup_system(setup_camera);
|
||||||
app.add_system(panic_on_error_system);
|
app.add_system(panic_on_error_system);
|
||||||
|
|
||||||
app.run();
|
app.run();
|
||||||
@ -159,8 +160,10 @@ fn client_sync_players(
|
|||||||
mut most_recent_tick: ResMut<MostRecentTick>,
|
mut most_recent_tick: ResMut<MostRecentTick>,
|
||||||
) {
|
) {
|
||||||
let client_id = client.client_id();
|
let client_id = client.client_id();
|
||||||
|
|
||||||
while let Some(message) = client.receive_message(ServerChannel::ServerMessages.id()) {
|
while let Some(message) = client.receive_message(ServerChannel::ServerMessages.id()) {
|
||||||
let server_message = bincode::deserialize(&message).unwrap();
|
let server_message = bincode::deserialize(&message).unwrap();
|
||||||
|
|
||||||
match server_message {
|
match server_message {
|
||||||
ServerMessages::PlayerCreate {
|
ServerMessages::PlayerCreate {
|
||||||
id,
|
id,
|
||||||
@ -168,10 +171,13 @@ fn client_sync_players(
|
|||||||
entity,
|
entity,
|
||||||
} => {
|
} => {
|
||||||
println!("Player {} connected.", id);
|
println!("Player {} connected.", id);
|
||||||
let mut client_entity = commands.spawn_bundle(PbrBundle {
|
|
||||||
mesh: meshes.add(Mesh::from(shape::Capsule::default())),
|
let mut client_entity = commands.spawn_bundle(SpriteBundle {
|
||||||
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
|
sprite: Sprite {
|
||||||
transform: Transform::from_xyz(translation[0], translation[1], translation[2]),
|
color: Color::rgb(0., 0.47, 1.),
|
||||||
|
custom_size: Some(Vec2::new(1., 1.)),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -183,11 +189,14 @@ fn client_sync_players(
|
|||||||
server_entity: entity,
|
server_entity: entity,
|
||||||
client_entity: client_entity.id(),
|
client_entity: client_entity.id(),
|
||||||
};
|
};
|
||||||
|
|
||||||
lobby.players.insert(id, player_info);
|
lobby.players.insert(id, player_info);
|
||||||
|
|
||||||
network_mapping.0.insert(entity, client_entity.id());
|
network_mapping.0.insert(entity, client_entity.id());
|
||||||
}
|
}
|
||||||
ServerMessages::PlayerRemove { id } => {
|
ServerMessages::PlayerRemove { id } => {
|
||||||
println!("Player {} disconnected.", id);
|
println!("Player {} disconnected.", id);
|
||||||
|
|
||||||
if let Some(PlayerInfo {
|
if let Some(PlayerInfo {
|
||||||
server_entity,
|
server_entity,
|
||||||
client_entity,
|
client_entity,
|
||||||
@ -223,6 +232,7 @@ fn client_sync_players(
|
|||||||
|
|
||||||
while let Some(message) = client.receive_message(ServerChannel::NetworkFrame.id()) {
|
while let Some(message) = client.receive_message(ServerChannel::NetworkFrame.id()) {
|
||||||
let frame: NetworkFrame = bincode::deserialize(&message).unwrap();
|
let frame: NetworkFrame = bincode::deserialize(&message).unwrap();
|
||||||
|
|
||||||
match most_recent_tick.0 {
|
match most_recent_tick.0 {
|
||||||
None => most_recent_tick.0 = Some(frame.tick),
|
None => most_recent_tick.0 = Some(frame.tick),
|
||||||
Some(tick) if tick < frame.tick => 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,
|
translation,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
commands.entity(*entity).insert(transform);
|
commands.entity(*entity).insert(transform);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn setup_camera(mut commands: Commands) {
|
||||||
|
commands.spawn_bundle(Camera2dBundle::default());
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Component)]
|
#[derive(Component)]
|
||||||
struct Target;
|
struct Target;
|
||||||
|
@ -92,6 +92,7 @@ fn server_update_system(
|
|||||||
match event {
|
match event {
|
||||||
ServerEvent::ClientConnected(id, _) => {
|
ServerEvent::ClientConnected(id, _) => {
|
||||||
println!("Player {} connected.", id);
|
println!("Player {} connected.", id);
|
||||||
|
|
||||||
visualizer.add_client(*id);
|
visualizer.add_client(*id);
|
||||||
|
|
||||||
// Initialize other players for this new client
|
// Initialize other players for this new client
|
||||||
@ -109,10 +110,12 @@ fn server_update_system(
|
|||||||
// Spawn new player
|
// Spawn new player
|
||||||
let transform = Transform::from_xyz(0., 0.51, 0.);
|
let transform = Transform::from_xyz(0., 0.51, 0.);
|
||||||
let player_entity = commands
|
let player_entity = commands
|
||||||
.spawn_bundle(PbrBundle {
|
.spawn_bundle(SpriteBundle {
|
||||||
mesh: meshes.add(Mesh::from(shape::Capsule::default())),
|
sprite: Sprite {
|
||||||
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
|
color: Color::rgb(0., 0.47, 1.),
|
||||||
transform,
|
custom_size: Some(Vec2::new(1., 1.)),
|
||||||
|
..Default::default()
|
||||||
|
},
|
||||||
..Default::default()
|
..Default::default()
|
||||||
})
|
})
|
||||||
.insert(RigidBody::Dynamic)
|
.insert(RigidBody::Dynamic)
|
||||||
@ -140,14 +143,17 @@ fn server_update_system(
|
|||||||
}
|
}
|
||||||
ServerEvent::ClientDisconnected(id) => {
|
ServerEvent::ClientDisconnected(id) => {
|
||||||
println!("Player {} disconnected.", id);
|
println!("Player {} disconnected.", id);
|
||||||
|
|
||||||
visualizer.remove_client(*id);
|
visualizer.remove_client(*id);
|
||||||
client_ticks.0.remove(id);
|
client_ticks.0.remove(id);
|
||||||
|
|
||||||
if let Some(player_entity) = lobby.players.remove(id) {
|
if let Some(player_entity) = lobby.players.remove(id) {
|
||||||
commands.entity(player_entity).despawn();
|
commands.entity(player_entity).despawn();
|
||||||
}
|
}
|
||||||
|
|
||||||
let message =
|
let message =
|
||||||
bincode::serialize(&ServerMessages::PlayerRemove { id: *id }).unwrap();
|
bincode::serialize(&ServerMessages::PlayerRemove { id: *id }).unwrap();
|
||||||
|
|
||||||
server.broadcast_message(ServerChannel::ServerMessages.id(), message);
|
server.broadcast_message(ServerChannel::ServerMessages.id(), message);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -156,6 +162,7 @@ fn server_update_system(
|
|||||||
for client_id in server.clients_id().into_iter() {
|
for client_id in server.clients_id().into_iter() {
|
||||||
while let Some(message) = server.receive_message(client_id, ClientChannel::Command.id()) {
|
while let Some(message) = server.receive_message(client_id, ClientChannel::Command.id()) {
|
||||||
let command: PlayerCommand = bincode::deserialize(&message).unwrap();
|
let command: PlayerCommand = bincode::deserialize(&message).unwrap();
|
||||||
|
|
||||||
match command {
|
match command {
|
||||||
PlayerCommand::BasicAttack { mut cast_at } => {
|
PlayerCommand::BasicAttack { mut cast_at } => {
|
||||||
println!(
|
println!(
|
||||||
@ -177,6 +184,7 @@ fn server_update_system(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let message = bincode::serialize(&message).unwrap();
|
let message = bincode::serialize(&message).unwrap();
|
||||||
|
|
||||||
server.broadcast_message(ServerChannel::ServerMessages.id(), message);
|
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()) {
|
while let Some(message) = server.receive_message(client_id, ClientChannel::Input.id()) {
|
||||||
let input: PlayerInput = bincode::deserialize(&message).unwrap();
|
let input: PlayerInput = bincode::deserialize(&message).unwrap();
|
||||||
|
|
||||||
client_ticks.0.insert(client_id, input.most_recent_tick);
|
client_ticks.0.insert(client_id, input.most_recent_tick);
|
||||||
|
|
||||||
if let Some(player_entity) = lobby.players.get(&client_id) {
|
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 x = (input.right as i8 - input.left as i8) as f32;
|
||||||
let y = (input.down as i8 - input.up 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();
|
let direction = Vec2::new(x, y).normalize_or_zero();
|
||||||
|
|
||||||
velocity.linvel.x = direction.x * PLAYER_MOVE_SPEED;
|
velocity.linvel.x = direction.x * PLAYER_MOVE_SPEED;
|
||||||
velocity.linvel.y = direction.y * PLAYER_MOVE_SPEED;
|
velocity.linvel.y = direction.y * PLAYER_MOVE_SPEED;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn setup_simple_camera(mut commands: Commands) {
|
pub fn setup_simple_camera(mut commands: Commands) {
|
||||||
// camera
|
commands.spawn_bundle(Camera2dBundle::default());
|
||||||
commands.spawn_bundle(Camera3dBundle {
|
|
||||||
transform: Transform::from_xyz(-5.5, 5.0, 5.5).looking_at(Vec3::ZERO, Vec3::Y),
|
|
||||||
..Default::default()
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn despawn_projectile_system(
|
fn despawn_projectile_system(
|
||||||
|
Loading…
Reference in New Issue
Block a user