From 8029edde0b5f6cbd0d9f3e74a66e9216782a1143 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Wed, 17 Apr 2019 09:59:38 +0100 Subject: [PATCH] Moved animation determination to Control ECS system Former-commit-id: 6650bc4d882d3f6779f99afea2e05155ad008282 --- client/src/lib.rs | 20 +++----------------- common/src/sys/control.rs | 18 ++++++++++++------ server/src/lib.rs | 4 +--- 3 files changed, 16 insertions(+), 26 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index 95c00e8bb7..bd9a7ae61f 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -148,23 +148,9 @@ impl Client { println!("Chunk at {:?}", k); }); - // Step 1 - if let (Some(_), Some(vel), Some(_), Some(_)) = ( - self.state.read_component_cloned::(self.player), - self.state.read_component_cloned::(self.player), - self.state.read_component_cloned::(self.player), - self.state.read_component_cloned::(self.player), - ) { - self.state.write_component(self.player, comp::Control { - move_dir: input.move_dir, - }); - - if input.move_dir.magnitude() > 0.01 { - self.state.write_component(self.player, comp::Animation::Run); - } else { - self.state.write_component(self.player, comp::Animation::Idle); - } - } + self.state.write_component(self.player, comp::Control { + move_dir: input.move_dir, + }); // Tick the client's LocalState (step 3) self.state.tick(dt); diff --git a/common/src/sys/control.rs b/common/src/sys/control.rs index 88b91cb3c7..750b9a2e04 100644 --- a/common/src/sys/control.rs +++ b/common/src/sys/control.rs @@ -1,27 +1,33 @@ // Library -use specs::{Join, Read, ReadStorage, System, WriteStorage}; +use specs::{Join, Read, ReadStorage, System, WriteStorage, Entities}; use vek::*; // Crate -use crate::comp::{Control, phys::{Pos, Vel, Dir}}; +use crate::comp::{Control, Animation, phys::{Pos, Vel, Dir}}; // Basic ECS AI agent system pub struct Sys; impl<'a> System<'a> for Sys { type SystemData = ( + Entities<'a>, WriteStorage<'a, Vel>, WriteStorage<'a, Dir>, + WriteStorage<'a, Animation>, ReadStorage<'a, Control>, ); - fn run(&mut self, (mut vels, mut dirs, controls): Self::SystemData) { - for (mut vel, mut dir, control) in (&mut vels, &mut dirs, &controls).join() { + fn run(&mut self, (entities, mut vels, mut dirs, mut anims, controls): Self::SystemData) { + for (entity, mut vel, mut dir, control) in (&entities, &mut vels, &mut dirs, &controls).join() { // TODO: Don't hard-code this - vel.0 = vel.0 + control.move_dir * 2.0 - vel.0.map(|e| e * e.abs() + e) * 0.03; + // 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; if control.move_dir.magnitude() > 0.01 { - dir.0 = vel.0.normalized() * Vec3::new(1.0, 1.0, 0.0) ; + dir.0 = vel.0.normalized() * Vec3::new(1.0, 1.0, 0.0); + anims.insert(entity, Animation::Run); + } else { + anims.insert(entity, Animation::Run); } } } diff --git a/server/src/lib.rs b/server/src/lib.rs index c246652d99..9b43de8e82 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -74,7 +74,6 @@ impl Server { this.create_character(comp::Character::test()) .with(comp::Agent::Wanderer(Vec2::zero())) .with(comp::Control::default()) - .with(comp::Animation::Run) .build(); } @@ -111,9 +110,8 @@ impl Server { .create_entity_synced() .with(comp::phys::Pos(Vec3::zero())) .with(comp::phys::Vel(Vec3::zero())) - .with(comp::phys::Dir(Vec3::zero())) + .with(comp::phys::Dir(Vec3::unit_y())) .with(character) - .with(comp::Animation::Idle) } /// Execute a single server tick, handle input and update the game state by the given duration