Fix #21 by checking animation history

Former-commit-id: 5272d1d7aee73fc07edeb31d9f29238d19b7d229
This commit is contained in:
timokoesters 2019-04-17 19:32:29 +02:00
parent 1e4d02a5d8
commit b9607ef405
9 changed files with 61 additions and 26 deletions

View File

@ -168,8 +168,12 @@ impl Client {
} }
// Update the server about the player's currently playing animation // Update the server about the player's currently playing animation
if let Some(animation) = self.state.read_storage().get(self.player).cloned() { if let Some(animationHistory) = self.state.read_storage::<comp::AnimationHistory>().get(self.player).cloned() {
self.postbox.send_message(ClientMsg::PlayerAnimation(animation)); if let Some(last) = animationHistory.last {
if animationHistory.current != last {
self.postbox.send_message(ClientMsg::PlayerAnimation(animationHistory));
}
}
} }
// Request chunks from the server // Request chunks from the server
@ -228,9 +232,9 @@ impl Client {
}, },
None => {}, None => {},
}, },
ServerMsg::EntityAnimation { entity, animation } => match self.state.ecs().entity_from_uid(entity) { ServerMsg::EntityAnimation { entity, animationHistory } => match self.state.ecs().entity_from_uid(entity) {
Some(entity) => { Some(entity) => {
self.state.write_component(entity, animation); self.state.write_component(entity, animationHistory);
}, },
None => {}, None => {},
}, },

View File

@ -19,6 +19,12 @@ pub enum Gender {
} }
#[derive(Copy, Clone, Debug, Serialize, Deserialize)] #[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct AnimationHistory {
pub last: Option<Animation>,
pub current: Animation,
}
#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Animation { pub enum Animation {
Idle, Idle,
Run, Run,
@ -55,6 +61,6 @@ impl Component for Character {
type Storage = FlaggedStorage<Self, VecStorage<Self>>; type Storage = FlaggedStorage<Self, VecStorage<Self>>;
} }
impl Component for Animation { impl Component for AnimationHistory {
type Storage = FlaggedStorage<Self, VecStorage<Self>>; type Storage = FlaggedStorage<Self, VecStorage<Self>>;
} }

View File

@ -7,4 +7,5 @@ pub mod phys;
pub use agent::{Agent, Control}; pub use agent::{Agent, Control};
pub use character::Character; pub use character::Character;
pub use player::Player; pub use player::Player;
pub use character::AnimationHistory;
pub use character::Animation; pub use character::Animation;

View File

@ -11,7 +11,7 @@ pub enum ClientMsg {
Ping, Ping,
Pong, Pong,
Chat(String), Chat(String),
PlayerAnimation(comp::character::Animation), PlayerAnimation(comp::character::AnimationHistory),
PlayerPhysics { PlayerPhysics {
pos: comp::phys::Pos, pos: comp::phys::Pos,
vel: comp::phys::Vel, vel: comp::phys::Vel,

View File

@ -25,7 +25,7 @@ pub enum ServerMsg {
}, },
EntityAnimation { EntityAnimation {
entity: u64, entity: u64,
animation: comp::Animation, animationHistory: comp::AnimationHistory,
}, },
TerrainChunkUpdate { TerrainChunkUpdate {
key: Vec3<i32>, key: Vec3<i32>,

View File

@ -100,7 +100,7 @@ impl State {
ecs.internal_mut().register::<comp::phys::Pos>(); ecs.internal_mut().register::<comp::phys::Pos>();
ecs.internal_mut().register::<comp::phys::Vel>(); ecs.internal_mut().register::<comp::phys::Vel>();
ecs.internal_mut().register::<comp::phys::Dir>(); ecs.internal_mut().register::<comp::phys::Dir>();
ecs.internal_mut().register::<comp::Animation>(); ecs.internal_mut().register::<comp::AnimationHistory>();
ecs.internal_mut().register::<comp::Agent>(); ecs.internal_mut().register::<comp::Agent>();
ecs.internal_mut().register::<comp::Control>(); ecs.internal_mut().register::<comp::Control>();

View File

@ -3,7 +3,7 @@ use specs::{Join, Read, ReadStorage, System, WriteStorage, Entities};
use vek::*; use vek::*;
// Crate // Crate
use crate::comp::{Control, Animation, phys::{Pos, Vel, Dir}}; use crate::comp::{Control, Animation, AnimationHistory, phys::{Pos, Vel, Dir}};
// Basic ECS AI agent system // Basic ECS AI agent system
pub struct Sys; pub struct Sys;
@ -13,7 +13,7 @@ impl<'a> System<'a> for Sys {
Entities<'a>, Entities<'a>,
WriteStorage<'a, Vel>, WriteStorage<'a, Vel>,
WriteStorage<'a, Dir>, WriteStorage<'a, Dir>,
WriteStorage<'a, Animation>, WriteStorage<'a, AnimationHistory>,
ReadStorage<'a, Control>, ReadStorage<'a, Control>,
); );
@ -23,12 +23,20 @@ impl<'a> System<'a> for Sys {
// Apply physics to the player: acceleration and non-linear decceleration // Apply physics to the player: acceleration and non-linear decceleration
vel.0 += control.move_dir * 2.0 - vel.0.map(|e| e * e.abs() + e) * 0.03; vel.0 += control.move_dir * 2.0 - vel.0.map(|e| e * e.abs() + e) * 0.03;
if control.move_dir.magnitude() > 0.01 { let animation =
dir.0 = vel.0.normalized() * Vec3::new(1.0, 1.0, 0.0); if control.move_dir.magnitude() > 0.01 {
anims.insert(entity, Animation::Run); dir.0 = vel.0.normalized() * Vec3::new(1.0, 1.0, 0.0);
} else { Animation::Run
anims.insert(entity, Animation::Idle); } else {
} Animation::Idle
};
let lastAnimation = anims.get_mut(entity).map(|h| h.current);
anims.insert(entity, AnimationHistory {
last: lastAnimation,
current: animation,
});
} }
} }
} }

View File

@ -266,7 +266,7 @@ impl Server {
ClientMsg::Ping => client.postbox.send_message(ServerMsg::Pong), ClientMsg::Ping => client.postbox.send_message(ServerMsg::Pong),
ClientMsg::Pong => {} ClientMsg::Pong => {}
ClientMsg::Chat(msg) => new_chat_msgs.push((entity, msg)), ClientMsg::Chat(msg) => new_chat_msgs.push((entity, msg)),
ClientMsg::PlayerAnimation(animation) => state.write_component(entity, animation), ClientMsg::PlayerAnimation(animationHistory) => state.write_component(entity, animationHistory),
ClientMsg::PlayerPhysics { pos, vel, dir } => { ClientMsg::PlayerPhysics { pos, vel, dir } => {
state.write_component(entity, pos); state.write_component(entity, pos);
state.write_component(entity, vel); state.write_component(entity, vel);
@ -369,15 +369,31 @@ impl Server {
} }
// Sync animation states // Sync animation states
for (entity, &uid, &animation) in ( for (entity, &uid, &animationHistory) in (
&self.state.ecs().internal().entities(), &self.state.ecs().internal().entities(),
&self.state.ecs().internal().read_storage::<Uid>(), &self.state.ecs().internal().read_storage::<Uid>(),
&self.state.ecs().internal().read_storage::<comp::Animation>(), &self.state.ecs().internal().read_storage::<comp::AnimationHistory>(),
).join() { ).join() {
self.clients.notify_connected_except(entity, ServerMsg::EntityAnimation { if let Some(last) = animationHistory.last {
entity: uid.into(), if animationHistory.current == last {
animation, continue;
}); }
self.clients.notify_connected_except(entity, ServerMsg::EntityAnimation {
entity: uid.into(),
animationHistory,
});
}
}
// Update animation last/current state
for (entity, mut animationHistory) in (
&self.state.ecs().internal().entities(),
&mut self.state.ecs().internal().write_storage::<comp::AnimationHistory>()
).join() {
animationHistory.last = None;
let mut new = animationHistory.clone();
new.last = Some(new.current);
} }
// Remove all force flags // Remove all force flags

View File

@ -83,18 +83,18 @@ impl Figures {
pub fn maintain(&mut self, renderer: &mut Renderer, client: &mut Client) { pub fn maintain(&mut self, renderer: &mut Renderer, client: &mut Client) {
let time = client.state().get_time(); let time = client.state().get_time();
let ecs = client.state_mut().ecs_mut().internal_mut(); let ecs = client.state_mut().ecs_mut().internal_mut();
for (entity, pos, dir, character, animation) in ( for (entity, pos, dir, character, animationHistory) in (
&ecs.entities(), &ecs.entities(),
&ecs.read_storage::<comp::phys::Pos>(), &ecs.read_storage::<comp::phys::Pos>(),
&ecs.read_storage::<comp::phys::Dir>(), &ecs.read_storage::<comp::phys::Dir>(),
&ecs.read_storage::<comp::Character>(), &ecs.read_storage::<comp::Character>(),
&ecs.read_storage::<comp::Animation>(), &ecs.read_storage::<comp::AnimationHistory>(),
).join() { ).join() {
let state = self.states let state = self.states
.entry(entity) .entry(entity)
.or_insert_with(|| FigureState::new(renderer, CharacterSkeleton::new())); .or_insert_with(|| FigureState::new(renderer, CharacterSkeleton::new()));
let target_skeleton = match animation { let target_skeleton = match animationHistory.current {
comp::character::Animation::Idle => IdleAnimation::update_skeleton(&mut state.skeleton, time), comp::character::Animation::Idle => IdleAnimation::update_skeleton(&mut state.skeleton, time),
comp::character::Animation::Run => RunAnimation::update_skeleton(&mut state.skeleton, time), comp::character::Animation::Run => RunAnimation::update_skeleton(&mut state.skeleton, time),
}; };