veloren/common/net/src/msg/ecs_packet.rs

225 lines
12 KiB
Rust
Raw Normal View History

use crate::sync;
use common::{
comp,
link::Is,
mounting::{Mount, Rider},
resources::Time,
};
use serde::{Deserialize, Serialize};
use specs::WorldExt;
use std::marker::PhantomData;
2019-11-24 20:12:03 +00:00
use sum_type::sum_type;
// Automatically derive From<T> for EcsCompPacket
// for each variant EcsCompPacket::T(T.)
2019-11-04 00:57:36 +00:00
sum_type! {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum EcsCompPacket {
2019-06-30 11:48:28 +00:00
Body(comp::Body),
Player(comp::Player),
CanBuild(comp::CanBuild),
Stats(comp::Stats),
SkillSet(comp::SkillSet),
2021-11-12 03:37:37 +00:00
ActiveAbilities(comp::ActiveAbilities),
Buffs(comp::Buffs),
2020-12-04 22:24:56 +00:00
Auras(comp::Auras),
Energy(comp::Energy),
2021-02-27 19:55:06 +00:00
Combo(comp::Combo),
Health(comp::Health),
Poise(comp::Poise),
2019-07-21 16:50:13 +00:00
LightEmitter(comp::LightEmitter),
Inventory(comp::Inventory),
2019-07-26 21:01:41 +00:00
Item(comp::Item),
Scale(comp::Scale),
Group(comp::Group),
IsMount(Is<Mount>),
IsRider(Is<Rider>),
2019-09-25 20:22:39 +00:00
Mass(comp::Mass),
Density(comp::Density),
Collider(comp::Collider),
Sticky(comp::Sticky),
CharacterState(comp::CharacterState),
Pos(comp::Pos),
Vel(comp::Vel),
Ori(comp::Ori),
Shockwave(comp::Shockwave),
BeamSegment(comp::BeamSegment),
Alignment(comp::Alignment),
}
}
// Automatically derive From<T> for EcsCompPhantom
// for each variant EcsCompPhantom::T(PhantomData<T>).
2019-11-04 00:57:36 +00:00
sum_type! {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum EcsCompPhantom {
2019-06-30 11:48:28 +00:00
Body(PhantomData<comp::Body>),
Player(PhantomData<comp::Player>),
CanBuild(PhantomData<comp::CanBuild>),
Stats(PhantomData<comp::Stats>),
SkillSet(PhantomData<comp::SkillSet>),
2021-11-12 03:37:37 +00:00
ActiveAbilities(PhantomData<comp::ActiveAbilities>),
Buffs(PhantomData<comp::Buffs>),
2020-12-04 22:24:56 +00:00
Auras(PhantomData<comp::Auras>),
Energy(PhantomData<comp::Energy>),
2021-02-27 19:55:06 +00:00
Combo(PhantomData<comp::Combo>),
Health(PhantomData<comp::Health>),
Poise(PhantomData<comp::Poise>),
2019-07-21 16:50:13 +00:00
LightEmitter(PhantomData<comp::LightEmitter>),
Inventory(PhantomData<comp::Inventory>),
2019-07-26 21:01:41 +00:00
Item(PhantomData<comp::Item>),
Scale(PhantomData<comp::Scale>),
Group(PhantomData<comp::Group>),
IsMount(PhantomData<Is<Mount>>),
IsRider(PhantomData<Is<Rider>>),
2019-09-25 20:22:39 +00:00
Mass(PhantomData<comp::Mass>),
Density(PhantomData<comp::Density>),
Collider(PhantomData<comp::Collider>),
Sticky(PhantomData<comp::Sticky>),
CharacterState(PhantomData<comp::CharacterState>),
Pos(PhantomData<comp::Pos>),
Vel(PhantomData<comp::Vel>),
Ori(PhantomData<comp::Ori>),
Shockwave(PhantomData<comp::Shockwave>),
BeamSegment(PhantomData<comp::BeamSegment>),
Alignment(PhantomData<comp::Alignment>),
}
}
2019-11-24 20:12:03 +00:00
impl sync::CompPacket for EcsCompPacket {
type Phantom = EcsCompPhantom;
fn apply_insert(self, entity: specs::Entity, world: &specs::World, force_update: bool) {
2019-11-04 00:57:36 +00:00
match self {
2019-11-24 20:12:03 +00:00
EcsCompPacket::Body(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Player(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::CanBuild(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Stats(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::SkillSet(comp) => sync::handle_insert(comp, entity, world),
2021-11-12 03:37:37 +00:00
EcsCompPacket::ActiveAbilities(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Buffs(comp) => sync::handle_insert(comp, entity, world),
2020-12-04 22:24:56 +00:00
EcsCompPacket::Auras(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Energy(comp) => sync::handle_insert(comp, entity, world),
2021-02-27 19:55:06 +00:00
EcsCompPacket::Combo(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Health(mut comp) => {
// Time isn't synced between client and server so replace the Time from the
// server with the Client's local Time to enable accurate comparison.
comp.last_change.time = *world.read_resource::<Time>();
sync::handle_insert(comp, entity, world)
},
EcsCompPacket::Poise(comp) => sync::handle_insert(comp, entity, world),
2019-11-24 20:12:03 +00:00
EcsCompPacket::LightEmitter(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Inventory(comp) => sync::handle_insert(comp, entity, world),
2019-11-24 20:12:03 +00:00
EcsCompPacket::Item(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Scale(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Group(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::IsMount(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::IsRider(comp) => sync::handle_insert(comp, entity, world),
2019-11-24 20:12:03 +00:00
EcsCompPacket::Mass(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Density(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Collider(comp) => sync::handle_insert(comp, entity, world),
2019-11-24 20:12:03 +00:00
EcsCompPacket::Sticky(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::CharacterState(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Pos(comp) => {
sync::handle_interp_insert(comp, entity, world, force_update)
},
EcsCompPacket::Vel(comp) => {
sync::handle_interp_insert(comp, entity, world, force_update)
},
EcsCompPacket::Ori(comp) => {
sync::handle_interp_insert(comp, entity, world, force_update)
},
EcsCompPacket::Shockwave(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::BeamSegment(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Alignment(comp) => sync::handle_insert(comp, entity, world),
2019-11-04 00:57:36 +00:00
}
}
fn apply_modify(self, entity: specs::Entity, world: &specs::World, force_update: bool) {
2019-11-04 00:57:36 +00:00
match self {
2019-11-24 20:12:03 +00:00
EcsCompPacket::Body(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Player(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::CanBuild(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Stats(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::SkillSet(comp) => sync::handle_modify(comp, entity, world),
2021-11-12 03:37:37 +00:00
EcsCompPacket::ActiveAbilities(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Buffs(comp) => sync::handle_modify(comp, entity, world),
2020-12-04 22:24:56 +00:00
EcsCompPacket::Auras(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Energy(comp) => sync::handle_modify(comp, entity, world),
2021-02-27 19:55:06 +00:00
EcsCompPacket::Combo(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Health(mut comp) => {
// Time isn't synced between client and server so replace the Time from the
// server with the Client's local Time to enable accurate comparison.
comp.last_change.time = *world.read_resource::<Time>();
sync::handle_modify(comp, entity, world)
},
EcsCompPacket::Poise(comp) => sync::handle_modify(comp, entity, world),
2019-11-24 20:12:03 +00:00
EcsCompPacket::LightEmitter(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Inventory(comp) => sync::handle_modify(comp, entity, world),
2019-11-24 20:12:03 +00:00
EcsCompPacket::Item(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Scale(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Group(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::IsMount(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::IsRider(comp) => sync::handle_modify(comp, entity, world),
2019-11-24 20:12:03 +00:00
EcsCompPacket::Mass(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Density(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Collider(comp) => sync::handle_modify(comp, entity, world),
2019-11-24 20:12:03 +00:00
EcsCompPacket::Sticky(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::CharacterState(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Pos(comp) => {
sync::handle_interp_modify(comp, entity, world, force_update)
},
EcsCompPacket::Vel(comp) => {
sync::handle_interp_modify(comp, entity, world, force_update)
},
EcsCompPacket::Ori(comp) => {
sync::handle_interp_modify(comp, entity, world, force_update)
},
EcsCompPacket::Shockwave(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::BeamSegment(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Alignment(comp) => sync::handle_modify(comp, entity, world),
2019-11-04 00:57:36 +00:00
}
}
2019-11-04 00:57:36 +00:00
fn apply_remove(phantom: Self::Phantom, entity: specs::Entity, world: &specs::World) {
match phantom {
2019-11-24 20:12:03 +00:00
EcsCompPhantom::Body(_) => sync::handle_remove::<comp::Body>(entity, world),
EcsCompPhantom::Player(_) => sync::handle_remove::<comp::Player>(entity, world),
EcsCompPhantom::CanBuild(_) => sync::handle_remove::<comp::CanBuild>(entity, world),
EcsCompPhantom::Stats(_) => sync::handle_remove::<comp::Stats>(entity, world),
EcsCompPhantom::SkillSet(_) => sync::handle_remove::<comp::SkillSet>(entity, world),
2021-11-12 03:37:37 +00:00
EcsCompPhantom::ActiveAbilities(_) => {
sync::handle_remove::<comp::ActiveAbilities>(entity, world)
2021-11-09 17:56:07 +00:00
},
EcsCompPhantom::Buffs(_) => sync::handle_remove::<comp::Buffs>(entity, world),
2020-12-04 22:24:56 +00:00
EcsCompPhantom::Auras(_) => sync::handle_remove::<comp::Auras>(entity, world),
EcsCompPhantom::Energy(_) => sync::handle_remove::<comp::Energy>(entity, world),
2021-02-27 19:55:06 +00:00
EcsCompPhantom::Combo(_) => sync::handle_remove::<comp::Combo>(entity, world),
EcsCompPhantom::Health(_) => sync::handle_remove::<comp::Health>(entity, world),
EcsCompPhantom::Poise(_) => sync::handle_remove::<comp::Poise>(entity, world),
2019-11-04 00:57:36 +00:00
EcsCompPhantom::LightEmitter(_) => {
2019-11-24 20:12:03 +00:00
sync::handle_remove::<comp::LightEmitter>(entity, world)
},
EcsCompPhantom::Inventory(_) => sync::handle_remove::<comp::Inventory>(entity, world),
2019-11-24 20:12:03 +00:00
EcsCompPhantom::Item(_) => sync::handle_remove::<comp::Item>(entity, world),
EcsCompPhantom::Scale(_) => sync::handle_remove::<comp::Scale>(entity, world),
EcsCompPhantom::Group(_) => sync::handle_remove::<comp::Group>(entity, world),
EcsCompPhantom::IsMount(_) => sync::handle_remove::<Is<Mount>>(entity, world),
EcsCompPhantom::IsRider(_) => sync::handle_remove::<Is<Rider>>(entity, world),
2019-11-24 20:12:03 +00:00
EcsCompPhantom::Mass(_) => sync::handle_remove::<comp::Mass>(entity, world),
EcsCompPhantom::Density(_) => sync::handle_remove::<comp::Density>(entity, world),
EcsCompPhantom::Collider(_) => sync::handle_remove::<comp::Collider>(entity, world),
2019-11-24 20:12:03 +00:00
EcsCompPhantom::Sticky(_) => sync::handle_remove::<comp::Sticky>(entity, world),
EcsCompPhantom::CharacterState(_) => {
sync::handle_remove::<comp::CharacterState>(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),
EcsCompPhantom::Shockwave(_) => sync::handle_remove::<comp::Shockwave>(entity, world),
EcsCompPhantom::BeamSegment(_) => {
sync::handle_remove::<comp::BeamSegment>(entity, world)
},
EcsCompPhantom::Alignment(_) => sync::handle_remove::<comp::Alignment>(entity, world),
2019-11-04 00:57:36 +00:00
}
}
}