use crate::{component::Component, traversal::Traversal}; #[cfg(feature = "bevy_reflect")] use bevy_reflect::Reflect; use core::{ cmp::Ordering, fmt, hash::{Hash, Hasher}, marker::PhantomData, }; /// Something that "happens" and might be read / observed by app logic. /// /// Events can be stored in an [`Events`] resource /// You can conveniently access events using the [`EventReader`] and [`EventWriter`] system parameter. /// /// Events can also be "triggered" on a [`World`], which will then cause any [`Observer`] of that trigger to run. /// /// This trait can be derived. /// /// Events implement the [`Component`] type (and they automatically do when they are derived). Events are (generally) /// not directly inserted as components. More often, the [`ComponentId`] is used to identify the event type within the /// context of the ECS. /// /// Events must be thread-safe. /// /// [`World`]: crate::world::World /// [`ComponentId`]: crate::component::ComponentId /// [`Observer`]: crate::observer::Observer /// [`Events`]: super::Events /// [`EventReader`]: super::EventReader /// [`EventWriter`]: super::EventWriter #[diagnostic::on_unimplemented( message = "`{Self}` is not an `Event`", label = "invalid `Event`", note = "consider annotating `{Self}` with `#[derive(Event)]`" )] pub trait Event: Component { /// The component that describes which Entity to propagate this event to next, when [propagation] is enabled. /// /// [propagation]: crate::observer::Trigger::propagate type Traversal: Traversal; /// When true, this event will always attempt to propagate when [triggered], without requiring a call /// to [`Trigger::propagate`]. /// /// [triggered]: crate::system::Commands::trigger_targets /// [`Trigger::propagate`]: crate::observer::Trigger::propagate const AUTO_PROPAGATE: bool = false; } /// An `EventId` uniquely identifies an event stored in a specific [`World`]. /// /// An `EventId` can among other things be used to trace the flow of an event from the point it was /// sent to the point it was processed. `EventId`s increase monotonically by send order. /// /// [`World`]: crate::world::World #[cfg_attr(feature = "bevy_reflect", derive(Reflect))] pub struct EventId { /// Uniquely identifies the event associated with this ID. // This value corresponds to the order in which each event was added to the world. pub id: usize, #[cfg_attr(feature = "bevy_reflect", reflect(ignore))] pub(super) _marker: PhantomData, } impl Copy for EventId {} impl Clone for EventId { fn clone(&self) -> Self { *self } } impl fmt::Display for EventId { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { ::fmt(self, f) } } impl fmt::Debug for EventId { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!( f, "event<{}>#{}", core::any::type_name::().split("::").last().unwrap(), self.id, ) } } impl PartialEq for EventId { fn eq(&self, other: &Self) -> bool { self.id == other.id } } impl Eq for EventId {} impl PartialOrd for EventId { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } impl Ord for EventId { fn cmp(&self, other: &Self) -> Ordering { self.id.cmp(&other.id) } } impl Hash for EventId { fn hash(&self, state: &mut H) { Hash::hash(&self.id, state); } } #[derive(Debug)] #[cfg_attr(feature = "bevy_reflect", derive(Reflect))] pub(crate) struct EventInstance { pub event_id: EventId, pub event: E, }