aboutsummaryrefslogtreecommitdiff
path: root/src/components.rs
blob: 1be2f2c9d28fc9035ce50470a964802598fbc6bf (plain)
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
//! Components used for interactions

use bevy::{prelude::*, utils::HashSet};

/// Component which enables an entity to request interactions.
///
/// An entity with an `Interactor` component can be passed to an `InteractorFiredEvent` in order to
/// start an interaction with any nearby `Interactable` entities.
#[derive(Component, Default)]
pub struct Interactor {
    /// All `Interactable` targets in-range of this interactor.
    pub targets: HashSet<Entity>,
    /// The closest `Interactable` target to this interactor, if any.
    pub closest: Option<Entity>,
}

/// Component which enables an entity to recieve interactions.
///
/// An entity with an `Interactable` component might get passed to an `InteractionEvent` when an
/// `Interactor` requests an interaction, if the interactable is in range.
#[derive(Component)]
pub struct Interactable {
    pub(crate) exclusive: bool,
    pub(crate) max_distance_squared: f32,
}

impl Interactable {
    /// Construct a new instance of this component.
    ///
    /// If exclusive, this interactable will only be interacted with if it's the closest one to the
    /// interactor, and the interaction will *not* be processed for any other in-range
    /// interactables.
    pub fn new(max_distance: f32, exclusive: bool) -> Self {
        Self {
            exclusive,
            max_distance_squared: max_distance * max_distance,
        }
    }
}

impl Default for Interactable {
    fn default() -> Self {
        Self::new(1.0, false)
    }
}