2022-01-17 06:51:56 +00:00
|
|
|
use crate::sync::{self, NetSync};
|
2022-01-18 05:24:37 +00:00
|
|
|
use common::comp;
|
2020-07-06 14:23:08 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2019-04-29 20:37:19 +00:00
|
|
|
use std::marker::PhantomData;
|
2019-04-10 17:23:27 +00:00
|
|
|
|
2022-01-19 05:56:26 +00:00
|
|
|
/// This macro defines [`EcsCompPacke`]
|
|
|
|
///
|
|
|
|
/// It is meant to be passed to the `synced_components!` macro which will call
|
|
|
|
/// it with a list of components.
|
2022-01-18 05:24:37 +00:00
|
|
|
macro_rules! comp_packet {
|
|
|
|
($($component_name:ident: $component_type:ident,)*) => {
|
2022-01-19 05:56:26 +00:00
|
|
|
|
|
|
|
// `sum_type!` will automatically derive From<T> for EcsCompPacket
|
|
|
|
// for each variant EcsCompPacket::T(T).
|
2022-01-18 05:24:37 +00:00
|
|
|
sum_type::sum_type! {
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum EcsCompPacket {
|
2022-01-19 05:56:26 +00:00
|
|
|
// Note: also use the component_type identifier
|
|
|
|
// to name the enum variant that contains the component.
|
2022-01-18 05:24:37 +00:00
|
|
|
$($component_type($component_type),)*
|
2022-01-19 05:56:26 +00:00
|
|
|
// These aren't included in the "synced_components" because the way
|
|
|
|
// we determine if they are changed and when to send them is different
|
|
|
|
// from the other components.
|
2022-01-18 05:24:37 +00:00
|
|
|
Pos(comp::Pos),
|
|
|
|
Vel(comp::Vel),
|
|
|
|
Ori(comp::Ori),
|
|
|
|
}
|
2019-11-04 00:57:36 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2022-01-19 05:56:26 +00:00
|
|
|
// `sum_type!` will automatically derive From<PhantomData<T>> for EcsCompPhantom
|
|
|
|
// for each variant EcsCompPhantom::T(PhantomData<T>).
|
2022-01-18 05:24:37 +00:00
|
|
|
sum_type::sum_type! {
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum EcsCompPhantom {
|
|
|
|
$($component_type(PhantomData<$component_type>),)*
|
|
|
|
Pos(PhantomData<comp::Pos>),
|
|
|
|
Vel(PhantomData<comp::Vel>),
|
|
|
|
Ori(PhantomData<comp::Ori>),
|
|
|
|
}
|
2019-11-04 00:57:36 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2022-01-18 05:24:37 +00:00
|
|
|
impl sync::CompPacket for EcsCompPacket {
|
|
|
|
type Phantom = EcsCompPhantom;
|
|
|
|
|
|
|
|
fn apply_insert(self, entity: specs::Entity, world: &specs::World, force_update: bool) {
|
|
|
|
match self {
|
2022-01-17 06:51:56 +00:00
|
|
|
$(Self::$component_type(mut comp) => {
|
|
|
|
comp.pre_insert(world);
|
2022-01-18 05:24:37 +00:00
|
|
|
sync::handle_insert(comp, entity, world);
|
|
|
|
},)*
|
|
|
|
Self::Pos(comp) => {
|
|
|
|
sync::handle_interp_insert(comp, entity, world, force_update)
|
|
|
|
},
|
|
|
|
Self::Vel(comp) => {
|
|
|
|
sync::handle_interp_insert(comp, entity, world, force_update)
|
|
|
|
},
|
|
|
|
Self::Ori(comp) => {
|
|
|
|
sync::handle_interp_insert(comp, entity, world, force_update)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_modify(self, entity: specs::Entity, world: &specs::World, force_update: bool) {
|
|
|
|
match self {
|
2022-01-17 06:51:56 +00:00
|
|
|
$(Self::$component_type(mut comp) => {
|
|
|
|
comp.pre_modify(world);
|
2022-01-18 05:24:37 +00:00
|
|
|
sync::handle_modify(comp, entity, world);
|
|
|
|
},)*
|
|
|
|
Self::Pos(comp) => {
|
|
|
|
sync::handle_interp_modify(comp, entity, world, force_update)
|
|
|
|
},
|
|
|
|
Self::Vel(comp) => {
|
|
|
|
sync::handle_interp_modify(comp, entity, world, force_update)
|
|
|
|
},
|
|
|
|
Self::Ori(comp) => {
|
|
|
|
sync::handle_interp_modify(comp, entity, world, force_update)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_remove(phantom: Self::Phantom, entity: specs::Entity, world: &specs::World) {
|
|
|
|
match phantom {
|
|
|
|
$(EcsCompPhantom::$component_type(_) => {
|
|
|
|
sync::handle_remove::<$component_type>(entity, world);
|
|
|
|
},)*
|
|
|
|
EcsCompPhantom::Pos(_) => {
|
|
|
|
sync::handle_interp_remove::<comp::Pos>(entity, world)
|
|
|
|
},
|
|
|
|
EcsCompPhantom::Vel(_) => {
|
|
|
|
sync::handle_interp_remove::<comp::Vel>(entity, world)
|
|
|
|
},
|
|
|
|
EcsCompPhantom::Ori(_) => {
|
|
|
|
sync::handle_interp_remove::<comp::Ori>(entity, world)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2019-11-04 00:57:36 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-10 17:23:27 +00:00
|
|
|
}
|
2022-01-18 05:24:37 +00:00
|
|
|
|
2022-01-19 05:56:26 +00:00
|
|
|
// Import all the component types so they will be available when expanding the
|
|
|
|
// macro below.
|
2022-01-18 05:24:37 +00:00
|
|
|
use crate::synced_components::*;
|
2022-01-19 05:56:26 +00:00
|
|
|
// Pass `comp_packet!` macro to this "x macro" which will invoke it with a list
|
|
|
|
// of components. This will declare the types defined in the macro above.
|
2022-01-18 05:24:37 +00:00
|
|
|
crate::synced_components!(comp_packet);
|