2019-04-16 21:06:33 +00:00
|
|
|
// Library
|
2019-05-25 21:13:38 +00:00
|
|
|
use specs::{Entities, Join, Read, ReadStorage, System, WriteStorage};
|
2019-04-16 21:06:33 +00:00
|
|
|
use vek::*;
|
2019-05-27 11:18:14 +00:00
|
|
|
use rand::Rng;
|
2019-04-16 21:06:33 +00:00
|
|
|
|
|
|
|
// Crate
|
2019-05-27 11:18:14 +00:00
|
|
|
use crate::{
|
|
|
|
comp::{phys::Pos, Agent, Control, Jumping, Attacking},
|
|
|
|
state::Time,
|
|
|
|
};
|
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-05-27 11:18:14 +00:00
|
|
|
Read<'a, Time>,
|
2019-05-25 21:13:38 +00:00
|
|
|
Entities<'a>,
|
2019-04-16 21:06:33 +00:00
|
|
|
WriteStorage<'a, Agent>,
|
|
|
|
ReadStorage<'a, Pos>,
|
2019-05-25 21:13:38 +00:00
|
|
|
WriteStorage<'a, Control>,
|
|
|
|
WriteStorage<'a, Jumping>,
|
2019-05-27 11:18:14 +00:00
|
|
|
WriteStorage<'a, Attacking>,
|
2019-04-16 21:06:33 +00:00
|
|
|
);
|
|
|
|
|
2019-05-25 21:13:38 +00:00
|
|
|
fn run(
|
|
|
|
&mut self,
|
2019-05-27 11:18:14 +00:00
|
|
|
(time, entities, mut agents, positions, mut controls, mut jumpings, mut attackings): Self::SystemData,
|
2019-05-25 21:13:38 +00:00
|
|
|
) {
|
|
|
|
for (entity, agent, pos, control) in
|
|
|
|
(&entities, &mut agents, &positions, &mut controls).join()
|
|
|
|
{
|
2019-04-16 21:06:33 +00:00
|
|
|
match agent {
|
|
|
|
Agent::Wanderer(bearing) => {
|
2019-05-12 19:57:39 +00:00
|
|
|
*bearing += Vec2::new(rand::random::<f32>() - 0.5, rand::random::<f32>() - 0.5)
|
|
|
|
* 0.1
|
2019-04-29 20:37:19 +00:00
|
|
|
- *bearing * 0.01
|
|
|
|
- pos.0 * 0.0002;
|
2019-04-16 21:06:33 +00:00
|
|
|
|
|
|
|
if bearing.magnitude_squared() != 0.0 {
|
2019-05-25 21:13:38 +00:00
|
|
|
control.move_dir = bearing.normalized();
|
2019-04-16 21:06:33 +00:00
|
|
|
}
|
2019-05-12 19:57:39 +00:00
|
|
|
}
|
2019-05-11 12:43:19 +00:00
|
|
|
Agent::Pet { target, offset } => {
|
|
|
|
// Run towards target
|
|
|
|
match positions.get(*target) {
|
|
|
|
Some(tgt_pos) => {
|
|
|
|
let tgt_pos = tgt_pos.0 + *offset;
|
|
|
|
|
2019-05-25 21:13:38 +00:00
|
|
|
if tgt_pos.z > pos.0.z + 1.0 {
|
|
|
|
jumpings.insert(entity, Jumping);
|
|
|
|
}
|
2019-05-11 12:43:19 +00:00
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Move towards the target.
|
2019-05-26 14:32:15 +00:00
|
|
|
let dist: f32 = Vec2::from(tgt_pos - pos.0).magnitude();
|
2019-05-25 21:13:38 +00:00
|
|
|
control.move_dir = if dist > 5.0 {
|
2019-05-11 12:43:19 +00:00
|
|
|
Vec2::from(tgt_pos - pos.0).normalized()
|
2019-05-13 17:26:07 +00:00
|
|
|
} else if dist < 1.5 && dist > 0.0 {
|
2019-05-11 12:43:19 +00:00
|
|
|
Vec2::from(pos.0 - tgt_pos).normalized()
|
|
|
|
} else {
|
|
|
|
Vec2::zero()
|
|
|
|
};
|
2019-05-12 19:57:39 +00:00
|
|
|
}
|
2019-05-25 21:13:38 +00:00
|
|
|
_ => control.move_dir = Vec2::zero(),
|
2019-05-11 12:43:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 09:22:32 +00:00
|
|
|
// Change offset occasionally.
|
2019-05-11 12:43:19 +00:00
|
|
|
if rand::random::<f32>() < 0.003 {
|
2019-05-12 19:57:39 +00:00
|
|
|
*offset =
|
|
|
|
Vec2::new(rand::random::<f32>() - 0.5, rand::random::<f32>() - 0.5)
|
|
|
|
* 10.0;
|
2019-05-11 12:43:19 +00:00
|
|
|
}
|
2019-05-27 11:18:14 +00:00
|
|
|
},
|
|
|
|
Agent::Enemy { target } => {
|
|
|
|
let choose_new = match target.map(|tgt| positions.get(tgt)).flatten() {
|
|
|
|
Some(tgt_pos) => {
|
|
|
|
let dist = Vec2::<f32>::from(tgt_pos.0 - pos.0).magnitude();
|
|
|
|
if dist < 2.0 {
|
|
|
|
control.move_dir = Vec2::zero();
|
|
|
|
|
|
|
|
if rand::random::<f32>() < 0.2 {
|
|
|
|
attackings.insert(entity, Attacking::start());
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
} else if dist < 60.0 {
|
|
|
|
control.move_dir = Vec2::<f32>::from(tgt_pos.0 - pos.0).normalized() * 0.96;
|
|
|
|
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => {
|
|
|
|
control.move_dir = Vec2::one();
|
|
|
|
rand::random::<f32>().fract() < 0.25
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
if choose_new {
|
|
|
|
let entities = (&entities, &positions)
|
|
|
|
.join()
|
|
|
|
.filter(|(_, e_pos)| Vec2::<f32>::from(e_pos.0 - pos.0).magnitude() < 30.0)
|
|
|
|
.map(|(e, _)| e)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
*target = rand::thread_rng().choose(&entities).cloned();
|
|
|
|
}
|
|
|
|
},
|
2019-04-16 21:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|