1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use crate::resource::{NavMeshID, NavPathMode, NavQuery, NavVec3};
use core::{
    ecs::{Component, Entity, NullStorage, VecStorage},
    id::ID,
    prefab::{Prefab, PrefabComponent},
    Ignite, Scalar,
};
use serde::{Deserialize, Serialize};

/// Nav agent identifier.

pub type NavAgentID = ID<NavAgent>;

/// Simple nav driver component tag to mark entity to use simple movement on nav mesh.

#[derive(Ignite, Debug, Default, Copy, Clone, Serialize, Deserialize)]
pub struct SimpleNavDriverTag;

impl Component for SimpleNavDriverTag {
    type Storage = NullStorage<Self>;
}

impl Prefab for SimpleNavDriverTag {}
impl PrefabComponent for SimpleNavDriverTag {}

/// Nav agent target.

#[derive(Debug, Clone, Copy)]
pub enum NavAgentTarget {
    /// Point in world space.

    Point(NavVec3),
    /// Entity to follow.

    Entity(Entity),
}

impl NavAgentTarget {
    pub fn is_point(&self) -> bool {
        match self {
            NavAgentTarget::Point(_) => true,
            _ => false,
        }
    }

    pub fn is_entity(&self) -> bool {
        match self {
            NavAgentTarget::Entity(_) => true,
            _ => false,
        }
    }
}

/// Nav agent destination descriptor.

#[derive(Debug, Clone)]
pub struct NavAgentDestination {
    /// Target.

    pub target: NavAgentTarget,
    /// Query quality.

    pub query: NavQuery,
    /// path finding quality.

    pub mode: NavPathMode,
    /// Nav mesh identifier that agent is moving on.

    pub mesh: NavMeshID,
}

/// Nav agent component.

#[derive(Ignite, Debug, Clone, Serialize, Deserialize)]
pub struct NavAgent {
    id: NavAgentID,
    /// Current agent position in world space.

    pub position: NavVec3,
    /// Current agent normalized direction.

    pub direction: NavVec3,
    /// Current speed (units per second).

    pub speed: Scalar,
    /// Agent sphere radius (used in obstacle and agent avoidance).

    pub radius: Scalar,
    /// Mnimal distance to target (affects direction, tells how far look for point to go to in an

    /// instant).

    pub min_target_distance: Scalar,
    #[serde(skip)]
    #[ignite(ignore)]
    pub(crate) destination: Option<NavAgentDestination>,
    #[serde(skip)]
    #[ignite(ignore)]
    pub(crate) path: Option<Vec<NavVec3>>,
    #[serde(skip)]
    #[ignite(ignore)]
    pub(crate) dirty_path: bool,
}

impl Component for NavAgent {
    type Storage = VecStorage<Self>;
}

impl Default for NavAgent {
    fn default() -> Self {
        Self::new(Default::default())
    }
}

impl NavAgent {
    pub fn new(position: NavVec3) -> Self {
        Self::new_with_direction(position, Default::default())
    }

    pub fn new_with_direction(position: NavVec3, direction: NavVec3) -> Self {
        Self {
            id: Default::default(),
            position,
            direction: direction.normalize(),
            speed: 10.0,
            radius: 1.0,
            min_target_distance: 1.0,
            destination: None,
            path: None,
            dirty_path: false,
        }
    }

    pub fn id(&self) -> NavAgentID {
        self.id
    }

    pub fn target(&self) -> Option<NavAgentTarget> {
        if let Some(destination) = &self.destination {
            Some(destination.target)
        } else {
            None
        }
    }

    pub fn destination(&self) -> Option<&NavAgentDestination> {
        if let Some(destination) = &self.destination {
            Some(destination)
        } else {
            None
        }
    }

    /// Sets destination to go to.

    ///

    /// # Arguments

    /// * `target` - target to go to.

    /// * `query` - query quality.

    /// * `mode` - path finding quality.

    /// * `mesh` - nav mesh to move on.

    pub fn set_destination(
        &mut self,
        target: NavAgentTarget,
        query: NavQuery,
        mode: NavPathMode,
        mesh: NavMeshID,
    ) {
        self.destination = Some(NavAgentDestination {
            target,
            query,
            mode,
            mesh,
        });
        self.dirty_path = true;
    }

    pub fn clear_path(&mut self) {
        self.destination = None;
        self.dirty_path = false;
        self.path = None;
    }

    pub fn recalculate_path(&mut self) {
        self.dirty_path = true;
    }

    pub fn path(&self) -> Option<&[NavVec3]> {
        if let Some(path) = &self.path {
            Some(path)
        } else {
            None
        }
    }

    pub fn set_path(&mut self, path: Vec<NavVec3>) {
        self.path = Some(path);
        self.dirty_path = false;
    }
}

impl Prefab for NavAgent {}
impl PrefabComponent for NavAgent {}