Moved animation determination to Control ECS system

Former-commit-id: 6650bc4d882d3f6779f99afea2e05155ad008282
This commit is contained in:
Joshua Barretto 2019-04-17 09:59:38 +01:00
parent edcabfa68f
commit 8029edde0b
3 changed files with 16 additions and 26 deletions

View File

@ -148,23 +148,9 @@ impl Client {
println!("Chunk at {:?}", k); println!("Chunk at {:?}", k);
}); });
// Step 1 self.state.write_component(self.player, comp::Control {
if let (Some(_), Some(vel), Some(_), Some(_)) = ( move_dir: input.move_dir,
self.state.read_component_cloned::<comp::phys::Pos>(self.player), });
self.state.read_component_cloned::<comp::phys::Vel>(self.player),
self.state.read_component_cloned::<comp::phys::Dir>(self.player),
self.state.read_component_cloned::<comp::Character>(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);
}
}
// Tick the client's LocalState (step 3) // Tick the client's LocalState (step 3)
self.state.tick(dt); self.state.tick(dt);

View File

@ -1,27 +1,33 @@
// Library // Library
use specs::{Join, Read, ReadStorage, System, WriteStorage}; use specs::{Join, Read, ReadStorage, System, WriteStorage, Entities};
use vek::*; use vek::*;
// Crate // Crate
use crate::comp::{Control, phys::{Pos, Vel, Dir}}; use crate::comp::{Control, Animation, phys::{Pos, Vel, Dir}};
// Basic ECS AI agent system // Basic ECS AI agent system
pub struct Sys; pub struct Sys;
impl<'a> System<'a> for Sys { impl<'a> System<'a> for Sys {
type SystemData = ( type SystemData = (
Entities<'a>,
WriteStorage<'a, Vel>, WriteStorage<'a, Vel>,
WriteStorage<'a, Dir>, WriteStorage<'a, Dir>,
WriteStorage<'a, Animation>,
ReadStorage<'a, Control>, ReadStorage<'a, Control>,
); );
fn run(&mut self, (mut vels, mut dirs, controls): Self::SystemData) { fn run(&mut self, (entities, mut vels, mut dirs, mut anims, controls): Self::SystemData) {
for (mut vel, mut dir, control) in (&mut vels, &mut dirs, &controls).join() { for (entity, mut vel, mut dir, control) in (&entities, &mut vels, &mut dirs, &controls).join() {
// TODO: Don't hard-code this // 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 { 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);
} }
} }
} }

View File

@ -74,7 +74,6 @@ impl Server {
this.create_character(comp::Character::test()) this.create_character(comp::Character::test())
.with(comp::Agent::Wanderer(Vec2::zero())) .with(comp::Agent::Wanderer(Vec2::zero()))
.with(comp::Control::default()) .with(comp::Control::default())
.with(comp::Animation::Run)
.build(); .build();
} }
@ -111,9 +110,8 @@ impl Server {
.create_entity_synced() .create_entity_synced()
.with(comp::phys::Pos(Vec3::zero())) .with(comp::phys::Pos(Vec3::zero()))
.with(comp::phys::Vel(Vec3::zero())) .with(comp::phys::Vel(Vec3::zero()))
.with(comp::phys::Dir(Vec3::zero())) .with(comp::phys::Dir(Vec3::unit_y()))
.with(character) .with(character)
.with(comp::Animation::Idle)
} }
/// Execute a single server tick, handle input and update the game state by the given duration /// Execute a single server tick, handle input and update the game state by the given duration