~starkingdoms/starkingdoms

ref: c7da20800d035a6491e612693b2039aa99988c15 starkingdoms/crates/unified/src/client/net.rs -rw-r--r-- 1.8 KiB
c7da2080 — core netcode: easing 6 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
pub mod incoming_planets;
pub mod incoming_parts;

use avian2d::math::{Scalar, Vector};
use bevy::ecs::query::QueryData;
use bevy_transform_interpolation::VelocitySource;
use crate::{prelude::*, shared::net::{ServerEntityMap}};
use crate::client::net::incoming_parts::handle_incoming_parts;
use crate::client::net::incoming_planets::handle_incoming_planets;

pub fn net_plugin(app: &mut App) {
    app
        .insert_resource(ServerEntityMap::default())
        .add_systems(FixedUpdate, (update_previous_velocities, (handle_incoming_planets, handle_incoming_parts)).chain());
}

#[derive(Component)]
pub struct PreviousAngularVelocity(Scalar);
#[derive(Component)]
pub struct PreviousLinearVelocity(Vector);
#[derive(QueryData)]
pub struct LinearVelocitySource;
#[derive(QueryData)]
pub struct AngularVelocitySource;
impl VelocitySource for LinearVelocitySource {
    type Previous = PreviousLinearVelocity;
    type Current = LinearVelocity;

    fn previous(start: &Self::Previous) -> Vec3 {
        let v = start.0.extend(0.0);
        Vec3::new(v.x as f32, v.y as f32, v.z as f32)
    }

    fn current(end: &Self::Current) -> Vec3 {
        let v = end.0.extend(0.0);
        Vec3::new(v.x as f32, v.y as f32, v.z as f32)
    }
}
impl VelocitySource for AngularVelocitySource {
    type Previous = PreviousAngularVelocity;
    type Current = AngularVelocity;

    fn previous(start: &Self::Previous) -> Vec3 {
        Vec3::Z * start.0 as f32
    }

    fn current(end: &Self::Current) -> Vec3 {
        Vec3::Z * end.0 as f32
    }
}
fn update_previous_velocities(
    mut entities: Query<(&mut PreviousAngularVelocity, &AngularVelocity, &mut PreviousLinearVelocity, &LinearVelocity)>
) {
    for mut entity in entities.iter_mut() {
        *entity.0 = PreviousAngularVelocity(**entity.1);
        *entity.2 = PreviousLinearVelocity(**entity.3);
    }
}