2019-08-04 08:21:29 +00:00
|
|
|
use crate::comp::{ActionState, Agent, Controller, Pos, Stats};
|
2019-06-06 14:48:41 +00:00
|
|
|
use rand::{seq::SliceRandom, thread_rng};
|
|
|
|
use specs::{Entities, Join, ReadStorage, System, WriteStorage};
|
2019-04-16 21:06:33 +00:00
|
|
|
use vek::*;
|
|
|
|
|
2019-06-09 19:33:20 +00:00
|
|
|
/// This system will allow NPCs to modify their controller
|
2019-04-16 21:06:33 +00:00
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
|
|
|
type SystemData = (
|
2019-05-25 21:13:38 +00:00
|
|
|
Entities<'a>,
|
2019-04-16 21:06:33 +00:00
|
|
|
ReadStorage<'a, Pos>,
|
2019-08-02 20:25:33 +00:00
|
|
|
ReadStorage<'a, Stats>,
|
2019-08-04 08:21:29 +00:00
|
|
|
ReadStorage<'a, ActionState>,
|
2019-08-02 20:25:33 +00:00
|
|
|
WriteStorage<'a, Agent>,
|
2019-06-09 14:20:20 +00:00
|
|
|
WriteStorage<'a, Controller>,
|
2019-04-16 21:06:33 +00:00
|
|
|
);
|
|
|
|
|
2019-08-04 08:21:29 +00:00
|
|
|
fn run(
|
|
|
|
&mut self,
|
|
|
|
(entities, positions, stats, action_states, mut agents, mut controllers): Self::SystemData,
|
|
|
|
) {
|
2019-08-02 20:25:33 +00:00
|
|
|
for (entity, pos, agent, controller) in
|
|
|
|
(&entities, &positions, &mut agents, &mut controllers).join()
|
2019-05-25 21:13:38 +00:00
|
|
|
{
|
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-06-09 14:20:20 +00:00
|
|
|
controller.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 } => {
|
2019-05-31 18:45:16 +00:00
|
|
|
// Run towards target.
|
2019-05-11 12:43:19 +00:00
|
|
|
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 {
|
2019-06-29 20:51:22 +00:00
|
|
|
controller.jump = true;
|
2019-05-25 21:13:38 +00:00
|
|
|
}
|
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-06-09 14:20:20 +00:00
|
|
|
controller.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-08-01 12:48:09 +00:00
|
|
|
};
|
2019-05-12 19:57:39 +00:00
|
|
|
}
|
2019-06-09 14:20:20 +00:00
|
|
|
_ => controller.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-28 18:59:32 +00:00
|
|
|
}
|
2019-08-02 18:56:37 +00:00
|
|
|
Agent::Enemy { bearing, target } => {
|
2019-08-02 20:35:03 +00:00
|
|
|
const SIGHT_DIST: f32 = 30.0;
|
2019-08-02 20:25:33 +00:00
|
|
|
let mut choose_new = false;
|
2019-08-02 20:35:03 +00:00
|
|
|
|
2019-08-04 08:21:29 +00:00
|
|
|
if let Some((Some(target_pos), Some(target_stats), Some(a))) =
|
|
|
|
target.map(|target| {
|
|
|
|
(
|
|
|
|
positions.get(target),
|
|
|
|
stats.get(target),
|
|
|
|
action_states.get(target),
|
|
|
|
)
|
|
|
|
})
|
2019-08-02 20:25:33 +00:00
|
|
|
{
|
|
|
|
let dist = Vec2::<f32>::from(target_pos.0 - pos.0).magnitude();
|
|
|
|
if target_stats.is_dead {
|
|
|
|
choose_new = true;
|
2019-08-03 21:11:57 +00:00
|
|
|
} else if dist < 6.0 {
|
2019-08-02 20:25:33 +00:00
|
|
|
// Fight and slowly move closer
|
|
|
|
controller.move_dir =
|
2019-08-03 12:31:40 +00:00
|
|
|
Vec2::<f32>::from(target_pos.0 - pos.0).normalized() * 0.5;
|
2019-05-27 11:18:14 +00:00
|
|
|
|
2019-08-03 18:28:26 +00:00
|
|
|
if rand::random::<f32>() < 0.05 {
|
2019-08-02 20:25:33 +00:00
|
|
|
controller.attack = true;
|
2019-05-27 11:18:14 +00:00
|
|
|
} else {
|
2019-08-02 20:25:33 +00:00
|
|
|
controller.attack = false;
|
2019-05-27 11:18:14 +00:00
|
|
|
}
|
2019-08-02 20:25:33 +00:00
|
|
|
} else if dist < SIGHT_DIST {
|
|
|
|
controller.move_dir =
|
|
|
|
Vec2::<f32>::from(target_pos.0 - pos.0).normalized() * 0.96;
|
2019-08-03 12:31:40 +00:00
|
|
|
|
|
|
|
if rand::random::<f32>() < 0.02 {
|
|
|
|
controller.roll = true;
|
|
|
|
}
|
2019-08-04 08:21:29 +00:00
|
|
|
|
|
|
|
if a.gliding && target_pos.0.z > pos.0.z + 5.0 {
|
|
|
|
controller.glide = true;
|
|
|
|
controller.jump = true;
|
|
|
|
}
|
2019-08-02 20:25:33 +00:00
|
|
|
} else {
|
|
|
|
choose_new = true;
|
2019-05-28 18:59:32 +00:00
|
|
|
}
|
2019-08-02 20:25:33 +00:00
|
|
|
} else {
|
|
|
|
*bearing +=
|
|
|
|
Vec2::new(rand::random::<f32>() - 0.5, rand::random::<f32>() - 0.5)
|
|
|
|
* 0.1
|
|
|
|
- *bearing * 0.005;
|
2019-08-02 18:56:37 +00:00
|
|
|
|
2019-08-02 20:25:33 +00:00
|
|
|
controller.move_dir = if bearing.magnitude_squared() > 0.1 {
|
|
|
|
bearing.normalized()
|
|
|
|
} else {
|
|
|
|
Vec2::zero()
|
|
|
|
};
|
|
|
|
|
|
|
|
choose_new = true;
|
|
|
|
}
|
2019-05-27 11:18:14 +00:00
|
|
|
|
2019-08-02 19:41:38 +00:00
|
|
|
if choose_new && rand::random::<f32>() < 0.1 {
|
2019-08-02 20:25:33 +00:00
|
|
|
let entities = (&entities, &positions, &stats)
|
2019-05-27 11:18:14 +00:00
|
|
|
.join()
|
2019-08-02 20:25:33 +00:00
|
|
|
.filter(|(e, e_pos, e_stats)| {
|
2019-08-04 07:58:41 +00:00
|
|
|
(e_pos.0 - pos.0).magnitude() < SIGHT_DIST
|
2019-08-02 18:22:51 +00:00
|
|
|
&& *e != entity
|
2019-08-02 20:25:33 +00:00
|
|
|
&& !e_stats.is_dead
|
2019-05-28 18:59:32 +00:00
|
|
|
})
|
2019-08-02 20:25:33 +00:00
|
|
|
.map(|(e, _, _)| e)
|
2019-05-27 11:18:14 +00:00
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
2019-06-06 14:48:41 +00:00
|
|
|
let mut rng = thread_rng();
|
|
|
|
*target = (&entities).choose(&mut rng).cloned();
|
2019-05-27 11:18:14 +00:00
|
|
|
}
|
2019-05-28 18:59:32 +00:00
|
|
|
}
|
2019-04-16 21:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|