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 08:59:38 +00:00
|
|
|
use crate::comp::{Control, Animation, 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 08:59:38 +00:00
|
|
|
WriteStorage<'a, Animation>,
|
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
|
|
|
|
|
|
|
if control.move_dir.magnitude() > 0.01 {
|
2019-04-17 08:59:38 +00:00
|
|
|
dir.0 = vel.0.normalized() * Vec3::new(1.0, 1.0, 0.0);
|
|
|
|
anims.insert(entity, Animation::Run);
|
|
|
|
} else {
|
|
|
|
anims.insert(entity, Animation::Run);
|
2019-04-16 21:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|