2020-08-12 14:10:12 +00:00
|
|
|
use crate::{comp::Body, path::Chaser, sync::Uid};
|
2020-04-26 17:03:19 +00:00
|
|
|
use specs::{Component, Entity as EcsEntity};
|
2020-07-06 05:56:02 +00:00
|
|
|
use specs_idvs::IdvStorage;
|
2019-07-29 19:54:58 +00:00
|
|
|
use vek::*;
|
2019-04-16 21:06:33 +00:00
|
|
|
|
2020-04-26 17:03:19 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
2020-01-24 21:24:57 +00:00
|
|
|
pub enum Alignment {
|
2020-05-26 02:45:13 +00:00
|
|
|
/// Wild animals and gentle giants
|
2020-01-24 21:24:57 +00:00
|
|
|
Wild,
|
2020-05-26 02:45:13 +00:00
|
|
|
/// Dungeon cultists and bandits
|
2020-01-24 21:24:57 +00:00
|
|
|
Enemy,
|
2020-05-26 02:45:13 +00:00
|
|
|
/// Friendly folk in villages
|
2020-01-24 21:24:57 +00:00
|
|
|
Npc,
|
2020-05-26 02:45:13 +00:00
|
|
|
/// Farm animals and pets of villagers
|
|
|
|
Tame,
|
|
|
|
/// Pets you've tamed with a collar
|
2020-07-07 00:01:39 +00:00
|
|
|
Owned(Uid),
|
2019-04-16 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 21:24:57 +00:00
|
|
|
impl Alignment {
|
2020-01-27 15:51:07 +00:00
|
|
|
// Always attacks
|
2020-01-24 21:24:57 +00:00
|
|
|
pub fn hostile_towards(self, other: Alignment) -> bool {
|
|
|
|
match (self, other) {
|
2020-01-27 15:51:07 +00:00
|
|
|
(Alignment::Enemy, Alignment::Enemy) => false,
|
|
|
|
(Alignment::Enemy, _) => true,
|
|
|
|
(_, Alignment::Enemy) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Never attacks
|
|
|
|
pub fn passive_towards(self, other: Alignment) -> bool {
|
|
|
|
match (self, other) {
|
2020-04-19 11:50:25 +00:00
|
|
|
(Alignment::Enemy, Alignment::Enemy) => true,
|
2020-01-27 15:51:07 +00:00
|
|
|
(Alignment::Owned(a), Alignment::Owned(b)) if a == b => true,
|
2020-05-26 02:45:13 +00:00
|
|
|
(Alignment::Npc, Alignment::Npc) => true,
|
|
|
|
(Alignment::Npc, Alignment::Tame) => true,
|
|
|
|
(Alignment::Tame, Alignment::Npc) => true,
|
|
|
|
(Alignment::Tame, Alignment::Tame) => true,
|
2020-01-27 15:51:07 +00:00
|
|
|
_ => false,
|
2019-08-02 18:56:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-06 20:18:30 +00:00
|
|
|
|
|
|
|
// TODO: Remove this hack
|
|
|
|
pub fn is_friendly_to_players(&self) -> bool {
|
|
|
|
match self {
|
2020-07-06 22:37:44 +00:00
|
|
|
Alignment::Npc | Alignment::Tame | Alignment::Owned(_) => true,
|
2020-07-06 20:18:30 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
2020-01-24 21:24:57 +00:00
|
|
|
}
|
2020-01-22 03:12:17 +00:00
|
|
|
|
2020-01-24 21:24:57 +00:00
|
|
|
impl Component for Alignment {
|
2020-04-26 17:03:19 +00:00
|
|
|
type Storage = IdvStorage<Self>;
|
2020-01-24 21:24:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 19:58:14 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct Psyche {
|
|
|
|
pub aggro: f32, // 0.0 = always flees, 1.0 = always attacks
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<&'a Body> for Psyche {
|
|
|
|
fn from(body: &'a Body) -> Self {
|
|
|
|
Self {
|
|
|
|
aggro: match body {
|
2020-07-30 19:26:49 +00:00
|
|
|
Body::Humanoid(_) => 0.5,
|
2020-07-29 19:58:14 +00:00
|
|
|
Body::QuadrupedSmall(_) => 0.35,
|
|
|
|
Body::QuadrupedMedium(_) => 0.5,
|
|
|
|
Body::QuadrupedLow(_) => 0.65,
|
|
|
|
Body::BirdMedium(_) => 1.0,
|
|
|
|
Body::BirdSmall(_) => 0.2,
|
|
|
|
Body::FishMedium(_) => 0.15,
|
|
|
|
Body::FishSmall(_) => 0.0,
|
|
|
|
Body::BipedLarge(_) => 1.0,
|
|
|
|
Body::Object(_) => 0.0,
|
|
|
|
Body::Golem(_) => 1.0,
|
|
|
|
Body::Critter(_) => 0.1,
|
|
|
|
Body::Dragon(_) => 1.0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 21:24:57 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct Agent {
|
|
|
|
pub patrol_origin: Option<Vec3<f32>>,
|
2020-01-25 18:49:47 +00:00
|
|
|
pub activity: Activity,
|
2020-05-26 02:45:13 +00:00
|
|
|
/// Does the agent talk when e.g. hit by the player
|
|
|
|
// TODO move speech patterns into a Behavior component
|
|
|
|
pub can_speak: bool,
|
2020-07-29 19:58:14 +00:00
|
|
|
pub psyche: Psyche,
|
2020-01-24 21:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Agent {
|
2020-01-25 02:15:15 +00:00
|
|
|
pub fn with_patrol_origin(mut self, origin: Vec3<f32>) -> Self {
|
|
|
|
self.patrol_origin = Some(origin);
|
|
|
|
self
|
|
|
|
}
|
2020-05-26 02:45:13 +00:00
|
|
|
|
2020-07-29 19:58:14 +00:00
|
|
|
pub fn new(origin: Vec3<f32>, can_speak: bool, body: &Body) -> Self {
|
2020-05-26 02:45:13 +00:00
|
|
|
let patrol_origin = Some(origin);
|
|
|
|
Agent {
|
|
|
|
patrol_origin,
|
|
|
|
can_speak,
|
2020-07-29 19:58:14 +00:00
|
|
|
psyche: Psyche::from(body),
|
2020-05-26 02:45:13 +00:00
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 18:56:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 21:06:33 +00:00
|
|
|
impl Component for Agent {
|
2020-07-06 05:56:02 +00:00
|
|
|
type Storage = IdvStorage<Self>;
|
2019-04-16 21:06:33 +00:00
|
|
|
}
|
2020-01-25 18:49:47 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum Activity {
|
2020-01-25 23:39:38 +00:00
|
|
|
Idle(Vec2<f32>),
|
2020-07-04 00:17:51 +00:00
|
|
|
Follow {
|
|
|
|
target: EcsEntity,
|
|
|
|
chaser: Chaser,
|
|
|
|
},
|
2020-01-27 15:51:07 +00:00
|
|
|
Attack {
|
|
|
|
target: EcsEntity,
|
|
|
|
chaser: Chaser,
|
|
|
|
time: f64,
|
|
|
|
been_close: bool,
|
2020-04-18 20:26:43 +00:00
|
|
|
powerup: f32,
|
2020-01-27 15:51:07 +00:00
|
|
|
},
|
2020-01-25 18:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Activity {
|
|
|
|
pub fn is_follow(&self) -> bool {
|
|
|
|
match self {
|
2020-07-04 00:17:51 +00:00
|
|
|
Activity::Follow { .. } => true,
|
2020-01-25 18:49:47 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_attack(&self) -> bool {
|
|
|
|
match self {
|
2020-01-27 15:51:07 +00:00
|
|
|
Activity::Attack { .. } => true,
|
2020-01-25 18:49:47 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Activity {
|
2020-02-01 20:39:39 +00:00
|
|
|
fn default() -> Self { Activity::Idle(Vec2::zero()) }
|
2020-01-25 18:49:47 +00:00
|
|
|
}
|