2019-04-16 21:06:33 +00:00
|
|
|
// Library
|
2019-04-17 08:59:38 +00:00
|
|
|
use specs::{Join, Read, ReadStorage, System, WriteStorage, Entities};
|
2019-04-16 21:06:33 +00:00
|
|
|
use vek::*;
|
|
|
|
|
|
|
|
// Crate
|
2019-04-17 17:32:29 +00:00
|
|
|
use crate::comp::{Control, Animation, AnimationHistory, phys::{Pos, Vel, Dir}};
|
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) {
|
|
|
|
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-17 17:32:29 +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
|
|
|
|
};
|
|
|
|
|
|
|
|
let lastAnimation = anims.get_mut(entity).map(|h| h.current);
|
|
|
|
|
|
|
|
anims.insert(entity, AnimationHistory {
|
|
|
|
last: lastAnimation,
|
|
|
|
current: animation,
|
|
|
|
});
|
2019-04-16 21:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|