Sync controllers with clients

This commit is contained in:
Joshua Barretto 2021-03-13 00:26:50 +00:00
parent 557d165f22
commit 5e9a35a7c9
5 changed files with 48 additions and 23 deletions

View File

@ -32,6 +32,7 @@ sum_type! {
Gravity(comp::Gravity), Gravity(comp::Gravity),
Sticky(comp::Sticky), Sticky(comp::Sticky),
CharacterState(comp::CharacterState), CharacterState(comp::CharacterState),
Controller(comp::Controller),
Pos(comp::Pos), Pos(comp::Pos),
Vel(comp::Vel), Vel(comp::Vel),
Ori(comp::Ori), Ori(comp::Ori),
@ -66,6 +67,7 @@ sum_type! {
Gravity(PhantomData<comp::Gravity>), Gravity(PhantomData<comp::Gravity>),
Sticky(PhantomData<comp::Sticky>), Sticky(PhantomData<comp::Sticky>),
CharacterState(PhantomData<comp::CharacterState>), CharacterState(PhantomData<comp::CharacterState>),
Controller(PhantomData<comp::Controller>),
Pos(PhantomData<comp::Pos>), Pos(PhantomData<comp::Pos>),
Vel(PhantomData<comp::Vel>), Vel(PhantomData<comp::Vel>),
Ori(PhantomData<comp::Ori>), Ori(PhantomData<comp::Ori>),
@ -100,6 +102,7 @@ impl sync::CompPacket for EcsCompPacket {
EcsCompPacket::Gravity(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), EcsCompPacket::Sticky(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::CharacterState(comp) => sync::handle_insert(comp, entity, world), EcsCompPacket::CharacterState(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Controller(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Pos(comp) => sync::handle_insert(comp, entity, world), EcsCompPacket::Pos(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Vel(comp) => sync::handle_insert(comp, entity, world), EcsCompPacket::Vel(comp) => sync::handle_insert(comp, entity, world),
EcsCompPacket::Ori(comp) => sync::handle_insert(comp, entity, world), EcsCompPacket::Ori(comp) => sync::handle_insert(comp, entity, world),
@ -132,6 +135,7 @@ impl sync::CompPacket for EcsCompPacket {
EcsCompPacket::Gravity(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), EcsCompPacket::Sticky(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::CharacterState(comp) => sync::handle_modify(comp, entity, world), EcsCompPacket::CharacterState(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Controller(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Pos(comp) => sync::handle_modify(comp, entity, world), EcsCompPacket::Pos(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Vel(comp) => sync::handle_modify(comp, entity, world), EcsCompPacket::Vel(comp) => sync::handle_modify(comp, entity, world),
EcsCompPacket::Ori(comp) => sync::handle_modify(comp, entity, world), EcsCompPacket::Ori(comp) => sync::handle_modify(comp, entity, world),
@ -168,6 +172,7 @@ impl sync::CompPacket for EcsCompPacket {
EcsCompPhantom::CharacterState(_) => { EcsCompPhantom::CharacterState(_) => {
sync::handle_remove::<comp::CharacterState>(entity, world) sync::handle_remove::<comp::CharacterState>(entity, world)
}, },
EcsCompPhantom::Controller(_) => sync::handle_remove::<comp::Controller>(entity, world),
EcsCompPhantom::Pos(_) => sync::handle_remove::<comp::Pos>(entity, world), EcsCompPhantom::Pos(_) => sync::handle_remove::<comp::Pos>(entity, world),
EcsCompPhantom::Vel(_) => sync::handle_remove::<comp::Vel>(entity, world), EcsCompPhantom::Vel(_) => sync::handle_remove::<comp::Vel>(entity, world),
EcsCompPhantom::Ori(_) => sync::handle_remove::<comp::Ori>(entity, world), EcsCompPhantom::Ori(_) => sync::handle_remove::<comp::Ori>(entity, world),

View File

@ -9,7 +9,7 @@ use crate::{
util::Dir, util::Dir,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use specs::{Component, DerefFlaggedStorage}; use specs::{Component, DerefFlaggedStorage, FlaggedStorage};
use specs_idvs::IdvStorage; use specs_idvs::IdvStorage;
use std::time::Duration; use std::time::Duration;
use vek::*; use vek::*;
@ -314,7 +314,7 @@ impl Controller {
} }
impl Component for Controller { impl Component for Controller {
type Storage = IdvStorage<Self>; type Storage = FlaggedStorage<Self, IdvStorage<Self>>;
} }
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]

View File

@ -1004,7 +1004,7 @@ fn cylinder_voxel_collision<'a, T: BaseVol<Vox = Block> + ReadVol>(
z_range.clone(), z_range.clone(),
) && vel.0.z < 0.5 ) && vel.0.z < 0.5
&& vel.0.z > -1.5 && vel.0.z > -1.5
&& was_on_ground && (was_on_ground || vel.0.z <= 0.05)
{ {
let snap_height = terrain let snap_height = terrain
.get(Vec3::new(pos.0.x, pos.0.y, pos.0.z - 0.1).map(|e| e.floor() as i32)) .get(Vec3::new(pos.0.x, pos.0.y, pos.0.z - 0.1).map(|e| e.floor() as i32))

View File

@ -118,7 +118,7 @@ impl<'a> System<'a> for Sys {
(read_data, event_bus, mut agents, mut controllers): Self::SystemData, (read_data, event_bus, mut agents, mut controllers): Self::SystemData,
) { ) {
job.cpu_stats.measure(ParMode::Rayon); job.cpu_stats.measure(ParMode::Rayon);
( let controllers_to_insert = (
&read_data.entities, &read_data.entities,
(&read_data.energies, &read_data.healths), (&read_data.energies, &read_data.healths),
&read_data.positions, &read_data.positions,
@ -130,7 +130,7 @@ impl<'a> System<'a> for Sys {
&read_data.physics_states, &read_data.physics_states,
&read_data.uids, &read_data.uids,
&mut agents, &mut agents,
&mut controllers, &controllers,
read_data.light_emitter.maybe(), read_data.light_emitter.maybe(),
read_data.groups.maybe(), read_data.groups.maybe(),
read_data.mount_states.maybe(), read_data.mount_states.maybe(),
@ -142,7 +142,7 @@ impl<'a> System<'a> for Sys {
.map(|ms| *ms == MountState::Unmounted) .map(|ms| *ms == MountState::Unmounted)
.unwrap_or(true) .unwrap_or(true)
}) })
.for_each( .map(
|( |(
entity, entity,
(energy, health), (energy, health),
@ -160,6 +160,8 @@ impl<'a> System<'a> for Sys {
groups, groups,
_, _,
)| { )| {
let mut controller = (*controller).clone();
//// Hack, replace with better system when groups are more sophisticated //// Hack, replace with better system when groups are more sophisticated
//// Override alignment if in a group unless entity is owned already //// Override alignment if in a group unless entity is owned already
let alignment = if !matches!( let alignment = if !matches!(
@ -266,7 +268,7 @@ impl<'a> System<'a> for Sys {
if hostile { if hostile {
data.hostile_tree( data.hostile_tree(
agent, agent,
controller, &mut controller,
&read_data, &read_data,
&mut event_emitter, &mut event_emitter,
); );
@ -279,7 +281,7 @@ impl<'a> System<'a> for Sys {
agent.bearing = Vec2::zero(); agent.bearing = Vec2::zero();
data.follow( data.follow(
agent, agent,
controller, &mut controller,
&read_data.terrain, &read_data.terrain,
tgt_pos, tgt_pos,
); );
@ -311,12 +313,12 @@ impl<'a> System<'a> for Sys {
hostile: false, hostile: false,
}); });
data.idle( data.idle(
agent, controller, &read_data, agent, &mut controller, &read_data,
); );
} else { } else {
data.attack( data.attack(
agent, agent,
controller, &mut controller,
&read_data.terrain, &read_data.terrain,
tgt_pos, tgt_pos,
read_data.bodies.get(attacker), read_data.bodies.get(attacker),
@ -331,7 +333,7 @@ impl<'a> System<'a> for Sys {
} else if dist_sqrd > MAX_FOLLOW_DIST.powi(2) { } else if dist_sqrd > MAX_FOLLOW_DIST.powi(2) {
data.follow( data.follow(
agent, agent,
controller, &mut controller,
&read_data.terrain, &read_data.terrain,
tgt_pos, tgt_pos,
); );
@ -340,7 +342,7 @@ impl<'a> System<'a> for Sys {
} else { } else {
data.idle_tree( data.idle_tree(
agent, agent,
controller, &mut controller,
&read_data, &read_data,
&mut event_emitter, &mut event_emitter,
); );
@ -349,18 +351,18 @@ impl<'a> System<'a> for Sys {
} else { } else {
data.idle_tree( data.idle_tree(
agent, agent,
controller, &mut controller,
&read_data, &read_data,
&mut event_emitter, &mut event_emitter,
); );
} }
} else { } else {
agent.target = None; agent.target = None;
data.idle_tree(agent, controller, &read_data, &mut event_emitter); data.idle_tree(agent, &mut controller, &read_data, &mut event_emitter);
} }
} else { } else {
agent.target = None; agent.target = None;
data.idle_tree(agent, controller, &read_data, &mut event_emitter); data.idle_tree(agent, &mut controller, &read_data, &mut event_emitter);
} }
} else { } else {
// Target an entity that's attacking us if the attack was recent // Target an entity that's attacking us if the attack was recent
@ -381,7 +383,7 @@ impl<'a> System<'a> for Sys {
agent.target = None; agent.target = None;
data.idle_tree( data.idle_tree(
agent, agent,
controller, &mut controller,
&read_data, &read_data,
&mut event_emitter, &mut event_emitter,
); );
@ -392,7 +394,7 @@ impl<'a> System<'a> for Sys {
}); });
data.attack( data.attack(
agent, agent,
controller, &mut controller,
&read_data.terrain, &read_data.terrain,
tgt_pos, tgt_pos,
read_data.bodies.get(attacker), read_data.bodies.get(attacker),
@ -403,7 +405,7 @@ impl<'a> System<'a> for Sys {
agent.target = None; agent.target = None;
data.idle_tree( data.idle_tree(
agent, agent,
controller, &mut controller,
&read_data, &read_data,
&mut event_emitter, &mut event_emitter,
); );
@ -411,17 +413,24 @@ impl<'a> System<'a> for Sys {
} }
} else { } else {
agent.target = None; agent.target = None;
data.idle_tree(agent, controller, &read_data, &mut event_emitter); data.idle_tree(agent, &mut controller, &read_data, &mut event_emitter);
} }
} else { } else {
data.idle_tree(agent, controller, &read_data, &mut event_emitter); data.idle_tree(agent, &mut controller, &read_data, &mut event_emitter);
} }
} }
debug_assert!(controller.inputs.move_dir.map(|e| !e.is_nan()).reduce_and()); debug_assert!(controller.inputs.move_dir.map(|e| !e.is_nan()).reduce_and());
debug_assert!(controller.inputs.look_dir.map(|e| !e.is_nan()).reduce_and()); debug_assert!(controller.inputs.look_dir.map(|e| !e.is_nan()).reduce_and());
(entity, controller)
}, },
); )
.collect::<Vec<_>>();
for (entity, controller) in controllers_to_insert {
controllers.insert(entity, controller);
}
} }
} }

View File

@ -2,7 +2,7 @@ use common::{
comp::{ comp::{
Auras, BeamSegment, Body, Buffs, CanBuild, CharacterState, Collider, Combo, Energy, Auras, BeamSegment, Body, Buffs, CanBuild, CharacterState, Collider, Combo, Energy,
Gravity, Group, Health, Inventory, Item, LightEmitter, Mass, MountState, Mounting, Ori, Gravity, Group, Health, Inventory, Item, LightEmitter, Mass, MountState, Mounting, Ori,
Player, Poise, Pos, Scale, Shockwave, Stats, Sticky, Vel, Player, Poise, Pos, Scale, Shockwave, Stats, Sticky, Vel, Controller,
}, },
uid::Uid, uid::Uid,
}; };
@ -63,6 +63,7 @@ pub struct TrackedComps<'a> {
pub character_state: ReadStorage<'a, CharacterState>, pub character_state: ReadStorage<'a, CharacterState>,
pub shockwave: ReadStorage<'a, Shockwave>, pub shockwave: ReadStorage<'a, Shockwave>,
pub beam_segment: ReadStorage<'a, BeamSegment>, pub beam_segment: ReadStorage<'a, BeamSegment>,
pub controller: ReadStorage<'a, Controller>,
} }
impl<'a> TrackedComps<'a> { impl<'a> TrackedComps<'a> {
pub fn create_entity_package( pub fn create_entity_package(
@ -166,6 +167,10 @@ impl<'a> TrackedComps<'a> {
.get(entity) .get(entity)
.cloned() .cloned()
.map(|c| comps.push(c.into())); .map(|c| comps.push(c.into()));
self.controller
.get(entity)
.cloned()
.map(|c| comps.push(c.into()));
// Add untracked comps // Add untracked comps
pos.map(|c| comps.push(c.into())); pos.map(|c| comps.push(c.into()));
vel.map(|c| comps.push(c.into())); vel.map(|c| comps.push(c.into()));
@ -201,6 +206,7 @@ pub struct ReadTrackers<'a> {
pub character_state: ReadExpect<'a, UpdateTracker<CharacterState>>, pub character_state: ReadExpect<'a, UpdateTracker<CharacterState>>,
pub shockwave: ReadExpect<'a, UpdateTracker<Shockwave>>, pub shockwave: ReadExpect<'a, UpdateTracker<Shockwave>>,
pub beam_segment: ReadExpect<'a, UpdateTracker<BeamSegment>>, pub beam_segment: ReadExpect<'a, UpdateTracker<BeamSegment>>,
pub controller: ReadExpect<'a, UpdateTracker<Controller>>,
} }
impl<'a> ReadTrackers<'a> { impl<'a> ReadTrackers<'a> {
pub fn create_sync_packages( pub fn create_sync_packages(
@ -245,7 +251,8 @@ impl<'a> ReadTrackers<'a> {
filter, filter,
) )
.with_component(&comps.uid, &*self.shockwave, &comps.shockwave, filter) .with_component(&comps.uid, &*self.shockwave, &comps.shockwave, filter)
.with_component(&comps.uid, &*self.beam_segment, &comps.beam_segment, filter); .with_component(&comps.uid, &*self.beam_segment, &comps.beam_segment, filter)
.with_component(&comps.uid, &*self.controller, &comps.controller, filter);
(entity_sync_package, comp_sync_package) (entity_sync_package, comp_sync_package)
} }
@ -278,6 +285,7 @@ pub struct WriteTrackers<'a> {
character_state: WriteExpect<'a, UpdateTracker<CharacterState>>, character_state: WriteExpect<'a, UpdateTracker<CharacterState>>,
shockwave: WriteExpect<'a, UpdateTracker<Shockwave>>, shockwave: WriteExpect<'a, UpdateTracker<Shockwave>>,
beam: WriteExpect<'a, UpdateTracker<BeamSegment>>, beam: WriteExpect<'a, UpdateTracker<BeamSegment>>,
controller: WriteExpect<'a, UpdateTracker<Controller>>,
} }
fn record_changes(comps: &TrackedComps, trackers: &mut WriteTrackers) { fn record_changes(comps: &TrackedComps, trackers: &mut WriteTrackers) {
@ -309,6 +317,7 @@ fn record_changes(comps: &TrackedComps, trackers: &mut WriteTrackers) {
.record_changes(&comps.character_state); .record_changes(&comps.character_state);
trackers.shockwave.record_changes(&comps.shockwave); trackers.shockwave.record_changes(&comps.shockwave);
trackers.beam.record_changes(&comps.beam_segment); trackers.beam.record_changes(&comps.beam_segment);
trackers.controller.record_changes(&comps.controller);
// Debug how many updates are being sent // Debug how many updates are being sent
/* /*
macro_rules! log_counts { macro_rules! log_counts {
@ -347,6 +356,7 @@ fn record_changes(comps: &TrackedComps, trackers: &mut WriteTrackers) {
log_counts!(character_state, "Character States"); log_counts!(character_state, "Character States");
log_counts!(shockwave, "Shockwaves"); log_counts!(shockwave, "Shockwaves");
log_counts!(beam, "Beams"); log_counts!(beam, "Beams");
log_counts!(controller, "Controllers");
*/ */
} }
@ -376,6 +386,7 @@ pub fn register_trackers(world: &mut World) {
world.register_tracker::<CharacterState>(); world.register_tracker::<CharacterState>();
world.register_tracker::<Shockwave>(); world.register_tracker::<Shockwave>();
world.register_tracker::<BeamSegment>(); world.register_tracker::<BeamSegment>();
world.register_tracker::<Controller>();
} }
/// Deleted entities grouped by region /// Deleted entities grouped by region