veloren/common/src/msg/ecs_packet.rs

118 lines
5.6 KiB
Rust
Raw Normal View History

2019-11-24 20:12:03 +00:00
use crate::{comp, state, sync};
use serde_derive::{Deserialize, Serialize};
use std::marker::PhantomData;
2019-11-24 20:12:03 +00:00
use sum_type::sum_type;
// TODO: remove me
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum MustHaveMoreThanOneVariant {}
// Automatically derive From<T> for EcsResPacket
// for each variant EcsResPacket::T(T).
2019-11-04 00:57:36 +00:00
sum_type! {
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum EcsResPacket {
MustHaveMoreThanOneVariant(MustHaveMoreThanOneVariant),
TimeOfDay(state::TimeOfDay),
}
}
2019-11-24 20:12:03 +00:00
impl sync::ResPacket for EcsResPacket {
2019-11-04 00:57:36 +00:00
fn apply(self, world: &specs::World) {
match self {
EcsResPacket::MustHaveMoreThanOneVariant(_) => unimplemented!(),
2019-11-24 20:12:03 +00:00
EcsResPacket::TimeOfDay(time_of_day) => sync::handle_res_update(time_of_day, world),
2019-11-04 00:57:36 +00:00
}
}
}
// 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),
2019-07-21 16:50:13 +00:00
LightEmitter(comp::LightEmitter),
2019-07-26 21:01:41 +00:00
Item(comp::Item),
Scale(comp::Scale),
MountState(comp::MountState),
Mounting(comp::Mounting),
2019-09-25 20:22:39 +00:00
Mass(comp::Mass),
Gravity(comp::Gravity),
Sticky(comp::Sticky),
}
}
// 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>),
2019-07-21 16:50:13 +00:00
LightEmitter(PhantomData<comp::LightEmitter>),
2019-07-26 21:01:41 +00:00
Item(PhantomData<comp::Item>),
Scale(PhantomData<comp::Scale>),
MountState(PhantomData<comp::MountState>),
Mounting(PhantomData<comp::Mounting>),
2019-09-25 20:22:39 +00:00
Mass(PhantomData<comp::Mass>),
Gravity(PhantomData<comp::Gravity>),
Sticky(PhantomData<comp::Sticky>),
}
}
2019-11-24 20:12:03 +00:00
impl sync::CompPacket for EcsCompPacket {
type Phantom = EcsCompPhantom;
2019-11-04 00:57:36 +00:00
fn apply_insert(self, entity: specs::Entity, world: &specs::World) {
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::LightEmitter(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Item(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Scale(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::MountState(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Mounting(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Mass(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Gravity(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Sticky(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) {
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::LightEmitter(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Item(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Scale(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::MountState(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Mounting(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Mass(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Gravity(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Sticky(comp) => sync::handle_modify(comp, entity, world),
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),
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)
2019-11-04 00:57:36 +00:00
}
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::MountState(_) => sync::handle_remove::<comp::MountState>(entity, world),
EcsCompPhantom::Mounting(_) => sync::handle_remove::<comp::Mounting>(entity, world),
EcsCompPhantom::Mass(_) => sync::handle_remove::<comp::Mass>(entity, world),
EcsCompPhantom::Gravity(_) => sync::handle_remove::<comp::Gravity>(entity, world),
EcsCompPhantom::Sticky(_) => sync::handle_remove::<comp::Sticky>(entity, world),
2019-11-04 00:57:36 +00:00
}
}
}