aboutsummaryrefslogtreecommitdiff
path: root/src/components.rs
diff options
context:
space:
mode:
authorLibravatar Silas Bartha <[email protected]>2024-08-23 09:30:30 -0400
committerLibravatar Silas Bartha <[email protected]>2024-08-23 09:30:30 -0400
commit85c80561669bdf7b0f491ee4d7e49f2cc8f7b81c (patch)
tree91fd51487ed4c855ed33cc2d7a2ec325aeb377d4 /src/components.rs
Initial Commit
Diffstat (limited to 'src/components.rs')
-rw-r--r--src/components.rs46
1 files changed, 46 insertions, 0 deletions
diff --git a/src/components.rs b/src/components.rs
new file mode 100644
index 0000000..1be2f2c
--- /dev/null
+++ b/src/components.rs
@@ -0,0 +1,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)
+ }
+}
+