2019-04-16 21:06:33 +00:00
|
|
|
// Library
|
2019-04-29 20:37:19 +00:00
|
|
|
use specs::{Entities, Join, Read, ReadStorage, System, WriteStorage};
|
2019-04-16 21:06:33 +00:00
|
|
|
use vek::*;
|
|
|
|
|
|
|
|
// Crate
|
2019-04-29 20:37:19 +00:00
|
|
|
use crate::comp::{
|
|
|
|
phys::{Dir, Pos, Vel},
|
|
|
|
Animation, AnimationHistory, Control,
|
|
|
|
};
|
2019-04-16 21:06:33 +00:00
|
|
|
|
|
|
|
// Basic ECS AI agent system
|
|
|
|
pub struct Sys;
|
|
|
|
|
|
|
|
impl<'a> System<'a> for Sys {
|
|
|
|
type SystemData = (
|
2019-04-17 08:59:38 +00:00
|
|
|
Entities<'a>,
|
2019-04-16 21:06:33 +00:00
|
|
|
WriteStorage<'a, Vel>,
|
|
|
|
WriteStorage<'a, Dir>,
|
2019-04-17 17:32:29 +00:00
|
|
|
WriteStorage<'a, AnimationHistory>,
|
2019-04-16 21:06:33 +00:00
|
|
|
ReadStorage<'a, Control>,
|
|
|
|
);
|
|
|
|
|
2019-04-17 08:59:38 +00:00
|
|
|
fn run(&mut self, (entities, mut vels, mut dirs, mut anims, controls): Self::SystemData) {
|
2019-04-29 20:37:19 +00:00
|
|
|
for (entity, mut vel, mut dir, control) in
|
|
|
|
(&entities, &mut vels, &mut dirs, &controls).join()
|
|
|
|
{
|
2019-04-16 21:06:33 +00:00
|
|
|
// TODO: Don't hard-code this
|
2019-04-17 08:59:38 +00:00
|
|
|
// 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;
|
2019-04-16 21:06:33 +00:00
|
|
|
|
2019-04-29 20:37:19 +00:00
|
|
|
let animation = if control.move_dir.magnitude() > 0.01 {
|
|
|
|
dir.0 = vel.0.normalized() * Vec3::new(1.0, 1.0, 0.0);
|
|
|
|
Animation::Run
|
|
|
|
} else {
|
|
|
|
Animation::Idle
|
|
|
|
};
|
2019-04-17 17:32:29 +00:00
|
|
|
|
2019-04-17 20:44:10 +00:00
|
|
|
let last_animation = anims.get_mut(entity).map(|h| h.current);
|
2019-04-17 17:32:29 +00:00
|
|
|
|
2019-04-29 20:37:19 +00:00
|
|
|
anims.insert(
|
|
|
|
entity,
|
|
|
|
AnimationHistory {
|
|
|
|
last: last_animation,
|
|
|
|
current: animation,
|
|
|
|
},
|
|
|
|
);
|
2019-04-16 21:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|